Debian Codegraph @

tests/lib/vendor/qunit-assert-close/qunit-assert-close.js

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