Skip to content

Commit

Permalink
Initial import from 'jquery/qunit.git:addons/close-enough'.
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesMGreene committed May 5, 2013
1 parent 394ac3d commit 1a3f742
Show file tree
Hide file tree
Showing 12 changed files with 247 additions and 17 deletions.
22 changes: 8 additions & 14 deletions .gitignore
@@ -1,14 +1,8 @@
lib-cov .project
*.seed *~
*.log *.diff
*.csv *.patch
*.dat .DS_Store
*.out .settings
*.pid node_modules
*.gz dist/

pids
logs
results

npm-debug.log
29 changes: 29 additions & 0 deletions .jshintrc
@@ -0,0 +1,29 @@
{
"globals": {
"QUnit": false
},

"bitwise": true,
"camelcase": true,
"curly": true,
"eqeqeq": true,
"forin": true,
"immed": true,
"latedef": false,
"newcap": true,
"noarg": true,
"noempty": true,
"nonew": true,
"plusplus": false,
"quotmark": "double",
"undef": true,
"unused": true,
"trailing": true,

"proto": true,
"sub": true,

"browser": true,

"onevar": true
}
5 changes: 5 additions & 0 deletions .mailmap
@@ -0,0 +1,5 @@
Jörn Zaefferer <joern.zaefferer@gmail.com> <JZA@JZA-R60>
Jörn Zaefferer <joern.zaefferer@gmail.com>
Michael Righi <michael@righi.me>
Timo Tijhof <krinklemail@gmail.com>
James M. Greene <james.m.greene@gmail.com>
6 changes: 6 additions & 0 deletions .travis.yml
@@ -0,0 +1,6 @@
language: node_js
node_js:
- "0.8"
- "0.10"
before_script:
- npm install -g grunt-cli
4 changes: 4 additions & 0 deletions AUTHORS.txt
@@ -0,0 +1,4 @@
Michael Righi <michael@righi.me>
Jörn Zaefferer <joern.zaefferer@gmail.com>
Timo Tijhof <krinklemail@gmail.com>
James M. Greene <james.m.greene@gmail.com>
23 changes: 23 additions & 0 deletions Gruntfile.js
@@ -0,0 +1,23 @@
/*jshint node:true */
module.exports = function(grunt) {

grunt.loadNpmTasks("grunt-git-authors");
grunt.loadNpmTasks("grunt-contrib-jshint");
grunt.loadNpmTasks("grunt-contrib-qunit");

grunt.initConfig({
jshint: {
options: {
jshintrc: ".jshintrc"
},
all: ["*.js", "test/**/*.js"]
},
qunit: {
all: ["test/index.html"]
}
});

grunt.registerTask("test", ["jshint", "qunit"]);
grunt.registerTask("default", ["test"]);

};
21 changes: 21 additions & 0 deletions MIT-LICENSE.txt
@@ -0,0 +1,21 @@
Copyright 2013 jQuery Foundation and other contributors
http://jquery.com/

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 changes: 25 additions & 3 deletions README.md
@@ -1,4 +1,26 @@
qunit-assert-close # QUnit Close assertion plugin
==================


A QUnit plugin for asserting that a number is approximately equal (or not) to an expected number, within a given tolerance. This plugin for [QUnit](https://github.com/jquery/qunit) adds `close` and `notClose` assertion methods
to test that a number is approximately equal (or not) to an expected number, within a given tolerance.

### Usage ###

```js
assert.close(actual, expected, maxDifference, message);
assert.notClose(actual, expected, minDifference, message);
```

Where:
- `maxDifference`: the maximum inclusive difference allowed (tolerance) between the `actual` and `expected` numbers
- `minDifference`: the minimum exclusive difference allowed (tolerance) between the `actual` and `expected` numbers
- `actual`, `expected`, `message`: The usual

### Example ###
```js
test('Example unit test', function(assert) {
assert.close(3.141, Math.PI, 0.001);
assert.notClose(3.1, Math.PI, 0.001);
}
```
For more examples, refer to the unit tests.
43 changes: 43 additions & 0 deletions package.json
@@ -0,0 +1,43 @@
{
"name": "qunit-assert-close",
"description": "A QUnit plugin for asserting that a number is approximately equal (or not) to an expected number, within a given tolerance.",
"version": "1.0.0",
"author": {
"name": "jQuery Foundation and other contributors",
"url": "https://github.com/JamesMGreene/qunit-assert-close/blob/master/AUTHORS.txt"
},
"homepage": "https://github.com/JamesMGreene/qunit-assert-close",
"repository": {
"type": "git",
"url": "git://github.com/JamesMGreene/qunit-assert-close.git"
},
"bugs": {
"url": "https://github.com/JamesMGreene/qunit-assert-close/issues"
},
"license": {
"name": "MIT",
"url": "http://www.opensource.org/licenses/mit-license.php"
},
"keywords": [
"qunit-plugin",
"qunit-assert",
"qunit",
"assert",
"close",
"close-enough",
"roughlyEquals",
"closeTo",
"approximate"
],
"main": "qunit-assert-close.js",
"scripts": {
"test": "grunt test"
},
"devDependencies": {
"grunt": "~0.4.1",
"grunt-contrib-jshint": "~0.4.3",
"grunt-contrib-qunit": "~0.2.1",
"grunt-git-authors": "~1.2.0",
"qunitjs": "1.x"
}
}
32 changes: 32 additions & 0 deletions qunit-assert-close.js
@@ -0,0 +1,32 @@
QUnit.extend( QUnit.assert, {
/**
* Checks that the first two arguments are equal, or are numbers close enough to be considered equal
* based on a specified maximum allowable difference.
*
* @example assert.close(3.141, Math.PI, 0.001);
*
* @param Number actual
* @param Number expected
* @param Number maxDifference (the maximum inclusive difference allowed between the actual and expected numbers)
* @param String message (optional)
*/
close: function(actual, expected, maxDifference, message) {
var passes = (actual === expected) || Math.abs(actual - expected) <= maxDifference;
QUnit.push(passes, actual, expected, message);
},

/**
* Checks that the first two arguments are numbers with differences greater than the specified
* minimum difference.
*
* @example assert.notClose(3.1, Math.PI, 0.001);
*
* @param Number actual
* @param Number expected
* @param Number minDifference (the minimum exclusive difference allowed between the actual and expected numbers)
* @param String message (optional)
*/
notClose: function(actual, expected, minDifference, message) {
QUnit.push(Math.abs(actual - expected) > minDifference, actual, expected, message);
}
});
37 changes: 37 additions & 0 deletions test/close-test.js
@@ -0,0 +1,37 @@
QUnit.module("qunit-assert-close plugin unit tests");

QUnit.test("Close Numbers", function(assert) {
var halfPi = Math.PI / 2,
sqrt2 = Math.sqrt(2);

assert.close(7, 7, 0);
assert.close(7, 7.1, 0.1);
assert.close(7, 7.1, 0.2);

assert.close(3.141, Math.PI, 0.001);
assert.close(3.1, Math.PI, 0.1);

assert.close(halfPi, 1.57, 0.001);

assert.close(sqrt2, 1.4142, 0.0001);

assert.close(Infinity, Infinity, 1);
});

QUnit.test("Distant Numbers", function(assert) {
var halfPi = Math.PI / 2,
sqrt2 = Math.sqrt(2);

assert.notClose(6, 7, 0);
assert.notClose(7, 7.2, 0.1);
assert.notClose(7, 7.2, 0.19999999999);

assert.notClose(3.141, Math.PI, 0.0001);
assert.notClose(3.1, Math.PI, 0.001);

assert.notClose(halfPi, 1.57, 0.0001);

assert.notClose(sqrt2, 1.4142, 0.00001);

assert.notClose(Infinity, -Infinity, 5);
});
14 changes: 14 additions & 0 deletions test/index.html
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>qunit-assert-close plugin unit tests</title>
<link rel="stylesheet" href="../node_modules/qunitjs/qunit/qunit.css">
<script src="../node_modules/qunitjs/qunit/qunit.js"></script>
<script src="../qunit-assert-close.js"></script>
<script src="close-test.js"></script>
</head>
<body>
<div id="qunit"></div>
</body>
</html>

0 comments on commit 1a3f742

Please sign in to comment.