Skip to content

Commit 23a05f8

Browse files
committed
Tests: Use the qunit-assert-close module
Moved QUnit assertion dependencies from `lib/bootstrap` to `lib/qunit`. Corrected bad draggable assertions.
1 parent 3065df6 commit 23a05f8

File tree

7 files changed

+141
-15
lines changed

7 files changed

+141
-15
lines changed

Gruntfile.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,9 @@ grunt.initConfig({
281281
"qunit-assert-classes/qunit-assert-classes.js": "qunit-assert-classes/qunit-assert-classes.js",
282282
"qunit-assert-classes/LICENSE.txt": "qunit-assert-classes/LICENSE",
283283

284+
"qunit-assert-close/qunit-assert-close.js": "qunit-assert-close/qunit-assert-close.js",
285+
"qunit-assert-close/MIT-LICENSE.txt": "qunit-assert-close/MIT-LICENSE.txt",
286+
284287
"qunit-composite/qunit-composite.js": "qunit-composite/qunit-composite.js",
285288
"qunit-composite/qunit-composite.css": "qunit-composite/qunit-composite.css",
286289
"qunit-composite/LICENSE.txt": "qunit-composite/LICENSE.txt",

bower.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"jshint": "2.4.4",
1717
"qunit": "1.18.0",
1818
"qunit-assert-classes": "0.1.5",
19+
"qunit-assert-close": "JamesMGreene/qunit-assert-close#v1.1.1",
1920
"qunit-composite": "JamesMGreene/qunit-composite#v1.0.4",
2021
"requirejs": "2.1.14",
2122

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Copyright jQuery Foundation and other contributors
2+
http://jquery.com/
3+
4+
Permission is hereby granted, free of charge, to any person obtaining
5+
a copy of this software and associated documentation files (the
6+
"Software"), to deal in the Software without restriction, including
7+
without limitation the rights to use, copy, modify, merge, publish,
8+
distribute, sublicense, and/or sell copies of the Software, and to
9+
permit persons to whom the Software is furnished to do so, subject to
10+
the following conditions:
11+
12+
The above copyright notice and this permission notice shall be
13+
included in all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
});

tests/lib/bootstrap.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ window.requirejs = {
1010
"lib": "../../lib",
1111
"phantom-bridge": "../../../node_modules/grunt-contrib-qunit/phantomjs/bridge",
1212
"qunit-assert-classes": "../../../external/qunit-assert-classes/qunit-assert-classes",
13+
"qunit-assert-close": "../../../external/qunit-assert-close/qunit-assert-close",
1314
"qunit": "../../../external/qunit/qunit",
1415
"ui": "../../../ui"
1516
},
1617
shim: {
1718
"globalize/ja-JP": [ "globalize" ],
1819
"jquery-simulate": [ "jquery" ],
19-
"qunit-assert-classes": [ "qunit" ]
20+
"qunit-assert-classes": [ "qunit" ],
21+
"qunit-assert-close": [ "qunit" ]
2022
}
2123
};
2224

@@ -43,11 +45,9 @@ function requireModules( dependencies, callback, modules ) {
4345
// Load a set of test file along with the required test infrastructure
4446
function requireTests( dependencies, noBackCompat ) {
4547
dependencies = [
46-
"../../lib/qunit",
48+
"lib/qunit",
4749
noBackCompat ? "jquery-no-back-compat" : "jquery",
48-
"jquery-simulate",
49-
"qunit-assert-classes",
50-
"../../lib/qunit-assert-domequal"
50+
"jquery-simulate"
5151
].concat( dependencies );
5252

5353
requireModules( dependencies, function( QUnit ) {

tests/lib/qunit.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
define( [
22
"qunit",
33
"jquery",
4+
"qunit-assert-classes",
5+
"qunit-assert-close",
6+
"lib/qunit-assert-domequal",
47
"phantom-bridge"
58
], function( QUnit, $ ) {
69

@@ -40,12 +43,6 @@ QUnit.reset = ( function( reset ) {
4043
};
4144
} )( QUnit.reset );
4245

43-
// TODO: switch to qunit-assert-close plugin
44-
QUnit.assert.close = function( actual, expected, maxDifference, message ) {
45-
var passes = ( actual === expected ) || Math.abs( actual - expected ) <= maxDifference;
46-
QUnit.push( passes, actual, expected, message );
47-
};
48-
4946
return QUnit;
5047

5148
} );

tests/unit/draggable/core.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -366,13 +366,11 @@ test( "setting right/bottom css shouldn't cause resize", function( assert ) {
366366
testHelper.move( element, -50, -50 );
367367

368368
finalOffset = element.offset();
369-
finalOffset.left += 50;
370-
finalOffset.top += 50;
371369

372370
assert.close( element.width(), origWidth, 1, "element retains width" );
373371
assert.close( element.height(), origHeight, 1, "element retains height" );
374-
assert.close( finalOffset.top, origOffset.top, "element moves the correct vertical distance" );
375-
assert.close( finalOffset.top, origOffset.top, "element moves the correct horizontal distance" );
372+
assert.close( finalOffset.top, origOffset.top - 50, 1, "element moves the correct vertical distance" );
373+
assert.close( finalOffset.top, origOffset.top - 50, 1, "element moves the correct horizontal distance" );
376374
});
377375

378376
} );

0 commit comments

Comments
 (0)