Skip to content

Instantly share code, notes, and snippets.

@rolandoam
Last active April 6, 2016 22:53
Show Gist options
  • Save rolandoam/5264666 to your computer and use it in GitHub Desktop.
Save rolandoam/5264666 to your computer and use it in GitHub Desktop.
/**
* the simplest unit testing framework. Compatible with require.js by Rolando
* Abarca (2013) - https://gist.github.com/funkaster/5264666
*
* This is public domain, do whatever you want with this. I'm not responsible if
* something does not work the way you want. Feel free to send pull requests to
* this gist.
*
* usage:
*
* pass the object you want to test to the `run` function. If there are own
* properties[1] that match the regexp `^test_(.+)`, then it is assumed that
* this properties are functions that will return either true or
* false. Internally there's a counter that will keep track of the passed,
* failed and error (exception throwns). The run function will report those
* later.
*
* The end
*
* Usage (from node):
*
* var requirejs = require('requirejs');
* requirejs(["mymodule", "test"], function (mymodule, test) {
* var test_mymodule = {
* test_prelude: function () {
* this.t = new mymodule.MyClass();
* },
* test_cleanup: function () {
* delete this.t;
* },
* test_creation: function () {
* test.assert(this.t instanceof mymodule.MyClass);
* },
* test_something_else: function () {
* this.t.set_bar_to_10();
* test.assert_equal(this.t.bar, 10);
* },
* };
* test.run(test_mymodule);
* });
*
* output:
* TypeError: Object [object Object] has no method 'set_bar_to_10'
* total: 2
* pass: 1; fail: 0; err: 1
*
* If you implement set_bar_to_10, but forget to set bar to 10:
* TypeError: Object [object Object] has no method 'set_bar_to_10'
* total: 2
* pass: 1; fail: 1; err: 0
* failed: something_else: Expecting 10 got 0
*
* [1]: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/hasOwnProperty
*/
define([], function () {
var Err = function (msg) {
this.message = msg;
};
var assert = function (a, msg) {
if (a === false) {
throw new Err(msg || "failed to pass " + a);
}
};
var assert_equal = function (a, b, msg) {
if (a !== b) {
throw new Err(msg || "Expecting " + b + " got " + a);
}
};
var assert_different = function (a, b, msg) {
if (a === b) {
throw new Err(msg || b + " equals " + a);
}
};
var should_fail = function (code, exception) {
var didThrow = false;
try {
code.call();
} catch (e) {
if (e.message == exception) {
didThrow = true;
}
} finally {
if (!didThrow) {
throw new Error("Did not fail correctly");
}
}
};
var run = function (module) {
var pass = [], fail = [], err = [], i,
prelude = null, cleanup = null;
if (module.hasOwnProperty("test_prelude")) {
prelude = module["test_prelude"];
}
if (module.hasOwnProperty("test_cleanup")) {
cleanup = module["test_cleanup"];
}
for (i in module) {
var md;
if (module.hasOwnProperty(i) &&
(md = i.match(/^test_(.+)/)) &&
i != "test_prelude" &&
i != "test_cleanup")
{
try {
prelude.call(module);
module[i]();
pass.push(md[1]);
} catch (e) {
if (e instanceof Err) {
fail.push(md[1] + ": " + e.message);
} else {
console.log(e.name + ": " + e.message);
err.push(md[1]);
}
} finally {
cleanup.call(module);
}
}
}
var total = pass.length + fail.length + err.length;
console.log("total: " + total);
console.log("pass: " + pass.length +
"; fail: " + fail.length +
"; err: " + err.length + "\n");
for (i in fail) {
console.log("failed: " + fail[i]);
}
for (i in err) {
console.log("err: " + err[i]);
}
};
return {
"assert": assert,
"assert_equal": assert_equal,
"assert_different": assert_different,
"should_fail": should_fail,
"run": run
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment