Skip to content

Instantly share code, notes, and snippets.

@TastyToast
Last active August 29, 2015 14:08
Show Gist options
  • Save TastyToast/9fdbbec58e27bbe7dad5 to your computer and use it in GitHub Desktop.
Save TastyToast/9fdbbec58e27bbe7dad5 to your computer and use it in GitHub Desktop.
Simple async test
<!doctype html>
<html>
<head>
<title>Simple Test Suite</title>
<script>
(function(){
var queue = []
var paused = false
var results;
this.test = function(name, fn){
queue.push(function(){
results = document.getElementById('results');
results = assert(true, name).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;
}
})();
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 test completed");
resume();
}, 1000);
});
}
</script>
<style>
#results li.pass {
color: green;
}
#results li.fail {
color: red;
}
</style>
</head>
<body>
<ul id="results"></ul>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment