Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added tests and type checking

  • Loading branch information...
commit 28ca074e591f504c1127d97910d46fdc39f06ea5 1 parent 4b97c6e
Caolan McMahon authored
Showing with 111 additions and 2 deletions.
  1. +42 −1 lib/behaviors.js
  2. +44 −1 test/test-behaviours.js
  3. +25 −0 test/test-types.js
43 lib/behaviors.js
View
@@ -1 +1,42 @@
-exports.hello = "hi";
+exports.testType = function(prop, t){
+ if(t === Number || t === String || t === Boolean || t === Function){
+ return typeof prop === typeof t();
+ }
+ else if(t === Object){
+ return (
+ prop instanceof Object
+ && !(prop instanceof Array)
+ && !(prop instanceof Function)
+ );
+ }
+ else if(t === null || t === undefined){
+ return prop === t;
+ }
+ return prop instanceof t;
+};
+
+exports.test = function(obj, behavior){
+ Object.keys(behavior).forEach(function(k){
+ var expects = behavior[k];
+ var prop = obj[k];
+ var types = (expects instanceof Array) ? expects: [expects];
+ var match = types.some(function(t){
+ return exports.testType(prop, t);
+ });
+ if(!match){
+ if(prop === undefined){
+ throw new Error(k + ' is required');
+ }
+ else {
+ var expected_types = types.map(function(t){
+ return typeof t();
+ });
+ throw new Error(
+ k + ' is of type ' + (typeof prop) + ', ' +
+ 'expected: ' + expected_types
+ );
+ }
+ }
+ });
+ return true;
+};
45 test/test-behaviours.js
View
@@ -2,4 +2,47 @@ var behaviors = require('behaviors'),
assert = require('assert');
-assert.ok(true, 'ok');
+var behavior = {
+ id: [String, Number],
+ name: String,
+ fn: Function
+};
+
+var testmodule = {
+ id: 123,
+ name: 'name',
+ fn: function(){return 'test';}
+};
+assert.strictEqual(
+ behaviors.test(testmodule, behavior), true,
+ 'test module implements behavior'
+);
+
+var testmodule2 = {
+ id: 'id',
+ name: 'name',
+ fn: function(){return 'test';}
+};
+assert.strictEqual(
+ behaviors.test(testmodule2, behavior), true,
+ 'test module 2 also implements behavior'
+);
+
+var testmodule3 = {
+ name: 'name',
+ fn: function(){return 'test';}
+};
+assert.throws(
+ behaviors.test(testmodule2, behavior), Error,
+ 'test module 3 does not implement behavior - missing export'
+);
+
+var testmodule3 = {
+ id: {},
+ name: 'name',
+ fn: function(){return 'test';}
+};
+assert.throws(
+ behaviors.test(testmodule2, behavior), Error,
+ 'test module 4 does not implement behavior - exported wrong type for id'
+);
25 test/test-types.js
View
@@ -0,0 +1,25 @@
+var testType = require('behaviors').testType,
+ assert = require('assert');
+
+var doTest = function(obj, should_match){
+ var types = [
+ Number, Array, String, Boolean, Object,
+ Function, null, undefined
+ ];
+ types.forEach(function(t){
+ assert.strictEqual(
+ testType(obj, t), (t === should_match),
+ 'testType(' + obj + ', ' +
+ (t instanceof Function && typeof(t()) || t) + ')'
+ );
+ });
+};
+
+doTest(1, Number);
+doTest([], Array);
+doTest("test", String);
+doTest(false, Boolean);
+doTest({}, Object);
+doTest(function(){return 'test';}, Function);
+doTest(null, null);
+doTest(undefined, undefined);
Please sign in to comment.
Something went wrong with that request. Please try again.