From c364d072d766f52544d8fb755e0c9a20fe2673cd Mon Sep 17 00:00:00 2001 From: Constantin Titarenko Date: Sat, 22 Sep 2012 01:19:10 +0300 Subject: [PATCH] Added #approximately(value, delta, description) for doing assertions on results of operations with numbers. --- examples/runner.js | 12 ++++++++++++ lib/should.js | 13 +++++++++++++ 2 files changed, 25 insertions(+) diff --git a/examples/runner.js b/examples/runner.js index 9355955..3c1dc69 100644 --- a/examples/runner.js +++ b/examples/runner.js @@ -50,4 +50,16 @@ test('Point#add()', function(){ point.should.respondTo('add'); }); +test('Math#sin()', function(){ + Math.sin(12).should.be.approximately(-0.5365, 1e-3); +}); + +test('Math#cos()', function(){ + Math.cos(0).should.not.be.approximately(10, 1e-3); +}); + +test('Math#log()', function(){ + Math.log(10).should.be.approximately(10, 1e-3); +}); + console.log(); \ No newline at end of file diff --git a/lib/should.js b/lib/should.js index f603a35..fa7a4f0 100644 --- a/lib/should.js +++ b/lib/should.js @@ -321,6 +321,19 @@ Assertion.prototype = { return this; }, + /** + * Assert within value +- delta (inclusive). + * + * @param {Number} value + * @param {Number} delta + * @param {String} description + * @api public + */ + + approximately: function(value, delta, description) { + return this.within(value - delta, value + delta, description); + }, + /** * Assert typeof. *