Skip to content

Commit

Permalink
Merge pull request #320 from jamescgibson/master
Browse files Browse the repository at this point in the history
Make assert.epsilon fail for NaN actual value
  • Loading branch information
indexzero committed Nov 6, 2014
2 parents 64857e3 + f5cec76 commit bc7cad0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/assert/macros.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ for (var key in messages) {
}

assert.epsilon = function (eps, actual, expected, message) {
if (Math.abs(actual - expected) > eps) {
if (isNaN(eps)) {
assert.fail(actual, expected, message || "cannot compare {actual} with {expected} \u00B1 NaN");
} else if (isNaN(actual) || Math.abs(actual - expected) > eps) {
assert.fail(actual, expected, message || "expected {expected} \u00B1"+ eps +", but was {actual}");
}
};
Expand Down
6 changes: 6 additions & 0 deletions test/assert-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ vows.describe('vows/assert').addBatch({
},
"`epsilon`": function() {
assert.epsilon(1e-5, 0.1+0.2, 0.3);
assert.throws(function() {
assert.epsilon(1e-5, NaN, 0.3);
});
assert.throws(function() {
assert.epsilon(NaN, 1.0, 1.0);
});
},
"`match`": function () {
assert.match("hello world", /^[a-z]+ [a-z]+$/);
Expand Down

0 comments on commit bc7cad0

Please sign in to comment.