Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial import from 'jquery/qunit.git:addons/close-enough'.

  • Loading branch information...
commit 1a3f742f09ec52f9d15a54805d2adb1958764d8a 1 parent 394ac3d
@JamesMGreene authored
View
22 .gitignore
@@ -1,14 +1,8 @@
-lib-cov
-*.seed
-*.log
-*.csv
-*.dat
-*.out
-*.pid
-*.gz
-
-pids
-logs
-results
-
-npm-debug.log
+.project
+*~
+*.diff
+*.patch
+.DS_Store
+.settings
+node_modules
+dist/
View
29 .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
+}
View
5 .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>
View
6 .travis.yml
@@ -0,0 +1,6 @@
+language: node_js
+node_js:
+ - "0.8"
+ - "0.10"
+before_script:
+ - npm install -g grunt-cli
View
4 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>
View
23 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"]);
+
+};
View
21 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.
View
28 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.
View
43 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"
+ }
+}
View
32 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);
+ }
+});
View
37 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);
+});
View
14 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>
Please sign in to comment.
Something went wrong with that request. Please try again.