Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Update should.js #112

Closed
wants to merge 2 commits into from

2 participants

@skeggse

Add Object.should.approximate(value, [precision]) for checking if two floating-point numbers approximate each other.

skeggse added some commits
@skeggse skeggse Update should.js
Add Object.should.approximate(value, [precision]) for checking if two floating-point numbers approximate each other
f65f9f2
@skeggse skeggse Update should.test.js
Add tests for Object.should.approximate
a064592
@skeggse

@visionmedia have you looked at this?

@btd
Collaborator
btd commented

Hi @skeggse , there is already such function, but it was undocumented. I added tests for it and updated readme.

@btd btd closed this
@skeggse

Wonderful, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 15, 2013
  1. @skeggse

    Update should.js

    skeggse authored
    Add Object.should.approximate(value, [precision]) for checking if two floating-point numbers approximate each other
  2. @skeggse

    Update should.test.js

    skeggse authored
    Add tests for Object.should.approximate
This page is out of date. Refresh to see the latest.
Showing with 23 additions and 1 deletion.
  1. +17 −0 lib/should.js
  2. +6 −1 test/should.test.js
View
17 lib/should.js
@@ -709,6 +709,23 @@ Assertion.prototype = {
, function(){ return 'expected no exception to be thrown, got "' + err.message + '"' });
return this;
+ },
+
+ /**
+ * Assert that this value approximates _value_ within _precision_ digits
+ *
+ * @return {Assertion} for chaining
+ * @api public
+ */
+ approximate: function(value, precision) {
+ precision = precision || 6;
+
+ this.assert(
+ Math.abs(this.obj - value) < Math.pow(10, -precision)
+ , function(){ return 'expected ' + this.inspect + ' to approximate ' + value + ' to ' + precision + ' digits' }
+ , function(){ return 'expected ' + this.inspect + ' to not approximate ' + value + ' to ' + precision + ' digits' });
+
+ return this;
}
};
View
7 test/should.test.js
@@ -1,4 +1,3 @@
-
/**
* Module dependencies.
*/
@@ -659,6 +658,12 @@ module.exports = {
err(function(){
(function(){ throw 'error'; }).should.throw(Error);
}, "expected an exception to be thrown of type Error, but got String");
+ },
+
+ 'test approximate()': function(){
+ (0.8).should.approximate(0.80000001);
+ (0.8).should.approximate(0.80000001, 3);
+ (0.8).should.not.approximate(0.8001, 8);
}
};
Something went wrong with that request. Please try again.