Skip to content

Instantly share code, notes, and snippets.

@prakhar1989
Created January 16, 2014 18:45
Show Gist options
  • Save prakhar1989/8460886 to your computer and use it in GitHub Desktop.
Save prakhar1989/8460886 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Test Suite!</title>
<style>
li.pass { background: green;
color: white;}
li.fail { background: red; color: white;}
ul#results ul { margin-bottom: 10px;}
</style>
</head>
<body>
<ul id="results"></ul>
</body>
</html>
// test library
(function() {
var results;
this.assert = function(value, desc) {
var li = document.createElement('li');
li.className = value ? "pass" : "fail";
li.appendChild(document.createTextNode(desc));
results.appendChild(li);
if (!value) {
li.parentNode.parentNode.className = "fail";
}
return li;
};
this.test = function(title, testCases) {
results = document.getElementById('results');
// logging the test name as TRUE
results = assert(true, title).appendChild(
document.createElement('ul'));
testCases();
};
})();
// testing functions
function sqr(n) { return n * n ; }
// bootstraper
window.onload = function() {
test("A test", function() {
assert(true, "assert is passing");
assert(false, "assert is failing");
assert(5 === 5, "simple shit");
});
test("my lib", function() {
assert(true, "passing");
assert(43 === 40 + 3, "addition");
assert(4 == sqr(2), "squaring");
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment