Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

- Pass test name to test function and setUp/tearDown

- Fixed failing test case tearDown scope
  • Loading branch information...
commit 25e9de75396124e1e01e2700b34a7d53a5f2859d 1 parent 8c0d76a
@atesgoral authored
Showing with 104 additions and 21 deletions.
  1. +2 −0  jsunity/CHANGES.txt
  2. +20 −5 jsunity/jsunity.js
  3. +82 −16 jsunity/test/core.js
View
2  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
View
25 jsunity/jsunity.js
@@ -172,6 +172,8 @@ jsUnity = (function () {
}
};
+ function empty() {}
+
function plural(cnt, unit) {
return cnt + " " + unit + (cnt == 1 ? "" : "s");
}
@@ -309,7 +311,7 @@ jsUnity = (function () {
}
},
- log: function () {},
+ log: empty,
error: function (s) { this.log("[ERROR] " + s); },
@@ -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);
}
View
98 jsunity/test/core.js
@@ -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() {
@@ -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);
}
@@ -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);
+ }
}
//%>
Please sign in to comment.
Something went wrong with that request. Please try again.