Commit | Line | Data |
---|---|---|
11c21001 RK |
1 | var DygraphsLocalTester = function() { |
2 | this.tc = null; // Selected test case | |
3 | this.name = null; | |
4 | this.resultDiv = null; | |
5 | }; | |
6 | ||
7 | /** | |
8 | * Call this to replace Dygraphs.warn so it throws an error. | |
9 | * | |
10 | * In some cases we will still allow warnings to be warnings, however. | |
11 | */ | |
12 | DygraphsLocalTester.prototype.overrideWarn = function() { | |
243eecfb | 13 | // save Dygraph.warn so we can catch warnings. |
11c21001 RK |
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); | |
19 | return; | |
243eecfb | 20 | } |
11c21001 | 21 | throw "Warnings not permitted: " + msg; |
243eecfb | 22 | } |
11c21001 | 23 | Dygraph.prototype.warn = Dygraph.warn; |
243eecfb RK |
24 | }; |
25 | ||
11c21001 | 26 | DygraphsLocalTester.prototype.processVariables = function() { |
243eecfb RK |
27 | var splitVariables = function() { // http://www.idealog.us/2006/06/javascript_to_p.html |
28 | var query = window.location.search.substring(1); | |
29 | var args = {}; | |
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]; | |
35 | } | |
36 | } | |
37 | return args; | |
38 | } | |
39 | ||
40 | var args = splitVariables(); | |
41 | var test = args.test; | |
42 | var command = args.command; | |
43 | ||
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(); | |
53 | break; | |
54 | } | |
55 | } | |
56 | } else if (args.testCase) { // The class name of the test. | |
57 | name = args.testCase; | |
58 | eval("tc = new " + args.testCase + "()"); | |
59 | } | |
60 | ||
61 | var results = null; | |
62 | // If the test class is defined. | |
11c21001 | 63 | if (this.tc != null) { |
243eecfb RK |
64 | if (args.command == "runAllTests") { |
65 | console.log("Running all tests for " + args.testCase); | |
11c21001 | 66 | results = this.tc.runAllTests(); |
243eecfb RK |
67 | } else if (args.command == "runTest") { |
68 | console.log("Running test " + args.testCase + "." + args.test); | |
11c21001 | 69 | results = this.tc.runTest(args.test); |
243eecfb RK |
70 | } |
71 | } else { | |
72 | if (args.command == "runAllTests") { | |
73 | console.log("Running all tests for all test cases"); | |
74 | var testCases = getAllTestCases(); | |
75 | results = {}; | |
76 | for (var idx in testCases) { | |
77 | var entry = testCases[idx]; | |
78 | var prototype = entry.testCase; | |
11c21001 RK |
79 | this.tc = new entry.testCase(); |
80 | results[entry.name] = this.tc.runAllTests(); | |
243eecfb RK |
81 | } |
82 | } | |
83 | } | |
11c21001 | 84 | this.resultsDiv = this.createResultsDiv(); |
243eecfb | 85 | var summary = { failed: 0, passed: 0 }; |
11c21001 RK |
86 | this.postResults(results, summary); |
87 | this.resultsDiv.appendChild(document.createElement("hr")); | |
243eecfb RK |
88 | document.getElementById('summary').innerHTML = "(" + summary.failed + " failed, " + summary.passed + " passed)"; |
89 | } | |
90 | ||
11c21001 | 91 | DygraphsLocalTester.prototype.createResultsDiv = function() { |
243eecfb RK |
92 | var body = document.getElementsByTagName("body")[0]; |
93 | div = document.createElement("div"); | |
94 | div.id='results'; | |
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); | |
97 | ||
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; | |
102 | } | |
103 | } | |
104 | ||
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'); | |
111 | ||
112 | passedAnchor.setAttribute("class", 'activeAnchor'); | |
113 | failedAnchor.setAttribute("class", ''); | |
114 | }; | |
115 | failedAnchor.onclick = function() { | |
116 | setByClassName('fail', 'block'); | |
117 | setByClassName('pass', 'none'); | |
118 | passedAnchor.setAttribute("class", ''); | |
119 | failedAnchor.setAttribute("class", 'activeAnchor'); | |
120 | }; | |
121 | allAnchor.onclick = function() { | |
122 | setByClassName('fail', 'block'); | |
123 | setByClassName('pass', 'block'); | |
124 | passedAnchor.setAttribute("class", ''); | |
125 | failedAnchor.setAttribute("class", ''); | |
126 | }; | |
127 | return div; | |
128 | } | |
129 | ||
11c21001 | 130 | DygraphsLocalTester.prototype.postResults = function(results, summary, title) { |
243eecfb RK |
131 | if (typeof(results) == "boolean") { |
132 | var elem = document.createElement("div"); | |
133 | elem.setAttribute("class", results ? 'pass' : 'fail'); | |
134 | ||
135 | var prefix = title ? (title + ": ") : ""; | |
136 | elem.innerHTML = prefix + '<span class=\'outcome\'>' + (results ? 'pass' : 'fail') + '</span>'; | |
11c21001 | 137 | this.resultsDiv.appendChild(elem); |
243eecfb RK |
138 | if (results) { |
139 | summary.passed++; | |
140 | } else { | |
141 | summary.failed++; | |
142 | } | |
143 | } else { // hash | |
144 | var failed = 0; | |
145 | var html = ""; | |
146 | for (var key in results) { | |
147 | if (results.hasOwnProperty(key)) { | |
148 | var elem = results[key]; | |
149 | if (typeof(elem) == "boolean" && title) { | |
11c21001 | 150 | this.postResults(results[key], summary, title + "." + key); |
243eecfb | 151 | } else { |
11c21001 | 152 | this.postResults(results[key], summary, key); |
243eecfb RK |
153 | } |
154 | } | |
155 | } | |
156 | } | |
157 | } | |
158 | ||
11c21001 | 159 | DygraphsLocalTester.prototype.run = function() { |
243eecfb RK |
160 | var selector = document.getElementById("selector"); |
161 | ||
162 | if (selector != null) { // running a test | |
163 | var createAttached = function(name, parent) { | |
164 | var elem = document.createElement(name); | |
165 | parent.appendChild(elem); | |
166 | return elem; | |
167 | } | |
168 | ||
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); | |
175 | a.innerHTML = text; | |
176 | a.href = url; | |
177 | } | |
11c21001 | 178 | if (this.tc == null) { |
243eecfb RK |
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); | |
185 | } | |
186 | } else { | |
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"); | |
193 | } | |
194 | } | |
195 | } | |
196 | } |