OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2016 Eyeo GmbH |
| 4 * |
| 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 |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 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/>. |
| 16 */ |
| 17 |
| 18 "use strict"; |
| 19 |
| 20 (function() |
| 21 { |
| 22 // TODO: This should use promises once PhantomJS supports them, this will make |
| 23 // wrapping all async calls in order to catch errors unnecessary. |
| 24 function safeCall(callback) |
| 25 { |
| 26 return function() |
| 27 { |
| 28 try |
| 29 { |
| 30 callback.apply(this, arguments); |
| 31 } |
| 32 catch (e) |
| 33 { |
| 34 var message = String(e); |
| 35 if (e.stack) |
| 36 message += "\n" + e.stack; |
| 37 console.log(message); |
| 38 phantom.exit(); |
| 39 } |
| 40 } |
| 41 } |
| 42 |
| 43 function loadScript(url, callback) |
| 44 { |
| 45 var script = document.createElement("script"); |
| 46 script.src = url; |
| 47 script.async = false; |
| 48 document.head.appendChild(script); |
| 49 if (callback) |
| 50 window.setTimeout(callback, 0); |
| 51 } |
| 52 |
| 53 function run() |
| 54 { |
| 55 window.loadScript = loadScript; |
| 56 |
| 57 var system = require("system"); |
| 58 var nodeunitUrl = system.args[1]; |
| 59 var urls = system.args.slice(2); |
| 60 |
| 61 loadScript(nodeunitUrl, safeCall(function() |
| 62 { |
| 63 loadModules(urls, safeCall(function(modules) |
| 64 { |
| 65 runTests(modules, function() |
| 66 { |
| 67 phantom.exit(); |
| 68 }); |
| 69 })); |
| 70 })); |
| 71 } |
| 72 |
| 73 function loadModules(urls, callback) |
| 74 { |
| 75 var modules = {}; |
| 76 |
| 77 var loadNext = safeCall(function() |
| 78 { |
| 79 if (urls.length) |
| 80 { |
| 81 window.exports = {}; |
| 82 window.module = {exports: window.exports}; |
| 83 |
| 84 var url = urls.shift(); |
| 85 var name = url.split("/").pop(); |
| 86 loadScript(url, safeCall(function() |
| 87 { |
| 88 modules[name] = nodeunit.testCase(window.module.exports); |
| 89 delete window.exports; |
| 90 delete window.module; |
| 91 loadNext(); |
| 92 })); |
| 93 } |
| 94 else |
| 95 callback(modules); |
| 96 }); |
| 97 |
| 98 loadNext(); |
| 99 } |
| 100 |
| 101 function runTests(modules, callback) |
| 102 { |
| 103 function bold(str) |
| 104 { |
| 105 return "\u001B[1m" + str + "\u001B[22m"; |
| 106 } |
| 107 |
| 108 function ok(str) |
| 109 { |
| 110 return "\u001B[32m" + str + "\u001B[39m"; |
| 111 } |
| 112 |
| 113 function error(str) |
| 114 { |
| 115 return "\u001B[31m" + str + "\u001B[39m"; |
| 116 } |
| 117 |
| 118 nodeunit.runModules(modules, { |
| 119 moduleStart: function(name) |
| 120 { |
| 121 console.log(bold(name)); |
| 122 }, |
| 123 testDone: function(name, assertions) |
| 124 { |
| 125 var errors = assertions.filter(function(assertion) |
| 126 { |
| 127 return assertion.failed(); |
| 128 }).map(function(assertion) |
| 129 { |
| 130 return assertion.error; |
| 131 }); |
| 132 |
| 133 if (errors.length == 0) |
| 134 console.log("\u2714 " + name); |
| 135 else |
| 136 { |
| 137 console.log(error("\u2716 " + name) + "\n"); |
| 138 errors.forEach(function(error) |
| 139 { |
| 140 console.log(String(error)); |
| 141 if (error.stack) |
| 142 console.log(error.stack); |
| 143 console.log(""); |
| 144 }) |
| 145 } |
| 146 }, |
| 147 done: function(assertions) |
| 148 { |
| 149 var failures = assertions.filter(function(assertion) |
| 150 { |
| 151 return assertion.failed(); |
| 152 }); |
| 153 if (failures.length) |
| 154 { |
| 155 console.log( |
| 156 "\n" + |
| 157 bold(error("FAILURES: ")) + |
| 158 failures.length + "/" + assertions.length + " assertions failed" |
| 159 ); |
| 160 } |
| 161 else |
| 162 { |
| 163 console.log( |
| 164 "\n" + bold(ok("OK: ")) + |
| 165 assertions.length + " assertions" |
| 166 ); |
| 167 } |
| 168 |
| 169 callback(); |
| 170 } |
| 171 }); |
| 172 } |
| 173 |
| 174 safeCall(run)(); |
| 175 })(); |
OLD | NEW |