Left: | ||
Right: |
LEFT | RIGHT |
---|---|
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 | 21 |
22 (function(nodeunitUrl, ...moduleUrls) | 22 (function(nodeunitUrl, ...moduleUrls) |
Felix Dahlke
2017/05/03 09:54:51
As discussed on IRC, I find this approach of loadi
Wladimir Palant
2017/05/03 12:19:42
This isn't a module, we are loading code in global
| |
23 { | 23 { |
24 function loadScript(doc, url) | 24 function loadScript(doc, url) |
25 { | 25 { |
26 return new Promise((resolve, reject) => | 26 return new Promise((resolve, reject) => |
27 { | 27 { |
28 let script = doc.createElement("script"); | 28 let script = doc.createElement("script"); |
29 script.src = url; | 29 script.src = url; |
30 script.onload = resolve; | 30 script.onload = resolve; |
31 doc.head.appendChild(script); | 31 doc.head.appendChild(script); |
32 }); | 32 }); |
33 } | 33 } |
34 | 34 |
35 function loadModules(urls) | 35 function loadModules(urls) |
36 { | 36 { |
37 let modules = {}; | 37 let modules = {}; |
38 | 38 |
39 let loadNext = () => | 39 return (function loadNext() |
Felix Dahlke
2017/05/03 09:54:51
I might be missing the reason for using let here,
Wladimir Palant
2017/05/03 12:19:42
Not sure whether it makes the code better but ok.
| |
40 { | 40 { |
41 if (urls.length) | 41 if (urls.length) |
42 { | 42 { |
43 // Load each module into a new frame so that their scopes don't clash | 43 // Load each module into a new frame so that their scopes don't clash |
44 let frame = document.createElement("iframe"); | 44 let frame = document.createElement("iframe"); |
45 document.body.appendChild(frame); | 45 document.body.appendChild(frame); |
46 | 46 |
47 let wnd = frame.contentWindow; | 47 let wnd = frame.contentWindow; |
48 wnd.loadScript = url => loadScript(wnd.document, url); | 48 wnd.loadScript = url => loadScript(wnd.document, url); |
49 wnd.exports = {}; | 49 wnd.exports = {}; |
50 wnd.module = {exports: wnd.exports}; | 50 wnd.module = {exports: wnd.exports}; |
51 | 51 |
52 let url = urls.shift(); | 52 let url = urls.shift(); |
53 let name = url.split("/").pop(); | 53 let name = url.split("/").pop(); |
54 return wnd.loadScript(url).then(() => | 54 return wnd.loadScript(url).then(() => |
55 { | 55 { |
56 modules[name] = nodeunit.testCase(wnd.module.exports); | 56 modules[name] = nodeunit.testCase(wnd.module.exports); |
57 return loadNext(); | 57 return loadNext(); |
58 }); | 58 }); |
59 } | 59 } |
60 | 60 |
61 return Promise.resolve(modules); | 61 return Promise.resolve(modules); |
62 }; | 62 })(); |
63 | |
64 return loadNext(); | |
65 } | 63 } |
66 | 64 |
67 function runTests(modules) | 65 function runTests(modules) |
68 { | 66 { |
69 function bold(str) | 67 function bold(str) |
70 { | 68 { |
71 return "\u001B[1m" + str + "\u001B[22m"; | 69 return "\u001B[1m" + str + "\u001B[22m"; |
72 } | 70 } |
73 | 71 |
74 function ok(str) | 72 function ok(str) |
75 { | 73 { |
76 return "\u001B[32m" + str + "\u001B[39m"; | 74 return "\u001B[32m" + str + "\u001B[39m"; |
77 } | 75 } |
78 | 76 |
79 function error(str) | 77 function error(str) |
80 { | 78 { |
81 return "\u001B[31m" + str + "\u001B[39m"; | 79 return "\u001B[31m" + str + "\u001B[39m"; |
82 } | 80 } |
83 | 81 |
84 return new Promise((resolve, reject) => | 82 return new Promise((resolve, reject) => |
85 { | 83 { |
86 nodeunit.runModules(modules, { | 84 nodeunit.runModules(modules, { |
87 moduleStart(name) | 85 moduleStart(name) |
88 { | 86 { |
89 console.log(bold(name)); | 87 console.log(bold(name)); |
90 }, | 88 }, |
91 testDone(name, assertions) | 89 testDone(name, assertions) |
92 { | 90 { |
93 let errors = assertions.filter(assertion => | 91 let errors = assertions.filter(assertion => assertion.failed()) |
94 { | 92 .map(assertion => assertion.error); |
95 return assertion.failed(); | |
96 }).map(assertion => | |
97 { | |
98 return assertion.error; | |
99 }); | |
Felix Dahlke
2017/05/03 09:54:51
Nit: How I see it, this could be shortened quite a
Wladimir Palant
2017/05/03 12:19:42
Done.
| |
100 | 93 |
101 if (errors.length == 0) | 94 if (errors.length == 0) |
102 console.log("\u2714 " + name); | 95 console.log("\u2714 " + name); |
103 else | 96 else |
104 { | 97 { |
105 console.log(error("\u2716 " + name) + "\n"); | 98 console.log(error("\u2716 " + name) + "\n"); |
106 errors.forEach(e => | 99 errors.forEach(e => |
107 { | 100 { |
108 if (e.stack) | 101 if (e.stack) |
109 console.log(e.stack); | 102 console.log(e.stack); |
110 else | 103 else |
111 console.log(String(e)); | 104 console.log(String(e)); |
112 console.log(""); | 105 console.log(""); |
113 }); | 106 }); |
114 } | 107 } |
115 }, | 108 }, |
116 done(assertions) | 109 done(assertions) |
117 { | 110 { |
118 let failures = assertions.filter(assertion => | 111 let failures = assertions.filter(assertion => assertion.failed()); |
Felix Dahlke
2017/05/03 09:54:51
Nit: As above, I think this can be shortened:
l
Wladimir Palant
2017/05/03 12:19:42
Done.
| |
119 { | |
120 return assertion.failed(); | |
121 }); | |
122 if (failures.length) | 112 if (failures.length) |
123 { | 113 { |
124 console.log( | 114 console.log( |
125 "\n" + | 115 "\n" + |
126 bold(error("FAILURES: ")) + | 116 bold(error("FAILURES: ")) + |
127 failures.length + "/" + assertions.length + " assertions failed" | 117 failures.length + "/" + assertions.length + " assertions failed" |
128 ); | 118 ); |
129 } | 119 } |
130 else | 120 else |
131 { | 121 { |
(...skipping 10 matching lines...) Expand all Loading... | |
142 } | 132 } |
143 | 133 |
144 return loadScript(document, nodeunitUrl).then(() => | 134 return loadScript(document, nodeunitUrl).then(() => |
145 { | 135 { |
146 return loadModules(moduleUrls); | 136 return loadModules(moduleUrls); |
147 }).then(modules => | 137 }).then(modules => |
148 { | 138 { |
149 return runTests(modules); | 139 return runTests(modules); |
150 }); | 140 }); |
151 }); | 141 }); |
LEFT | RIGHT |