Unit Testing - xUnit Approach
Fiddle based on Christian Johansen's "xUnit" testing example discussed in chapter 1 of his book, Test-Driven JavaScript Development.
by brady houseknecht
HTML
<div class="col-lg-12 enter-stage-south">
<div class="page-header">
<h1 id="navbar">xUnit Testing</h1>
</div>
<div class="bs-component">
<div id="fiddleHook"></div>
</div>
</div>
CSS
.enter-stage-south {
-moz-animation-duration: 3s;
-webkit-animation-duration: 3s;
-moz-animation-name: slide-up;
-webkit-animation-name: slide-up;
}
@-moz-keyframes slide-up {
from {
margin-top: 100%;
}
to {
margin-top: 0%;
}
}
@-webkit-keyframes slide-up {
from {
margin-top: 100%;
}
to {
margin-top: 0%;
}
}
JavaScript
"use strict";
Date.prototype.pop = (function () {
function pop(format) {
var date = this;
return (format + "").replace(/%([a-zA-Z])/g,
function (m, f) {
var formatter = Date.formats && Date.formats[f];
if (typeof formatter === "function") {
return formatter.call(Date.formats, date);
} else if (typeof formatter === "string") {
return date.pop(formatter);
}
return;
})
}
function pad(digit) {
return (+digit < 10 ? "0" : "") + digit;
}
Date.formats = {
d: function (date) {
return pad(date.getDate());
},
m: function (date) {
return pad(date.getMonth() + 1);
},
y: function (date) {
return pad(date.getYear() % 100);
},
Y: function (date) {
return date.getFullYear();
},
F: "%Y-%m-%d",
D: "%m/%d/%y"
};
return pop();
}());
(function (app, $, undefined) {
app.test = app.test || {
assert: function (message, expr) {
if (!expr) {
throw new Error(message);
}
this.assert.count++;
return true;
},
output: function (text, color) {
var div = document.createElement('div');
div.innerHTML = text;
div.style.color = color;
$("#fiddleHook").append(div);
},
testCase: function (name, tests) {
var me = this;
me.assert.count = 0;
var successful = 0,
testCount = 0,
hasSetup = typeof tests.setUp == "function",
hasTearDown = typeof tests.tearDown == "function";
for (var test in tests) {
if (!/^test/.test(test)) {
continue;
}
testCount++;
try {
if (hasSetup)...