Skip to content

Commit

Permalink
hacked to use new QUnit.log params, include browser version, fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminplee committed Nov 7, 2010
1 parent 398e3c2 commit 151406c
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 14 deletions.
19 changes: 19 additions & 0 deletions index.html
@@ -0,0 +1,19 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link rel="stylesheet" href="../qunit/qunit/qunit.css" type="text/css"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"> </script>
<script type="text/javascript" src="../qunit/qunit/qunit.js"></script>
<script type="text/javascript" src="myLib.js"></script>
<script type="text/javascript" src="myLibTest.js"></script>
</head>
<body>
<h1 id="qunit-header">QUnit example</h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
<div id="qunit-fixture">test markup, will be hidden</div>
</body>
</html>
4 changes: 4 additions & 0 deletions myLib.js
@@ -1,3 +1,7 @@
function newAddition(x, y) {
return x + y;
}

function newSubtraction(x, y) {
return x - y - y; // bug!
}
28 changes: 22 additions & 6 deletions 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");
});
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();
}
84 changes: 76 additions & 8 deletions 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");

0 comments on commit 151406c

Please sign in to comment.