Tests are enterprisey. At least, that’s the impression I have at times. If not enterprisey, they are something extra and not fun. I don’t know if I agree any more.
What if, for example, the canonical first program was not hello_world but some sort of assert() function or statement? What is hello_world, anyways, if not a test that the environment is working and that the programs we write will run, and do what we want them to do?
The following is found very early on in John Resig’s excellent Secrets of the Javascript Ninja:
<html>
<head>
<title>Test Suite </title>
<script>
function assert( value, desc ) {
var li = document.createElement("li");
li.className = value ? "pass" : "fail";
li.appendChild( document.createTextNode( desc ) );
document.getElementById("results").appendChild( li );
}
window.onload = function(){
assert( true, "The test suite is running." );
assert( 1==2, "1 equals 2" );
};
</script>
<style>
#results li.pass { color: green; }
#results li.fail { color: red; }
</style>
</head>
<body>
<ul id="results"></ul>
</body>
</html>
It’s a fairly simple block of code, this assert thing. Makes just as good a sanity check as hello world. We could also add hello world and make sure that it works, also.
function helloWorld(){
div = document.createElement("div");
div.setAttribute("id", "hello");
div.appendChild( document.createTextNode ( "Hello, world!") );
document.body.appendChild( div );
}
And test it, maybe like:
helloWorld();
assert( document.getElementById("hello"), "Hello world creates a '#hello' div" );
I’m just saying. There’s no reason tests can’t be part of hacking. Most hacks are just experimental tests of their own, anyways. It can be sort of fun.
Update/edit: Yes, that is a little bit verbose for a helloWorld() function, I suppose, but I wanted to make it easily testable. And really, everything in there is Javascript I would have liked to have learned earlier, so it seems (overall) like a reasonable helloWorld to me.