HTML and CSS Reference
In-Depth Information
Listing 1.8 Testing with assert
var date = new Date(2009, 9, 2);
try {
assert("%Y should return full year",
date.strftime("%Y") === "2009");
assert("%m should return month",
date.strftime("%m") === "10");
assert("%d should return date",
date.strftime("%d") === "02");
assert("%y should return year as two digits",
date.strftime("%y") === "09");
assert("%F should act as %Y-%m-%d",
date.strftime("%F") === "2009-10-02");
console.log(assert.count + " tests OK");
} catch (e) {
console.log("Test failed:"+e.message);
This requires slightly more typing, but the test now speaks for itself and is able
to verify itself. The manual labor has been reduced from inspecting each and every
outcome to simply inspecting the final status reported by the test.
1.2.1 Red and Green
In the world of unit testing, “red” and “green” are often used in place of “failure”
and “success,” respectively. Having tests go red or green makes the outcome even
clearer to interpret, and demands less effort on our part. Listing 1.9 provides a
simplified output function which uses the DOM to display messages in color.
Listing 1.9 Outputting messages in color
function output(text, color) {
var p = document.createElement("p");
p.innerHTML = text; = color;
// console.log can now be replaced with
output(assert.count + " tests OK", "#0c0");
// and, for failures:
output("Test failed: " + e.message, "#c00");
Search WWH ::

Custom Search