4cfd41de9d36f22ca485cd5c313c89f4a45a6785
1 var overrideWarn
= function() {
2 // save Dygraph.warn so we can catch warnings.
3 if (false) { // Set true if you want warnings to cause failures.
4 var originalDygraphWarn
= Dygraph
.warn
;
5 Dygraph
.warn
= function(msg
) {
6 if (msg
== "Using default labels. Set labels explicitly via 'labels' in the options parameter") {
7 originalDygraphWarn(msg
);
10 throw "Warnings not permitted: " + msg
;
12 Dygraph
.prototype.warn
= Dygraph
.warn
;
16 var tc
= null; // Selected test case
21 function processVariables() {
22 var splitVariables
= function() { // http://www.idealog.us/2006/06/javascript_to_p
.html
23 var query
= window
.location
.search
.substring(1);
25 var vars
= query
.split("&");
26 for (var i
= 0; i
< vars
.length
; i
++) {
27 if (vars
[i
].length
> 0) {
28 var pair
= vars
[i
].split("=");
29 args
[pair
[0]] = pair
[1];
35 var args
= splitVariables();
37 var command
= args
.command
;
39 // args.testCaseName uses the string name of the test.
40 if (args
.testCaseName
) {
41 var testCases
= getAllTestCases();
42 name
= args
.testCaseName
;
43 for (var idx
in testCases
) {
44 var entry
= testCases
[idx
];
45 if (entry
.name
== args
.testCaseName
) {
46 var prototype = entry
.testCase
;
47 tc
= new entry
.testCase();
51 } else if (args
.testCase
) { // The class name of the test.
53 eval("tc = new " + args
.testCase
+ "()");
57 // If the test class is defined.
59 if (args
.command
== "runAllTests") {
60 console
.log("Running all tests for " + args
.testCase
);
61 results
= tc
.runAllTests();
62 } else if (args
.command
== "runTest") {
63 console
.log("Running test " + args
.testCase
+ "." + args
.test
);
64 results
= tc
.runTest(args
.test
);
67 if (args
.command
== "runAllTests") {
68 console
.log("Running all tests for all test cases");
69 var testCases
= getAllTestCases();
71 for (var idx
in testCases
) {
72 var entry
= testCases
[idx
];
73 var prototype = entry
.testCase
;
74 tc
= new entry
.testCase();
75 results
[entry
.name
] = tc
.runAllTests();
79 resultsDiv
= createResultsDiv();
80 var summary
= { failed
: 0, passed
: 0 };
81 postResults(results
, summary
);
82 resultsDiv
.appendChild(document
.createElement("hr"));
83 document
.getElementById('summary').innerHTML
= "(" + summary
.failed
+ " failed, " + summary
.passed
+ " passed)";
86 function createResultsDiv() {
87 var body
= document
.getElementsByTagName("body")[0];
88 div
= document
.createElement("div");
90 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/>";
91 body
.insertBefore(div
, body
.firstChild
);
93 var setByClassName
= function(name
, displayStyle
) {
94 var elements
= div
.getElementsByClassName(name
);
95 for (var i
= 0; i
< elements
.length
; i
++) {
96 elements
[i
].style
.display
= displayStyle
;
100 var passedAnchor
= document
.getElementById('passed');
101 var failedAnchor
= document
.getElementById('failed');
102 var allAnchor
= document
.getElementById('all');
103 passedAnchor
.onclick
= function() {
104 setByClassName('fail', 'none');
105 setByClassName('pass', 'block');
107 passedAnchor
.setAttribute("class", 'activeAnchor');
108 failedAnchor
.setAttribute("class", '');
110 failedAnchor
.onclick
= function() {
111 setByClassName('fail', 'block');
112 setByClassName('pass', 'none');
113 passedAnchor
.setAttribute("class", '');
114 failedAnchor
.setAttribute("class", 'activeAnchor');
116 allAnchor
.onclick
= function() {
117 setByClassName('fail', 'block');
118 setByClassName('pass', 'block');
119 passedAnchor
.setAttribute("class", '');
120 failedAnchor
.setAttribute("class", '');
125 function postResults(results
, summary
, title
) {
126 if (typeof(results
) == "boolean") {
127 var elem
= document
.createElement("div");
128 elem
.setAttribute("class", results
? 'pass' : 'fail');
130 var prefix
= title
? (title
+ ": ") : "";
131 elem
.innerHTML
= prefix
+ '<span class=\'outcome\'>' + (results
? 'pass' : 'fail') + '</span>';
132 resultsDiv
.appendChild(elem
);
141 for (var key
in results
) {
142 if (results
.hasOwnProperty(key
)) {
143 var elem
= results
[key
];
144 if (typeof(elem
) == "boolean" && title
) {
145 postResults(results
[key
], summary
, title
+ "." + key
);
147 postResults(results
[key
], summary
, key
);
154 var run
= function() {
155 var selector
= document
.getElementById("selector");
157 if (selector
!= null) { // running a test
158 var createAttached
= function(name
, parent
) {
159 var elem
= document
.createElement(name
);
160 parent
.appendChild(elem
);
164 var description
= createAttached("div", selector
);
165 var list
= createAttached("ul", selector
);
166 var parent
= list
.parentElement
;
167 var createLink
= function(parent
, text
, url
) {
168 var li
= createAttached("li", parent
);
169 var a
= createAttached("a", li
);
174 description
.innerHTML
= "Test cases:";
175 var testCases
= getAllTestCases();
176 createLink(list
, "(run all tests)", document
.URL
+ "?command=runAllTests");
177 for (var idx
in testCases
) {
178 var entryName
= testCases
[idx
].name
;
179 createLink(list
, entryName
, document
.URL
+ "?testCaseName=" + entryName
);
182 description
.innerHTML
= "Tests for " + name
;
183 var names
= tc
.getTestNames();
184 createLink(list
, "Run All Tests", document
.URL
+ "&command=runAllTests");
185 for (var idx
in names
) {
186 var name
= names
[idx
];
187 createLink(list
, name
, document
.URL
+ "&test=" + name
+ "&command=runTest");