diff --git a/index.html b/index.html new file mode 100644 index 0000000..e4e7fc4 --- /dev/null +++ b/index.html @@ -0,0 +1,19 @@ + + + + + + + + + + +

QUnit example

+

+
+

+
    +
    test markup, will be hidden
    + + diff --git a/myLib.js b/myLib.js index 3517dff..b97a082 100644 --- a/myLib.js +++ b/myLib.js @@ -1,3 +1,7 @@ function newAddition(x, y) { return x + y; +} + +function newSubtraction(x, y) { + return x - y - y; // bug! } \ No newline at end of file diff --git a/myLibTest.js b/myLibTest.js index ed11633..a505e6e 100644 --- a/myLibTest.js +++ b/myLibTest.js @@ -1,6 +1,22 @@ -test("Adding numbers works", function() { - expect(3); - ok(newAddition, "function exists"); - equals(4, newAddition(2, 2), "2 + 2 = 4"); - equals(100, newAddition(100, 0), "zero is zero"); -}); \ No newline at end of file +var myLibTests = function() { + + test("Adding numbers", function() { + expect(3); + ok(newAddition, "function exists"); + equals(newAddition(2, 2), 4, "2 + 2 = 4"); + equals(newAddition(100, 0), 100, "zero is zero"); + }); + + test("Subtracting numbers", function() { + expect(2); + ok(newSubtraction, "subtraction function exists"); + equals(newSubtraction(2, 2), 0, "2 - 2 = 0"); + }); +}; + +if(typeof document !== "undefined" && typeof jQuery !== "undefined") { + jQuery(myLibTests); +} +else { + myLibTests(); +} \ No newline at end of file diff --git a/suite.js b/suite.js index 6aa1ac9..c0c5843 100644 --- a/suite.js +++ b/suite.js @@ -1,13 +1,81 @@ load("../qunit/qunit/qunit.js"); -QUnit.init(); -QUnit.config.blocking = false; -QUnit.config.autorun = true; -QUnit.config.updateRate = 0; - -QUnit.log = function(result, message, details) { - print(result ? 'PASS' : 'FAIL', details.message); -}; +(function() { + QUnit.init(); + QUnit.config.blocking = false; + QUnit.config.autorun = true; + QUnit.config.updateRate = 0; + + var stop_watch = { + start_time: null, stop_time: null, + + start: function() { + this.start_time = new Date(); + }, + + stop: function() { + this.stop_time = new Date(); + }, + + elapsed_seconds: function() { + return ( this.stop_time.getMilliseconds() - this.start_time.getMilliseconds() ) / 1000; + } + }; + + var current_test_name = null; + var current_test_assertions = []; + var totals = { pass: 0, fail: 0}; + + QUnit.testStart = function(name) { + current_test_name = name; + }; + + QUnit.testDone = function(name, fail_count, total_count) { + if(fail_count > 0) { + print("FAIL - " + name); + + for(var i = 0; i < current_test_assertions.length; i++) { + print(" " + current_test_assertions[i]); + } + + + totals.fail = totals.fail + 1; + } + else { + print("PASS - " + name); + totals.pass = totals.pass + 1; + } + }; + + QUnit.log = function(result, message, details) { + details.message = details.message || ""; + + var type = (typeof details.expected !== "undefined") ? "EQ" : "OK"; + + var outcome = result ? "PASS" : "FAIL"; + + var response = ""; + if(!result && typeof details.expected !== "undefined") { + response = "Expected: " + details.expected + ", Actual: " + details.actual; + } + + current_test_assertions.push([outcome, type, details.message, response].join("|")); + }; + + // executing twice per test? + QUnit.done = function() { + stop_watch.stop(); + + print("----------------------------------------"); + print(" PASS: " + totals.pass + " FAIL: " + totals.fail + " TOTAL: " + (totals.pass + totals.fail)); + print(" Finished in " + stop_watch.elapsed_seconds() + " seconds."); + print("----------------------------------------"); + }; + + stop_watch.start(); // hacked b/c QUnit.begin only executes in a browser env on dom ready +})(); + + load("myLib.js"); load("myLibTest.js"); \ No newline at end of file