X-Git-Url: https://adrianiainlam.tk/git/?a=blobdiff_plain;ds=inline;f=auto_tests%2Fmisc%2Flocal.js;h=4b1ea402a059ff63a58fb00a157231601aefcdf5;hb=ec8cc8b206a5ec69ec5c9ca786d2b127e6c00c72;hp=39a3cb5999844723a2216e029dfb014acea7e0a0;hpb=24e420411024c92bc458d10db8aba29f8ca64b73;p=dygraphs.git
diff --git a/auto_tests/misc/local.js b/auto_tests/misc/local.js
index 39a3cb5..4b1ea40 100644
--- a/auto_tests/misc/local.js
+++ b/auto_tests/misc/local.js
@@ -1,9 +1,11 @@
+'use strict';
+
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({
@@ -22,169 +24,154 @@ var DygraphsLocalTester = function() {
* In some cases we will still allow warnings to be warnings, however.
*/
DygraphsLocalTester.prototype.overrideWarn = function() {
- // save Dygraph.warn so we can catch warnings.
- var originalDygraphWarn = Dygraph.warn;
- Dygraph.warn = function(msg) {
- // This warning is still
+ // save console.warn so we can catch warnings.
+ var originalWarn = console.warn;
+ console.warn = function(msg) {
+ // This warning is pervasive enough that we'll let it slide (for now).
if (msg == "Using default labels. Set labels explicitly via 'labels' in the options parameter") {
- originalDygraphWarn(msg);
+ originalWarn(msg);
return;
}
- throw "Warnings not permitted: " + msg;
+ throw 'Warnings not permitted: ' + msg;
}
- Dygraph.prototype.warn = Dygraph.warn;
};
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.createResultsDiv = function() {
- div = document.createElement("div");
+DygraphsLocalTester.prototype.createAnchor = function(href, id, text) {
+ var a = document.createElement('a');
+ a.href = href;
+ a.id = id;
+ a.textContent = text;
+ return a;
+}
+
+DygraphsLocalTester.prototype.createResultsDiv = function(summary, durationms) {
+ var 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");
- tdResult.innerText = result.result ? 'pass' : 'fail';
+ var tdResult = document.createElement('td');
+ tdResult.setAttribute('class', 'outcome');
+ tdResult.textContent = 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.textContent = 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 +179,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 +189,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.textContent = title;
a.href = url;
+ return li;
}
if (this.tc == null) {
- description.innerHTML = "Test cases:";
+ description.textContent = '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.textContent = '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 +260,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