Geeks With Blogs
Liam McLennan hackingon.net

Tonight the Brisbane Alt.NET group is doing a coding dojo. I am hoping to talk someone into pairing with me to solve the kata in CoffeeScript. CoffeeScript is an awesome language, half javascript, half ruby, that compiles to javascript. To assist with tonight’s dojo I wrote the following micro test framework for CoffeeScript:

<html>
<body>

<div>
	<h2>Test Results:</h2>
	<p class='results' />
</div>
	<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>

<script type="text/coffeescript">

# super simple test framework
test: {
	write: (s) ->
		$('.results').append(s + '<br/>')		
		
	assert: (b, message...) -> 
		test.write(if b then "pass" else "fail: " + message)
		
	tests: []
	
	exec: () ->
		for t in test.tests
			test.write("<br/><b>$t.name</b>")
			t.func()
}

# add some tests
test.tests.push { 
		name: "First Test"
		func: () ->	
			test.assert(true)
	}
	
test.tests.push {
		name: "Another Test" 
		func: () ->
			test.assert(false, "You loose")
	}
	
# run them
test.exec(test.tests)
</script>

<script type="text/javascript" src="coffee-script.js"></script>

</body>
</html>

It’s not the prettiest, but as far as I know it is the only CoffeeScript test framework in existence. Of course, I could just use one of the javascript test frameworks but that would be no fun. To get this example to run you need the coffeescript compiler in the same directory as the page.

Posted on Thursday, May 6, 2010 1:14 AM | Back to top

Copyright © Liam McLennan | Powered by: GeeksWithBlogs.net