├── .gitignore ├── .npmignore ├── .travis.yml ├── LICENSE ├── README.md ├── appveyor.yml ├── index.js ├── package.json └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 18 | .grunt 19 | 20 | # node-waf configuration 21 | .lock-wscript 22 | 23 | # Compiled binary addons (http://nodejs.org/api/addons.html) 24 | build/Release 25 | 26 | # Dependency directory 27 | node_modules 28 | 29 | # Optional npm cache directory 30 | .npm 31 | 32 | # Optional REPL history 33 | .node_repl_history 34 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | test.js 2 | .gitignore 3 | .travis.yml 4 | appveyor.yml 5 | CHANGELOG.md 6 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '0.10' 4 | - '0.12' 5 | - '4' 6 | - '5' 7 | deploy: 8 | provider: npm 9 | email: tobias.bieniek@gmx.de 10 | api_key: 11 | secure: bogWVemzcl20WIX+oBF1gdV65RRNTv2S18zlwnZv1IlWn/HoS4ZFpuv3cb1qfAxTGkKqZGFeyguPMBNDZ93qG/QxLea1IZrE/d0HpX9aCYZHl3xKQnZsItyNm/IdbyQCc1uHtXTsSBpV7JyXrSxs31B61p69bnsfl+yiD47OmqhH0Ktsx/2oIFbzzY1pD3DZs5DPAlnZSH8t763ae7H1nM6RjE1dVc/Qb9kjNZBHMiruiknvc8qzfbjb24DzyeEIkC7IIGic08YD1/lCgo2Pdjr/uM4JbjduJpwW1vpG5xQ/M6Zcda0YS02JG946EhSyGD3PP2SeLVJjeguVFpgJIMhil+sGKWUarKMctwn781D7FiU/1KaRFBvEOnwBlUEV5x7GKaC8TX3FMfa6nWHOTlzZKArJmwj/FQ7RXq9iP5SXbgdHPPKnpqeOCQX2qBrL3BKLBGvXnODHYAuHZUYc7vklAmJUjNs3oc1J9+ySf3RL1GuuNxl3IcTFxaUIbsX1v/w5nM8WW4dBMnT99Nm9ZQPDZhqtMOyejKMEDXTVV7SVQ6bF3XWyJ5B8YJ+vBuBnhWR2Qx1XMeD1n1YN6cetbsDkr8eisbGx6o/FDi2Z6tTU+SwGV/Nz83ffbyDm9LgRYbJCk7pRE6bvi3fDH2dVhPfScx0k6srd9zQSB46F1QY= 12 | on: 13 | tags: true 14 | repo: Turbo87/chai-roughly 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Tobias Bieniek 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | chai-roughly 3 | ============================================================================== 4 | 5 | [![Build Status](https://travis-ci.org/Turbo87/chai-roughly.svg?branch=master)](https://travis-ci.org/Turbo87/chai-roughly) 6 | [![Build status](https://ci.appveyor.com/api/projects/status/github/Turbo87/chai-roughly?svg=true)](https://ci.appveyor.com/project/Turbo87/chai-roughly/branch/master) 7 | [![npm](https://img.shields.io/npm/v/chai-roughly.svg)](https://www.npmjs.com/package/chai-roughly) 8 | 9 | deep equals assertions with tolerance for chai 10 | 11 | 12 | Installation 13 | ------------------------------------------------------------------------------ 14 | 15 | ``` 16 | npm install --save-dev chai-roughly 17 | ``` 18 | 19 | Usage 20 | ------------------------------------------------------------------------------ 21 | 22 | After importing `chai` add the following code to use `chai-roughly` assertions: 23 | 24 | ```js 25 | var chai = require('chai'); 26 | var expect = chai.expect; 27 | 28 | chai.use(require('chai-roughly')); 29 | ``` 30 | 31 | Now you can use the `expect(...).to.roughly.deep.equal(...)` chain for deep 32 | equals assertions with tolerance for numbers. The default tolerance is `1e-6` 33 | and can be overwritten by using e.g. 34 | `expect(...).to.roughly(0.001).deep.equal(...)`. 35 | 36 | ```js 37 | it('works', function() { 38 | expect({ value: 42 }).to.roughly.deep.equal({ value: 41.9999999 }); 39 | }); 40 | ``` 41 | 42 | 43 | License 44 | ------------------------------------------------------------------------------ 45 | chai-roughly is licensed under the [MIT License](LICENSE). 46 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | # http://www.appveyor.com/docs/appveyor-yml 2 | 3 | environment: 4 | matrix: 5 | - nodejs_version: "0.10" 6 | - nodejs_version: "0.12" 7 | - nodejs_version: "4" 8 | - nodejs_version: "5" 9 | 10 | install: 11 | - ps: Install-Product node $env:nodejs_version 12 | - npm install 13 | 14 | test_script: 15 | - npm test 16 | 17 | build: off 18 | 19 | version: "{build}" 20 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var deepEql = require('deep-eql'); 2 | 3 | var DEFAULT_TOLERANCE = 1e-6; 4 | 5 | module.exports = function (chai, utils) { 6 | var Assertion = chai.Assertion; 7 | 8 | function assertEql(_super) { 9 | return function(obj, msg) { 10 | var tolerance = utils.flag(this, 'tolerance'); 11 | if (tolerance) { 12 | if (msg) flag(this, 'message', msg); 13 | this.assert( 14 | deepEql(obj, utils.flag(this, 'object'), { tolerance: tolerance }) 15 | , 'expected #{this} to roughly deeply equal #{exp}' 16 | , 'expected #{this} to not roughly deeply equal #{exp}' 17 | , obj 18 | , this._obj 19 | , true 20 | ); 21 | 22 | } else { 23 | _super.apply(this, arguments); 24 | } 25 | }; 26 | } 27 | 28 | Assertion.overwriteMethod('eql', assertEql); 29 | Assertion.overwriteMethod('eqls', assertEql); 30 | 31 | function explicitRoughly(tolerance) { 32 | utils.flag(this, 'tolerance', tolerance); 33 | } 34 | 35 | function defaultRoughly() { 36 | utils.flag(this, 'tolerance', DEFAULT_TOLERANCE); 37 | } 38 | 39 | Assertion.addChainableMethod('roughly', explicitRoughly, defaultRoughly); 40 | }; 41 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "chai-roughly", 3 | "version": "1.0.0", 4 | "description": "deep equals assertions with tolerance for chai", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "mocha" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/Turbo87/chai-roughly.git" 12 | }, 13 | "keywords": [ 14 | "assertions", 15 | "testing", 16 | "chai", 17 | "chai-plugin", 18 | "numbers", 19 | "deep-eql", 20 | "tolerance" 21 | ], 22 | "author": "Tobias Bieniek ", 23 | "license": "MIT", 24 | "bugs": { 25 | "url": "https://github.com/Turbo87/chai-roughly/issues" 26 | }, 27 | "homepage": "https://github.com/Turbo87/chai-roughly#readme", 28 | "devDependencies": { 29 | "chai": "^3.5.0", 30 | "mocha": "^3.0.0" 31 | }, 32 | "dependencies": { 33 | "deep-eql": "git+https://github.com/Turbo87/deep-eql.git#9bad4db" 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var mocha = require('mocha'); 2 | var chai = require('chai'); 3 | 4 | var describe = mocha.describe; 5 | var it = mocha.it; 6 | var expect = chai.expect; 7 | 8 | chai.use(require('./index')); 9 | 10 | describe('expect(...).to.roughly.deep.equal(...)', function() { 11 | it('passes for empty objects', function() { 12 | expect({}).to.roughly.deep.equal({}); 13 | }); 14 | 15 | it('passes for roughly similar values', function() { 16 | expect({ value: 12345 }).to.roughly.deep.equal({ value: 12345.01 }); 17 | }); 18 | 19 | it('fails for different values', function() { 20 | expect(function() { 21 | expect({ value: 12345 }).to.roughly.deep.equal({ value: 12345.1 }); 22 | }).to.throw(); 23 | }); 24 | 25 | it('passes for multiple roughly similar values', function() { 26 | expect({ 27 | value: 12345, 28 | other: 0.1234, 29 | }).to.roughly.deep.equal({ 30 | other: 0.123400001, 31 | value: 12345.00001, 32 | }); 33 | }); 34 | 35 | it('passes for roughly similar nested values', function() { 36 | expect({ 37 | sub: { 38 | value: 42, 39 | }, 40 | }).to.roughly.deep.equal({ 41 | sub: { 42 | value: 41.99999, 43 | }, 44 | }); 45 | }); 46 | }); 47 | --------------------------------------------------------------------------------