Skip to content

Commit

Permalink
(new) added assert.inDelta
Browse files Browse the repository at this point in the history
  • Loading branch information
mynyml authored and Alexis Sellier committed Jun 28, 2011
1 parent 5b770a4 commit bc868fa
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
8 changes: 8 additions & 0 deletions lib/assert/macros.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ assert.lesser = function (actual, expected, message) {
}
};

assert.inDelta = function (actual, expected, delta, message) {
lower = expected - delta;
upper = expected + delta;
if (actual < lower || actual > upper) {
assert.fail(actual, expected, message || "expected {actual} to be in within *"+ delta.toString() +"* of {expected}", null, assert.inDelta);
}
};

//
// Inclusion
//
Expand Down
14 changes: 11 additions & 3 deletions test/assert-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ vows.describe('vows/assert').addBatch({
assert.greater(5, 4);
assert.lesser(4, 5);
},
"`inDelta`": function (assert) {
assert.inDelta(42, 40, 5);
assert.inDelta(42, 40, 2);
assert.inDelta(42, 42, 0);
assert.inDelta(3.1, 3.0, 0.2);
assertError(assert.inDelta, [42, 40, 1]);
},
"`isEmpty`": function (assert) {
assert.isEmpty({});
assert.isEmpty([]);
Expand All @@ -98,13 +105,14 @@ vows.describe('vows/assert').addBatch({
}
}).export(module);

function assertError(assertion, value, fail) {
function assertError(assertion, args, fail) {
if (!Array.isArray(args)) { args = [args]; }
try {
assertion(value);
assertion.apply(null, args);
fail = true;
} catch (e) {/* Success */}

fail && assert.fail(value, assert.AssertionError,
fail && assert.fail(args.join(' '), assert.AssertionError,
"expected an AssertionError for {actual}",
"assertError", assertError);
}
Expand Down

0 comments on commit bc868fa

Please sign in to comment.