Skip to content

Instantly share code, notes, and snippets.

@wilmoore
Created September 20, 2012 16:20
Show Gist options
  • Save wilmoore/3756886 to your computer and use it in GitHub Desktop.
Save wilmoore/3756886 to your computer and use it in GitHub Desktop.
Wherein we write a naive implementation of a test runner, reporter, and assertion API in order to demonstrate how non-magical testing is.
/**
* How to test:
*
* (1) You provide an expression
* (2) You provide what you "expect" that expression's result/outcome will be
* (3) You express how you'd like to compare "expected" outcome to the actual outcome
* (4) Your test should fail initially because you haven't yet writtne the code to support the positive outcome
* (5) Refactor until the test passes
* (6) Repeat
*/
var expect, totalTests = 0, passed = 0, failed = 0;
expect = function (actual) {
var predicate = function (expression){
totalTests++;
return (expression) ? ++passed : ++failed;
}
// assertion api (a better API would throw exceptions, but let's keep this on topic shall we)
return {
toEqual: function (expected) { return predicate(actual === expected); },
toNotEqual: function (expected) { return predicate(actual !== expected); }
};
};
expect(1).toEqual(1);
expect(2).toEqual(2);
expect(8).toEqual(9);
console.log('');
console.log('Total number of tests:', totalTests);
console.log('passed:', passed);
console.log('failed:', failed);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment