diff --git a/test/fixtures/global/plain.js b/test/fixtures/global/plain.js new file mode 100644 index 0000000000..ae187c29fb --- /dev/null +++ b/test/fixtures/global/plain.js @@ -0,0 +1,6 @@ +foo = "foo"; +global.bar = "bar"; + +exports.fooBar = function () { + return {foo: global.foo, bar: bar}; +}; diff --git a/test/fixtures/global/sub1.js b/test/fixtures/global/sub1.js new file mode 100644 index 0000000000..a89fc7fd04 --- /dev/null +++ b/test/fixtures/global/sub1.js @@ -0,0 +1,13 @@ +foo1 = "foo1"; +global.bar1 = "bar1"; + +var sub2 = require('./sub2'); + + +exports.subGlobalKeys = function () { + return sub2.globalKeys(); +}; + +exports.subAllFooBars = function () { + return sub2.allFooBars(); +}; diff --git a/test/fixtures/global/sub2.js b/test/fixtures/global/sub2.js new file mode 100644 index 0000000000..540bde4ca1 --- /dev/null +++ b/test/fixtures/global/sub2.js @@ -0,0 +1,18 @@ +foo2 = "foo2"; +global.bar2 = "bar2"; + + +exports.globalKeys = function () { + return Object.getOwnPropertyNames(global); +}; + +exports.allFooBars = function () { + return { + foo0: global.foo0, + bar0: bar0, + foo1: global.foo1, + bar1: bar1, + foo2: global.foo2, + bar2: bar2 + }; +}; diff --git a/test/simple/test-global-between-modules.js b/test/simple/test-global-between-modules.js new file mode 100644 index 0000000000..a171840b8e --- /dev/null +++ b/test/simple/test-global-between-modules.js @@ -0,0 +1,21 @@ +common = require("../common"); +assert = common.assert + +foo0 = "foo0"; +global.bar0 = "bar0"; + +var module = require("../fixtures/global/sub1"), + keys = module.subGlobalKeys(); + +var fooBarKeys = keys.filter( + function (x) { return x.match(/^foo/) || x.match(/^bar/); } +); +fooBarKeys.sort(); +assert.equal("bar0,bar1,bar2,foo0,foo1,foo2", fooBarKeys.join(), "global keys not as expected: "+JSON.stringify(keys)); + +var fooBars = module.subAllFooBars(); + +assert.equal("foo0", fooBars.foo0, "x from base level not visible in deeper levels."); +assert.equal("bar0", fooBars.bar0, "global.x from base level not visible in deeper levels."); +assert.equal("foo1", fooBars.foo1, "x from medium level not visible in deeper levels."); +assert.equal("bar1", fooBars.bar1, "global.x from medium level not visible in deeper levels."); diff --git a/test/simple/test-global.js b/test/simple/test-global.js new file mode 100644 index 0000000000..0e9ec7df23 --- /dev/null +++ b/test/simple/test-global.js @@ -0,0 +1,16 @@ +common = require("../common"); +assert = common.assert + +baseFoo = "foo"; +global.baseBar = "bar"; + +assert.equal("foo", global.baseFoo, "x -> global.x in base level not working"); + +assert.equal("bar", baseBar, "global.x -> x in base level not working"); + +var module = require("../fixtures/global/plain"), + fooBar = module.fooBar(); + +assert.equal("foo", fooBar.foo, "x -> global.x in sub level not working"); + +assert.equal("bar", fooBar.bar, "global.x -> x in sub level not working");