Permalink
Browse files

Using require.id to canonicalize module identifiers before passing th…

…em into require.memoize and require.isMemoized, since those expect IDs instead of identifiers.
  • Loading branch information...
domenic committed Sep 2, 2011
1 parent ff8a534 commit a532b75b3450c139ba1200d8b474afc3a2913255
Showing with 17 additions and 21 deletions.
  1. +10 −10 test/moduleNamespace.js
  2. +7 −11 test/requireNamespace.js
View
@@ -2,8 +2,8 @@ newTestSet("module namespace");
asyncTest("declare: accepts labeled dependency objects and correctly provides them to factory function's require", function () {
module.declare([{ mathLabel: "demos/math" }], function (require, exports, module) {
- strictEqual(require.id("mathLabel"), "demos/math", "The label was translated into the correct ID when using require.id");
- strictEqual(require.uri("mathLabel"), "demos/math.js", "The label was translated into the correct URI when using require.uri");
+ strictEqual(require.id("mathLabel"), require.id("demos/math"), "The label was translated into the correct ID when using require.id");
+ strictEqual(require.uri("mathLabel"), require.uri("demos/math"), "The label was translated into the correct URI when using require.uri");
var math = require("mathLabel");
strictEqual(typeof math, "object", 'demos/math module, labeled as "mathLabel," has been provided');
@@ -59,7 +59,7 @@ asyncModuleTest("load: when called twice in a row for the same nonextant module,
asyncModuleTest("load: does not memoize the loaded module", function (require, exports, module) {
module.load("demos/math", function onModuleLoaded() {
- strictEqual(require.isMemoized("demos/math"), false, "The module was not memoized");
+ strictEqual(require.isMemoized(require.id("demos/math")), false, "The module was not memoized");
start();
});
});
@@ -73,24 +73,24 @@ asyncModuleTest("provide: passing an empty dependency array still results in the
asyncModuleTest("provide: when passing multiple dependencies, all of them are memoized by the time the callback is called", function (require, exports, module) {
module.provide(["demos/area", "demos/perimeter"], function onModulesProvided() {
- strictEqual(require.isMemoized("demos/area"), true, "First dependency is memoized");
- strictEqual(require.isMemoized("demos/perimeter"), true, "Second dependency is memoized");
+ strictEqual(require.isMemoized(require.id("demos/area")), true, "First dependency is memoized");
+ strictEqual(require.isMemoized(require.id("demos/perimeter")), true, "Second dependency is memoized");
start();
});
});
asyncModuleTest("provide: understands relative identifiers", function (require, exports, module) {
module.provide(["demos/../demos/math"], function onModulesProvided() {
- strictEqual(require.isMemoized("demos/math"), true, "It figured out demos/../demos");
+ strictEqual(require.isMemoized(require.id("demos/math")), true, "It figured out demos/../demos");
start();
});
});
asyncModuleTest("provide: still calls the callback even if one of the modules in the dependencies array doesn't exist", function (require, exports, module) {
module.provide(["asdf", "demos/math"], function onModulesProvided() {
ok(true, "Callback still got called");
- strictEqual(require.isMemoized("demos/math"), true, "The extant module is memoized");
- strictEqual(require.isMemoized("asdf"), false, "The nonextant module is not memoized");
+ strictEqual(require.isMemoized(require.id("demos/math")), true, "The extant module is memoized");
+ strictEqual(require.isMemoized(require.id("asdf")), false, "The nonextant module is not memoized");
start();
});
@@ -124,8 +124,8 @@ asyncModuleTest("provide: providing an extant module then a nonextant module doe
module.provide(["asdf"], function () {
ok(true, "Callback for nonextant module reached");
- strictEqual(require.isMemoized("demos/math"), true, "The extant module is memoized");
- strictEqual(require.isMemoized("asdf"), false, "The nonextant module is not memoized");
+ strictEqual(require.isMemoized(require.id("demos/math")), true, "The extant module is memoized");
+ strictEqual(require.isMemoized(require.id("asdf")), false, "The nonextant module is not memoized");
start();
});
View
@@ -1,13 +1,9 @@
newTestSet("require namespace");
-moduleTest("id: when given an absolute ID, returns it directly", function (require, exports, module) {
- strictEqual(require.id("demos/math"), "demos/math", "It returned what it was given for the case with no ..s or .s");
-});
-
moduleTest("id: canonicalizes relative identifiers", function (require, exports, module) {
- strictEqual(require.id("./demos/math"), "demos/math", "It figured out ./ relative to the main module");
- strictEqual(require.id("demos/../demos/math"), "demos/math", "It figured out demos/../demos");
- strictEqual(require.id("demos/../demos/circular/../area"), "demos/area", "It figured out an identifier with two ../s");
+ strictEqual(require.id("./demos/math"), require.id("demos/math"), "It figured out ./ relative to the main module");
+ strictEqual(require.id("demos/../demos/math"), require.id("demos/math"), "It figured out demos/../demos");
+ strictEqual(require.id("demos/../demos/circular/../area"), require.id("demos/area"), "It figured out an identifier with two ../s");
});
moduleTest("uri: turns an absolute ID into the corresponding URI", function (require, exports, module) {
@@ -29,26 +25,26 @@ moduleTest("memoize: throws an error when trying to memoize a module that is alr
});
moduleTest("memoize: makes the memoized module available to require", function (require, exports, module) {
- require.memoize("please/be/memoized", [], function (require, exports, module) { exports.hi = "hello"; });
+ require.memoize(require.id("please/be/memoized"), [], function (require, exports, module) { exports.hi = "hello"; });
var memoized = require("please/be/memoized");
deepEqual(memoized, { hi: "hello" }, "The memoized module was provided to require");
});
moduleTest("memoize: can be used to memoize dependencies of memoized modules", function (require, exports, module) {
- require.memoize("memoizing/dependent", ["memoizing/dependency"], function (require, exports, module) {
+ require.memoize(require.id("memoizing/dependent"), ["memoizing/dependency"], function (require, exports, module) {
var dependency = require("memoizing/dependency");
exports.greeting = dependency.hi;
});
- require.memoize("memoizing/dependency", [], function (require, exports, module) { exports.hi = "hello"; });
+ require.memoize(require.id("memoizing/dependency"), [], function (require, exports, module) { exports.hi = "hello"; });
var dependent = require("memoizing/dependent");
deepEqual(dependent, { greeting: "hello" }, "The dependent module was memoized");
});
moduleTest('memoize: works even for a module named "hasOwnProperty"', function (require, exports, module) {
- require.memoize("hasOwnProperty", [], function (require, exports, module) { exports.hi = "hello"; });
+ require.memoize(require.id("hasOwnProperty"), [], function (require, exports, module) { exports.hi = "hello"; });
var memoized = require("hasOwnProperty");
deepEqual(memoized, { hi: "hello" }, "The memoized module was provided to require");

0 comments on commit a532b75

Please sign in to comment.