corrade-http-templates – Blame information for rev 62

Subversion Repositories:
Rev:
Rev Author Line No. Line
62 office 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 <= maxPercentDifference; }
41 <= maxPercentDifference; else {
42 <= maxPercentDifference; // Dividing by zero (0)! Should return `false` unless the max percentage was `Infinity`
43 <= maxPercentDifference; actualDiff = Infinity;
44 <= maxPercentDifference; result = maxPercentDifference === Infinity;
45 <= maxPercentDifference; }
46 <= maxPercentDifference; message = message || (actual + " should be within " + maxPercentDifference + "% (inclusive) of " + expected + (result ? "" : ". Actual: " + actualDiff + "%"));
47  
48 <= maxPercentDifference; QUnit.push(result, actual, expected, message);
49 <= maxPercentDifference;};
50  
51  
52 <= maxPercentDifference;/**
53 <= maxPercentDifference; * Checks that the first two arguments are numbers with differences greater than the specified
54 <= maxPercentDifference; * minimum difference.
55 <= maxPercentDifference; *
56 <= maxPercentDifference; * @example assert.notClose(3.1, Math.PI, 0.001);
57 <= maxPercentDifference; *
58 <= maxPercentDifference; * @param Number actual
59 <= maxPercentDifference; * @param Number expected
60 <= maxPercentDifference; * @param Number minDifference (the minimum exclusive difference allowed between the actual and expected numbers)
61 <= maxPercentDifference; * @param String message (optional)
62 <= maxPercentDifference; */
63 <= maxPercentDifference;function notClose(actual, expected, minDifference, message) {
64 <= maxPercentDifference; var actualDiff = Math.abs(actual - expected),
65 <= maxPercentDifference; result = actualDiff > minDifference;
66 <= maxPercentDifference; message = message || (actual + " should not be within " + minDifference + " (exclusive) of " + expected + (result ? "" : ". Actual: " + actualDiff));
67 <= maxPercentDifference; QUnit.push(result, actual, expected, message);
68 <= maxPercentDifference;}
69  
70  
71 <= maxPercentDifference;/**
72 <= maxPercentDifference; * Checks that the first two arguments are numbers with differences greater than the specified
73 <= maxPercentDifference; * minimum difference percentage.
74 <= maxPercentDifference; *
75 <= maxPercentDifference; * @example assert.notClose.percent(156, 150, 3.5); // Difference is 4.0%
76 <= maxPercentDifference; *
77 <= maxPercentDifference; * @param Number actual
78 <= maxPercentDifference; * @param Number expected
79 <= maxPercentDifference; * @param Number minPercentDifference (the minimum exclusive difference percentage allowed between the actual and expected numbers)
80 <= maxPercentDifference; * @param String message (optional)
81 <= maxPercentDifference; */
82 <= maxPercentDifference;notClose.percent = function notClosePercent(actual, expected, minPercentDifference, message) {
83 <= maxPercentDifference; var actualDiff, result;
84 <= maxPercentDifference; if (actual === expected) {
85 <= maxPercentDifference; actualDiff = 0;
86 <= maxPercentDifference; result = actualDiff > minPercentDifference;
87 <= maxPercentDifference; }
88 <= maxPercentDifference; else if (actual !== 0 && expected !== 0 && expected !== Infinity && expected !== -Infinity) {
89 <= maxPercentDifference; actualDiff = Math.abs(100 * (actual - expected) / expected);
90 <= maxPercentDifference; result = actualDiff > minPercentDifference;
91 <= maxPercentDifference; }
92 <= maxPercentDifference; else {
93 <= maxPercentDifference; // Dividing by zero (0)! Should only return `true` if the min percentage was `Infinity`
94 <= maxPercentDifference; actualDiff = Infinity;
95 <= maxPercentDifference; result = minPercentDifference !== Infinity;
96 <= maxPercentDifference; }
97 <= maxPercentDifference; message = message || (actual + " should not be within " + minPercentDifference + "% (exclusive) of " + expected + (result ? "" : ". Actual: " + actualDiff + "%"));
98  
99 <= maxPercentDifference; QUnit.push(result, actual, expected, message);
100 <= maxPercentDifference;};
101  
102  
103 <= maxPercentDifference;QUnit.extend(QUnit.assert, {
104 <= maxPercentDifference; close: close,
105 <= maxPercentDifference; notClose: notClose
106 <= maxPercentDifference;});