1 var HELPER_MATCHERS = (function() { 2 var EPSILON = 0.00001; 3 4 return { 5 /* 6 Returns true if `actual` has the same length as `expected`, and 7 if each element of both arrays is within 0.000001 of each other. 8 This is a way to check for "equal enough" conditions, as a way 9 of working around floating point imprecision. 10 */ 11 toBeEqualish: function(expected) { 12 if (typeof(this.actual) == 'number') 13 return Math.abs(this.actual - expected) < EPSILON; 14 15 if (this.actual.length != expected.length) return false; 16 for (var i = 0; i < this.actual.length; i++) { 17 if (isNaN(this.actual[i]) !== isNaN(expected[i])) 18 return false; 19 if (Math.abs(this.actual[i] - expected[i]) >= EPSILON) 20 return false; 21 } 22 return true; 23 } 24 }; 25 })(); 26 27 beforeEach(function() { 28 this.addMatchers(HELPER_MATCHERS); 29 }); 30 31 if (typeof(global) != 'undefined') 32 global.HELPER_MATCHERS = HELPER_MATCHERS; 33