Skip to content

Commit af734ce

Browse files
committed
Fixes #6. When an assertion is created but never executed, the test is marked as failed
1 parent 1fca315 commit af734ce

File tree

3 files changed

+71
-10
lines changed

3 files changed

+71
-10
lines changed

Tyrtle.js

+36-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*!
22
* Tyrtle - A JavaScript Unit Testing Framework
33
*
4-
* Copyright (c) 2011 Nick Fisher
4+
* Copyright (c) 2011-2012 Nick Fisher
55
* Distributed under the terms of the LGPL
66
* http://www.gnu.org/licenses/lgpl.html
77
*/
@@ -26,8 +26,9 @@
2626
setParams,
2727
root,
2828
runningInNode,
29-
moduleAssertions = null, // the extra assertions added by an individual module
30-
currentTestAssertions // a counter for the number of assertions run in an individual test
29+
unexecutedAssertions = 0, // the count of assertions created minus the number of assertions executed.
30+
moduleAssertions = null, // the extra assertions added by an individual module
31+
currentTestAssertions // a counter for the number of assertions run in an individual test
3132
;
3233
// Gets the global object, regardless of whether run as ES3, ES5 or ES5 Strict Mode.
3334
root = (function () {
@@ -784,7 +785,7 @@
784785
* @protected
785786
*/
786787
run : function (callback) {
787-
var start, success, handleError, test = this;
788+
var start, success, handleError, test = this, originalUnexecutedAssertions;
788789
success = function () {
789790
test.runTime = new Date() - start;
790791
test.status = PASS;
@@ -812,28 +813,45 @@
812813
this.body(function (variables) {
813814
variables = variables || {};
814815
try {
815-
currentTestAssertions = 0; // this is incremented by the `since` function
816+
// this is incremented by the `since` function
817+
currentTestAssertions = 0;
818+
819+
// remember how many unexecuted assertion there were at the start
820+
originalUnexecutedAssertions = unexecutedAssertions;
816821
test.asyncFn.call(variables, assert);
822+
817823
if (test.expectedAssertions !== -1) {
818824
assert.that(currentTestAssertions)
819825
.is(test.expectedAssertions)
820826
.since("Incorrect number of assertions made by this test.")
821827
;
822828
}
829+
// check that no assertions were left unexecuted.
830+
assert
831+
.that(unexecutedAssertions - originalUnexecutedAssertions)
832+
.is(0)
833+
.since("This test defines assertions which are never executed");
834+
823835
success();
824836
} catch (ee) {
825837
handleError(ee);
826838
}
827839
});
828840
} else {
829841
currentTestAssertions = 0;
842+
originalUnexecutedAssertions = unexecutedAssertions;
830843
this.body(assert);
831844
if (test.expectedAssertions !== -1) {
832845
assert.that(currentTestAssertions)
833846
.is(test.expectedAssertions)
834847
.since("Incorrect number of assertions made by this test.")
835848
;
836849
}
850+
// check that no assertions were left unexecuted.
851+
assert
852+
.that(unexecutedAssertions - originalUnexecutedAssertions)
853+
.is(0)
854+
.since("This test defines assertions which are never executed");
837855
success();
838856
}
839857
} catch (e) {
@@ -887,7 +905,10 @@
887905
// Assertions //
888906
//////////////////
889907
(function () {
890-
var assertions, build, handleAssertionResult, internalAssertionCount = 0;
908+
var assertions,
909+
build,
910+
handleAssertionResult,
911+
internalAssertionCount = 0;
891912
assertions = {
892913
/**
893914
* Assert that two values are not identical. Uses strict equality checking: `!==`.
@@ -1273,18 +1294,25 @@
12731294
*/
12741295
build = function (condition, message/*, args */) {
12751296
var args = Array.prototype.slice.call(arguments, 2),
1276-
since
1277-
;
1297+
since;
1298+
++unexecutedAssertions;
12781299
since = function (userMessage) {
12791300
try {
12801301
if (internalAssertionCount++ === 0) {
12811302
++currentTestAssertions;
12821303
}
1304+
// if this is the first time we've executed this assertion, then decrease the counter, and don't count this
1305+
// one again
1306+
if (!since.executed) {
1307+
--unexecutedAssertions;
1308+
since.executed = true;
1309+
}
12831310
handleAssertionResult(condition.apply(assert, args), args, message, userMessage);
12841311
} finally {
12851312
--internalAssertionCount;
12861313
}
12871314
};
1315+
since.executed = false;
12881316
since.since = since;
12891317
return since;
12901318
};

demo/demo-tests.js

+6
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,12 @@
175175
this.test("endsWith 3", function (assert) {
176176
assert.that(123456).endsWith('456')();
177177
});
178+
this.test("endsWith 4", function (assert) {
179+
assert.that("123456").endsWith(456)();
180+
});
181+
this.test("This test forgets to execute an assertion", function (assert) {
182+
assert.that(true).is(true);
183+
});
178184
});
179185
tests.module("Demonstrating the variable logging (these should fail)", function () {
180186
this.test("Number, String", function (assert) {

test/assertions.js

+29-2
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ asyncTest("Globally added assertions", function () {
568568
});
569569
t.run();
570570
});
571-
asyncTest("Assertion functions has access to other assertions via this", function () {
571+
asyncTest("Assertion functions have access to other assertions via this", function () {
572572
var t, passing = [], failing = [];
573573
t = new Tyrtle({
574574
callback : function () {
@@ -600,7 +600,7 @@ asyncTest("Assertion functions has access to other assertions via this", functio
600600
passing.push(this.test("passing", function (assert) {
601601
assert(true).globalA()("foo");
602602
assert(true).globalB()("foo");
603-
assert(true).localA();
603+
assert(true).localA()();
604604
assert('woo').globalToLocal()();
605605
}));
606606
failing.push(this.test("failing", function (assert) {
@@ -657,3 +657,30 @@ asyncTest("Assertions calling other assertions do not raise the expected count",
657657
});
658658
t.run();
659659
});
660+
661+
asyncTest("Unexecuted assertions are treated as an error", function () {
662+
var t, passing = [], failing = [];
663+
t = new Tyrtle({
664+
callback: function () {
665+
equal(t.passes, passing.length, "Incorrect number of passes");
666+
equal(t.fails, failing.length, "Incorrect number of failures");
667+
start();
668+
}
669+
});
670+
t.module("a", function () {
671+
failing.push(this.test("doesn't execute an assertion", function (assert) {
672+
assert.that(4).is(4);
673+
}));
674+
failing.push(this.test("one unexecuted, one executed twice", function (assert) {
675+
assert.that(4).is(4);
676+
var a = assert.that(2 + 2).is(4);
677+
a();
678+
a('still');
679+
}));
680+
passing.push(this.test("delayed execution", function (assert) {
681+
var a = assert.that(2 + 2).is(4);
682+
a();
683+
}));
684+
});
685+
t.run();
686+
});

0 commit comments

Comments
 (0)