Permalink
Browse files

add group support to testCase, thanks to christophsturm

  • Loading branch information...
1 parent e29a1ba commit 5420bb859da89bf1bd149501b65b3fd4609cdbf0 @caolan caolan committed Nov 22, 2010
Showing with 154 additions and 45 deletions.
  1. +74 −45 lib/core.js
  2. +80 −0 test/test-testcase.js
View
@@ -146,6 +146,79 @@ exports.runModules = function (modules, opt) {
/**
+ * Wraps a test function with setUp and tearDown functions.
+ * Used by testCase.
+ *
+ * @param {Function} setUp
+ * @param {Function} tearDown
+ * @param {Function} fn
+ * @api private
+ */
+
+var wrapTest = function (setUp, tearDown, fn) {
+ return function (test) {
+ var context = {};
+ if (tearDown) {
+ var done = test.done;
+ test.done = function (err) {
+ try {
+ tearDown.call(context, function (err2) {
+ if (err && err2) {
+ test._assertion_list.push(
+ types.assertion({error: err})
+ );
+ return done(err2);
+ }
+ done(err || err2);
+ });
+ }
+ catch (e) {
+ done(e);
+ }
+ };
+ }
+ if (setUp) {
+ setUp.call(context, function (err) {
+ if (err) {
+ return test.done(err);
+ }
+ fn.call(context, test);
+ });
+ }
+ else {
+ fn.call(context, test);
+ }
+ }
+};
+
+
+/**
+ * Wraps a group of tests with setUp and tearDown functions.
+ * Used by testCase.
+ *
+ * @param {Function} setUp
+ * @param {Function} tearDown
+ * @param {Object} group
+ * @api private
+ */
+
+var wrapGroup = function (setUp, tearDown, group) {
+ var tests = {};
+ var keys = _keys(group);
+ for (var i=0; i<keys.length; i++) {
+ var k = keys[i];
+ if (typeof group[k] === 'function') {
+ tests[k] = wrapTest(setUp, tearDown, group[k]);
+ }
+ else if (typeof group[k] === 'object') {
+ tests[k] = wrapGroup(setUp, tearDown, group[k]);
+ }
+ }
+ return tests;
+}
+
+
+/**
* Utility for wrapping a suite of test functions with setUp and tearDown
* functions.
*
@@ -155,53 +228,9 @@ exports.runModules = function (modules, opt) {
*/
exports.testCase = function (suite) {
- var tests = {};
-
var setUp = suite.setUp;
var tearDown = suite.tearDown;
delete suite.setUp;
delete suite.tearDown;
-
- var keys = _keys(suite);
-
- var tests = {};
- for (var i=0; i<keys.length; i++) {
- (function (k) {
- tests[k] = function (test) {
- var context = {};
- if (tearDown) {
- var done = test.done;
- test.done = function (err) {
- try {
- tearDown.call(context, function (err2) {
- if (err && err2) {
- test._assertion_list.push(
- types.assertion({error: err})
- );
- return done(err2);
- }
- done(err || err2);
- });
- }
- catch (e) {
- done(e);
- }
- };
- }
- if (setUp) {
- setUp.call(context, function (err) {
- if (err) {
- return test.done(err);
- }
- suite[k].call(context, test);
- });
- }
- else {
- suite[k].call(context, test);
- }
- };
- }(keys[i]));
- }
-
- return tests;
+ return wrapGroup(setUp, tearDown, suite);
};
View
@@ -152,3 +152,83 @@ exports.testErrorAndtearDownError = function (test) {
test.done();
});
};
+
+exports.testCaseGroups = function (test) {
+ var call_order = [];
+ var s = testCase({
+ setUp: function (callback) {
+ call_order.push('setUp');
+ callback();
+ },
+ tearDown: function (callback) {
+ call_order.push('tearDown');
+ callback();
+ },
+ test1: function (test) {
+ call_order.push('test1');
+ test.done();
+ },
+ group1: {
+ test2: function (test) {
+ call_order.push('group1.test2');
+ test.done();
+ }
+ }
+ });
+ nodeunit.runSuite(null, s, {}, function (err, assertions) {
+ test.same(call_order, [
+ 'setUp',
+ 'test1',
+ 'tearDown',
+ 'setUp',
+ 'group1.test2',
+ 'tearDown'
+ ]);
+ test.done();
+ });
+};
+
+exports.nestedTestCases = function (test) {
+ var call_order = [];
+ var s = testCase({
+ setUp: function (callback) {
+ call_order.push('setUp');
+ callback();
+ },
+ tearDown: function (callback) {
+ call_order.push('tearDown');
+ callback();
+ },
+ test1: function (test) {
+ call_order.push('test1');
+ test.done();
+ },
+ group1: testCase({
+ setUp: function (callback) {
+ call_order.push('group1.setUp');
+ callback();
+ },
+ tearDown: function (callback) {
+ call_order.push('group1.tearDown');
+ callback();
+ },
+ test2: function (test) {
+ call_order.push('group1.test2');
+ test.done();
+ }
+ })
+ });
+ nodeunit.runSuite(null, s, {}, function (err, assertions) {
+ test.same(call_order, [
+ 'setUp',
+ 'test1',
+ 'tearDown',
+ 'setUp',
+ 'group1.setUp',
+ 'group1.test2',
+ 'group1.tearDown',
+ 'tearDown'
+ ]);
+ test.done();
+ });
+};

0 comments on commit 5420bb8

Please sign in to comment.