Permalink
Browse files

added behaviors.require

  • Loading branch information...
1 parent cba9c40 commit 23ec6e6f0ccecf4b29a58cd9a38fc981a56137ca @caolan committed Feb 27, 2010
Showing with 52 additions and 0 deletions.
  1. +6 −0 lib/behaviors.js
  2. +46 −0 test/test-require.js
View
@@ -40,3 +40,9 @@ exports.test = function(obj, behavior){
});
return true;
};
+
+exports.require = function(modulename, behavior){
+ var m = require(modulename);
+ exports.test(m, behavior);
+ return m;
+};
View
@@ -0,0 +1,46 @@
+var behaviors = require('behaviors'),
+ assert = require('assert');
+
+var call_order = [];
+var test_module = module.exports = {test: 'module'};
+var test_behavior = {test: 'behavior'};
+
+// copy behaviors.test
+var test_copy = behaviors.test;
+
+
+//---------- Module implements behavior ----------//
+behaviors.test = function(m, b){
+ assert.deepEqual(m, test_module, 'test called with module');
+ assert.deepEqual(b, test_behavior, 'test called with behavior');
+ call_order.push('test');
+ return true;
+};
+
+var r = behaviors.require('./test/test-require', test_behavior);
+assert.deepEqual(call_order, ['test'], 'correct call order');
+assert.deepEqual(r, test_module, 'module returned ok');
+
+
+//---------- Module does not implement behavior ----------//
+behaviors.test = function(m, b){
+ assert.deepEqual(m, test_module, 'test called with module');
+ assert.deepEqual(b, test_behavior, 'test called with behavior');
+ call_order.push('test');
+ throw new Error('test');
+};
+
+call_order = [];
+try {
+ var r2 = behaviors.require('./test/test-require', test_behavior);
+}
+catch (e){
+ assert.ok(e instanceof Error, 'throws error on invalid module');
+ call_order.push('error');
+}
+assert.deepEqual(call_order, ['test', 'error'], 'correct call order');
+assert.strictEqual(r2, undefined, 'module not returned');
+
+
+// restore behaviors.test
+behaviors.test = test_copy;

0 comments on commit 23ec6e6

Please sign in to comment.