remove all traces of Dygraph.log
[dygraphs.git] / auto_tests / misc / local.js
1 'use strict';
2
3 var DygraphsLocalTester = function() {
4 this.tc = null; // Selected test case
5 this.name = null;
6 this.results = [];
7 this.summary = { failed: 0, passed: 0 };
8 this.start;
9
10 var self = this;
11 jstestdriver.attachListener({
12 start : function(tc) {
13 self.start_(tc);
14 },
15 finish : function(tc, name, result, e) {
16 self.finish_(tc, name, result, e);
17 }
18 });
19 };
20
21 /**
22 * Call this to replace Dygraphs.warn so it throws an error.
23 *
24 * In some cases we will still allow warnings to be warnings, however.
25 */
26 DygraphsLocalTester.prototype.overrideWarn = function() {
27 // save console.warn so we can catch warnings.
28 var originalWarn = console.warn;
29 console.warn = function(msg) {
30 // This warning is pervasive enough that we'll let it slide (for now).
31 if (msg == "Using default labels. Set labels explicitly via 'labels' in the options parameter") {
32 originalWarn(msg);
33 return;
34 }
35 throw 'Warnings not permitted: ' + msg;
36 }
37 };
38
39 DygraphsLocalTester.prototype.processVariables = function() {
40 var splitVariables = function() { // http://www.idealog.us/2006/06/javascript_to_p.html
41 var query = window.location.search.substring(1);
42 var args = {};
43 var vars = query.split('&');
44 for (var i = 0; i < vars.length; i++) {
45 if (vars[i].length > 0) {
46 var pair = vars[i].split('=');
47 args[pair[0]] = pair[1];
48 }
49 }
50 return args;
51 }
52
53 var findTestCase = function(stringName, className) {
54 if (stringName) {
55 var testCases = getAllTestCases();
56 for (var idx in testCases) {
57 var entry = testCases[idx];
58 if (entry.name == stringName) {
59 var prototype = entry.testCase;
60 return new entry.testCase();
61 }
62 }
63 } else if (className) {
64 eval('tc__ = new ' + className + '()');
65 return tc__;
66 }
67 return null;
68 }
69
70 var args = splitVariables();
71 this.tc = findTestCase(args.testCaseName, args.testCase);
72 this.test = args.test;
73 this.command = args.command;
74 }
75
76 DygraphsLocalTester.prototype.createAnchor = function(href, id, text) {
77 var a = document.createElement('a');
78 a.href = href;
79 a.id = id;
80 a.textContent = text;
81 return a;
82 }
83
84 DygraphsLocalTester.prototype.createResultsDiv = function(summary, durationms) {
85 var div = document.createElement('div');
86 div.id='results';
87
88 var body = document.getElementsByTagName('body')[0];
89 body.insertBefore(div, body.firstChild);
90
91 var addText = function(div, text) {
92 div.appendChild(document.createTextNode(text));
93 };
94
95 var passedAnchor = this.createAnchor('#', 'passed', '' + summary.passed + ' passed');
96 var failedAnchor = this.createAnchor('#', 'failed', '' + summary.failed + ' failed');
97 var allAnchor = this.createAnchor('#', 'all', '(view all)');
98
99 addText(div, 'Test results: ');
100 div.appendChild(passedAnchor);
101 addText(div, ', ');
102 div.appendChild(failedAnchor);
103 addText(div, ', ');
104 div.appendChild(allAnchor);
105 addText(div, ', (' + durationms + ' ms)');
106
107 var table = document.createElement('table');
108 div.appendChild(table);
109 div.appendChild(document.createElement('hr'));
110
111 var setByClassName = function(name, displayStyle) {
112 var elements = table.getElementsByClassName(name);
113 for (var i = 0; i < elements.length; i++) {
114 elements[i].style.display = displayStyle;
115 }
116 }
117
118 passedAnchor.onclick = function() {
119 setByClassName('fail', 'none');
120 setByClassName('pass', 'block');
121
122 passedAnchor.setAttribute('class', 'activeAnchor');
123 failedAnchor.setAttribute('class', '');
124 };
125 failedAnchor.onclick = function() {
126 setByClassName('fail', 'block');
127 setByClassName('pass', 'none');
128 passedAnchor.setAttribute('class', '');
129 failedAnchor.setAttribute('class', 'activeAnchor');
130 };
131 allAnchor.onclick = function() {
132 setByClassName('fail', 'block');
133 setByClassName('pass', 'block');
134 passedAnchor.setAttribute('class', '');
135 failedAnchor.setAttribute('class', '');
136 };
137 return div;
138 }
139
140 DygraphsLocalTester.prototype.postResults = function(summary, durationms) {
141 var resultsDiv = this.createResultsDiv(summary, durationms);
142
143 var table = resultsDiv.getElementsByTagName('table')[0];
144 for (var idx = 0; idx < this.results.length; idx++) {
145 var result = this.results[idx];
146 var tr = document.createElement('tr');
147 tr.setAttribute('class', result.result ? 'pass' : 'fail');
148
149 var tdResult = document.createElement('td');
150 tdResult.setAttribute('class', 'outcome');
151 tdResult.textContent = result.result ? 'pass' : 'fail';
152 tr.appendChild(tdResult);
153
154 var tdName = document.createElement('td');
155 var s = result.name.split('.');
156 var url = window.location.pathname + '?testCaseName=' + s[0] + '&test=' + s[1] + '&command=runTest';
157 var a = this.createAnchor(url, null, result.name);
158
159 tdName.appendChild(a);
160 tr.appendChild(tdName);
161
162 var tdDuration = document.createElement('td');
163 tdDuration.textContent = result.duration + ' ms';
164 tr.appendChild(tdDuration);
165
166 if (result.e) {
167 var tdDetails = document.createElement('td');
168 var a = this.createAnchor('#', null, '(stack trace)');
169 a.onclick = function(e) {
170 return function() {
171 alert(e + '\n' + e.stack);
172 };
173 }(result.e);
174 tdDetails.appendChild(a);
175 tr.appendChild(tdDetails);
176 }
177
178 table.appendChild(tr);
179 }
180 }
181
182 DygraphsLocalTester.prototype.listTests = function() {
183 var selector = document.getElementById('selector');
184
185 if (selector != null) { // running a test
186 var createAttached = function(name, parent) {
187 var elem = document.createElement(name);
188 parent.appendChild(elem);
189 return elem;
190 }
191
192 var description = createAttached('div', selector);
193 var list = createAttached('ul', selector);
194 var parent = list.parentElement;
195 var createLink = function(parent, title, url) {
196 var li = createAttached('li', parent);
197 var a = createAttached('a', li);
198 a.textContent = title;
199 a.href = url;
200 return li;
201 }
202 if (this.tc == null) {
203 description.textContent = 'Test cases:';
204 var testCases = getAllTestCases();
205 createLink(list, '(run all tests)', document.URL + '?command=runAllTests');
206 for (var idx in testCases) {
207 var entryName = testCases[idx].name;
208 createLink(list, entryName, document.URL + '?testCaseName=' + entryName);
209 }
210 } else {
211 description.textContent = 'Tests for ' + name;
212 var names = this.tc.getTestNames();
213 createLink(list, 'Run All Tests', document.URL + '&command=runAllTests');
214 for (var idx in names) {
215 var name = names[idx];
216 createLink(list, name, document.URL + '&test=' + name + '&command=runTest');
217 }
218 }
219 }
220 }
221
222 DygraphsLocalTester.prototype.run = function() {
223 var executed = false;
224 var start = new Date(). getTime();
225 if (this.tc != null) {
226 if (this.command == 'runAllTests') {
227 console.log('Running all tests for ' + this.tc.name);
228 this.tc.runAllTests();
229 executed = true;
230 } else if (this.command == 'runTest') {
231 console.log('Running test ' + this.tc.name + '.' + this.test);
232 this.tc.runTest(this.test);
233 executed = true;
234 }
235 } else if (this.command == 'runAllTests') {
236 console.log('Running all tests for all test cases');
237 var testCases = getAllTestCases();
238 for (var idx in testCases) {
239 var entry = testCases[idx];
240 var prototype = entry.testCase;
241 this.tc = new entry.testCase();
242 this.tc.runAllTests();
243 }
244 executed = true;
245 }
246
247 var durationms = new Date().getTime() - start;
248
249 if (executed) {
250 this.postResults(this.summary, durationms);
251 } else {
252 this.listTests();
253 }
254 }
255
256 DygraphsLocalTester.prototype.start_ = function(tc) {
257 this.startms_ = new Date().getTime();
258 }
259
260 DygraphsLocalTester.prototype.finish_ = function(tc, name, result, e) {
261 var endms_ = new Date().getTime();
262 this.results.push({
263 name : tc.name + '.' + name,
264 result : result,
265 duration : endms_ - this.startms_,
266 e : e
267 });
268 this.summary.passed += result ? 1 : 0;
269 this.summary.failed += result ? 0 : 1;
270 }