-
-
Save javisantana/965414 to your computer and use it in GitHub Desktop.
quick'n'dirty javascript testing framework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html lang=en> | |
<head> | |
<title>quick'n'dirty js testing framework</title> | |
<meta charset=utf-8> | |
<style type="text/css" media="screen"> | |
h1{ font: 1.5em Helvetica; } | |
p { font: .9em courier; padding: .5em 1em; margin: 0;} | |
.result { margin-top: 1px; } | |
.pass { color: #4F8A10; background: #DFF2BF } | |
.fail { color: #D8000C; background: #FFBABA } | |
.error{ color: white; background: red; font-weight: bold; } | |
.describe { border-bottom: 1px solid #ccc; padding: 20px; } | |
</style> | |
<script> | |
var setup_fn = null; | |
function describe(desc, fn) { | |
document.write('<div class="describe">'); | |
document.write('<h1>' + desc +'</h1>'); | |
fn(); | |
document.write('</div>'); | |
setup_fn = null; | |
} | |
function before_each(be) { | |
setup_fn = be; | |
} | |
function it(desc, fn) { | |
var output = function(klass, msg){ | |
document.write('<p class="result ' + klass + '">' + msg + '</p>') | |
} | |
var result | |
try{ | |
if (setup_fn != null) | |
setup_fn(); | |
result = fn() | |
}catch(err){ result = err } | |
if (typeof result === 'boolean'){ | |
result ? output('pass', desc) : output('fail', desc) | |
}else{ | |
output('error', ['ERROR:',result.message,'when testing',desc].join(' ')) | |
} | |
} | |
</script> | |
<script> | |
// usage | |
function Adder(a) { | |
this.a = a; | |
this.accum = function(b) { | |
return this.a+=b; | |
} | |
} | |
describe("add", function() { | |
var adder; | |
before_each(function() { | |
adder = new Adder(2); | |
}); | |
it("should add", function() { | |
return adder.accum(3) == 5; | |
}); | |
it("should fail", function() { | |
return adder.accum(3) == 6; | |
}); | |
}); | |
</script> | |
</head> | |
<body></body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment