Skip to content

Commit

Permalink
Make assert.epsilon fail for NaN actual value
Browse files Browse the repository at this point in the history
It makes sense than NaN would not be within a tolerance of a number. but
since Math.abs(NaN) = NaN and NaN > x evalutes to false for all numeric
x, assert.epsilon will pass if NaN is passed as the 'actual' argument.

I could not determine how to use vows to assert that an assertion would
fail, so there is no test case for this change, but the existing tests
pass. I'm not sure asserting that an assertion will fail is possible
with the library's promise-oriented design.

This bug was originally found while working on the jStat project which
relies on vows; if the vows team does not want to make assert.epsilon
fail for NaN actual value, it would be good to have an additional method
that would check both that the argument is a number and within the
tolerance.
  • Loading branch information
James Gibson committed Nov 5, 2014
1 parent 64857e3 commit ec0442c
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion lib/assert/macros.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ for (var key in messages) {
}

assert.epsilon = function (eps, actual, expected, message) {
if (Math.abs(actual - expected) > eps) {
if (isNaN(actual) || Math.abs(actual - expected) > eps) {
assert.fail(actual, expected, message || "expected {expected} \u00B1"+ eps +", but was {actual}");
}
};
Expand Down

0 comments on commit ec0442c

Please sign in to comment.