X-Git-Url: https://adrianiainlam.tk/git/?a=blobdiff_plain;f=auto_tests%2Fmisc%2Flocal.js;h=4b1ea402a059ff63a58fb00a157231601aefcdf5;hb=575b2dcdd982fc2a79d829556fdbbb5c6f6b6b6e;hp=4cfd41de9d36f22ca485cd5c313c89f4a45a6785;hpb=243eecfbc577151058204c4783098ac26016b3cb;p=dygraphs.git
diff --git a/auto_tests/misc/local.js b/auto_tests/misc/local.js
index 4cfd41d..4b1ea40 100644
--- a/auto_tests/misc/local.js
+++ b/auto_tests/misc/local.js
@@ -1,158 +1,186 @@
-var overrideWarn = function() {
- // save Dygraph.warn so we can catch warnings.
- if (false) { // Set true if you want warnings to cause failures.
- var originalDygraphWarn = Dygraph.warn;
- Dygraph.warn = function(msg) {
- if (msg == "Using default labels. Set labels explicitly via 'labels' in the options parameter") {
- originalDygraphWarn(msg);
- return;
- }
- throw "Warnings not permitted: " + msg;
+'use strict';
+
+var DygraphsLocalTester = function() {
+ this.tc = null; // Selected test case
+ this.name = null;
+ this.results = [];
+ this.summary = { failed: 0, passed: 0 };
+ this.start;
+
+ var self = this;
+ jstestdriver.attachListener({
+ start : function(tc) {
+ self.start_(tc);
+ },
+ finish : function(tc, name, result, e) {
+ self.finish_(tc, name, result, e);
}
- Dygraph.prototype.warn = Dygraph.warn;
- }
+ });
};
-var tc = null; // Selected test case
-var name = null;
-
-var resultDiv = null;
+/**
+ * Call this to replace Dygraphs.warn so it throws an error.
+ *
+ * In some cases we will still allow warnings to be warnings, however.
+ */
+DygraphsLocalTester.prototype.overrideWarn = function() {
+ // 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") {
+ originalWarn(msg);
+ return;
+ }
+ throw 'Warnings not permitted: ' + msg;
+ }
+};
-function processVariables() {
+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;
- tc = new entry.testCase();
- break;
- }
- }
- } else if (args.testCase) { // The class name of the test.
- name = args.testCase;
- eval("tc = new " + args.testCase + "()");
- }
-
- var results = null;
- // If the test class is defined.
- if (tc != null) {
- if (args.command == "runAllTests") {
- console.log("Running all tests for " + args.testCase);
- results = tc.runAllTests();
- } else if (args.command == "runTest") {
- console.log("Running test " + args.testCase + "." + args.test);
- results = 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();
- results = {};
for (var idx in testCases) {
var entry = testCases[idx];
- var prototype = entry.testCase;
- tc = new entry.testCase();
- results[entry.name] = 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;
}
- resultsDiv = createResultsDiv();
- var summary = { failed: 0, passed: 0 };
- postResults(results, summary);
- resultsDiv.appendChild(document.createElement("hr"));
- document.getElementById('summary').innerHTML = "(" + summary.failed + " failed, " + 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.textContent = text;
+ return a;
}
-function createResultsDiv() {
- var body = document.getElementsByTagName("body")[0];
- div = document.createElement("div");
+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];
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;
}
-function postResults(results, summary, title) {
- if (typeof(results) == "boolean") {
- var elem = document.createElement("div");
- elem.setAttribute("class", results ? 'pass' : 'fail');
+DygraphsLocalTester.prototype.postResults = function(summary, durationms) {
+ var resultsDiv = this.createResultsDiv(summary, durationms);
- var prefix = title ? (title + ": ") : "";
- elem.innerHTML = prefix + '' + (results ? 'pass' : 'fail') + '';
- resultsDiv.appendChild(elem);
- if (results) {
- summary.passed++;
- } else {
- summary.failed++;
- }
- } else { // hash
- var failed = 0;
- var html = "";
- for (var key in results) {
- if (results.hasOwnProperty(key)) {
- var elem = results[key];
- if (typeof(elem) == "boolean" && title) {
- postResults(results[key], summary, title + "." + key);
- } else {
- postResults(results[key], summary, key);
- }
- }
+ 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 tdResult = document.createElement('td');
+ tdResult.setAttribute('class', 'outcome');
+ tdResult.textContent = result.result ? 'pass' : 'fail';
+ tr.appendChild(tdResult);
+
+ 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.textContent = result.duration + ' ms';
+ tr.appendChild(tdDuration);
+
+ if (result.e) {
+ var tdDetails = document.createElement('td');
+ var a = this.createAnchor('#', null, '(stack trace)');
+ a.onclick = function(e) {
+ return function() {
+ alert(e + '\n' + e.stack);
+ };
+ }(result.e);
+ tdDetails.appendChild(a);
+ tr.appendChild(tdDetails);
}
+
+ table.appendChild(tr);
}
}
-var 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) {
@@ -161,31 +189,82 @@ var 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 (tc == null) {
- description.innerHTML = "Test cases:";
+ if (this.tc == null) {
+ 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;
- var names = tc.getTestNames();
- createLink(list, "Run All Tests", document.URL + "&command=runAllTests");
+ description.textContent = 'Tests for ' + name;
+ var names = this.tc.getTestNames();
+ 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();
+}
+
+DygraphsLocalTester.prototype.finish_ = function(tc, name, result, e) {
+ var endms_ = new Date().getTime();
+ this.results.push({
+ name : tc.name + '.' + name,
+ result : result,
+ duration : endms_ - this.startms_,
+ e : e
+ });
+ this.summary.passed += result ? 1 : 0;
+ this.summary.failed += result ? 0 : 1;
+}