| 1 | var DygraphsLocalTester = function() { |
| 2 | this.tc = null; // Selected test case |
| 3 | this.name = null; |
| 4 | this.results = []; |
| 5 | this.summary = { failed: 0, passed: 0 }; |
| 6 | this.start; |
| 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 | }); |
| 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() { |
| 25 | // save Dygraph.warn so we can catch warnings. |
| 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; |
| 32 | } |
| 33 | throw 'Warnings not permitted: ' + msg; |
| 34 | } |
| 35 | Dygraph.prototype.warn = Dygraph.warn; |
| 36 | }; |
| 37 | |
| 38 | DygraphsLocalTester.prototype.processVariables = function() { |
| 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 = {}; |
| 42 | var vars = query.split('&'); |
| 43 | for (var i = 0; i < vars.length; i++) { |
| 44 | if (vars[i].length > 0) { |
| 45 | var pair = vars[i].split('='); |
| 46 | args[pair[0]] = pair[1]; |
| 47 | } |
| 48 | } |
| 49 | return args; |
| 50 | } |
| 51 | |
| 52 | var findTestCase = function(stringName, className) { |
| 53 | if (stringName) { |
| 54 | var testCases = getAllTestCases(); |
| 55 | for (var idx in testCases) { |
| 56 | var entry = testCases[idx]; |
| 57 | if (entry.name == stringName) { |
| 58 | var prototype = entry.testCase; |
| 59 | return new entry.testCase(); |
| 60 | } |
| 61 | } |
| 62 | } else if (className) { |
| 63 | eval('tc__ = new ' + className + '()'); |
| 64 | return tc__; |
| 65 | } |
| 66 | return null; |
| 67 | } |
| 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; |
| 81 | } |
| 82 | |
| 83 | DygraphsLocalTester.prototype.createResultsDiv = function(summary, durationms) { |
| 84 | div = document.createElement('div'); |
| 85 | div.id='results'; |
| 86 | |
| 87 | var body = document.getElementsByTagName('body')[0]; |
| 88 | body.insertBefore(div, body.firstChild); |
| 89 | |
| 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 | |
| 110 | var setByClassName = function(name, displayStyle) { |
| 111 | var elements = table.getElementsByClassName(name); |
| 112 | for (var i = 0; i < elements.length; i++) { |
| 113 | elements[i].style.display = displayStyle; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | passedAnchor.onclick = function() { |
| 118 | setByClassName('fail', 'none'); |
| 119 | setByClassName('pass', 'block'); |
| 120 | |
| 121 | passedAnchor.setAttribute('class', 'activeAnchor'); |
| 122 | failedAnchor.setAttribute('class', ''); |
| 123 | }; |
| 124 | failedAnchor.onclick = function() { |
| 125 | setByClassName('fail', 'block'); |
| 126 | setByClassName('pass', 'none'); |
| 127 | passedAnchor.setAttribute('class', ''); |
| 128 | failedAnchor.setAttribute('class', 'activeAnchor'); |
| 129 | }; |
| 130 | allAnchor.onclick = function() { |
| 131 | setByClassName('fail', 'block'); |
| 132 | setByClassName('pass', 'block'); |
| 133 | passedAnchor.setAttribute('class', ''); |
| 134 | failedAnchor.setAttribute('class', ''); |
| 135 | }; |
| 136 | return div; |
| 137 | } |
| 138 | |
| 139 | DygraphsLocalTester.prototype.postResults = function(summary, durationms) { |
| 140 | var resultsDiv = this.createResultsDiv(summary, durationms); |
| 141 | |
| 142 | var table = resultsDiv.getElementsByTagName('table')[0]; |
| 143 | for (var idx = 0; idx < this.results.length; idx++) { |
| 144 | var result = this.results[idx]; |
| 145 | var tr = document.createElement('tr'); |
| 146 | tr.setAttribute('class', result.result ? 'pass' : 'fail'); |
| 147 | |
| 148 | var tdResult = document.createElement('td'); |
| 149 | tdResult.setAttribute('class', 'outcome'); |
| 150 | tdResult.innerText = result.result ? 'pass' : 'fail'; |
| 151 | tr.appendChild(tdResult); |
| 152 | |
| 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); |
| 159 | tr.appendChild(tdName); |
| 160 | |
| 161 | var tdDuration = document.createElement('td'); |
| 162 | tdDuration.innerText = result.duration + ' ms'; |
| 163 | tr.appendChild(tdDuration); |
| 164 | |
| 165 | if (result.e) { |
| 166 | var tdDetails = document.createElement('td'); |
| 167 | var a = this.createAnchor('#', null, '(stack trace)'); |
| 168 | a.onclick = function(e) { |
| 169 | return function() { |
| 170 | alert(e + '\n' + e.stack); |
| 171 | }; |
| 172 | }(result.e); |
| 173 | tdDetails.appendChild(a); |
| 174 | tr.appendChild(tdDetails); |
| 175 | } |
| 176 | |
| 177 | table.appendChild(tr); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | DygraphsLocalTester.prototype.listTests = function() { |
| 182 | var selector = document.getElementById('selector'); |
| 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 | |
| 191 | var description = createAttached('div', selector); |
| 192 | var list = createAttached('ul', selector); |
| 193 | var parent = list.parentElement; |
| 194 | var createLink = function(parent, title, url) { |
| 195 | var li = createAttached('li', parent); |
| 196 | var a = createAttached('a', li); |
| 197 | a.innerHTML = title; |
| 198 | a.href = url; |
| 199 | return li; |
| 200 | } |
| 201 | if (this.tc == null) { |
| 202 | description.innerHTML = 'Test cases:'; |
| 203 | var testCases = getAllTestCases(); |
| 204 | createLink(list, '(run all tests)', document.URL + '?command=runAllTests'); |
| 205 | for (var idx in testCases) { |
| 206 | var entryName = testCases[idx].name; |
| 207 | createLink(list, entryName, document.URL + '?testCaseName=' + entryName); |
| 208 | } |
| 209 | } else { |
| 210 | description.innerHTML = 'Tests for ' + name; |
| 211 | var names = this.tc.getTestNames(); |
| 212 | createLink(list, 'Run All Tests', document.URL + '&command=runAllTests'); |
| 213 | for (var idx in names) { |
| 214 | var name = names[idx]; |
| 215 | createLink(list, name, document.URL + '&test=' + name + '&command=runTest'); |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
| 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 | |
| 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({ |
| 262 | name : tc.name + '.' + name, |
| 263 | result : result, |
| 264 | duration : endms_ - this.startms_, |
| 265 | e : e |
| 266 | }); |
| 267 | this.summary.passed += result ? 1 : 0; |
| 268 | this.summary.failed += result ? 0 : 1; |
| 269 | } |