1+ /**
2+ * Checks that the first two arguments are equal, or are numbers close enough to be considered equal
3+ * based on a specified maximum allowable difference.
4+ *
5+ * @example assert.close(3.141, Math.PI, 0.001);
6+ *
7+ * @param Number actual
8+ * @param Number expected
9+ * @param Number maxDifference (the maximum inclusive difference allowed between the actual and expected numbers)
10+ * @param String message (optional)
11+ */
12+ function close ( actual , expected , maxDifference , message ) {
13+ var actualDiff = ( actual === expected ) ? 0 : Math . abs ( actual - expected ) ,
14+ result = actualDiff <= maxDifference ;
15+ message = message || ( actual + " should be within " + maxDifference + " (inclusive) of " + expected + ( result ? "" : ". Actual: " + actualDiff ) ) ;
16+ QUnit . push ( result , actual , expected , message ) ;
17+ }
18+
19+
20+ /**
21+ * Checks that the first two arguments are equal, or are numbers close enough to be considered equal
22+ * based on a specified maximum allowable difference percentage.
23+ *
24+ * @example assert.close.percent(155, 150, 3.4); // Difference is ~3.33%
25+ *
26+ * @param Number actual
27+ * @param Number expected
28+ * @param Number maxPercentDifference (the maximum inclusive difference percentage allowed between the actual and expected numbers)
29+ * @param String message (optional)
30+ */
31+ close . percent = function closePercent ( actual , expected , maxPercentDifference , message ) {
32+ var actualDiff , result ;
33+ if ( actual === expected ) {
34+ actualDiff = 0 ;
35+ result = actualDiff <= maxPercentDifference ;
36+ }
37+ else if ( actual !== 0 && expected !== 0 && expected !== Infinity && expected !== - Infinity ) {
38+ actualDiff = Math . abs ( 100 * ( actual - expected ) / expected ) ;
39+ result = actualDiff <= maxPercentDifference ;
40+ }
41+ else {
42+ // Dividing by zero (0)! Should return `false` unless the max percentage was `Infinity`
43+ actualDiff = Infinity ;
44+ result = maxPercentDifference === Infinity ;
45+ }
46+ message = message || ( actual + " should be within " + maxPercentDifference + "% (inclusive) of " + expected + ( result ? "" : ". Actual: " + actualDiff + "%" ) ) ;
47+
48+ QUnit . push ( result , actual , expected , message ) ;
49+ } ;
50+
51+
52+ /**
53+ * Checks that the first two arguments are numbers with differences greater than the specified
54+ * minimum difference.
55+ *
56+ * @example assert.notClose(3.1, Math.PI, 0.001);
57+ *
58+ * @param Number actual
59+ * @param Number expected
60+ * @param Number minDifference (the minimum exclusive difference allowed between the actual and expected numbers)
61+ * @param String message (optional)
62+ */
63+ function notClose ( actual , expected , minDifference , message ) {
64+ var actualDiff = Math . abs ( actual - expected ) ,
65+ result = actualDiff > minDifference ;
66+ message = message || ( actual + " should not be within " + minDifference + " (exclusive) of " + expected + ( result ? "" : ". Actual: " + actualDiff ) ) ;
67+ QUnit . push ( result , actual , expected , message ) ;
68+ }
69+
70+
71+ /**
72+ * Checks that the first two arguments are numbers with differences greater than the specified
73+ * minimum difference percentage.
74+ *
75+ * @example assert.notClose.percent(156, 150, 3.5); // Difference is 4.0%
76+ *
77+ * @param Number actual
78+ * @param Number expected
79+ * @param Number minPercentDifference (the minimum exclusive difference percentage allowed between the actual and expected numbers)
80+ * @param String message (optional)
81+ */
82+ notClose . percent = function notClosePercent ( actual , expected , minPercentDifference , message ) {
83+ var actualDiff , result ;
84+ if ( actual === expected ) {
85+ actualDiff = 0 ;
86+ result = actualDiff > minPercentDifference ;
87+ }
88+ else if ( actual !== 0 && expected !== 0 && expected !== Infinity && expected !== - Infinity ) {
89+ actualDiff = Math . abs ( 100 * ( actual - expected ) / expected ) ;
90+ result = actualDiff > minPercentDifference ;
91+ }
92+ else {
93+ // Dividing by zero (0)! Should only return `true` if the min percentage was `Infinity`
94+ actualDiff = Infinity ;
95+ result = minPercentDifference !== Infinity ;
96+ }
97+ message = message || ( actual + " should not be within " + minPercentDifference + "% (exclusive) of " + expected + ( result ? "" : ". Actual: " + actualDiff + "%" ) ) ;
98+
99+ QUnit . push ( result , actual , expected , message ) ;
100+ } ;
101+
102+
103+ QUnit . extend ( QUnit . assert , {
104+ close : close ,
105+ notClose : notClose
106+ } ) ;
0 commit comments