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-present eyeo GmbH | 3 * Copyright (C) 2006-present 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 require("mocha"); |
21 require("nodeunit"); | |
22 | 21 |
23 function runTests(moduleNames) | 22 if (typeof window._consoleLogs == "undefined") |
| 23 window._consoleLogs = {passes: 0, failures: 0, log: []}; |
| 24 |
| 25 function Reporter(runner) |
24 { | 26 { |
25 function bold(str) | 27 mocha.reporters.Base.call(this, runner); |
| 28 |
| 29 runner.on("pass", test => |
26 { | 30 { |
27 return "\u001B[1m" + str + "\u001B[22m"; | 31 window._consoleLogs.passes++; |
28 } | 32 console.log("pass: %s", test.fullTitle()); |
| 33 }); |
29 | 34 |
30 function ok(str) | 35 runner.on("fail", (test, err) => |
31 { | 36 { |
32 return "\u001B[32m" + str + "\u001B[39m"; | 37 window._consoleLogs.failures++; |
33 } | 38 console.log("fail: %s -- error: %s", test.fullTitle(), err.message); |
| 39 }); |
34 | 40 |
35 function error(str) | 41 runner.on("end", () => |
36 { | 42 { |
37 return "\u001B[31m" + str + "\u001B[39m"; | 43 console.log("end: %d/%d", window._consoleLogs.passes, |
38 } | 44 window._consoleLogs.passes + window._consoleLogs.failures); |
39 | |
40 let tests = {}; | |
41 for (let module of moduleNames) | |
42 tests[module] = nodeunit.testCase(require("./" + module + ".js")); | |
43 | |
44 return new Promise((resolve, reject) => | |
45 { | |
46 nodeunit.runModules(tests, { | |
47 moduleStart(name) | |
48 { | |
49 if (typeof window._consoleLogs == "undefined") | |
50 window._consoleLogs = {failures: 0, log: []}; | |
51 console.log(bold(name)); | |
52 }, | |
53 testDone(name, assertions) | |
54 { | |
55 let errors = assertions.filter(assertion => assertion.failed()) | |
56 .map(assertion => assertion.error); | |
57 | |
58 if (errors.length == 0) | |
59 console.log("\u2714 " + name); | |
60 else | |
61 { | |
62 console.log(error("\u2716 " + name) + "\n"); | |
63 errors.forEach(e => | |
64 { | |
65 if (e.stack) | |
66 console.log(e.stack); | |
67 else | |
68 console.log(String(e)); | |
69 console.log(""); | |
70 }); | |
71 } | |
72 }, | |
73 done(assertions) | |
74 { | |
75 let failures = assertions.filter(assertion => assertion.failed()); | |
76 if (failures.length) | |
77 { | |
78 window._consoleLogs.failures += failures.length; | |
79 console.log( | |
80 "\n" + | |
81 bold(error("FAILURES: ")) + | |
82 failures.length + "/" + assertions.length + " assertions failed" | |
83 ); | |
84 } | |
85 else | |
86 { | |
87 console.log( | |
88 `\n ${bold(ok("OK: "))}${assertions.length} assertions (${assertions
.duration}ms)` | |
89 ); | |
90 } | |
91 | |
92 resolve(); | |
93 } | |
94 }); | |
95 }); | 45 }); |
96 } | 46 } |
97 | 47 |
98 module.exports = runTests; | 48 function run(moduleNames) |
| 49 { |
| 50 mocha.setup("bdd"); |
| 51 mocha.reporter = Reporter; |
| 52 |
| 53 for (let module of moduleNames) |
| 54 { |
| 55 describe(module, () => |
| 56 { |
| 57 require("./" + module + ".js"); |
| 58 }); |
| 59 } |
| 60 |
| 61 mocha.run(); |
| 62 } |
| 63 |
| 64 module.exports = run; |
OLD | NEW |