Skip to content

Instantly share code, notes, and snippets.

@mohayonao
Created July 27, 2012 13:25
Show Gist options
  • Save mohayonao/3188055 to your computer and use it in GitHub Desktop.
Save mohayonao/3188055 to your computer and use it in GitHub Desktop.
だいたいあってたらOKになるテスト
var should = require("should");
var i = require("util").inspect;
var NEARLY_PRECISION = 1/1000000;
if (! Object.getOwnPropertyDescriptor(should.Assertion.prototype, "nearly") ) {
Object.defineProperty(should.Assertion.prototype, "nearly", {
get: function() {
this.nearlyflag = true
return this;
}
});
}
should.Assertion.prototype.equal = function(val, desc) {
var actual = this.nearlyflag ?
Math.abs(val.valueOf() - this.obj) < NEARLY_PRECISION :
val.valueOf() === this.obj;
this.nearlyflag = false;
this.assert(
actual
, 'expected ' + this.inspect + ' to equal ' + i(val) + (desc ? " | " + desc : "")
, 'expected ' + this.inspect + ' to not equal ' + i(val) + (desc ? " | " + desc : "")
, val);
return this;
};
describe "usage", ->
# これだとテストが通らない
# AssertionError: expected 1.4142135623730951 to equal 1.41421356
it "sqrt(2) is 1.41421356", ->
Math.sqrt(2).should.equal 1.41421356
# こっちは通る
it "sqrt(2) is nearly 1.41421356", ->
Math.sqrt(2).should.nearly.equal 1.41421356
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment