|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 "use strict"; |
|
5 |
|
6 importScripts("utils_worker.js"); // Test suite code |
|
7 info("Test suite configured"); |
|
8 |
|
9 importScripts("resource://gre/modules/workers/require.js"); |
|
10 info("Loader imported"); |
|
11 |
|
12 let PATH = "chrome://mochitests/content/chrome/toolkit/components/workerloader/tests/"; |
|
13 let tests = []; |
|
14 let add_test = function(test) { |
|
15 tests.push(test); |
|
16 }; |
|
17 |
|
18 add_test(function test_setup() { |
|
19 ok(typeof require != "undefined", "Function |require| is defined"); |
|
20 }); |
|
21 |
|
22 // Test simple loading (moduleA-depends.js requires moduleB-dependency.js) |
|
23 add_test(function test_load() { |
|
24 let A = require(PATH + "moduleA-depends.js"); |
|
25 ok(true, "Opened module A"); |
|
26 |
|
27 is(A.A, true, "Module A exported value A"); |
|
28 ok(!("B" in A), "Module A did not export value B"); |
|
29 is(A.importedFoo, "foo", "Module A re-exported B.foo"); |
|
30 |
|
31 // re-evaluating moduleB-dependency.js would cause an error, but re-requiring it shouldn't |
|
32 let B = require(PATH + "moduleB-dependency.js"); |
|
33 ok(true, "Managed to re-require module B"); |
|
34 is(B.B, true, "Module B exported value B"); |
|
35 is(B.foo, "foo", "Module B exported value foo"); |
|
36 }); |
|
37 |
|
38 // Test simple circular loading (moduleC-circular.js and moduleD-circular.js require each other) |
|
39 add_test(function test_circular() { |
|
40 let C = require(PATH + "moduleC-circular.js"); |
|
41 ok(true, "Loaded circular modules C and D"); |
|
42 is(C.copiedFromD.copiedFromC.enteredC, true, "Properties exported by C before requiring D can be seen by D immediately"); |
|
43 |
|
44 let D = require(PATH + "moduleD-circular.js"); |
|
45 is(D.exportedFromC.finishedC, true, "Properties exported by C after requiring D can be seen by D eventually"); |
|
46 }); |
|
47 |
|
48 // Testing error cases |
|
49 add_test(function test_exceptions() { |
|
50 let should_throw = function(f) { |
|
51 try { |
|
52 f(); |
|
53 return null; |
|
54 } catch (ex) { |
|
55 return ex; |
|
56 } |
|
57 }; |
|
58 |
|
59 let exn = should_throw(() => require(PATH + "this module doesn't exist")); |
|
60 ok(!!exn, "Attempting to load a module that doesn't exist raises an error"); |
|
61 |
|
62 exn = should_throw(() => require(PATH + "moduleE-throws-during-require.js")); |
|
63 ok(!!exn, "Attempting to load a module that throws at toplevel raises an error"); |
|
64 is(exn.moduleName, PATH + "moduleE-throws-during-require.js", |
|
65 "moduleName is correct"); |
|
66 isnot(exn.moduleStack.indexOf("moduleE-throws-during-require.js"), -1, |
|
67 "moduleStack contains the name of the module"); |
|
68 is(exn.lineNumber, 10, "The error comes with the right line number"); |
|
69 |
|
70 exn = should_throw(() => require(PATH + "moduleF-syntaxerror.xml")); |
|
71 ok(!!exn, "Attempting to load a non-well formatted module raises an error"); |
|
72 |
|
73 exn = should_throw(() => require(PATH + "moduleG-throws-later.js").doThrow()); |
|
74 ok(!!exn, "G.doThrow() has raised an error"); |
|
75 info(exn); |
|
76 ok(exn.toString().startsWith("TypeError"), "The exception is a TypeError."); |
|
77 is(exn.moduleName, PATH + "moduleG-throws-later.js", "The name of the module is correct"); |
|
78 isnot(exn.moduleStack.indexOf("moduleG-throws-later.js"), -1, |
|
79 "The name of the right file appears somewhere in the stack"); |
|
80 is(exn.lineNumber, 11, "The error comes with the right line number"); |
|
81 }); |
|
82 |
|
83 function get_exn(f) { |
|
84 try { |
|
85 f(); |
|
86 return undefined; |
|
87 } catch (ex) { |
|
88 return ex; |
|
89 } |
|
90 } |
|
91 |
|
92 // Test module.exports |
|
93 add_test(function test_module_dot_exports() { |
|
94 let H = require(PATH + "moduleH-module-dot-exports.js"); |
|
95 is(H.key, "value", "module.exports worked"); |
|
96 let H2 = require(PATH + "moduleH-module-dot-exports.js"); |
|
97 is(H2.key, "value", "module.exports returned the same key"); |
|
98 ok(H2 === H, "module.exports returned the same module the second time"); |
|
99 let exn = get_exn(() => H.key = "this should not be accepted"); |
|
100 ok(exn instanceof TypeError, "Cannot alter value in module.exports after export"); |
|
101 exn = get_exn(() => H.key2 = "this should not be accepted, either"); |
|
102 ok(exn instanceof TypeError, "Cannot add value to module.exports after export"); |
|
103 }); |
|
104 |
|
105 self.onmessage = function(message) { |
|
106 for (let test of tests) { |
|
107 info("Entering " + test.name); |
|
108 try { |
|
109 test(); |
|
110 } catch (ex) { |
|
111 ok(false, "Test " + test.name + " failed"); |
|
112 info(ex); |
|
113 info(ex.stack); |
|
114 } |
|
115 info("Leaving " + test.name); |
|
116 } |
|
117 finish(); |
|
118 }; |
|
119 |
|
120 |
|
121 |