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 try |
| 21 { |
| 22 var fs = require("fs"); |
| 23 var system = require("system"); |
| 24 var webpage = require("webpage"); |
| 25 |
| 26 var nodeunitUrl = system.args[1]; |
| 27 var urls = system.args.slice(2); |
| 28 |
| 29 // No need to load anything, about:blank will do for us |
| 30 var page = webpage.create(); |
| 31 page.evaluate(initPage, nodeunitUrl); |
| 32 window.setTimeout(function() |
| 33 { |
| 34 runTests(urls, function() |
| 35 { |
| 36 phantom.exit(); |
| 37 }); |
| 38 }, 0); |
| 39 } |
| 40 catch(e) |
| 41 { |
| 42 console.error(e); |
| 43 phantom.exit(); |
| 44 } |
| 45 |
| 46 function runTests(urls, callback) |
| 47 { |
| 48 page.onError = function(msg, trace) |
| 49 { |
| 50 var message = "Error: " + msg + "\n"; |
| 51 if (trace) |
| 52 { |
| 53 for (var i = 0; i < trace.length; i++) |
| 54 { |
| 55 message += "\n " + trace[i].file + ":" + trace[i].line + |
| 56 (trace[i].function ? " in function " + trace[i].function |
| 57 : " in global code"); |
| 58 } |
| 59 } |
| 60 console.error(message); |
| 61 }; |
| 62 |
| 63 page.onCallback = function(data) |
| 64 { |
| 65 function bold(str) |
| 66 { |
| 67 return "\u001B[1m" + str + "\u001B[22m"; |
| 68 } |
| 69 |
| 70 function ok(str) |
| 71 { |
| 72 return "\u001B[32m" + str + "\u001B[39m"; |
| 73 } |
| 74 |
| 75 function error(str) |
| 76 { |
| 77 return "\u001B[31m" + str + "\u001B[39m"; |
| 78 } |
| 79 |
| 80 if (data.type == "moduleStart") |
| 81 console.log(bold(data.name)); |
| 82 else if (data.type == "testDone") |
| 83 { |
| 84 if (data.failures.length == 0) |
| 85 console.log("\u2714 " + data.name); |
| 86 else |
| 87 { |
| 88 console.log(error("\u2716 " + data.name) + "\n"); |
| 89 data.failures.forEach(function(failure) |
| 90 { |
| 91 console.log(failure.join("\n") + "\n"); |
| 92 }) |
| 93 } |
| 94 } |
| 95 else if (data.type == "done") |
| 96 { |
| 97 if (data.failures) |
| 98 { |
| 99 console.log( |
| 100 "\n" + |
| 101 bold(error("FAILURES: ")) + |
| 102 data.failures + "/" + data.assertions + " assertions failed" |
| 103 ); |
| 104 } |
| 105 else |
| 106 { |
| 107 console.log("\n" + bold(ok("OK: ")) + data.assertions + " assertions"); |
| 108 } |
| 109 |
| 110 page.close(); |
| 111 callback(); |
| 112 } |
| 113 else if (data.type == "log") |
| 114 { |
| 115 console.log(data.args.join(" ")); |
| 116 } |
| 117 else if (data.type == "error") |
| 118 { |
| 119 console.error(data.args.join(" ")); |
| 120 } |
| 121 }; |
| 122 |
| 123 page.evaluate(runTestsContent, urls); |
| 124 } |
| 125 |
| 126 function initPage(nodeunitUrl) |
| 127 { |
| 128 function normalizeArgs(args) |
| 129 { |
| 130 var normalized = []; |
| 131 for (var i = 0; i < args.length; i++) |
| 132 { |
| 133 try |
| 134 { |
| 135 if (typeof args[i] == "string") |
| 136 normalized.push(args[i]); |
| 137 else |
| 138 normalized.push(JSON.stringify(args[i])); |
| 139 } |
| 140 catch (e) |
| 141 { |
| 142 normalized.push(String(args[i])); |
| 143 } |
| 144 } |
| 145 return normalized; |
| 146 } |
| 147 |
| 148 window.console = { |
| 149 log: function() |
| 150 { |
| 151 window.callPhantom({type: "log", args: normalizeArgs(arguments)}); |
| 152 }, |
| 153 error: function() |
| 154 { |
| 155 window.callPhantom({type: "error", args: normalizeArgs(arguments)}); |
| 156 } |
| 157 }; |
| 158 |
| 159 window.loadScript = function(url, callback) |
| 160 { |
| 161 var script = document.createElement("script"); |
| 162 script.src = url; |
| 163 script.async = false; |
| 164 document.head.appendChild(script); |
| 165 if (callback) |
| 166 window.setTimeout(callback, 0); |
| 167 }; |
| 168 |
| 169 loadScript(nodeunitUrl); |
| 170 } |
| 171 |
| 172 function runTestsContent(urls) |
| 173 { |
| 174 var modules = {}; |
| 175 |
| 176 var loadNext = function() |
| 177 { |
| 178 if (urls.length) |
| 179 { |
| 180 window.exports = {}; |
| 181 window.module = {exports: window.exports}; |
| 182 |
| 183 var url = urls.shift(); |
| 184 var name = url.split("/").pop(); |
| 185 loadScript(url, function() |
| 186 { |
| 187 modules[name] = nodeunit.testCase(window.module.exports); |
| 188 loadNext(); |
| 189 }); |
| 190 } |
| 191 else |
| 192 { |
| 193 nodeunit.runModules(modules, { |
| 194 moduleStart: function(name) |
| 195 { |
| 196 window.callPhantom({type: "moduleStart", name: name}); |
| 197 }, |
| 198 testDone: function(name, assertions) |
| 199 { |
| 200 var failures = assertions.filter(function(assertion) |
| 201 { |
| 202 return assertion.failed(); |
| 203 }).map(function(assertion) |
| 204 { |
| 205 return [String(assertion.error), assertion.error.stack]; |
| 206 }); |
| 207 window.callPhantom({ |
| 208 type: "testDone", |
| 209 name: name, |
| 210 failures: failures |
| 211 }); |
| 212 }, |
| 213 done: function(assertions) |
| 214 { |
| 215 window.callPhantom({ |
| 216 type: "done", |
| 217 assertions: assertions.length, |
| 218 failures: assertions.filter(function(assertion) |
| 219 { |
| 220 return assertion.failed(); |
| 221 }).length |
| 222 }); |
| 223 } |
| 224 }); |
| 225 } |
| 226 } |
| 227 loadNext(); |
| 228 } |
OLD | NEW |