4cfd41de9d36f22ca485cd5c313c89f4a45a6785
[dygraphs.git] / auto_tests / misc / local.js
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);
8 return;
9 }
10 throw "Warnings not permitted: " + msg;
11 }
12 Dygraph.prototype.warn = Dygraph.warn;
13 }
14 };
15
16 var tc = null; // Selected test case
17 var name = null;
18
19 var resultDiv = null;
20
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);
24 var args = {};
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];
30 }
31 }
32 return args;
33 }
34
35 var args = splitVariables();
36 var test = args.test;
37 var command = args.command;
38
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();
48 break;
49 }
50 }
51 } else if (args.testCase) { // The class name of the test.
52 name = args.testCase;
53 eval("tc = new " + args.testCase + "()");
54 }
55
56 var results = null;
57 // If the test class is defined.
58 if (tc != null) {
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);
65 }
66 } else {
67 if (args.command == "runAllTests") {
68 console.log("Running all tests for all test cases");
69 var testCases = getAllTestCases();
70 results = {};
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();
76 }
77 }
78 }
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)";
84 }
85
86 function createResultsDiv() {
87 var body = document.getElementsByTagName("body")[0];
88 div = document.createElement("div");
89 div.id='results';
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);
92
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;
97 }
98 }
99
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');
106
107 passedAnchor.setAttribute("class", 'activeAnchor');
108 failedAnchor.setAttribute("class", '');
109 };
110 failedAnchor.onclick = function() {
111 setByClassName('fail', 'block');
112 setByClassName('pass', 'none');
113 passedAnchor.setAttribute("class", '');
114 failedAnchor.setAttribute("class", 'activeAnchor');
115 };
116 allAnchor.onclick = function() {
117 setByClassName('fail', 'block');
118 setByClassName('pass', 'block');
119 passedAnchor.setAttribute("class", '');
120 failedAnchor.setAttribute("class", '');
121 };
122 return div;
123 }
124
125 function postResults(results, summary, title) {
126 if (typeof(results) == "boolean") {
127 var elem = document.createElement("div");
128 elem.setAttribute("class", results ? 'pass' : 'fail');
129
130 var prefix = title ? (title + ": ") : "";
131 elem.innerHTML = prefix + '<span class=\'outcome\'>' + (results ? 'pass' : 'fail') + '</span>';
132 resultsDiv.appendChild(elem);
133 if (results) {
134 summary.passed++;
135 } else {
136 summary.failed++;
137 }
138 } else { // hash
139 var failed = 0;
140 var html = "";
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);
146 } else {
147 postResults(results[key], summary, key);
148 }
149 }
150 }
151 }
152 }
153
154 var run = function() {
155 var selector = document.getElementById("selector");
156
157 if (selector != null) { // running a test
158 var createAttached = function(name, parent) {
159 var elem = document.createElement(name);
160 parent.appendChild(elem);
161 return elem;
162 }
163
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);
170 a.innerHTML = text;
171 a.href = url;
172 }
173 if (tc == null) {
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);
180 }
181 } else {
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");
188 }
189 }
190 }
191 }