Skip to content

Instantly share code, notes, and snippets.

@prakhar1989
Created January 16, 2014 20:04
Show Gist options
  • Select an option

  • Save prakhar1989/8462393 to your computer and use it in GitHub Desktop.

Select an option

Save prakhar1989/8462393 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>
(function() {
var queue = [], paused = false;
var results;
this.test = function(title, fn) {
queue.push(function() {
results = document.getElementById('results');
results = assert(true, title).appendChild(
document.createElement("ul"));
fn();
});
runTest();
};
this.pause = function() {
paused = true;
};
this.resume = function() {
paused = false;
setTimeout(runTest, 1);
};
function runTest() {
if (!paused && queue.length) {
queue.shift()();
if (!paused) { resume(); }
}
}
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;
};
})();
// bootstrapper
window.onload = function() {
test("Async test 1", function() {
pause();
setTimeout(function() {
assert(true, "first test completed");
resume();
}, 1000);
});
test("Async test 2", function() {
pause();
setTimeout(function() {
assert(false, "second has failed");
resume();
}, 1000);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment