X-Git-Url: https://adrianiainlam.tk/git/?a=blobdiff_plain;f=auto_tests%2Fmisc%2Ffake-jstestdriver.js;h=4a9b0eabd340cb4b936191f0ff5531d7bb9c385a;hb=61f10b65766cfa6844059a43efb456bb7e91dabb;hp=4849f99a4968e7a2d8599fece1f6716b71dec982;hpb=f8e02b3c7ea1ccbf567bbeda1ceaa959b769dd43;p=dygraphs.git diff --git a/auto_tests/misc/fake-jstestdriver.js b/auto_tests/misc/fake-jstestdriver.js index 4849f99..4a9b0ea 100644 --- a/auto_tests/misc/fake-jstestdriver.js +++ b/auto_tests/misc/fake-jstestdriver.js @@ -36,25 +36,95 @@ var jstd = { } }; +var testCaseList = []; + function TestCase(name) { - jstd.sucker("Not really creating TestCase(" + name + ")"); this.name = name; this.toString = function() { return "Fake test case " + name; }; - var emptyFunction = function() { return this; }; - emptyFunction.prototype.setUp = function() { }; - emptyFunction.prototype.tearDown = function() { }; - emptyFunction.prototype.runTest = function(name) { + var testCase = function() { return this; }; + testCase.prototype.setUp = function() { }; + testCase.prototype.tearDown = function() { }; + /** + * name can be a string, which is looked up in this object, or it can be a + * function, in which case it's run. + * + * Examples: + * var tc = new MyTestCase(); + * tc.runTest("testThis"); + * tc.runTest(tc.testThis); + * + * The duplication tc in runTest is irritating, but it plays well with + * Chrome's console completion. + */ + testCase.prototype.runTest = function(func) { try { this.setUp(); - var fn = this[name]; + + 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; } catch (e) { - console.log(e.stack); + console.log(e); + if (e.stack) { + console.log(e.stack); + } + return false; + } + }; + testCase.prototype.runAllTests = function() { + // what's better than for ... in for non-array objects? + 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; + } } + console.log(prettyPrintEntity_(tests)); }; - return emptyFunction; + + testCaseList.push(testCase); + return testCase; }; + +// Note: this creates a bunch of global variables intentionally. +function addGlobalTestSymbols() { + globalTestDb = {}; // maps test name -> test function wrapper + + var num_tests = 0; + for (var i = 0; i < testCaseList.length; i++) { + var tc_class = testCaseList[i]; + for (var name in tc_class.prototype) { + if (name.indexOf('test') == 0 && typeof(tc_class.prototype[name]) == 'function') { + if (globalTestDb.hasOwnProperty(name)) { + console.log('Duplicated test name: ' + name); + } else { + globalTestDb[name] = function(name, tc_class) { + return function() { + var tc = new tc_class; + return tc.runTest(name); + }; + }(name, tc_class); + eval(name + " = globalTestDb['" + name + "'];"); + num_tests += 1; + } + } + } + } + console.log('Loaded ' + num_tests + ' tests in ' + + testCaseList.length + ' test cases'); +}