michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: "use strict"; michael@0: michael@0: importScripts("utils_worker.js"); // Test suite code michael@0: info("Test suite configured"); michael@0: michael@0: importScripts("resource://gre/modules/workers/require.js"); michael@0: info("Loader imported"); michael@0: michael@0: let PATH = "chrome://mochitests/content/chrome/toolkit/components/workerloader/tests/"; michael@0: let tests = []; michael@0: let add_test = function(test) { michael@0: tests.push(test); michael@0: }; michael@0: michael@0: add_test(function test_setup() { michael@0: ok(typeof require != "undefined", "Function |require| is defined"); michael@0: }); michael@0: michael@0: // Test simple loading (moduleA-depends.js requires moduleB-dependency.js) michael@0: add_test(function test_load() { michael@0: let A = require(PATH + "moduleA-depends.js"); michael@0: ok(true, "Opened module A"); michael@0: michael@0: is(A.A, true, "Module A exported value A"); michael@0: ok(!("B" in A), "Module A did not export value B"); michael@0: is(A.importedFoo, "foo", "Module A re-exported B.foo"); michael@0: michael@0: // re-evaluating moduleB-dependency.js would cause an error, but re-requiring it shouldn't michael@0: let B = require(PATH + "moduleB-dependency.js"); michael@0: ok(true, "Managed to re-require module B"); michael@0: is(B.B, true, "Module B exported value B"); michael@0: is(B.foo, "foo", "Module B exported value foo"); michael@0: }); michael@0: michael@0: // Test simple circular loading (moduleC-circular.js and moduleD-circular.js require each other) michael@0: add_test(function test_circular() { michael@0: let C = require(PATH + "moduleC-circular.js"); michael@0: ok(true, "Loaded circular modules C and D"); michael@0: is(C.copiedFromD.copiedFromC.enteredC, true, "Properties exported by C before requiring D can be seen by D immediately"); michael@0: michael@0: let D = require(PATH + "moduleD-circular.js"); michael@0: is(D.exportedFromC.finishedC, true, "Properties exported by C after requiring D can be seen by D eventually"); michael@0: }); michael@0: michael@0: // Testing error cases michael@0: add_test(function test_exceptions() { michael@0: let should_throw = function(f) { michael@0: try { michael@0: f(); michael@0: return null; michael@0: } catch (ex) { michael@0: return ex; michael@0: } michael@0: }; michael@0: michael@0: let exn = should_throw(() => require(PATH + "this module doesn't exist")); michael@0: ok(!!exn, "Attempting to load a module that doesn't exist raises an error"); michael@0: michael@0: exn = should_throw(() => require(PATH + "moduleE-throws-during-require.js")); michael@0: ok(!!exn, "Attempting to load a module that throws at toplevel raises an error"); michael@0: is(exn.moduleName, PATH + "moduleE-throws-during-require.js", michael@0: "moduleName is correct"); michael@0: isnot(exn.moduleStack.indexOf("moduleE-throws-during-require.js"), -1, michael@0: "moduleStack contains the name of the module"); michael@0: is(exn.lineNumber, 10, "The error comes with the right line number"); michael@0: michael@0: exn = should_throw(() => require(PATH + "moduleF-syntaxerror.xml")); michael@0: ok(!!exn, "Attempting to load a non-well formatted module raises an error"); michael@0: michael@0: exn = should_throw(() => require(PATH + "moduleG-throws-later.js").doThrow()); michael@0: ok(!!exn, "G.doThrow() has raised an error"); michael@0: info(exn); michael@0: ok(exn.toString().startsWith("TypeError"), "The exception is a TypeError."); michael@0: is(exn.moduleName, PATH + "moduleG-throws-later.js", "The name of the module is correct"); michael@0: isnot(exn.moduleStack.indexOf("moduleG-throws-later.js"), -1, michael@0: "The name of the right file appears somewhere in the stack"); michael@0: is(exn.lineNumber, 11, "The error comes with the right line number"); michael@0: }); michael@0: michael@0: function get_exn(f) { michael@0: try { michael@0: f(); michael@0: return undefined; michael@0: } catch (ex) { michael@0: return ex; michael@0: } michael@0: } michael@0: michael@0: // Test module.exports michael@0: add_test(function test_module_dot_exports() { michael@0: let H = require(PATH + "moduleH-module-dot-exports.js"); michael@0: is(H.key, "value", "module.exports worked"); michael@0: let H2 = require(PATH + "moduleH-module-dot-exports.js"); michael@0: is(H2.key, "value", "module.exports returned the same key"); michael@0: ok(H2 === H, "module.exports returned the same module the second time"); michael@0: let exn = get_exn(() => H.key = "this should not be accepted"); michael@0: ok(exn instanceof TypeError, "Cannot alter value in module.exports after export"); michael@0: exn = get_exn(() => H.key2 = "this should not be accepted, either"); michael@0: ok(exn instanceof TypeError, "Cannot add value to module.exports after export"); michael@0: }); michael@0: michael@0: self.onmessage = function(message) { michael@0: for (let test of tests) { michael@0: info("Entering " + test.name); michael@0: try { michael@0: test(); michael@0: } catch (ex) { michael@0: ok(false, "Test " + test.name + " failed"); michael@0: info(ex); michael@0: info(ex.stack); michael@0: } michael@0: info("Leaving " + test.name); michael@0: } michael@0: finish(); michael@0: }; michael@0: michael@0: michael@0: