Commit | Line | Data |
---|---|---|
11c21001 RK |
1 | var DygraphsLocalTester = function() { |
2 | this.tc = null; // Selected test case | |
3 | this.name = null; | |
add4749b RK |
4 | this.results = []; |
5 | this.summary = { failed: 0, passed: 0 }; | |
02b1c284 | 6 | this.start; |
add4749b RK |
7 | |
8 | var self = this; | |
9 | jstestdriver.attachListener({ | |
10 | start : function(tc) { | |
11 | self.start_(tc); | |
12 | }, | |
13 | finish : function(tc, name, result, e) { | |
14 | self.finish_(tc, name, result, e); | |
15 | } | |
16 | }); | |
11c21001 RK |
17 | }; |
18 | ||
19 | /** | |
20 | * Call this to replace Dygraphs.warn so it throws an error. | |
21 | * | |
22 | * In some cases we will still allow warnings to be warnings, however. | |
23 | */ | |
24 | DygraphsLocalTester.prototype.overrideWarn = function() { | |
243eecfb | 25 | // save Dygraph.warn so we can catch warnings. |
11c21001 RK |
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); | |
31 | return; | |
243eecfb | 32 | } |
02b1c284 | 33 | throw 'Warnings not permitted: ' + msg; |
243eecfb | 34 | } |
11c21001 | 35 | Dygraph.prototype.warn = Dygraph.warn; |
243eecfb RK |
36 | }; |
37 | ||
11c21001 | 38 | DygraphsLocalTester.prototype.processVariables = function() { |
243eecfb RK |
39 | var splitVariables = function() { // http://www.idealog.us/2006/06/javascript_to_p.html |
40 | var query = window.location.search.substring(1); | |
41 | var args = {}; | |
02b1c284 | 42 | var vars = query.split('&'); |
243eecfb RK |
43 | for (var i = 0; i < vars.length; i++) { |
44 | if (vars[i].length > 0) { | |
02b1c284 | 45 | var pair = vars[i].split('='); |
243eecfb RK |
46 | args[pair[0]] = pair[1]; |
47 | } | |
48 | } | |
49 | return args; | |
50 | } | |
51 | ||
02b1c284 RK |
52 | var findTestCase = function(stringName, className) { |
53 | if (stringName) { | |
243eecfb | 54 | var testCases = getAllTestCases(); |
243eecfb RK |
55 | for (var idx in testCases) { |
56 | var entry = testCases[idx]; | |
02b1c284 RK |
57 | if (entry.name == stringName) { |
58 | var prototype = entry.testCase; | |
59 | return new entry.testCase(); | |
60 | } | |
243eecfb | 61 | } |
02b1c284 RK |
62 | } else if (className) { |
63 | eval('tc__ = new ' + className + '()'); | |
64 | return tc__; | |
243eecfb | 65 | } |
02b1c284 | 66 | return null; |
243eecfb | 67 | } |
02b1c284 RK |
68 | |
69 | var args = splitVariables(); | |
70 | this.tc = findTestCase(args.testCaseName, args.testCase); | |
71 | this.test = args.test; | |
72 | this.command = args.command; | |
73 | } | |
74 | ||
75 | DygraphsLocalTester.prototype.createAnchor = function(href, id, text) { | |
76 | var a = document.createElement('a'); | |
77 | a.href = href; | |
78 | a.id = id; | |
79 | a.innerText = text; | |
80 | return a; | |
243eecfb RK |
81 | } |
82 | ||
02b1c284 RK |
83 | DygraphsLocalTester.prototype.createResultsDiv = function(summary, durationms) { |
84 | div = document.createElement('div'); | |
243eecfb | 85 | div.id='results'; |
add4749b | 86 | |
02b1c284 | 87 | var body = document.getElementsByTagName('body')[0]; |
243eecfb RK |
88 | body.insertBefore(div, body.firstChild); |
89 | ||
02b1c284 RK |
90 | var addText = function(div, text) { |
91 | div.appendChild(document.createTextNode(text)); | |
92 | }; | |
93 | ||
94 | var passedAnchor = this.createAnchor('#', 'passed', '' + summary.passed + ' passed'); | |
95 | var failedAnchor = this.createAnchor('#', 'failed', '' + summary.failed + ' failed'); | |
96 | var allAnchor = this.createAnchor('#', 'all', '(view all)'); | |
97 | ||
98 | addText(div, 'Test results: '); | |
99 | div.appendChild(passedAnchor); | |
100 | addText(div, ', '); | |
101 | div.appendChild(failedAnchor); | |
102 | addText(div, ', '); | |
103 | div.appendChild(allAnchor); | |
104 | addText(div, ', (' + durationms + ' ms)'); | |
105 | ||
106 | var table = document.createElement('table'); | |
107 | div.appendChild(table); | |
108 | div.appendChild(document.createElement('hr')); | |
109 | ||
243eecfb | 110 | var setByClassName = function(name, displayStyle) { |
02b1c284 | 111 | var elements = table.getElementsByClassName(name); |
243eecfb RK |
112 | for (var i = 0; i < elements.length; i++) { |
113 | elements[i].style.display = displayStyle; | |
114 | } | |
115 | } | |
116 | ||
243eecfb RK |
117 | passedAnchor.onclick = function() { |
118 | setByClassName('fail', 'none'); | |
119 | setByClassName('pass', 'block'); | |
120 | ||
02b1c284 RK |
121 | passedAnchor.setAttribute('class', 'activeAnchor'); |
122 | failedAnchor.setAttribute('class', ''); | |
243eecfb RK |
123 | }; |
124 | failedAnchor.onclick = function() { | |
125 | setByClassName('fail', 'block'); | |
126 | setByClassName('pass', 'none'); | |
02b1c284 RK |
127 | passedAnchor.setAttribute('class', ''); |
128 | failedAnchor.setAttribute('class', 'activeAnchor'); | |
243eecfb RK |
129 | }; |
130 | allAnchor.onclick = function() { | |
131 | setByClassName('fail', 'block'); | |
132 | setByClassName('pass', 'block'); | |
02b1c284 RK |
133 | passedAnchor.setAttribute('class', ''); |
134 | failedAnchor.setAttribute('class', ''); | |
243eecfb RK |
135 | }; |
136 | return div; | |
137 | } | |
138 | ||
02b1c284 RK |
139 | DygraphsLocalTester.prototype.postResults = function(summary, durationms) { |
140 | var resultsDiv = this.createResultsDiv(summary, durationms); | |
141 | ||
142 | var table = resultsDiv.getElementsByTagName('table')[0]; | |
add4749b RK |
143 | for (var idx = 0; idx < this.results.length; idx++) { |
144 | var result = this.results[idx]; | |
02b1c284 RK |
145 | var tr = document.createElement('tr'); |
146 | tr.setAttribute('class', result.result ? 'pass' : 'fail'); | |
7cf85dd5 | 147 | |
02b1c284 RK |
148 | var tdResult = document.createElement('td'); |
149 | tdResult.setAttribute('class', 'outcome'); | |
7cf85dd5 RK |
150 | tdResult.innerText = result.result ? 'pass' : 'fail'; |
151 | tr.appendChild(tdResult); | |
152 | ||
02b1c284 RK |
153 | var tdName = document.createElement('td'); |
154 | var s = result.name.split('.'); | |
155 | var url = window.location.pathname + '?testCaseName=' + s[0] + '&test=' + s[1] + '&command=runTest'; | |
156 | var a = this.createAnchor(url, null, result.name); | |
157 | ||
158 | tdName.appendChild(a); | |
7cf85dd5 RK |
159 | tr.appendChild(tdName); |
160 | ||
02b1c284 RK |
161 | var tdDuration = document.createElement('td'); |
162 | tdDuration.innerText = result.duration + ' ms'; | |
7cf85dd5 RK |
163 | tr.appendChild(tdDuration); |
164 | ||
b99970f8 | 165 | if (result.e) { |
02b1c284 RK |
166 | var tdDetails = document.createElement('td'); |
167 | var a = this.createAnchor('#', null, '(stack trace)'); | |
168 | a.onclick = function(e) { | |
b99970f8 | 169 | return function() { |
02b1c284 | 170 | alert(e + '\n' + e.stack); |
b99970f8 RK |
171 | }; |
172 | }(result.e); | |
02b1c284 | 173 | tdDetails.appendChild(a); |
b99970f8 RK |
174 | tr.appendChild(tdDetails); |
175 | } | |
176 | ||
7cf85dd5 | 177 | table.appendChild(tr); |
243eecfb RK |
178 | } |
179 | } | |
180 | ||
02b1c284 RK |
181 | DygraphsLocalTester.prototype.listTests = function() { |
182 | var selector = document.getElementById('selector'); | |
243eecfb RK |
183 | |
184 | if (selector != null) { // running a test | |
185 | var createAttached = function(name, parent) { | |
186 | var elem = document.createElement(name); | |
187 | parent.appendChild(elem); | |
188 | return elem; | |
189 | } | |
190 | ||
02b1c284 RK |
191 | var description = createAttached('div', selector); |
192 | var list = createAttached('ul', selector); | |
243eecfb | 193 | var parent = list.parentElement; |
02b1c284 RK |
194 | var createLink = function(parent, title, url) { |
195 | var li = createAttached('li', parent); | |
196 | var a = createAttached('a', li); | |
197 | a.innerHTML = title; | |
243eecfb | 198 | a.href = url; |
02b1c284 | 199 | return li; |
243eecfb | 200 | } |
11c21001 | 201 | if (this.tc == null) { |
02b1c284 | 202 | description.innerHTML = 'Test cases:'; |
243eecfb | 203 | var testCases = getAllTestCases(); |
02b1c284 | 204 | createLink(list, '(run all tests)', document.URL + '?command=runAllTests'); |
243eecfb RK |
205 | for (var idx in testCases) { |
206 | var entryName = testCases[idx].name; | |
02b1c284 | 207 | createLink(list, entryName, document.URL + '?testCaseName=' + entryName); |
243eecfb RK |
208 | } |
209 | } else { | |
02b1c284 | 210 | description.innerHTML = 'Tests for ' + name; |
add4749b | 211 | var names = this.tc.getTestNames(); |
02b1c284 | 212 | createLink(list, 'Run All Tests', document.URL + '&command=runAllTests'); |
243eecfb RK |
213 | for (var idx in names) { |
214 | var name = names[idx]; | |
02b1c284 | 215 | createLink(list, name, document.URL + '&test=' + name + '&command=runTest'); |
243eecfb RK |
216 | } |
217 | } | |
218 | } | |
219 | } | |
add4749b | 220 | |
02b1c284 RK |
221 | DygraphsLocalTester.prototype.run = function() { |
222 | var executed = false; | |
223 | var start = new Date(). getTime(); | |
224 | if (this.tc != null) { | |
225 | if (this.command == 'runAllTests') { | |
226 | console.log('Running all tests for ' + this.tc.name); | |
227 | this.tc.runAllTests(); | |
228 | executed = true; | |
229 | } else if (this.command == 'runTest') { | |
230 | console.log('Running test ' + this.tc.name + '.' + this.test); | |
231 | this.tc.runTest(this.test); | |
232 | executed = true; | |
233 | } | |
234 | } else if (this.command == 'runAllTests') { | |
235 | console.log('Running all tests for all test cases'); | |
236 | var testCases = getAllTestCases(); | |
237 | for (var idx in testCases) { | |
238 | var entry = testCases[idx]; | |
239 | var prototype = entry.testCase; | |
240 | this.tc = new entry.testCase(); | |
241 | this.tc.runAllTests(); | |
242 | } | |
243 | executed = true; | |
244 | } | |
245 | ||
246 | var durationms = new Date().getTime() - start; | |
247 | ||
248 | if (executed) { | |
249 | this.postResults(this.summary, durationms); | |
250 | } else { | |
251 | this.listTests(); | |
252 | } | |
253 | } | |
254 | ||
add4749b RK |
255 | DygraphsLocalTester.prototype.start_ = function(tc) { |
256 | this.startms_ = new Date().getTime(); | |
257 | } | |
258 | ||
259 | DygraphsLocalTester.prototype.finish_ = function(tc, name, result, e) { | |
260 | var endms_ = new Date().getTime(); | |
261 | this.results.push({ | |
02b1c284 | 262 | name : tc.name + '.' + name, |
add4749b | 263 | result : result, |
b99970f8 RK |
264 | duration : endms_ - this.startms_, |
265 | e : e | |
add4749b RK |
266 | }); |
267 | this.summary.passed += result ? 1 : 0; | |
268 | this.summary.failed += result ? 0 : 1; | |
269 | } |