3 var DygraphsLocalTester
= function() {
4 this.tc
= null; // Selected test case
7 this.summary
= { failed
: 0, passed
: 0 };
11 jstestdriver
.attachListener({
12 start
: function(tc
) {
15 finish
: function(tc
, name
, result
, e
) {
16 self
.finish_(tc
, name
, result
, e
);
22 * Call this to replace Dygraphs.warn so it throws an error.
24 * In some cases we will still allow warnings to be warnings, however.
26 DygraphsLocalTester
.prototype.overrideWarn
= function() {
27 // save Dygraph.warn so we can catch warnings.
28 var originalDygraphWarn
= Dygraph
.warn
;
29 Dygraph
.warn
= function(msg
) {
30 // This warning is still
31 if (msg
== "Using default labels. Set labels explicitly via 'labels' in the options parameter") {
32 originalDygraphWarn(msg
);
35 throw 'Warnings not permitted: ' + msg
;
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);
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];
53 var findTestCase
= function(stringName
, className
) {
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();
63 } else if (className
) {
64 eval('tc__ = new ' + className
+ '()');
70 var args
= splitVariables();
71 this.tc
= findTestCase(args
.testCaseName
, args
.testCase
);
72 this.test
= args
.test
;
73 this.command
= args
.command
;
76 DygraphsLocalTester
.prototype.createAnchor
= function(href
, id
, text
) {
77 var a
= document
.createElement('a');
84 DygraphsLocalTester
.prototype.createResultsDiv
= function(summary
, durationms
) {
85 var div
= document
.createElement('div');
88 var body
= document
.getElementsByTagName('body')[0];
89 body
.insertBefore(div
, body
.firstChild
);
91 var addText
= function(div
, text
) {
92 div
.appendChild(document
.createTextNode(text
));
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)');
99 addText(div
, 'Test results: ');
100 div
.appendChild(passedAnchor
);
102 div
.appendChild(failedAnchor
);
104 div
.appendChild(allAnchor
);
105 addText(div
, ', (' + durationms
+ ' ms)');
107 var table
= document
.createElement('table');
108 div
.appendChild(table
);
109 div
.appendChild(document
.createElement('hr'));
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
;
118 passedAnchor
.onclick
= function() {
119 setByClassName('fail', 'none');
120 setByClassName('pass', 'block');
122 passedAnchor
.setAttribute('class', 'activeAnchor');
123 failedAnchor
.setAttribute('class', '');
125 failedAnchor
.onclick
= function() {
126 setByClassName('fail', 'block');
127 setByClassName('pass', 'none');
128 passedAnchor
.setAttribute('class', '');
129 failedAnchor
.setAttribute('class', 'activeAnchor');
131 allAnchor
.onclick
= function() {
132 setByClassName('fail', 'block');
133 setByClassName('pass', 'block');
134 passedAnchor
.setAttribute('class', '');
135 failedAnchor
.setAttribute('class', '');
140 DygraphsLocalTester
.prototype.postResults
= function(summary
, durationms
) {
141 var resultsDiv
= this.createResultsDiv(summary
, durationms
);
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');
149 var tdResult
= document
.createElement('td');
150 tdResult
.setAttribute('class', 'outcome');
151 tdResult
.textContent
= result
.result
? 'pass' : 'fail';
152 tr
.appendChild(tdResult
);
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
);
159 tdName
.appendChild(a
);
160 tr
.appendChild(tdName
);
162 var tdDuration
= document
.createElement('td');
163 tdDuration
.textContent
= result
.duration
+ ' ms';
164 tr
.appendChild(tdDuration
);
167 var tdDetails
= document
.createElement('td');
168 var a
= this.createAnchor('#', null, '(stack trace)');
169 a
.onclick
= function(e
) {
171 alert(e
+ '\n' + e
.stack
);
174 tdDetails
.appendChild(a
);
175 tr
.appendChild(tdDetails
);
178 table
.appendChild(tr
);
182 DygraphsLocalTester
.prototype.listTests
= function() {
183 var selector
= document
.getElementById('selector');
185 if (selector
!= null) { // running a test
186 var createAttached
= function(name
, parent
) {
187 var elem
= document
.createElement(name
);
188 parent
.appendChild(elem
);
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
;
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
);
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');
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();
230 } else if (this.command
== 'runTest') {
231 console
.log('Running test ' + this.tc
.name
+ '.' + this.test
);
232 this.tc
.runTest(this.test
);
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();
247 var durationms
= new Date().getTime() - start
;
250 this.postResults(this.summary
, durationms
);
256 DygraphsLocalTester
.prototype.start_
= function(tc
) {
257 this.startms_
= new Date().getTime();
260 DygraphsLocalTester
.prototype.finish_
= function(tc
, name
, result
, e
) {
261 var endms_
= new Date().getTime();
263 name
: tc
.name
+ '.' + name
,
265 duration
: endms_
- this.startms_
,
268 this.summary
.passed
+= result
? 1 : 0;
269 this.summary
.failed
+= result
? 0 : 1;