OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-2017 eyeo GmbH | 3 * Copyright (C) 2006-2017 eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 "use strict"; | 18 "use strict"; |
19 | 19 |
20 /* globals nodeunit */ | 20 /* globals nodeunit */ |
| 21 require("nodeunit"); |
21 | 22 |
22 (function(nodeunitUrl, ...moduleUrls) | 23 function runTests(moduleNames) |
23 { | 24 { |
24 function loadScript(doc, url) | 25 function bold(str) |
25 { | 26 { |
26 return new Promise((resolve, reject) => | 27 return "\u001B[1m" + str + "\u001B[22m"; |
27 { | |
28 let script = doc.createElement("script"); | |
29 script.src = url; | |
30 script.onload = resolve; | |
31 doc.head.appendChild(script); | |
32 }); | |
33 } | 28 } |
34 | 29 |
35 function loadModules(urls) | 30 function ok(str) |
36 { | 31 { |
37 let modules = {}; | 32 return "\u001B[32m" + str + "\u001B[39m"; |
38 | |
39 return (function loadNext() | |
40 { | |
41 if (urls.length) | |
42 { | |
43 // Load each module into a new frame so that their scopes don't clash | |
44 let frame = document.createElement("iframe"); | |
45 document.body.appendChild(frame); | |
46 | |
47 let wnd = frame.contentWindow; | |
48 wnd.loadScript = url => loadScript(wnd.document, url); | |
49 wnd.exports = {}; | |
50 wnd.module = {exports: wnd.exports}; | |
51 | |
52 let url = urls.shift(); | |
53 let name = url.split("/").pop(); | |
54 return wnd.loadScript(url).then(() => | |
55 { | |
56 modules[name] = nodeunit.testCase(wnd.module.exports); | |
57 return loadNext(); | |
58 }); | |
59 } | |
60 | |
61 return Promise.resolve(modules); | |
62 })(); | |
63 } | 33 } |
64 | 34 |
65 function runTests(modules) | 35 function error(str) |
66 { | 36 { |
67 function bold(str) | 37 return "\u001B[31m" + str + "\u001B[39m"; |
68 { | |
69 return "\u001B[1m" + str + "\u001B[22m"; | |
70 } | |
71 | |
72 function ok(str) | |
73 { | |
74 return "\u001B[32m" + str + "\u001B[39m"; | |
75 } | |
76 | |
77 function error(str) | |
78 { | |
79 return "\u001B[31m" + str + "\u001B[39m"; | |
80 } | |
81 | |
82 return new Promise((resolve, reject) => | |
83 { | |
84 nodeunit.runModules(modules, { | |
85 moduleStart(name) | |
86 { | |
87 console.log(bold(name)); | |
88 }, | |
89 testDone(name, assertions) | |
90 { | |
91 let errors = assertions.filter(assertion => assertion.failed()) | |
92 .map(assertion => assertion.error); | |
93 | |
94 if (errors.length == 0) | |
95 console.log("\u2714 " + name); | |
96 else | |
97 { | |
98 console.log(error("\u2716 " + name) + "\n"); | |
99 errors.forEach(e => | |
100 { | |
101 if (e.stack) | |
102 console.log(e.stack); | |
103 else | |
104 console.log(String(e)); | |
105 console.log(""); | |
106 }); | |
107 } | |
108 }, | |
109 done(assertions) | |
110 { | |
111 let failures = assertions.filter(assertion => assertion.failed()); | |
112 if (failures.length) | |
113 { | |
114 console.log( | |
115 "\n" + | |
116 bold(error("FAILURES: ")) + | |
117 failures.length + "/" + assertions.length + " assertions failed" | |
118 ); | |
119 } | |
120 else | |
121 { | |
122 console.log( | |
123 "\n" + bold(ok("OK: ")) + | |
124 assertions.length + " assertions" | |
125 ); | |
126 } | |
127 | |
128 resolve(); | |
129 } | |
130 }); | |
131 }); | |
132 } | 38 } |
133 | 39 |
134 return loadScript(document, nodeunitUrl).then(() => | 40 let tests = {}; |
| 41 for (let module of moduleNames) |
| 42 tests[module] = nodeunit.testCase(require("./" + module + ".js")); |
| 43 |
| 44 return new Promise((resolve, reject) => |
135 { | 45 { |
136 return loadModules(moduleUrls); | 46 nodeunit.runModules(tests, { |
137 }).then(modules => | 47 moduleStart(name) |
138 { | 48 { |
139 return runTests(modules); | 49 console.log(bold(name)); |
| 50 }, |
| 51 testDone(name, assertions) |
| 52 { |
| 53 let errors = assertions.filter(assertion => assertion.failed()) |
| 54 .map(assertion => assertion.error); |
| 55 |
| 56 if (errors.length == 0) |
| 57 console.log("\u2714 " + name); |
| 58 else |
| 59 { |
| 60 console.log(error("\u2716 " + name) + "\n"); |
| 61 errors.forEach(e => |
| 62 { |
| 63 if (e.stack) |
| 64 console.log(e.stack); |
| 65 else |
| 66 console.log(String(e)); |
| 67 console.log(""); |
| 68 }); |
| 69 } |
| 70 }, |
| 71 done(assertions) |
| 72 { |
| 73 let failures = assertions.filter(assertion => assertion.failed()); |
| 74 if (failures.length) |
| 75 { |
| 76 console.log( |
| 77 "\n" + |
| 78 bold(error("FAILURES: ")) + |
| 79 failures.length + "/" + assertions.length + " assertions failed" |
| 80 ); |
| 81 } |
| 82 else |
| 83 { |
| 84 console.log( |
| 85 "\n" + bold(ok("OK: ")) + |
| 86 assertions.length + " assertions" |
| 87 ); |
| 88 } |
| 89 |
| 90 resolve(); |
| 91 } |
| 92 }); |
140 }); | 93 }); |
141 }); | 94 } |
| 95 |
| 96 module.exports = runTests; |
OLD | NEW |