e8414ae4dbcf4beb070cd9dd7d119847ff3759cb
1 var DygraphsLocalTester
= function() {
2 this.tc
= null; // Selected test case
4 this.resultsDiv
= null;
6 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 args
= splitVariables();
54 var command
= args
.command
;
56 // args.testCaseName uses the string name of the test.
57 if (args
.testCaseName
) {
58 var testCases
= getAllTestCases();
59 name
= args
.testCaseName
;
60 for (var idx
in testCases
) {
61 var entry
= testCases
[idx
];
62 if (entry
.name
== args
.testCaseName
) {
63 var prototype = entry
.testCase
;
64 this.tc
= new entry
.testCase();
68 } else if (args
.testCase
) { // The class name of the test.
70 eval("tc__= new " + args
.testCase
+ "()");
74 // If the test class is defined.
75 if (this.tc
!= null) {
76 if (args
.command
== "runAllTests") {
77 console
.log("Running all tests for " + args
.testCase
);
78 this.tc
.runAllTests();
79 } else if (args
.command
== "runTest") {
80 console
.log("Running test " + args
.testCase
+ "." + args
.test
);
81 this.tc
.runTest(args
.test
);
84 if (args
.command
== "runAllTests") {
85 console
.log("Running all tests for all test cases");
86 var testCases
= getAllTestCases();
87 for (var idx
in testCases
) {
88 var entry
= testCases
[idx
];
89 var prototype = entry
.testCase
;
90 this.tc
= new entry
.testCase();
91 this.tc
.runAllTests();
95 this.resultsDiv
= this.createResultsDiv();
97 this.resultsDiv
.appendChild(document
.createElement("hr"));
98 document
.getElementById('summary').innerHTML
= "(" + this.summary
.failed
+ " failed, " + this.summary
.passed
+ " passed)";
101 DygraphsLocalTester
.prototype.createResultsDiv
= function() {
102 div
= document
.createElement("div");
104 div
.innerHTML
= "Test results: <span id='summary'></span> <a href='#' id='passed'>passed</a> <a href='#' id='failed'>failed</a> <a href='#' id='all'>all</a><br/>";
106 var body
= document
.getElementsByTagName("body")[0];
107 body
.insertBefore(div
, body
.firstChild
);
109 var setByClassName
= function(name
, displayStyle
) {
110 var elements
= div
.getElementsByClassName(name
);
111 for (var i
= 0; i
< elements
.length
; i
++) {
112 elements
[i
].style
.display
= displayStyle
;
116 var passedAnchor
= document
.getElementById('passed');
117 var failedAnchor
= document
.getElementById('failed');
118 var allAnchor
= document
.getElementById('all');
119 passedAnchor
.onclick
= function() {
120 setByClassName('fail', 'none');
121 setByClassName('pass', 'block');
123 passedAnchor
.setAttribute("class", 'activeAnchor');
124 failedAnchor
.setAttribute("class", '');
126 failedAnchor
.onclick
= function() {
127 setByClassName('fail', 'block');
128 setByClassName('pass', 'none');
129 passedAnchor
.setAttribute("class", '');
130 failedAnchor
.setAttribute("class", 'activeAnchor');
132 allAnchor
.onclick
= function() {
133 setByClassName('fail', 'block');
134 setByClassName('pass', 'block');
135 passedAnchor
.setAttribute("class", '');
136 failedAnchor
.setAttribute("class", '');
141 DygraphsLocalTester
.prototype.postResults
= function() {
142 var table
= document
.createElement("table");
143 this.resultsDiv
.appendChild(table
);
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
.innerText
= result
.result
? 'pass' : 'fail';
152 tr
.appendChild(tdResult
);
154 var tdName
= document
.createElement("td");
155 tdName
.innerText
= result
.name
;
156 tr
.appendChild(tdName
);
158 var tdDuration
= document
.createElement("td");
159 tdDuration
.innerText
= result
.duration
;
160 tr
.appendChild(tdDuration
);
162 table
.appendChild(tr
);
166 DygraphsLocalTester
.prototype.run
= function() {
167 var selector
= document
.getElementById("selector");
169 if (selector
!= null) { // running a test
170 var createAttached
= function(name
, parent
) {
171 var elem
= document
.createElement(name
);
172 parent
.appendChild(elem
);
176 var description
= createAttached("div", selector
);
177 var list
= createAttached("ul", selector
);
178 var parent
= list
.parentElement
;
179 var createLink
= function(parent
, text
, url
) {
180 var li
= createAttached("li", parent
);
181 var a
= createAttached("a", li
);
185 if (this.tc
== null) {
186 description
.innerHTML
= "Test cases:";
187 var testCases
= getAllTestCases();
188 createLink(list
, "(run all tests)", document
.URL
+ "?command=runAllTests");
189 for (var idx
in testCases
) {
190 var entryName
= testCases
[idx
].name
;
191 createLink(list
, entryName
, document
.URL
+ "?testCaseName=" + entryName
);
194 description
.innerHTML
= "Tests for " + name
;
195 var names
= this.tc
.getTestNames();
196 createLink(list
, "Run All Tests", document
.URL
+ "&command=runAllTests");
197 for (var idx
in names
) {
198 var name
= names
[idx
];
199 createLink(list
, name
, document
.URL
+ "&test=" + name
+ "&command=runTest");
205 DygraphsLocalTester
.prototype.start_
= function(tc
) {
206 this.startms_
= new Date().getTime();
209 DygraphsLocalTester
.prototype.finish_
= function(tc
, name
, result
, e
) {
210 var endms_
= new Date().getTime();
212 name
: tc
.name
+ "." + name
,
214 duration
: endms_
- this.startms_
216 this.summary
.passed
+= result
? 1 : 0;
217 this.summary
.failed
+= result
? 0 : 1;