Browse Source

Add in dynamic require test, fill out the basic test some more.

jrburke 12 years ago
parent
commit
2e41820c3d

+ 12 - 7
README.md

@@ -1,11 +1,16 @@
+# amdefine
 
+A module that can be used to implement AMD's define() in Node.
 
-module.id is a path, need to adjust normalization, just use path normalization?
+More explanation here...
 
-## Test
+Run sync, except require([], function() {}) where callback is triggered on nextTick
 
-* require.toUrl
-* ask for require, exports, module but return a value.
-* ask for require, exports, module, modify exports but return value?
-* loader plugin
-*
+
+
+
+## Tests
+
+To run the tests, cd to **tests** and run:
+
+    node all.js

+ 2 - 0
tests/all.js

@@ -27,6 +27,8 @@ load('doh/_' + env + 'Runner.js');
 
 require("./basic/basic-tests");
 require("./plugins/relative/relative-tests");
+//require("./plugins/coffeescript/coffeescript-tests");
+require("./require/require-tests");
 
 //Print out the final report
 doh.run();

+ 1 - 0
tests/basic/basic-tests.js

@@ -8,6 +8,7 @@ doh.register(
             t.is('b', a.b.name);
             t.is('d', a.d.name);
             t.is('c', a.d.cName);
+            t.is('e', a.d.e.name);
         }
     ]
 );

+ 9 - 2
tests/basic/sub/c.js

@@ -1,5 +1,12 @@
 if (typeof define !== 'function') { var define = (require('../../../amdefine'))(module); }
 
-define({
-    name: 'c'
+define(function (require, exports, module) {
+
+    //A fake out, modify the exports, but still prefer the
+    //return value as the module value.
+    exports.name = 'badc';
+
+    return {
+        name: 'c'
+    };
 });

+ 6 - 3
tests/basic/sub/nested/d.js

@@ -1,12 +1,15 @@
 if (typeof define !== 'function') { var define = (require('../../../../amdefine'))(module); }
 
+//Define's a named module, but one that does not match the current module name
+//expected by node. amdefine should just ignore this ID and use the ID expected
+//by node.
 define('whatever', function (require, exports, module) {
-
-debugger;
-    var c = require('../c');
+    var c = require('../c'),
+        e = require('./e');
 
     return {
         name: 'd',
+        e: e,
         cName: c.name
     };
 });

+ 6 - 0
tests/basic/sub/nested/e.js

@@ -0,0 +1,6 @@
+if (typeof define !== 'function') { var define = (require('../../../../amdefine'))(module); }
+
+//Just testing a plain exports case.
+define(function (require, exports) {
+    exports.name = 'e';
+});

+ 3 - 0
tests/require/addTwo.js

@@ -0,0 +1,3 @@
+module.exports = function () {
+    return 2;
+};

+ 14 - 0
tests/require/require-tests.js

@@ -0,0 +1,14 @@
+doh.register(
+    "requireTests",
+    [
+        function requireTests(t){
+            var dynamic = require('./sub/dynamic');
+
+            t.is(0, dynamic.count);
+            dynamic.action();
+            t.is(1, dynamic.count);
+        }
+    ]
+);
+
+doh.run();

+ 20 - 0
tests/require/sub/dynamic.js

@@ -0,0 +1,20 @@
+if (typeof define !== 'function') { var define = (require('../../../amdefine'))(module); }
+
+//Just testing a plain exports case.
+define(function (require) {
+    var dynamic = {
+        count: 0,
+        action: function () {
+            dynamic.count += 1;
+
+            //Test that dynamic require is not synchronous. If it was, this
+            //would add two to the counter, instead of just one.
+            require(['../addTwo'], function (addTwo) {
+                console.log('dynamic require should fire after TEST SUMMARY is shown.');
+                dynamic.count += addTwo();
+            });
+        }
+    };
+
+    return dynamic;
+});