Created
July 27, 2012 13:25
-
-
Save mohayonao/3188055 to your computer and use it in GitHub Desktop.
だいたいあってたらOKになるテスト
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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