1 var DygraphsLocalTester
= function() {
2 this.tc
= null; // Selected test case
8 * Call this to replace Dygraphs.warn so it throws an error.
10 * In some cases we will still allow warnings to be warnings, however.
12 DygraphsLocalTester
.prototype.overrideWarn
= function() {
13 // save Dygraph.warn so we can catch warnings.
14 var originalDygraphWarn
= Dygraph
.warn
;
15 Dygraph
.warn
= function(msg
) {
16 // This warning is still
17 if (msg
== "Using default labels. Set labels explicitly via 'labels' in the options parameter") {
18 originalDygraphWarn(msg
);
21 throw "Warnings not permitted: " + msg
;
23 Dygraph
.prototype.warn
= Dygraph
.warn
;
26 DygraphsLocalTester
.prototype.processVariables
= function() {
27 var splitVariables
= function() { // http://www.idealog.us/2006/06/javascript_to_p
.html
28 var query
= window
.location
.search
.substring(1);
30 var vars
= query
.split("&");
31 for (var i
= 0; i
< vars
.length
; i
++) {
32 if (vars
[i
].length
> 0) {
33 var pair
= vars
[i
].split("=");
34 args
[pair
[0]] = pair
[1];
40 var args
= splitVariables();
42 var command
= args
.command
;
44 // args.testCaseName uses the string name of the test.
45 if (args
.testCaseName
) {
46 var testCases
= getAllTestCases();
47 name
= args
.testCaseName
;
48 for (var idx
in testCases
) {
49 var entry
= testCases
[idx
];
50 if (entry
.name
== args
.testCaseName
) {
51 var prototype = entry
.testCase
;
52 tc
= new entry
.testCase();
56 } else if (args
.testCase
) { // The class name of the test.
58 eval("tc = new " + args
.testCase
+ "()");
62 // If the test class is defined.
63 if (this.tc
!= null) {
64 if (args
.command
== "runAllTests") {
65 console
.log("Running all tests for " + args
.testCase
);
66 results
= this.tc
.runAllTests();
67 } else if (args
.command
== "runTest") {
68 console
.log("Running test " + args
.testCase
+ "." + args
.test
);
69 results
= this.tc
.runTest(args
.test
);
72 if (args
.command
== "runAllTests") {
73 console
.log("Running all tests for all test cases");
74 var testCases
= getAllTestCases();
76 for (var idx
in testCases
) {
77 var entry
= testCases
[idx
];
78 var prototype = entry
.testCase
;
79 this.tc
= new entry
.testCase();
80 results
[entry
.name
] = this.tc
.runAllTests();
84 this.resultsDiv
= this.createResultsDiv();
85 var summary
= { failed
: 0, passed
: 0 };
86 this.postResults(results
, summary
);
87 this.resultsDiv
.appendChild(document
.createElement("hr"));
88 document
.getElementById('summary').innerHTML
= "(" + summary
.failed
+ " failed, " + summary
.passed
+ " passed)";
91 DygraphsLocalTester
.prototype.createResultsDiv
= function() {
92 var body
= document
.getElementsByTagName("body")[0];
93 div
= document
.createElement("div");
95 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/>";
96 body
.insertBefore(div
, body
.firstChild
);
98 var setByClassName
= function(name
, displayStyle
) {
99 var elements
= div
.getElementsByClassName(name
);
100 for (var i
= 0; i
< elements
.length
; i
++) {
101 elements
[i
].style
.display
= displayStyle
;
105 var passedAnchor
= document
.getElementById('passed');
106 var failedAnchor
= document
.getElementById('failed');
107 var allAnchor
= document
.getElementById('all');
108 passedAnchor
.onclick
= function() {
109 setByClassName('fail', 'none');
110 setByClassName('pass', 'block');
112 passedAnchor
.setAttribute("class", 'activeAnchor');
113 failedAnchor
.setAttribute("class", '');
115 failedAnchor
.onclick
= function() {
116 setByClassName('fail', 'block');
117 setByClassName('pass', 'none');
118 passedAnchor
.setAttribute("class", '');
119 failedAnchor
.setAttribute("class", 'activeAnchor');
121 allAnchor
.onclick
= function() {
122 setByClassName('fail', 'block');
123 setByClassName('pass', 'block');
124 passedAnchor
.setAttribute("class", '');
125 failedAnchor
.setAttribute("class", '');
130 DygraphsLocalTester
.prototype.postResults
= function(results
, summary
, title
) {
131 if (typeof(results
) == "boolean") {
132 var elem
= document
.createElement("div");
133 elem
.setAttribute("class", results
? 'pass' : 'fail');
135 var prefix
= title
? (title
+ ": ") : "";
136 elem
.innerHTML
= prefix
+ '<span class=\'outcome\'>' + (results
? 'pass' : 'fail') + '</span>';
137 this.resultsDiv
.appendChild(elem
);
146 for (var key
in results
) {
147 if (results
.hasOwnProperty(key
)) {
148 var elem
= results
[key
];
149 if (typeof(elem
) == "boolean" && title
) {
150 this.postResults(results
[key
], summary
, title
+ "." + key
);
152 this.postResults(results
[key
], summary
, key
);
159 DygraphsLocalTester
.prototype.run
= function() {
160 var selector
= document
.getElementById("selector");
162 if (selector
!= null) { // running a test
163 var createAttached
= function(name
, parent
) {
164 var elem
= document
.createElement(name
);
165 parent
.appendChild(elem
);
169 var description
= createAttached("div", selector
);
170 var list
= createAttached("ul", selector
);
171 var parent
= list
.parentElement
;
172 var createLink
= function(parent
, text
, url
) {
173 var li
= createAttached("li", parent
);
174 var a
= createAttached("a", li
);
178 if (this.tc
== null) {
179 description
.innerHTML
= "Test cases:";
180 var testCases
= getAllTestCases();
181 createLink(list
, "(run all tests)", document
.URL
+ "?command=runAllTests");
182 for (var idx
in testCases
) {
183 var entryName
= testCases
[idx
].name
;
184 createLink(list
, entryName
, document
.URL
+ "?testCaseName=" + entryName
);
187 description
.innerHTML
= "Tests for " + name
;
188 var names
= tc
.getTestNames();
189 createLink(list
, "Run All Tests", document
.URL
+ "&command=runAllTests");
190 for (var idx
in names
) {
191 var name
= names
[idx
];
192 createLink(list
, name
, document
.URL
+ "&test=" + name
+ "&command=runTest");