CoffeeScript Test Framework

Posted by Liam McLennan on Geeks with Blogs See other posts from Geeks with Blogs or by Liam McLennan
Published on Thu, 06 May 2010 00:14:45 GMT Indexed on 2010/05/06 6:28 UTC
Read the original article Hit count: 507

Filed under:

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.

© Geeks with Blogs or respective owner