1 var DygraphsLocalTester
= function() {
2 this.tc
= null; // Selected test case
5 this.summary
= { failed
: 0, passed
: 0 };
9 jstestdriver
.attachListener({
10 start
: function(tc
) {
13 finish
: function(tc
, name
, result
, e
) {
14 self
.finish_(tc
, name
, result
, e
);
20 * Call this to replace Dygraphs.warn so it throws an error.
22 * In some cases we will still allow warnings to be warnings, however.
24 DygraphsLocalTester
.prototype.overrideWarn
= function() {
25 // save Dygraph.warn so we can catch warnings.
26 var originalDygraphWarn
= Dygraph
.warn
;
27 Dygraph
.warn
= function(msg
) {
28 // This warning is still
29 if (msg
== "Using default labels. Set labels explicitly via 'labels' in the options parameter") {
30 originalDygraphWarn(msg
);
33 throw 'Warnings not permitted: ' + msg
;
35 Dygraph
.prototype.warn
= Dygraph
.warn
;
38 DygraphsLocalTester
.prototype.processVariables
= function() {
39 var splitVariables
= function() { // http://www.idealog.us/2006/06/javascript_to_p
.html
40 var query
= window
.location
.search
.substring(1);
42 var vars
= query
.split('&');
43 for (var i
= 0; i
< vars
.length
; i
++) {
44 if (vars
[i
].length
> 0) {
45 var pair
= vars
[i
].split('=');
46 args
[pair
[0]] = pair
[1];
52 var findTestCase
= function(stringName
, className
) {
54 var testCases
= getAllTestCases();
55 for (var idx
in testCases
) {
56 var entry
= testCases
[idx
];
57 if (entry
.name
== stringName
) {
58 var prototype = entry
.testCase
;
59 return new entry
.testCase();
62 } else if (className
) {
63 eval('tc__ = new ' + className
+ '()');
69 var args
= splitVariables();
70 this.tc
= findTestCase(args
.testCaseName
, args
.testCase
);
71 this.test
= args
.test
;
72 this.command
= args
.command
;
75 DygraphsLocalTester
.prototype.createAnchor
= function(href
, id
, text
) {
76 var a
= document
.createElement('a');
83 DygraphsLocalTester
.prototype.createResultsDiv
= function(summary
, durationms
) {
84 div
= document
.createElement('div');
87 var body
= document
.getElementsByTagName('body')[0];
88 body
.insertBefore(div
, body
.firstChild
);
90 var addText
= function(div
, text
) {
91 div
.appendChild(document
.createTextNode(text
));
94 var passedAnchor
= this.createAnchor('#', 'passed', '' + summary
.passed
+ ' passed');
95 var failedAnchor
= this.createAnchor('#', 'failed', '' + summary
.failed
+ ' failed');
96 var allAnchor
= this.createAnchor('#', 'all', '(view all)');
98 addText(div
, 'Test results: ');
99 div
.appendChild(passedAnchor
);
101 div
.appendChild(failedAnchor
);
103 div
.appendChild(allAnchor
);
104 addText(div
, ', (' + durationms
+ ' ms)');
106 var table
= document
.createElement('table');
107 div
.appendChild(table
);
108 div
.appendChild(document
.createElement('hr'));
110 var setByClassName
= function(name
, displayStyle
) {
111 var elements
= table
.getElementsByClassName(name
);
112 for (var i
= 0; i
< elements
.length
; i
++) {
113 elements
[i
].style
.display
= displayStyle
;
117 passedAnchor
.onclick
= function() {
118 setByClassName('fail', 'none');
119 setByClassName('pass', 'block');
121 passedAnchor
.setAttribute('class', 'activeAnchor');
122 failedAnchor
.setAttribute('class', '');
124 failedAnchor
.onclick
= function() {
125 setByClassName('fail', 'block');
126 setByClassName('pass', 'none');
127 passedAnchor
.setAttribute('class', '');
128 failedAnchor
.setAttribute('class', 'activeAnchor');
130 allAnchor
.onclick
= function() {
131 setByClassName('fail', 'block');
132 setByClassName('pass', 'block');
133 passedAnchor
.setAttribute('class', '');
134 failedAnchor
.setAttribute('class', '');
139 DygraphsLocalTester
.prototype.postResults
= function(summary
, durationms
) {
140 var resultsDiv
= this.createResultsDiv(summary
, durationms
);
142 var table
= resultsDiv
.getElementsByTagName('table')[0];
143 for (var idx
= 0; idx
< this.results
.length
; idx
++) {
144 var result
= this.results
[idx
];
145 var tr
= document
.createElement('tr');
146 tr
.setAttribute('class', result
.result
? 'pass' : 'fail');
148 var tdResult
= document
.createElement('td');
149 tdResult
.setAttribute('class', 'outcome');
150 tdResult
.innerText
= result
.result
? 'pass' : 'fail';
151 tr
.appendChild(tdResult
);
153 var tdName
= document
.createElement('td');
154 var s
= result
.name
.split('.');
155 var url
= window
.location
.pathname
+ '?testCaseName=' + s
[0] + '&test=' + s
[1] + '&command=runTest';
156 var a
= this.createAnchor(url
, null, result
.name
);
158 tdName
.appendChild(a
);
159 tr
.appendChild(tdName
);
161 var tdDuration
= document
.createElement('td');
162 tdDuration
.innerText
= result
.duration
+ ' ms';
163 tr
.appendChild(tdDuration
);
166 var tdDetails
= document
.createElement('td');
167 var a
= this.createAnchor('#', null, '(stack trace)');
168 a
.onclick
= function(e
) {
170 alert(e
+ '\n' + e
.stack
);
173 tdDetails
.appendChild(a
);
174 tr
.appendChild(tdDetails
);
177 table
.appendChild(tr
);
181 DygraphsLocalTester
.prototype.listTests
= function() {
182 var selector
= document
.getElementById('selector');
184 if (selector
!= null) { // running a test
185 var createAttached
= function(name
, parent
) {
186 var elem
= document
.createElement(name
);
187 parent
.appendChild(elem
);
191 var description
= createAttached('div', selector
);
192 var list
= createAttached('ul', selector
);
193 var parent
= list
.parentElement
;
194 var createLink
= function(parent
, title
, url
) {
195 var li
= createAttached('li', parent
);
196 var a
= createAttached('a', li
);
201 if (this.tc
== null) {
202 description
.innerHTML
= 'Test cases:';
203 var testCases
= getAllTestCases();
204 createLink(list
, '(run all tests)', document
.URL
+ '?command=runAllTests');
205 for (var idx
in testCases
) {
206 var entryName
= testCases
[idx
].name
;
207 createLink(list
, entryName
, document
.URL
+ '?testCaseName=' + entryName
);
210 description
.innerHTML
= 'Tests for ' + name
;
211 var names
= this.tc
.getTestNames();
212 createLink(list
, 'Run All Tests', document
.URL
+ '&command=runAllTests');
213 for (var idx
in names
) {
214 var name
= names
[idx
];
215 createLink(list
, name
, document
.URL
+ '&test=' + name
+ '&command=runTest');
221 DygraphsLocalTester
.prototype.run
= function() {
222 var executed
= false;
223 var start
= new Date(). getTime();
224 if (this.tc
!= null) {
225 if (this.command
== 'runAllTests') {
226 console
.log('Running all tests for ' + this.tc
.name
);
227 this.tc
.runAllTests();
229 } else if (this.command
== 'runTest') {
230 console
.log('Running test ' + this.tc
.name
+ '.' + this.test
);
231 this.tc
.runTest(this.test
);
234 } else if (this.command
== 'runAllTests') {
235 console
.log('Running all tests for all test cases');
236 var testCases
= getAllTestCases();
237 for (var idx
in testCases
) {
238 var entry
= testCases
[idx
];
239 var prototype = entry
.testCase
;
240 this.tc
= new entry
.testCase();
241 this.tc
.runAllTests();
246 var durationms
= new Date().getTime() - start
;
249 this.postResults(this.summary
, durationms
);
255 DygraphsLocalTester
.prototype.start_
= function(tc
) {
256 this.startms_
= new Date().getTime();
259 DygraphsLocalTester
.prototype.finish_
= function(tc
, name
, result
, e
) {
260 var endms_
= new Date().getTime();
262 name
: tc
.name
+ '.' + name
,
264 duration
: endms_
- this.startms_
,
267 this.summary
.passed
+= result
? 1 : 0;
268 this.summary
.failed
+= result
? 0 : 1;