toolkit/components/workerloader/tests/worker_test_loading.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/workerloader/tests/worker_test_loading.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,121 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +"use strict";
     1.8 +
     1.9 +importScripts("utils_worker.js"); // Test suite code
    1.10 +info("Test suite configured");
    1.11 +
    1.12 +importScripts("resource://gre/modules/workers/require.js");
    1.13 +info("Loader imported");
    1.14 +
    1.15 +let PATH = "chrome://mochitests/content/chrome/toolkit/components/workerloader/tests/";
    1.16 +let tests = [];
    1.17 +let add_test = function(test) {
    1.18 +  tests.push(test);
    1.19 +};
    1.20 +
    1.21 +add_test(function test_setup() {
    1.22 +  ok(typeof require != "undefined", "Function |require| is defined");
    1.23 +});
    1.24 +
    1.25 +// Test simple loading (moduleA-depends.js requires moduleB-dependency.js)
    1.26 +add_test(function test_load() {
    1.27 +  let A = require(PATH + "moduleA-depends.js");
    1.28 +  ok(true, "Opened module A");
    1.29 +
    1.30 +  is(A.A, true, "Module A exported value A");
    1.31 +  ok(!("B" in A), "Module A did not export value B");
    1.32 +  is(A.importedFoo, "foo", "Module A re-exported B.foo");
    1.33 +
    1.34 +  // re-evaluating moduleB-dependency.js would cause an error, but re-requiring it shouldn't
    1.35 +  let B = require(PATH + "moduleB-dependency.js");
    1.36 +  ok(true, "Managed to re-require module B");
    1.37 +  is(B.B, true, "Module B exported value B");
    1.38 +  is(B.foo, "foo", "Module B exported value foo");
    1.39 +});
    1.40 +
    1.41 +// Test simple circular loading (moduleC-circular.js and moduleD-circular.js require each other)
    1.42 +add_test(function test_circular() {
    1.43 +  let C = require(PATH + "moduleC-circular.js");
    1.44 +  ok(true, "Loaded circular modules C and D");
    1.45 +  is(C.copiedFromD.copiedFromC.enteredC, true, "Properties exported by C before requiring D can be seen by D immediately");
    1.46 +
    1.47 +  let D = require(PATH + "moduleD-circular.js");
    1.48 +  is(D.exportedFromC.finishedC, true, "Properties exported by C after requiring D can be seen by D eventually");
    1.49 +});
    1.50 +
    1.51 +// Testing error cases
    1.52 +add_test(function test_exceptions() {
    1.53 +  let should_throw = function(f) {
    1.54 +    try {
    1.55 +      f();
    1.56 +      return null;
    1.57 +    } catch (ex) {
    1.58 +      return ex;
    1.59 +    }
    1.60 +  };
    1.61 +
    1.62 +  let exn = should_throw(() => require(PATH + "this module doesn't exist"));
    1.63 +  ok(!!exn, "Attempting to load a module that doesn't exist raises an error");
    1.64 +
    1.65 +  exn = should_throw(() => require(PATH + "moduleE-throws-during-require.js"));
    1.66 +  ok(!!exn, "Attempting to load a module that throws at toplevel raises an error");
    1.67 +  is(exn.moduleName, PATH + "moduleE-throws-during-require.js",
    1.68 +    "moduleName is correct");
    1.69 +  isnot(exn.moduleStack.indexOf("moduleE-throws-during-require.js"), -1,
    1.70 +    "moduleStack contains the name of the module");
    1.71 +  is(exn.lineNumber, 10, "The error comes with the right line number");
    1.72 +
    1.73 +  exn = should_throw(() => require(PATH + "moduleF-syntaxerror.xml"));
    1.74 +  ok(!!exn, "Attempting to load a non-well formatted module raises an error");
    1.75 +
    1.76 +  exn = should_throw(() => require(PATH + "moduleG-throws-later.js").doThrow());
    1.77 +  ok(!!exn, "G.doThrow() has raised an error");
    1.78 +  info(exn);
    1.79 +  ok(exn.toString().startsWith("TypeError"), "The exception is a TypeError.");
    1.80 +  is(exn.moduleName, PATH + "moduleG-throws-later.js", "The name of the module is correct");
    1.81 +  isnot(exn.moduleStack.indexOf("moduleG-throws-later.js"), -1,
    1.82 +    "The name of the right file appears somewhere in the stack");
    1.83 +  is(exn.lineNumber, 11, "The error comes with the right line number");
    1.84 +});
    1.85 +
    1.86 +function get_exn(f) {
    1.87 +  try {
    1.88 +    f();
    1.89 +    return undefined;
    1.90 +  } catch (ex) {
    1.91 +    return ex;
    1.92 +  }
    1.93 +}
    1.94 +
    1.95 +// Test module.exports
    1.96 +add_test(function test_module_dot_exports() {
    1.97 +  let H = require(PATH + "moduleH-module-dot-exports.js");
    1.98 +  is(H.key, "value", "module.exports worked");
    1.99 +  let H2 = require(PATH + "moduleH-module-dot-exports.js");
   1.100 +  is(H2.key, "value", "module.exports returned the same key");
   1.101 +  ok(H2 === H, "module.exports returned the same module the second time");
   1.102 +  let exn = get_exn(() => H.key = "this should not be accepted");
   1.103 +  ok(exn instanceof TypeError, "Cannot alter value in module.exports after export");
   1.104 +  exn = get_exn(() => H.key2 = "this should not be accepted, either");
   1.105 +  ok(exn instanceof TypeError, "Cannot add value to module.exports after export");
   1.106 +});
   1.107 +
   1.108 +self.onmessage = function(message) {
   1.109 +  for (let test of tests) {
   1.110 +    info("Entering " + test.name);
   1.111 +    try {
   1.112 +      test();
   1.113 +    } catch (ex) {
   1.114 +      ok(false, "Test " + test.name + " failed");
   1.115 +      info(ex);
   1.116 +      info(ex.stack);
   1.117 +    }
   1.118 +    info("Leaving " + test.name);
   1.119 +  }
   1.120 +  finish();
   1.121 +};
   1.122 +
   1.123 +
   1.124 +

mercurial