Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: browsertests.js

Issue 29373596: Issue 4838 - Use nodeunit framework for integration tests running in browser (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore
Patch Set: Addressed comments and added additional minor changes Created Feb. 24, 2017, 9:17 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « README.md ('k') | package.json » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2017 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 // We are currently limited to ECMAScript 5 in this file, because it is being
23 // used in the browser tests. See https://issues.adblockplus.org/ticket/4796
24 // Once this is resolved we should use promises here.
25 function safeCall(callback)
26 {
27 return function()
28 {
29 try
30 {
31 callback.apply(this, arguments);
32 }
33 catch (e)
34 {
35 var message = String(e);
36 if (e.stack)
37 message += "\n" + e.stack;
38 console.log(message);
39 phantom.exit(1);
40 }
41 };
42 }
43
44 function loadScript(doc, url, callback)
45 {
46 var script = doc.createElement("script");
47 script.src = url;
48 script.async = false;
49 doc.head.appendChild(script);
50 if (callback)
51 window.setTimeout(callback, 0);
52 }
53
54 function loadModules(urls, callback)
55 {
56 var modules = {};
57
58 var loadNext = safeCall(function()
59 {
60 if (urls.length)
61 {
62 // Load each module into a new frame so that their scopes don't clash
63 var frame = document.createElement("iframe");
64 document.body.appendChild(frame);
65
66 var wnd = frame.contentWindow;
67 wnd.loadScript = loadScript.bind(null, wnd.document);
68 wnd.console = console;
69 wnd.require = require;
70 wnd.exports = {};
71 wnd.module = {exports: wnd.exports};
72
73 var url = urls.shift();
74 var name = url.split("/").pop();
75 wnd.loadScript(url, safeCall(function()
76 {
77 modules[name] = nodeunit.testCase(wnd.module.exports);
78 loadNext();
79 }));
80 }
81 else
82 callback(modules);
83 });
84
85 loadNext();
86 }
87
88 function runTests(modules, callback)
89 {
90 function bold(str)
91 {
92 return "\u001B[1m" + str + "\u001B[22m";
93 }
94
95 function ok(str)
96 {
97 return "\u001B[32m" + str + "\u001B[39m";
98 }
99
100 function error(str)
101 {
102 return "\u001B[31m" + str + "\u001B[39m";
103 }
104
105 nodeunit.runModules(modules, {
106 moduleStart: function(name)
107 {
108 console.log(bold(name));
109 },
110 testDone: function(name, assertions)
111 {
112 var errors = assertions.filter(function(assertion)
113 {
114 return assertion.failed();
115 }).map(function(assertion)
116 {
117 return assertion.error;
118 });
119
120 if (errors.length == 0)
121 console.log("\u2714 " + name);
122 else
123 {
124 console.log(error("\u2716 " + name) + "\n");
125 errors.forEach(function(error)
126 {
127 console.log(String(error));
128 if (error.stack)
129 console.log(error.stack);
130 console.log("");
131 });
132 }
133 },
134 done: function(assertions)
135 {
136 var failures = assertions.filter(function(assertion)
137 {
138 return assertion.failed();
139 });
140 if (failures.length)
141 {
142 console.log(
143 "\n" +
144 bold(error("FAILURES: ")) +
145 failures.length + "/" + assertions.length + " assertions failed"
146 );
147 }
148 else
149 {
150 console.log(
151 "\n" + bold(ok("OK: ")) +
152 assertions.length + " assertions"
153 );
154 }
155
156 callback(!failures.length);
157 }
158 });
159 }
160
161 function run()
162 {
163 var system = require("system");
164 var nodeunitUrl = system.args[1];
165 var urls = system.args.slice(2);
166
167 loadScript(document, nodeunitUrl, safeCall(function()
168 {
169 loadModules(urls, safeCall(function(modules)
170 {
171 runTests(modules, function(success)
172 {
173 phantom.exit(success ? 0 : 1);
174 });
175 }));
176 }));
177 }
178
179 safeCall(run)();
180 })();
OLDNEW
« no previous file with comments | « README.md ('k') | package.json » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld