- var emptyFunction = function() { return this; };
- emptyFunction.prototype.setUp = function() { };
- emptyFunction.prototype.tearDown = function() { };
- emptyFunction.prototype.runTest = function(name) {
- this.setUp();
- var fn = this[name];
- fn.apply(this, []);
- this.tearDown();
+ testCase.prototype.setUp = function() { };
+ testCase.prototype.tearDown = function() { };
+ /**
+ * name can be a string, which is looked up in this object, or it can be a
+ * function, in which case it's run.
+ *
+ * Examples:
+ * var tc = new MyTestCase();
+ * tc.runTest("testThis");
+ * tc.runTest(tc.testThis);
+ *
+ * The duplication tc in runTest is irritating, but it plays well with
+ * Chrome's console completion.
+ */
+ testCase.prototype.runTest = function(func) {
+ try {
+ this.setUp();
+
+ var fn = null;
+ var parameterType = typeof(func);
+ if (typeof(func) == "function") {
+ fn = func;
+ } else if (typeof(func) == "string") {
+ fn = this[func];
+ } else {
+ fail("can't supply " + typeof(func) + " to runTest");
+ }
+
+ fn.apply(this, []);
+ this.tearDown();
+ return true;
+ } catch (e) {
+ console.log(e);
+ if (e.stack) {
+ console.log(e.stack);
+ }
+ return false;
+ }
+ };
+
+ testCase.prototype.runAllTests = function() {
+ var results = {};
+ var names = this.getTestNames();
+ for (var idx in names) {
+ var name = names[idx];
+ console.log("Running " + name);
+ var result = this.runTest(name);
+ results[name] = result;
+ }
+ console.log(prettyPrintEntity_(results));
+ return results;