X-Git-Url: https://adrianiainlam.tk/git/?a=blobdiff_plain;f=auto_tests%2Fmisc%2Ffake-jstestdriver.js;h=404d1655acefb3531b8b774ad06d0ded0aa92f77;hb=add4749bae3e7c1ee7e2387fd562364243994b29;hp=4a9b0eabd340cb4b936191f0ff5531d7bb9c385a;hpb=947ab5a6dd8278d85ff74e42151797cf8b9dbaf4;p=dygraphs.git diff --git a/auto_tests/misc/fake-jstestdriver.js b/auto_tests/misc/fake-jstestdriver.js index 4a9b0ea..404d165 100644 --- a/auto_tests/misc/fake-jstestdriver.js +++ b/auto_tests/misc/fake-jstestdriver.js @@ -24,9 +24,29 @@ * @author konigsberg@google.com (Robert Konigsberg) */ var jstestdriver = { - jQuery : jQuery + jQuery : jQuery, + listeners_ : [], + announce_ : function(name, args) { + for (var idx = 0; idx < jstestdriver.listeners_.length; idx++) { + var listener = jstestdriver.listeners_[idx]; + if (listener[name]) { + listener[name].apply(null, args); + } + } + }, + attachListener: function(listener) { + jstestdriver.listeners_.push(listener); + } }; +if (!console) { + var console = { + log: function(x) { + // ... + } + }; +} + var jstd = { include : function(name) { this.sucker("Not including " + name); @@ -39,14 +59,15 @@ var jstd = { var testCaseList = []; function TestCase(name) { - this.name = name; - this.toString = function() { + var testCase = function() { return this; }; + testCase.name = name; + testCase.toString = function() { return "Fake test case " + name; }; - var testCase = function() { return this; }; testCase.prototype.setUp = function() { }; testCase.prototype.tearDown = function() { }; + testCase.prototype.name = name; /** * name can be a string, which is looked up in this object, or it can be a * function, in which case it's run. @@ -60,44 +81,61 @@ function TestCase(name) { * Chrome's console completion. */ testCase.prototype.runTest = function(func) { + var result = false; + var ex = null; + var name = typeof(func) == "string" ? func : "(anonymous function)"; + jstestdriver.announce_("start", [this, name]); try { - this.setUp(); - - var fn = null; - var parameterType = typeof(func); - if (typeof(func) == "function") { - fn = func; - } else if (typeof(func) == "string") { - fn = this[func]; - } else { - fail("can't supply " + typeof(func) + " to runTest"); - } - - fn.apply(this, []); - this.tearDown(); - return true; + result = this.runTest_(func); } catch (e) { - console.log(e); - if (e.stack) { - console.log(e.stack); - } - return false; + ex = e; } + jstestdriver.announce_("finish", [this, name, result, ex]); + } + + testCase.prototype.runTest_ = function(func) { + this.setUp(); + + var fn = null; + var parameterType = typeof(func); + if (typeof(func) == "function") { + fn = func; + } else if (typeof(func) == "string") { + fn = this[func]; + } else { + fail("can't supply " + typeof(func) + " to runTest"); + } + + fn.apply(this, []); + this.tearDown(); + return true; }; + testCase.prototype.runAllTests = function() { + var results = {}; + var names = this.getTestNames(); + for (var idx in names) { + var name = names[idx]; + console.log("Running " + name); + var result = this.runTest(name); + results[name] = result; + } + console.log(prettyPrintEntity_(results)); + return results; + }; + + testCase.prototype.getTestNames = function() { // what's better than for ... in for non-array objects? - var tests = {}; + var tests = []; for (var name in this) { if (name.indexOf('test') == 0 && typeof(this[name]) == 'function') { - console.log("Running " + name); - var result = this.runTest(name); - tests[name] = result; + tests.push(name); } } - console.log(prettyPrintEntity_(tests)); - }; + return tests; + } - testCaseList.push(testCase); + testCaseList.push({name : name, testCase : testCase}); return testCase; }; @@ -107,7 +145,7 @@ function addGlobalTestSymbols() { var num_tests = 0; for (var i = 0; i < testCaseList.length; i++) { - var tc_class = testCaseList[i]; + var tc_class = testCaseList[i].testCase; for (var name in tc_class.prototype) { if (name.indexOf('test') == 0 && typeof(tc_class.prototype[name]) == 'function') { if (globalTestDb.hasOwnProperty(name)) { @@ -128,3 +166,19 @@ function addGlobalTestSymbols() { console.log('Loaded ' + num_tests + ' tests in ' + testCaseList.length + ' test cases'); } + +function getAllTestCases() { + return testCaseList; +} + +jstestdriver.attachListener({ + finish : function(tc, name, result, e) { + if (e) { + console.log(e); + if (e.stack) { + console.log(e.stack); + } + } + } +}); +