From 02b1c284cb1f6d1627bc0912bd831ec2f30fdb45 Mon Sep 17 00:00:00 2001 From: Robert Konigsberg Date: Wed, 5 Jun 2013 17:02:30 -0400 Subject: [PATCH] Clean up more of the local testing script, and also make it prettier. Add execution time to header. --- auto_tests/misc/local.html | 18 +++- auto_tests/misc/local.js | 235 ++++++++++++++++++++++++--------------------- 2 files changed, 142 insertions(+), 111 deletions(-) diff --git a/auto_tests/misc/local.html b/auto_tests/misc/local.html index 6950a0a..1297072 100644 --- a/auto_tests/misc/local.html +++ b/auto_tests/misc/local.html @@ -70,13 +70,23 @@ color: red; } - .activeAnchor { - color: black; + /* Broken: the active anchor isn't gray. Why not? */ + #results .activeAnchor { + color: grey; + } + + #results a { + text-decoration: none; + } + + #results a:visited { + color: blue; text-decoration: none; } - .anchor:hover { - color:blue; + #results a:hover { + color: blue; + text-decoration: underline; } diff --git a/auto_tests/misc/local.js b/auto_tests/misc/local.js index 39a3cb5..9ca6e1a 100644 --- a/auto_tests/misc/local.js +++ b/auto_tests/misc/local.js @@ -1,9 +1,9 @@ var DygraphsLocalTester = function() { this.tc = null; // Selected test case this.name = null; - this.resultsDiv = null; this.results = []; this.summary = { failed: 0, passed: 0 }; + this.start; var self = this; jstestdriver.attachListener({ @@ -30,7 +30,7 @@ DygraphsLocalTester.prototype.overrideWarn = function() { originalDygraphWarn(msg); return; } - throw "Warnings not permitted: " + msg; + throw 'Warnings not permitted: ' + msg; } Dygraph.prototype.warn = Dygraph.warn; }; @@ -39,152 +39,138 @@ DygraphsLocalTester.prototype.processVariables = function() { var splitVariables = function() { // http://www.idealog.us/2006/06/javascript_to_p.html var query = window.location.search.substring(1); var args = {}; - var vars = query.split("&"); + var vars = query.split('&'); for (var i = 0; i < vars.length; i++) { if (vars[i].length > 0) { - var pair = vars[i].split("="); + var pair = vars[i].split('='); args[pair[0]] = pair[1]; } } return args; } - var args = splitVariables(); - var test = args.test; - var command = args.command; - - // args.testCaseName uses the string name of the test. - if (args.testCaseName) { - var testCases = getAllTestCases(); - name = args.testCaseName; - for (var idx in testCases) { - var entry = testCases[idx]; - if (entry.name == args.testCaseName) { - var prototype = entry.testCase; - this.tc = new entry.testCase(); - break; - } - } - } else if (args.testCase) { // The class name of the test. - name = args.testCase; - eval("tc__= new " + args.testCase + "()"); - this.tc = tc_; - } - - // If the test class is defined. - if (this.tc != null) { - if (args.command == "runAllTests") { - console.log("Running all tests for " + args.testCase); - this.tc.runAllTests(); - } else if (args.command == "runTest") { - console.log("Running test " + args.testCase + "." + args.test); - this.tc.runTest(args.test); - } - } else { - if (args.command == "runAllTests") { - console.log("Running all tests for all test cases"); + var findTestCase = function(stringName, className) { + if (stringName) { var testCases = getAllTestCases(); for (var idx in testCases) { var entry = testCases[idx]; - var prototype = entry.testCase; - this.tc = new entry.testCase(); - this.tc.runAllTests(); + if (entry.name == stringName) { + var prototype = entry.testCase; + return new entry.testCase(); + } } + } else if (className) { + eval('tc__ = new ' + className + '()'); + return tc__; } + return null; } - this.resultsDiv = this.createResultsDiv(); - this.postResults(); - this.resultsDiv.appendChild(document.createElement("hr")); - document.getElementById('summary').innerHTML = "(" + this.summary.failed + " failed, " + this.summary.passed + " passed)"; + + var args = splitVariables(); + this.tc = findTestCase(args.testCaseName, args.testCase); + this.test = args.test; + this.command = args.command; +} + +DygraphsLocalTester.prototype.createAnchor = function(href, id, text) { + var a = document.createElement('a'); + a.href = href; + a.id = id; + a.innerText = text; + return a; } -DygraphsLocalTester.prototype.createResultsDiv = function() { - div = document.createElement("div"); +DygraphsLocalTester.prototype.createResultsDiv = function(summary, durationms) { + div = document.createElement('div'); div.id='results'; - div.innerHTML = "Test results: passed failed all
"; - var body = document.getElementsByTagName("body")[0]; + var body = document.getElementsByTagName('body')[0]; body.insertBefore(div, body.firstChild); + var addText = function(div, text) { + div.appendChild(document.createTextNode(text)); + }; + + var passedAnchor = this.createAnchor('#', 'passed', '' + summary.passed + ' passed'); + var failedAnchor = this.createAnchor('#', 'failed', '' + summary.failed + ' failed'); + var allAnchor = this.createAnchor('#', 'all', '(view all)'); + + addText(div, 'Test results: '); + div.appendChild(passedAnchor); + addText(div, ', '); + div.appendChild(failedAnchor); + addText(div, ', '); + div.appendChild(allAnchor); + addText(div, ', (' + durationms + ' ms)'); + + var table = document.createElement('table'); + div.appendChild(table); + div.appendChild(document.createElement('hr')); + var setByClassName = function(name, displayStyle) { - var elements = div.getElementsByClassName(name); + var elements = table.getElementsByClassName(name); for (var i = 0; i < elements.length; i++) { elements[i].style.display = displayStyle; } } - var passedAnchor = document.getElementById('passed'); - var failedAnchor = document.getElementById('failed'); - var allAnchor = document.getElementById('all'); passedAnchor.onclick = function() { setByClassName('fail', 'none'); setByClassName('pass', 'block'); - passedAnchor.setAttribute("class", 'activeAnchor'); - failedAnchor.setAttribute("class", ''); + passedAnchor.setAttribute('class', 'activeAnchor'); + failedAnchor.setAttribute('class', ''); }; failedAnchor.onclick = function() { setByClassName('fail', 'block'); setByClassName('pass', 'none'); - passedAnchor.setAttribute("class", ''); - failedAnchor.setAttribute("class", 'activeAnchor'); + passedAnchor.setAttribute('class', ''); + failedAnchor.setAttribute('class', 'activeAnchor'); }; allAnchor.onclick = function() { setByClassName('fail', 'block'); setByClassName('pass', 'block'); - passedAnchor.setAttribute("class", ''); - failedAnchor.setAttribute("class", ''); + passedAnchor.setAttribute('class', ''); + failedAnchor.setAttribute('class', ''); }; return div; } -DygraphsLocalTester.prototype.postResults = function() { - var table = document.createElement("table"); - this.resultsDiv.appendChild(table); +DygraphsLocalTester.prototype.postResults = function(summary, durationms) { + var resultsDiv = this.createResultsDiv(summary, durationms); + + var table = resultsDiv.getElementsByTagName('table')[0]; for (var idx = 0; idx < this.results.length; idx++) { var result = this.results[idx]; - var tr = document.createElement("tr"); - tr.setAttribute("class", result.result ? 'pass' : 'fail'); + var tr = document.createElement('tr'); + tr.setAttribute('class', result.result ? 'pass' : 'fail'); - var tdResult = document.createElement("td"); - tdResult.setAttribute("class", "outcome"); + var tdResult = document.createElement('td'); + tdResult.setAttribute('class', 'outcome'); tdResult.innerText = result.result ? 'pass' : 'fail'; tr.appendChild(tdResult); - var tdName = document.createElement("td"); - var div = document.createElement("div"); - div.innerText = result.name; - div.onclick = function(name) { - return function() { - var s = name.split("."); - var testCase = s[0]; - var testName = s[1]; - url = window.location.pathname + - "?testCaseName=" + testCase + - "&test=" + testName + - "&command=runTest"; - window.location.href = url; - }; - }(result.name); - div.setAttribute("class", "anchor"); - tdName.appendChild(div); + var tdName = document.createElement('td'); + var s = result.name.split('.'); + var url = window.location.pathname + '?testCaseName=' + s[0] + '&test=' + s[1] + '&command=runTest'; + var a = this.createAnchor(url, null, result.name); + + tdName.appendChild(a); tr.appendChild(tdName); - var tdDuration = document.createElement("td"); - tdDuration.innerText = result.duration; + var tdDuration = document.createElement('td'); + tdDuration.innerText = result.duration + ' ms'; tr.appendChild(tdDuration); if (result.e) { - var tdDetails = document.createElement("td"); - div = document.createElement("div"); - div.innerText = "more..."; - div.setAttribute("class", "anchor"); - div.onclick = function(e) { + var tdDetails = document.createElement('td'); + var a = this.createAnchor('#', null, '(stack trace)'); + a.onclick = function(e) { return function() { - alert(e + "\n" + e.stack); + alert(e + '\n' + e.stack); }; }(result.e); - tdDetails.appendChild(div); + tdDetails.appendChild(a); tr.appendChild(tdDetails); } @@ -192,8 +178,8 @@ DygraphsLocalTester.prototype.postResults = function() { } } -DygraphsLocalTester.prototype.run = function() { - var selector = document.getElementById("selector"); +DygraphsLocalTester.prototype.listTests = function() { + var selector = document.getElementById('selector'); if (selector != null) { // running a test var createAttached = function(name, parent) { @@ -202,35 +188,70 @@ DygraphsLocalTester.prototype.run = function() { return elem; } - var description = createAttached("div", selector); - var list = createAttached("ul", selector); + var description = createAttached('div', selector); + var list = createAttached('ul', selector); var parent = list.parentElement; - var createLink = function(parent, text, url) { - var li = createAttached("li", parent); - var a = createAttached("a", li); - a.innerHTML = text; + var createLink = function(parent, title, url) { + var li = createAttached('li', parent); + var a = createAttached('a', li); + a.innerHTML = title; a.href = url; + return li; } if (this.tc == null) { - description.innerHTML = "Test cases:"; + description.innerHTML = 'Test cases:'; var testCases = getAllTestCases(); - createLink(list, "(run all tests)", document.URL + "?command=runAllTests"); + createLink(list, '(run all tests)', document.URL + '?command=runAllTests'); for (var idx in testCases) { var entryName = testCases[idx].name; - createLink(list, entryName, document.URL + "?testCaseName=" + entryName); + createLink(list, entryName, document.URL + '?testCaseName=' + entryName); } } else { - description.innerHTML = "Tests for " + name; + description.innerHTML = 'Tests for ' + name; var names = this.tc.getTestNames(); - createLink(list, "Run All Tests", document.URL + "&command=runAllTests"); + createLink(list, 'Run All Tests', document.URL + '&command=runAllTests'); for (var idx in names) { var name = names[idx]; - createLink(list, name, document.URL + "&test=" + name + "&command=runTest"); + createLink(list, name, document.URL + '&test=' + name + '&command=runTest'); } } } } +DygraphsLocalTester.prototype.run = function() { + var executed = false; + var start = new Date(). getTime(); + if (this.tc != null) { + if (this.command == 'runAllTests') { + console.log('Running all tests for ' + this.tc.name); + this.tc.runAllTests(); + executed = true; + } else if (this.command == 'runTest') { + console.log('Running test ' + this.tc.name + '.' + this.test); + this.tc.runTest(this.test); + executed = true; + } + } else if (this.command == 'runAllTests') { + console.log('Running all tests for all test cases'); + var testCases = getAllTestCases(); + for (var idx in testCases) { + var entry = testCases[idx]; + var prototype = entry.testCase; + this.tc = new entry.testCase(); + this.tc.runAllTests(); + } + executed = true; + } + + var durationms = new Date().getTime() - start; + + if (executed) { + this.postResults(this.summary, durationms); + } else { + this.listTests(); + } +} + DygraphsLocalTester.prototype.start_ = function(tc) { this.startms_ = new Date().getTime(); } @@ -238,7 +259,7 @@ DygraphsLocalTester.prototype.start_ = function(tc) { DygraphsLocalTester.prototype.finish_ = function(tc, name, result, e) { var endms_ = new Date().getTime(); this.results.push({ - name : tc.name + "." + name, + name : tc.name + '.' + name, result : result, duration : endms_ - this.startms_, e : e -- 2.7.4