Skip to content

Commit

Permalink
- Pass test name to test function and setUp/tearDown
Browse files Browse the repository at this point in the history
- Fixed failing test case tearDown scope
  • Loading branch information
atesgoral committed Apr 29, 2009
1 parent 8c0d76a commit 25e9de7
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 21 deletions.
2 changes: 2 additions & 0 deletions jsunity/CHANGES.txt
@@ -1,3 +1,5 @@
- Pass test name to test function and setUp/tearDown
- Fixed failing test case tearDown scope
- Removed "invalid function" exception that doesn't seem to be possible, for
100% code coverage
- Bound the test suite object on setUp and tearDown calls
Expand Down
25 changes: 20 additions & 5 deletions jsunity/jsunity.js
Expand Up @@ -172,6 +172,8 @@ jsUnity = (function () {
}
};

function empty() {}

function plural(cnt, unit) {
return cnt + " " + unit + (cnt == 1 ? "" : "s");
}
Expand Down Expand Up @@ -309,7 +311,7 @@ jsUnity = (function () {
}
},

log: function () {},
log: empty,

error: function (s) { this.log("[ERROR] " + s); },

Expand Down Expand Up @@ -352,20 +354,33 @@ jsUnity = (function () {

suiteNames.push(suite.suiteName);
results.total += cnt;

function getFixtureUtil(fnName) {
var fn = suite[fnName];

return fn
? function (testName) {
fn.call(suite.scope, testName);
}
: empty;
}

var setUp = getFixtureUtil("setUp");
var tearDown = getFixtureUtil("tearDown");

for (var j = 0; j < cnt; j++) {
var test = suite.tests[j];

try {
suite.setUp && suite.setUp.call(suite.scope);
test.fn.call(suite.scope);
suite.tearDown && suite.tearDown.call(suite.scope);
setUp(test.name);
test.fn.call(suite.scope, test.name);
tearDown(test.name);

results.passed++;

this.log("[PASSED] " + test.name);
} catch (e) {
suite.tearDown && suite.tearDown();
tearDown(test.name);

this.log("[FAILED] " + test.name + ": " + e);
}
Expand Down
98 changes: 82 additions & 16 deletions jsunity/test/core.js
Expand Up @@ -46,26 +46,78 @@ function CoreTestSuite() {
delete origLog;
}

function testSetUpTearDownCalled() {
function setUpTearDownTestSuite() {
function setUp() {
function testSetUpTearDownCalledPassing() {
var calls = [];

jsUnity.run({
setUp: function () {
calls.push("setUp");
},
tearDown: function () {
calls.push("tearDown");
},
testPassing: function () {
calls.push("testPassing");
}

function tearDown() {
});

a.assertEqual([ "setUp", "testPassing", "tearDown" ], calls);
}

function testSetUpTearDownCalledFailing() {
var calls = [];

jsUnity.run({
setUp: function () {
calls.push("setUp");
},
tearDown: function () {
calls.push("tearDown");
},
testFailing: function () {
calls.push("testFailing");
a.fail();
}

function testDummy() {
calls.push("testDummy");
});

a.assertEqual([ "setUp", "testFailing", "tearDown" ], calls);
}

function testArgumentsPassing() {
var calls = [];

jsUnity.run({
setUp: function (testName) {
calls.push(testName);
},
tearDown: function (testName) {
calls.push(testName);
},
testPassing: function (testName) {
calls.push(testName);
}
}
});

a.assertEqual([ "testPassing", "testPassing", "testPassing" ], calls);
}

calls = [];
function testArgumentsFailing() {
var calls = [];

jsUnity.run(setUpTearDownTestSuite);
jsUnity.run({
setUp: function (testName) {
calls.push(testName);
},
tearDown: function (testName) {
calls.push(testName);
},
testFailing: function (testName) {
calls.push(testName);
a.fail();
}
});

a.assertIdentical("setUp,testDummy,tearDown", calls.join(","));
a.assertEqual([ "testFailing", "testFailing", "testFailing" ], calls);
}

function testLogCalled() {
Expand Down Expand Up @@ -466,14 +518,12 @@ function CoreTestSuite() {
a.assertIdentical(1, results.passed);
}

function testRunObjectBindsObjectAsTearDownScope() {
function testRunObjectBindsObjectAsPassingTearDownScope() {
var results = jsUnity.run({
tearDown: function () {
this.marker = true;
},
testNoop: function () {
a.assertTrue(true);
},
testPassing: function () {},
testMarker: function () {
a.assertTrue(this.marker);
}
Expand All @@ -482,5 +532,21 @@ function CoreTestSuite() {
a.assertIdentical(2, results.passed);
}

function testRunObjectBindsObjectAsFailingTearDownScope() {
var results = jsUnity.run({
tearDown: function () {
this.marker = true;
},
testFailing: function () {
a.fail();
},
testMarker: function () {
a.assertTrue(this.marker);
}
});

a.assertIdentical(1, results.passed);
a.assertIdentical(1, results.failed);
}
}
//%>

0 comments on commit 25e9de7

Please sign in to comment.