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 ;
1+ ( function ( factory ) {
2+
3+ // NOTE:
4+ // All techniques except for the "browser globals" fallback will extend the
5+ // provided QUnit object but return the isolated API methods
6+
7+ // For AMD: Register as an anonymous AMD module with a named dependency on "qunit".
8+ if ( typeof define === "function" && define . amd ) {
9+ define ( [ "qunit" ] , factory ) ;
10+ }
11+ // For Node.js
12+ else if ( typeof module !== "undefined" && module && module . exports && typeof require === "function" ) {
13+ module . exports = factory ( require ( "qunitjs" ) ) ;
3614 }
37- else if ( actual !== 0 && expected !== 0 && expected !== Infinity && expected !== - Infinity ) {
38- actualDiff = Math . abs ( 100 * ( actual - expected ) / expected ) ;
39- result = actualDiff <= maxPercentDifference ;
15+ // For CommonJS with `exports`, but without `module.exports`, like Rhino
16+ else if ( typeof exports !== "undefined" && exports && typeof require === "function" ) {
17+ var qunit = require ( "qunitjs" ) ;
18+ qunit . extend ( exports , factory ( qunit ) ) ;
4019 }
20+ // For browser globals
4121 else {
42- // Dividing by zero (0)! Should return `false` unless the max percentage was `Infinity`
43- actualDiff = Infinity ;
44- result = maxPercentDifference === Infinity ;
22+ factory ( QUnit ) ;
4523 }
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 ;
24+
25+ } ( function ( QUnit ) {
26+
27+ /**
28+ * Find an appropriate `Assert` context to `push` results to.
29+ * @param * context - An unknown context, possibly `Assert`, `Test`, or neither
30+ * @private
31+ */
32+ function _getPushContext ( context ) {
33+ var pushContext ;
34+
35+ if ( context && typeof context . push === "function" ) {
36+ // `context` is an `Assert` context
37+ pushContext = context ;
38+ }
39+ else if ( context && context . assert && typeof context . assert . push === "function" ) {
40+ // `context` is a `Test` context
41+ pushContext = context . assert ;
42+ }
43+ else if (
44+ QUnit && QUnit . config && QUnit . config . current && QUnit . config . current . assert &&
45+ typeof QUnit . config . current . assert . push === "function"
46+ ) {
47+ // `context` is an unknown context but we can find the `Assert` context via QUnit
48+ pushContext = QUnit . config . current . assert ;
49+ }
50+ else if ( QUnit && typeof QUnit . push === "function" ) {
51+ pushContext = QUnit . push ;
52+ }
53+ else {
54+ throw new Error ( "Could not find the QUnit `Assert` context to push results" ) ;
55+ }
56+
57+ return pushContext ;
8758 }
88- else if ( actual !== 0 && expected !== 0 && expected !== Infinity && expected !== - Infinity ) {
89- actualDiff = Math . abs ( 100 * ( actual - expected ) / expected ) ;
90- result = actualDiff > minPercentDifference ;
59+
60+ /**
61+ * Checks that the first two arguments are equal, or are numbers close enough to be considered equal
62+ * based on a specified maximum allowable difference.
63+ *
64+ * @example assert.close(3.141, Math.PI, 0.001);
65+ *
66+ * @param Number actual
67+ * @param Number expected
68+ * @param Number maxDifference (the maximum inclusive difference allowed between the actual and expected numbers)
69+ * @param String message (optional)
70+ */
71+ function close ( actual , expected , maxDifference , message ) {
72+ var actualDiff = ( actual === expected ) ? 0 : Math . abs ( actual - expected ) ,
73+ result = actualDiff <= maxDifference ,
74+ pushContext = _getPushContext ( this ) ;
75+
76+ message = message || ( actual + " should be within " + maxDifference + " (inclusive) of " + expected + ( result ? "" : ". Actual: " + actualDiff ) ) ;
77+
78+ pushContext . push ( result , actual , expected , message ) ;
9179 }
92- else {
93- // Dividing by zero (0)! Should only return `true` if the min percentage was `Infinity`
94- actualDiff = Infinity ;
95- result = minPercentDifference !== Infinity ;
80+
81+
82+ /**
83+ * Checks that the first two arguments are equal, or are numbers close enough to be considered equal
84+ * based on a specified maximum allowable difference percentage.
85+ *
86+ * @example assert.close.percent(155, 150, 3.4); // Difference is ~3.33%
87+ *
88+ * @param Number actual
89+ * @param Number expected
90+ * @param Number maxPercentDifference (the maximum inclusive difference percentage allowed between the actual and expected numbers)
91+ * @param String message (optional)
92+ */
93+ close . percent = function closePercent ( actual , expected , maxPercentDifference , message ) {
94+ var actualDiff , result ,
95+ pushContext = _getPushContext ( this ) ;
96+
97+ if ( actual === expected ) {
98+ actualDiff = 0 ;
99+ result = actualDiff <= maxPercentDifference ;
100+ }
101+ else if ( actual !== 0 && expected !== 0 && expected !== Infinity && expected !== - Infinity ) {
102+ actualDiff = Math . abs ( 100 * ( actual - expected ) / expected ) ;
103+ result = actualDiff <= maxPercentDifference ;
104+ }
105+ else {
106+ // Dividing by zero (0)! Should return `false` unless the max percentage was `Infinity`
107+ actualDiff = Infinity ;
108+ result = maxPercentDifference === Infinity ;
109+ }
110+ message = message || ( actual + " should be within " + maxPercentDifference + "% (inclusive) of " + expected + ( result ? "" : ". Actual: " + actualDiff + "%" ) ) ;
111+
112+ pushContext . push ( result , actual , expected , message ) ;
113+ } ;
114+
115+
116+ /**
117+ * Checks that the first two arguments are numbers with differences greater than the specified
118+ * minimum difference.
119+ *
120+ * @example assert.notClose(3.1, Math.PI, 0.001);
121+ *
122+ * @param Number actual
123+ * @param Number expected
124+ * @param Number minDifference (the minimum exclusive difference allowed between the actual and expected numbers)
125+ * @param String message (optional)
126+ */
127+ function notClose ( actual , expected , minDifference , message ) {
128+ var actualDiff = Math . abs ( actual - expected ) ,
129+ result = actualDiff > minDifference ,
130+ pushContext = _getPushContext ( this ) ;
131+
132+ message = message || ( actual + " should not be within " + minDifference + " (exclusive) of " + expected + ( result ? "" : ". Actual: " + actualDiff ) ) ;
133+
134+ pushContext . push ( result , actual , expected , message ) ;
96135 }
97- message = message || ( actual + " should not be within " + minPercentDifference + "% (exclusive) of " + expected + ( result ? "" : ". Actual: " + actualDiff + "%" ) ) ;
98136
99- QUnit . push ( result , actual , expected , message ) ;
100- } ;
101137
138+ /**
139+ * Checks that the first two arguments are numbers with differences greater than the specified
140+ * minimum difference percentage.
141+ *
142+ * @example assert.notClose.percent(156, 150, 3.5); // Difference is 4.0%
143+ *
144+ * @param Number actual
145+ * @param Number expected
146+ * @param Number minPercentDifference (the minimum exclusive difference percentage allowed between the actual and expected numbers)
147+ * @param String message (optional)
148+ */
149+ notClose . percent = function notClosePercent ( actual , expected , minPercentDifference , message ) {
150+ var actualDiff , result ,
151+ pushContext = _getPushContext ( this ) ;
152+
153+ if ( actual === expected ) {
154+ actualDiff = 0 ;
155+ result = actualDiff > minPercentDifference ;
156+ }
157+ else if ( actual !== 0 && expected !== 0 && expected !== Infinity && expected !== - Infinity ) {
158+ actualDiff = Math . abs ( 100 * ( actual - expected ) / expected ) ;
159+ result = actualDiff > minPercentDifference ;
160+ }
161+ else {
162+ // Dividing by zero (0)! Should only return `true` if the min percentage was `Infinity`
163+ actualDiff = Infinity ;
164+ result = minPercentDifference !== Infinity ;
165+ }
166+ message = message || ( actual + " should not be within " + minPercentDifference + "% (exclusive) of " + expected + ( result ? "" : ". Actual: " + actualDiff + "%" ) ) ;
167+
168+ pushContext . push ( result , actual , expected , message ) ;
169+ } ;
170+
171+
172+ var api = {
173+ close : close ,
174+ notClose : notClose ,
175+ closePercent : close . percent ,
176+ notClosePercent : notClose . percent
177+ } ;
178+
179+ QUnit . extend ( QUnit . assert , api ) ;
102180
103- QUnit . extend ( QUnit . assert , {
104- close : close ,
105- notClose : notClose
106- } ) ;
181+ return api ;
182+ } ) ) ;
0 commit comments