Skip to content

Instantly share code, notes, and snippets.

@snaka
Created December 29, 2008 12:44
Show Gist options
  • Save snaka/41255 to your computer and use it in GitHub Desktop.
Save snaka/41255 to your computer and use it in GitHub Desktop.
Simple test utility for javascript
/**
* Simple JS test script
* @return
*/
function kickTest() {
testLog("This is tester JS");
new Test('TEST CASE 1', function(){
testLog("This is TEST CASE1");
}).run();
new Test('TEST CASE 2').run();
new TestSuite('SUITE for my self')
.add(new Test('TEST CASE 1 OF SUITE', function() {
testLog("Suite1!!");
}))
.add(new Test('TEST CASE 2 OF SUITE', function() {
testLog("Suite2!!");
this.assert(true, true);
}))
.add(new Test('TEST CASE 3 OF SUITE', function() {
testLog("Suite2!!");
this.assert(false, false);
}))
.add(new Test('TEST CASE 4 OF SUITE', function() {
testLog("Suite2!!");
this.assert(true, false);
}))
.run();
new TestSuite('SUITE for my self2')
.add(new Test('TEST CASE 1 OF SUITE', function() {
testLog("Suite2-1!!");
}))
.add(new Test('TEST CASE 2 OF SUITE', function() {
testLog("Suite2-2!!");
this.assert(true, true);
}))
.run();
}
// Test utility >>>
/*
* Show test log to another window
*/
(function() {
var _out = null;
testLog = function(msg, className) {
className = (className || 'normal');
if (!_out) _out = createNewWindow();
try {
_out.write('<span class=' + className + '>' + msg + '</span><br>');
}
catch(e) {
_out = createNewWindow();
_out.write(msg + '<br>');
}
}
function createNewWindow() {
var result = window.open().document;
result.write([
'<style type="text/css">' ,
'span.success { color: green; font-weight: bold; }',
'span.fail { color: red; font-weight: bold; }',
'span.normal { color: gray; }',
'</style>'
].join(' '));
return result;
}
})();
/**
* Simple Test case class
* @param name
* @param testImpl
* @return
*/
function Test(name, testImpl) {
testLog('create test :' + name);
this.name = name;
this.state = Test.STATE.NOT_RUNNING;
this.impl = (testImpl || function(){this.state = Test.STATE.FAIL});
}
Test.STATE = {
NOT_RUNNNING: -1,
SUCCESS: 1,
FAIL: 0
}
Test.prototype = {
run : function() {
testLog('run test :' + this.name);
this.impl();
if(this.state != Test.STATE.FAIL) {
this.state = Test.STATE.SUCCESS;
testLog(this.name + ' : SUCCESS.', 'success');
}
else {
testLog(this.name + ' : FAIL', 'fail');
}
},
assert : function(expected, actual) {
if (expected != actual) {
this.state = Test.STATE.FAIL;
}
}
}
/**
Simple test suite
*/
function TestSuite(name) {
testLog("<hr/>");
testLog("<h1>Create suite [" + name + "].</h1>");
this.name = (name || 'no name');
this.tests = [];
}
TestSuite.prototype = {
add: function(testObj) {
this.tests.push(testObj);
return this;
},
run: function() {
testLog("<h2>Start test suite [" + this.name + "].</h2>");
for(var i = 0; i < this.tests.length; i++) {
if (this.tests[i]) this.tests[i].run();
}
if (this.suiteAllCleared()) {
testLog("<h1>Test suite [" + this.name + "] all SUCCEEDED.</h1>", 'success');
}
else {
testLog("<h1>Test suite [" + this.name + "] some FAILED!!</h1>", 'fail');
}
testLog("<hr/>");
},
suiteAllCleared: function() {
for (var i = 0; i < this.tests.length; i++) {
if (this.tests[i].state != Test.STATE.SUCCESS) {
return false;
}
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment