Index: browsertests.js |
=================================================================== |
new file mode 100644 |
--- /dev/null |
+++ b/browsertests.js |
@@ -0,0 +1,175 @@ |
+/* |
+ * This file is part of Adblock Plus <https://adblockplus.org/>, |
+ * Copyright (C) 2006-2016 Eyeo GmbH |
+ * |
+ * Adblock Plus is free software: you can redistribute it and/or modify |
+ * it under the terms of the GNU General Public License version 3 as |
+ * published by the Free Software Foundation. |
+ * |
+ * Adblock Plus is distributed in the hope that it will be useful, |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
+ * GNU General Public License for more details. |
+ * |
+ * You should have received a copy of the GNU General Public License |
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
+ */ |
+ |
+"use strict"; |
+ |
+(function() |
+{ |
+ // TODO: This should use promises once PhantomJS supports them, this will make |
+ // wrapping all async calls in order to catch errors unnecessary. |
+ function safeCall(callback) |
+ { |
+ return function() |
+ { |
+ try |
+ { |
+ callback.apply(this, arguments); |
+ } |
+ catch (e) |
+ { |
+ var message = String(e); |
+ if (e.stack) |
+ message += "\n" + e.stack; |
+ console.log(message); |
+ phantom.exit(); |
+ } |
+ } |
+ } |
+ |
+ function loadScript(url, callback) |
+ { |
+ var script = document.createElement("script"); |
+ script.src = url; |
+ script.async = false; |
+ document.head.appendChild(script); |
+ if (callback) |
+ window.setTimeout(callback, 0); |
+ } |
+ |
+ function run() |
+ { |
+ window.loadScript = loadScript; |
+ |
+ var system = require("system"); |
+ var nodeunitUrl = system.args[1]; |
+ var urls = system.args.slice(2); |
+ |
+ loadScript(nodeunitUrl, safeCall(function() |
+ { |
+ loadModules(urls, safeCall(function(modules) |
+ { |
+ runTests(modules, function() |
+ { |
+ phantom.exit(); |
+ }); |
+ })); |
+ })); |
+ } |
+ |
+ function loadModules(urls, callback) |
+ { |
+ var modules = {}; |
+ |
+ var loadNext = safeCall(function() |
+ { |
+ if (urls.length) |
+ { |
+ window.exports = {}; |
+ window.module = {exports: window.exports}; |
+ |
+ var url = urls.shift(); |
+ var name = url.split("/").pop(); |
+ loadScript(url, safeCall(function() |
+ { |
+ modules[name] = nodeunit.testCase(window.module.exports); |
+ delete window.exports; |
+ delete window.module; |
+ loadNext(); |
+ })); |
+ } |
+ else |
+ callback(modules); |
+ }); |
+ |
+ loadNext(); |
+ } |
+ |
+ function runTests(modules, callback) |
+ { |
+ function bold(str) |
+ { |
+ return "\u001B[1m" + str + "\u001B[22m"; |
+ } |
+ |
+ function ok(str) |
+ { |
+ return "\u001B[32m" + str + "\u001B[39m"; |
+ } |
+ |
+ function error(str) |
+ { |
+ return "\u001B[31m" + str + "\u001B[39m"; |
+ } |
+ |
+ nodeunit.runModules(modules, { |
+ moduleStart: function(name) |
+ { |
+ console.log(bold(name)); |
+ }, |
+ testDone: function(name, assertions) |
+ { |
+ var errors = assertions.filter(function(assertion) |
+ { |
+ return assertion.failed(); |
+ }).map(function(assertion) |
+ { |
+ return assertion.error; |
+ }); |
+ |
+ if (errors.length == 0) |
+ console.log("\u2714 " + name); |
+ else |
+ { |
+ console.log(error("\u2716 " + name) + "\n"); |
+ errors.forEach(function(error) |
+ { |
+ console.log(String(error)); |
+ if (error.stack) |
+ console.log(error.stack); |
+ console.log(""); |
+ }) |
+ } |
+ }, |
+ done: function(assertions) |
+ { |
+ var failures = assertions.filter(function(assertion) |
+ { |
+ return assertion.failed(); |
+ }); |
+ if (failures.length) |
+ { |
+ console.log( |
+ "\n" + |
+ bold(error("FAILURES: ")) + |
+ failures.length + "/" + assertions.length + " assertions failed" |
+ ); |
+ } |
+ else |
+ { |
+ console.log( |
+ "\n" + bold(ok("OK: ")) + |
+ assertions.length + " assertions" |
+ ); |
+ } |
+ |
+ callback(); |
+ } |
+ }); |
+ } |
+ |
+ safeCall(run)(); |
+})(); |