Created
December 17, 2014 06:58
-
-
Save ichabodcole/c7c6a2007d18c94377ba to your computer and use it in GitHub Desktop.
jasmine 2.1 - CustomMatcher -- toBeNearTo
This file contains 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
jasmine.addMatchers({ | |
toBeNearTo: function (utils, customEqualityTesters) { | |
return { | |
compare: function(actual, expected, within) { | |
var diff, | |
result = {}; | |
within = Math.abs(within) || 1; | |
diff = Math.abs(actual - expected); | |
result.pass = diff <= within; | |
if (result.pass) { | |
result.message = "Expected " + actual + " to be within " + within + " of " + expected + "."; | |
} else { | |
result.message = "Expected " + actual + " to be within " + within + " of " + expected + ", but was within " + diff + "."; | |
} | |
return result; | |
} | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment