From cb08aefc47365550205bf67d0191e495d1a089a5 Mon Sep 17 00:00:00 2001 From: Daniel Michalski Date: Sat, 16 Feb 2019 22:01:59 +0100 Subject: [PATCH 01/13] add toBeArrayWithFloats matcher --- src/matchers/toBeArrayWithFloats/index.js | 18 ++++++++++ .../toBeArrayWithFloats/index.test.js | 31 +++++++++++++++++ src/matchers/toBeArrayWithFloats/predicate.js | 32 ++++++++++++++++++ .../toBeArrayWithFloats/predicate.test.js | 33 +++++++++++++++++++ types/index.d.ts | 7 ++++ 5 files changed, 121 insertions(+) create mode 100644 src/matchers/toBeArrayWithFloats/index.js create mode 100644 src/matchers/toBeArrayWithFloats/index.test.js create mode 100644 src/matchers/toBeArrayWithFloats/predicate.js create mode 100644 src/matchers/toBeArrayWithFloats/predicate.test.js diff --git a/src/matchers/toBeArrayWithFloats/index.js b/src/matchers/toBeArrayWithFloats/index.js new file mode 100644 index 00000000..4a02549d --- /dev/null +++ b/src/matchers/toBeArrayWithFloats/index.js @@ -0,0 +1,18 @@ +import predicate from './predicate'; + +export default { + toBeArrayWithFloats: (received, expected, precision = 4) => { + const pass = predicate(received, expected, precision); + if (pass) { + return { + message: () => `expected [${received}] not to be equal to [${expected}] with ${precision}-digit precision`, + pass: true + }; + } else { + return { + message: () => `expected [${received}] to be equal to [${expected}] with ${precision}-digit precision`, + pass: false + }; + } + } +}; diff --git a/src/matchers/toBeArrayWithFloats/index.test.js b/src/matchers/toBeArrayWithFloats/index.test.js new file mode 100644 index 00000000..189beb16 --- /dev/null +++ b/src/matchers/toBeArrayWithFloats/index.test.js @@ -0,0 +1,31 @@ +import matcher from './'; + +expect.extend(matcher); + +describe('.toBeArrayWithFloats', () => { + test('passes for empty arrays', () => { + expect([]).toBeArrayWithFloats([]); + }); + + test('passes for arrays with same floats with default precision', () => { + expect([1.2, 2.45]).toBeArrayWithFloats([1.2, 2.45001]); + }); + + test('passes for arrays with floats rounded to same value with default precision', () => { + expect([0.00011]).toBeArrayWithFloats([0.0001]); + }); + + test('passes for arrays with same floats with specified precision', () => { + expect([1.014, 1.23456]).toBeArrayWithFloats([1.0104, 1.23112], 2); + }); +}); + +describe('.not.toBeArrayWithFloats', () => { + test('passes for arrays with different floats', () => { + expect([1.0]).not.toBeArrayWithFloats([1.0001]); + }); + + test('passes for arrays with different floats due to rounding', () => { + expect([1.0]).not.toBeArrayWithFloats([1.00005]); + }); +}); diff --git a/src/matchers/toBeArrayWithFloats/predicate.js b/src/matchers/toBeArrayWithFloats/predicate.js new file mode 100644 index 00000000..4db53a02 --- /dev/null +++ b/src/matchers/toBeArrayWithFloats/predicate.js @@ -0,0 +1,32 @@ +const areArrays = function(...objects) { + if (typeof objects === 'undefined') { + return false; + } + let allArrays = true; + objects.forEach(param => (allArrays = allArrays && Array.isArray(param))); + return allArrays; +}; + +const cutDecimals = function(num, precision = 4) { + return Number.parseFloat(Number.parseFloat(num).toFixed(precision)); +}; + +const arrayWithFloatsEquals = function(current, other, precision = 4) { + if (current.length !== other.length) { + return false; + } + + for (let i = 0; i < current.length; i++) { + let first = cutDecimals(current[i], precision); + let second = cutDecimals(other[i], precision); + if (first / second !== 1) { + return false; + } + } + + return true; +}; + +export default (actual, expected, precision = 4) => { + return areArrays(actual, expected) && arrayWithFloatsEquals(actual, expected, precision); +}; diff --git a/src/matchers/toBeArrayWithFloats/predicate.test.js b/src/matchers/toBeArrayWithFloats/predicate.test.js new file mode 100644 index 00000000..2786d5b7 --- /dev/null +++ b/src/matchers/toBeArrayWithFloats/predicate.test.js @@ -0,0 +1,33 @@ +import predicate from './predicate'; + +describe('toBeArrayWithFloats predicate - positive test cases', () => { + test('returns true for empty arrays', () => { + expect(predicate([], [])).toBe(true); + }); + + test('returns true for arrays with same floats with default precision', () => { + expect(predicate([1.0001, 2.123], [1.00011, 2.123])).toBe(true); + }); + + test('returns true for arrays with same floats with specified precision', () => { + expect(predicate([1.231, 2.344], [1.23, 2.34], 2)).toBe(true); + }); + + test('returns true when floats do not differ after rounding', () => { + expect(predicate([1.0001], [1.00014])).toBe(true); + }); +}); + +describe('toBeArrayWithFloats predicate - negative test cases', () => { + test('returns false for arrays with different floats with default precision', () => { + expect(predicate([1.1111], [1.1112])).toBe(false); + }); + + test('returns false for arrays with different floats with specified precision', () => { + expect(predicate([1.1334], [1.1234], 2)).toBe(false); + }); + + test('returns false when floats differ by single digit due to rounding', () => { + expect(predicate([1.0001], [1.00015])).toBe(false); + }); +}); diff --git a/types/index.d.ts b/types/index.d.ts index cd4cc1f4..e81141ca 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -52,6 +52,13 @@ declare namespace jest { */ toBeArrayOfSize(x: number): R; + /** + * Use `.toBeArrayWithFloats` when checking if a value is an `Array` with floating-point numbers. Equality is checked using default or specified precision. + * @param {Array.} expected + * @param {Number} precision, i.e. decimal places taken into account during number comparison (1.0001 is not 1.00016, but 1.0001 is 1.00015 for precision = 4) + */ + toBeArrayWithFloats(expected: number[], precision: number = 4): R; + /** * Use `.toBeAfter` when checking if a date occurs after `date`. * @param {Date} date From 64e4a7a8e95ba865f5bd4f81a20a3eb95d8b34ff Mon Sep 17 00:00:00 2001 From: Daniel Michalski Date: Sat, 16 Feb 2019 22:09:37 +0100 Subject: [PATCH 02/13] update contributors --- .all-contributorsrc | 12 ++++++++++++ README.md | 8 -------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/.all-contributorsrc b/.all-contributorsrc index a23adf00..f0254c3c 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -450,6 +450,18 @@ "contributions": [ "code" ] + }, + { + "login": "danielpmichalski", + "name": "Daniel Michalski", + "avatar_url": "", + "profile": "", + "contributions": [ + "code", + "doc", + "example", + "test" + ] } ], "repoType": "github" diff --git a/README.md b/README.md index 44b8a5e9..52b7fd6f 100644 --- a/README.md +++ b/README.md @@ -920,14 +920,6 @@ test('passes when value includes all substrings', () => { ## Contributors -| [
Matt Phillips](http://mattphillips.io)
[πŸ“](#blog-mattphillips "Blogposts") [πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=mattphillips "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=mattphillips "Documentation") [πŸ’‘](#example-mattphillips "Examples") [πŸš‡](#infra-mattphillips "Infrastructure (Hosting, Build-Tools, etc)") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=mattphillips "Tests") | [
Stephen Bluck](https://github.com/stevebluck)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=stevebluck "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=stevebluck "Tests") | [
Christoffer Hasselberg](https://github.com/stofolus)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=stofolus "Code") | [
Brandon Newton](https://btnwtn.com)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=btnwtn "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=btnwtn "Documentation") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=btnwtn "Tests") | [
Devan Patel](http://www.devanpatel.me)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=devanp92 "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=devanp92 "Documentation") | [
Gary Leutheuser](https://GaryLeutheuser.github.io)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=GaryLeutheuser "Code") | [
Johan Lindgren](https://github.com/lindgr3n)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=lindgr3n "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=lindgr3n "Documentation") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=lindgr3n "Tests") | -| :---: | :---: | :---: | :---: | :---: | :---: | :---: | -| [
Andrew Hayward](http://andrewhayward.net)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=andrewhayward "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=andrewhayward "Tests") | [
Oliver Schneider](https://ols.io)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=olsio "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=olsio "Tests") | [
Tyle Whalen](https://github.com/tjwhalen16)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=tjwhalen16 "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=tjwhalen16 "Documentation") | [
Martius](https://github.com/martiuslim)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=martiuslim "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=martiuslim "Tests") | [
Eli Collis](https://github.com/ecollis6)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=ecollis6 "Code") | [
Marcin LichwaΕ‚a](https://github.com/marcinlichwala)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=marcinlichwala "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=marcinlichwala "Tests") | [
Massimo Prencipe](https://github.com/mprencipe)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=mprencipe "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=mprencipe "Tests") | -| [
mjmiles](https://github.com/mjmiles)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=mjmiles "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=mjmiles "Documentation") | [
Gary Meehan](https://github.com/garmeeh)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=garmeeh "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=garmeeh "Documentation") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=garmeeh "Tests") | [
Fredrik MΓ€kilΓ€](https://github.com/GitHug)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=GitHug "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=GitHug "Tests") | [
Daniel Reinoso](http://kloc.io/)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=danielr18 "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=danielr18 "Tests") | [
Chris Hut](https://github.com/tophernuts)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=tophernuts "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=tophernuts "Tests") | [
Kelvin Ly](https://github.com/cactorium)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=cactorium "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=cactorium "Tests") | [
Francis Ngo](https://github.com/francisngo)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=francisngo "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=francisngo "Tests") | -| [
Amish Shah](https://hydrabolt.me/)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=hydrabolt "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=hydrabolt "Tests") | [
Dave Cooper](http://davecooper.org)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=grug "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=grug "Tests") | [
Swann Polydor](https://github.com/soueuls)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=soueuls "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=soueuls "Tests") | [
vikneshwar](https://github.com/vikneshwar)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=vikneshwar "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=vikneshwar "Tests") | [
Budi Irawan](http://budiirawan.com)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=deerawan "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=deerawan "Tests") | [
Tejas Bubane](http://foss-geek.blogspot.com/)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=tejasbubane "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=tejasbubane "Tests") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=tejasbubane "Documentation") | [
Subinoy Ghosh](https://github.com/subinoy7)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=subinoy7 "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=subinoy7 "Tests") | -| [
Simen Bekkhus](https://github.com/SimenB)
[πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=SimenB "Documentation") | [
Orta](http://orta.io)
[πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=orta "Documentation") | [
Tom](https://jsdevtom.com)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=jsdevtom "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=jsdevtom "Documentation") [πŸ’‘](#example-jsdevtom "Examples") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=jsdevtom "Tests") | [
Lucian Buzzo](https://github.com/LucianBuzzo)
| [
Thiago Delgado Pinto](https://github.com/thiagodp)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=thiagodp "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=thiagodp "Documentation") [πŸ’‘](#example-thiagodp "Examples") [πŸ€”](#ideas-thiagodp "Ideas, Planning, & Feedback") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=thiagodp "Tests") | [
Ragnar Laud](https://github.com/xprn)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=xprn "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=xprn "Documentation") | [
Luiz AmΓ©rico](https://github.com/blikblum)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=blikblum "Code") | -| [
Frederick Fogerty](https://github.com/frederickfogerty)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=frederickfogerty "Code") [πŸ€”](#ideas-frederickfogerty "Ideas, Planning, & Feedback") | [
Benjamin Kay](https://github.com/benjaminkay93)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=benjaminkay93 "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=benjaminkay93 "Documentation") | [
Gilles De Mey](https://demey.io)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=gillesdemey "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=gillesdemey "Documentation") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=gillesdemey "Tests") | [
Deniz Dogan](https://github.com/denizdogan)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=denizdogan "Code") | [
Mikey Powers](https://github.com/mvpowers)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=mvpowers "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=mvpowers "Documentation") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=mvpowers "Tests") | [
Tony Trinh](https://github.com/tony19)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=tony19 "Code") | [
Nikita Kurpas](https://github.com/NikitaKurpas)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=NikitaKurpas "Code") | -| [
Alcedo Nathaniel De Guzman Jr](https://twitter.com/natealcedo)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=natealcedo "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=natealcedo "Documentation") [πŸ’‘](#example-natealcedo "Examples") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=natealcedo "Tests") | [
Pete Hodgson](http://thepete.net)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=moredip "Code") | ## LICENSE From 601995f7fcc175c1cf5b42511ab1345fd86cabac Mon Sep 17 00:00:00 2001 From: Daniel Michalski <5457711+danielpmichalski@users.noreply.github.com> Date: Sat, 16 Feb 2019 22:11:39 +0100 Subject: [PATCH 03/13] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 52b7fd6f..89803206 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,7 @@ If you've come here to help contribute - Thanks! Take a look at the [contributin - [Array](#array) - [.toBeArray()](#tobearray) - [.toBeArrayOfSize()](#tobearrayofsize) + - [.toBeArrayWithFloats](#tobearraywithfloats) - [.toIncludeAllMembers([members])](#toincludeallmembersmembers) - [.toIncludeAnyMembers([members])](#toincludeanymembersmembers) - [.toIncludeSameMembers([members])](#toincludesamemembersmembers) From 8c33286d552c94ff5ff3bc5e5ba67f826fb5c1d9 Mon Sep 17 00:00:00 2001 From: Daniel Michalski <5457711+danielpmichalski@users.noreply.github.com> Date: Sat, 16 Feb 2019 22:12:57 +0100 Subject: [PATCH 04/13] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 89803206..bbc2e59e 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ If you've come here to help contribute - Thanks! Take a look at the [contributin - [Array](#array) - [.toBeArray()](#tobearray) - [.toBeArrayOfSize()](#tobearrayofsize) - - [.toBeArrayWithFloats](#tobearraywithfloats) + - [.toBeArrayWithFloats([members], ?precision)](#tobearraywithfloatsmembersprecision) - [.toIncludeAllMembers([members])](#toincludeallmembersmembers) - [.toIncludeAnyMembers([members])](#toincludeanymembersmembers) - [.toIncludeSameMembers([members])](#toincludesamemembersmembers) From 0294826e136ef40ff4084f679763b64d08f3a14b Mon Sep 17 00:00:00 2001 From: Daniel Michalski <5457711+danielpmichalski@users.noreply.github.com> Date: Sat, 16 Feb 2019 22:17:27 +0100 Subject: [PATCH 05/13] Update README.md --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index bbc2e59e..5c84f7af 100644 --- a/README.md +++ b/README.md @@ -273,6 +273,17 @@ test('passes when value is an array', () => { }); ``` +#### .toBeArrayWithFloats([members], ?precision) + +Use `.toBeArrayWithFloats` when comparing arrays of floating-point numbers with chosen precision. + +```js +test('passes when arrays have same floats', () => { + expect([1.11112, 2.22221]).toBeArrayWithFloats([1.11114, 2.22223]); // default 4-digit decimal precision + expect([1.112]).not.toBeArrayWithFloats([1.111], 3); // specified 3-digit decimal precision +}); +``` + #### .toIncludeAllMembers([members]) Use `.toIncludeAllMembers` when checking if an `Array` contains all of the same members of a given set. From a2fc1d0cbb0655034d5f0a4dc215247551c7461b Mon Sep 17 00:00:00 2001 From: Daniel Michalski <5457711+danielpmichalski@users.noreply.github.com> Date: Sat, 16 Feb 2019 22:19:30 +0100 Subject: [PATCH 06/13] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5c84f7af..fa53e1bb 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ If you've come here to help contribute - Thanks! Take a look at the [contributin - [Array](#array) - [.toBeArray()](#tobearray) - [.toBeArrayOfSize()](#tobearrayofsize) - - [.toBeArrayWithFloats([members], ?precision)](#tobearraywithfloatsmembersprecision) + - [.toBeArrayWithFloats([members], ?precision)](#tobearraywithfloatsmembers-precision) - [.toIncludeAllMembers([members])](#toincludeallmembersmembers) - [.toIncludeAnyMembers([members])](#toincludeanymembersmembers) - [.toIncludeSameMembers([members])](#toincludesamemembersmembers) From cd893baaa1ba9c8cb2a37e80a37e0d620a16ac90 Mon Sep 17 00:00:00 2001 From: Daniel Michalski <5457711+danielpmichalski@users.noreply.github.com> Date: Sat, 16 Feb 2019 22:20:18 +0100 Subject: [PATCH 07/13] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index fa53e1bb..722f3d39 100644 --- a/README.md +++ b/README.md @@ -275,7 +275,7 @@ test('passes when value is an array', () => { #### .toBeArrayWithFloats([members], ?precision) -Use `.toBeArrayWithFloats` when comparing arrays of floating-point numbers with chosen precision. +Use `.toBeArrayWithFloats` when comparing arrays of floating-point numbers with chosen precision in the same order. ```js test('passes when arrays have same floats', () => { From 6af2bf688292c352fea625ef14a737d3f3790c06 Mon Sep 17 00:00:00 2001 From: Daniel Michalski Date: Sat, 16 Feb 2019 22:27:50 +0100 Subject: [PATCH 08/13] contributor update --- .all-contributorsrc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.all-contributorsrc b/.all-contributorsrc index f0254c3c..a5356095 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -455,7 +455,7 @@ "login": "danielpmichalski", "name": "Daniel Michalski", "avatar_url": "", - "profile": "", + "profile": "https://github.com/danielpmichalski", "contributions": [ "code", "doc", From 4979172a585a40c46dcc14345061eeaaa09ce552 Mon Sep 17 00:00:00 2001 From: Daniel Michalski <5457711+danielpmichalski@users.noreply.github.com> Date: Sat, 16 Feb 2019 22:36:05 +0100 Subject: [PATCH 09/13] Update README.md --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 722f3d39..5a180a9e 100644 --- a/README.md +++ b/README.md @@ -932,6 +932,14 @@ test('passes when value includes all substrings', () => { ## Contributors +| [
Matt Phillips](http://mattphillips.io)
[πŸ“](#blog-mattphillips "Blogposts") [πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=mattphillips "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=mattphillips "Documentation") [πŸ’‘](#example-mattphillips "Examples") [πŸš‡](#infra-mattphillips "Infrastructure (Hosting, Build-Tools, etc)") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=mattphillips "Tests") | [
Stephen Bluck](https://github.com/stevebluck)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=stevebluck "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=stevebluck "Tests") | [
Christoffer Hasselberg](https://github.com/stofolus)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=stofolus "Code") | [
Brandon Newton](https://btnwtn.com)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=btnwtn "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=btnwtn "Documentation") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=btnwtn "Tests") | [
Devan Patel](http://www.devanpatel.me)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=devanp92 "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=devanp92 "Documentation") | [
Gary Leutheuser](https://GaryLeutheuser.github.io)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=GaryLeutheuser "Code") | [
Johan Lindgren](https://github.com/lindgr3n)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=lindgr3n "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=lindgr3n "Documentation") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=lindgr3n "Tests") | +| :---: | :---: | :---: | :---: | :---: | :---: | :---: | +| [
Andrew Hayward](http://andrewhayward.net)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=andrewhayward "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=andrewhayward "Tests") | [
Oliver Schneider](https://ols.io)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=olsio "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=olsio "Tests") | [
Tyle Whalen](https://github.com/tjwhalen16)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=tjwhalen16 "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=tjwhalen16 "Documentation") | [
Martius](https://github.com/martiuslim)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=martiuslim "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=martiuslim "Tests") | [
Eli Collis](https://github.com/ecollis6)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=ecollis6 "Code") | [
Marcin LichwaΕ‚a](https://github.com/marcinlichwala)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=marcinlichwala "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=marcinlichwala "Tests") | [
Massimo Prencipe](https://github.com/mprencipe)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=mprencipe "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=mprencipe "Tests") | +| [
mjmiles](https://github.com/mjmiles)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=mjmiles "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=mjmiles "Documentation") | [
Gary Meehan](https://github.com/garmeeh)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=garmeeh "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=garmeeh "Documentation") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=garmeeh "Tests") | [
Fredrik MΓ€kilΓ€](https://github.com/GitHug)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=GitHug "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=GitHug "Tests") | [
Daniel Reinoso](http://kloc.io/)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=danielr18 "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=danielr18 "Tests") | [
Chris Hut](https://github.com/tophernuts)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=tophernuts "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=tophernuts "Tests") | [
Kelvin Ly](https://github.com/cactorium)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=cactorium "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=cactorium "Tests") | [
Francis Ngo](https://github.com/francisngo)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=francisngo "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=francisngo "Tests") | +| [
Amish Shah](https://hydrabolt.me/)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=hydrabolt "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=hydrabolt "Tests") | [
Dave Cooper](http://davecooper.org)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=grug "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=grug "Tests") | [
Swann Polydor](https://github.com/soueuls)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=soueuls "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=soueuls "Tests") | [
vikneshwar](https://github.com/vikneshwar)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=vikneshwar "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=vikneshwar "Tests") | [
Budi Irawan](http://budiirawan.com)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=deerawan "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=deerawan "Tests") | [
Tejas Bubane](http://foss-geek.blogspot.com/)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=tejasbubane "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=tejasbubane "Tests") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=tejasbubane "Documentation") | [
Subinoy Ghosh](https://github.com/subinoy7)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=subinoy7 "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=subinoy7 "Tests") | +| [
Simen Bekkhus](https://github.com/SimenB)
[πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=SimenB "Documentation") | [
Orta](http://orta.io)
[πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=orta "Documentation") | [
Tom](https://jsdevtom.com)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=jsdevtom "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=jsdevtom "Documentation") [πŸ’‘](#example-jsdevtom "Examples") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=jsdevtom "Tests") | [
Lucian Buzzo](https://github.com/LucianBuzzo)
| [
Thiago Delgado Pinto](https://github.com/thiagodp)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=thiagodp "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=thiagodp "Documentation") [πŸ’‘](#example-thiagodp "Examples") [πŸ€”](#ideas-thiagodp "Ideas, Planning, & Feedback") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=thiagodp "Tests") | [
Ragnar Laud](https://github.com/xprn)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=xprn "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=xprn "Documentation") | [
Luiz AmΓ©rico](https://github.com/blikblum)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=blikblum "Code") | +| [
Frederick Fogerty](https://github.com/frederickfogerty)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=frederickfogerty "Code") [πŸ€”](#ideas-frederickfogerty "Ideas, Planning, & Feedback") | [
Benjamin Kay](https://github.com/benjaminkay93)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=benjaminkay93 "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=benjaminkay93 "Documentation") | [
Gilles De Mey](https://demey.io)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=gillesdemey "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=gillesdemey "Documentation") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=gillesdemey "Tests") | [
Deniz Dogan](https://github.com/denizdogan)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=denizdogan "Code") | [
Mikey Powers](https://github.com/mvpowers)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=mvpowers "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=mvpowers "Documentation") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=mvpowers "Tests") | [
Tony Trinh](https://github.com/tony19)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=tony19 "Code") | [
Nikita Kurpas](https://github.com/NikitaKurpas)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=NikitaKurpas "Code") | +| [
Alcedo Nathaniel De Guzman Jr](https://twitter.com/natealcedo)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=natealcedo "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=natealcedo "Documentation") [πŸ’‘](#example-natealcedo "Examples") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=natealcedo "Tests") | [
Pete Hodgson](http://thepete.net)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=moredip "Code") | ## LICENSE From c54aef3c37a29eb12cebaea04fc7ad59333a5194 Mon Sep 17 00:00:00 2001 From: Daniel Michalski <5457711+danielpmichalski@users.noreply.github.com> Date: Sat, 16 Feb 2019 22:42:56 +0100 Subject: [PATCH 10/13] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5a180a9e..1355078a 100644 --- a/README.md +++ b/README.md @@ -939,7 +939,7 @@ test('passes when value includes all substrings', () => { | [
Amish Shah](https://hydrabolt.me/)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=hydrabolt "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=hydrabolt "Tests") | [
Dave Cooper](http://davecooper.org)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=grug "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=grug "Tests") | [
Swann Polydor](https://github.com/soueuls)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=soueuls "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=soueuls "Tests") | [
vikneshwar](https://github.com/vikneshwar)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=vikneshwar "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=vikneshwar "Tests") | [
Budi Irawan](http://budiirawan.com)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=deerawan "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=deerawan "Tests") | [
Tejas Bubane](http://foss-geek.blogspot.com/)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=tejasbubane "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=tejasbubane "Tests") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=tejasbubane "Documentation") | [
Subinoy Ghosh](https://github.com/subinoy7)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=subinoy7 "Code") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=subinoy7 "Tests") | | [
Simen Bekkhus](https://github.com/SimenB)
[πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=SimenB "Documentation") | [
Orta](http://orta.io)
[πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=orta "Documentation") | [
Tom](https://jsdevtom.com)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=jsdevtom "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=jsdevtom "Documentation") [πŸ’‘](#example-jsdevtom "Examples") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=jsdevtom "Tests") | [
Lucian Buzzo](https://github.com/LucianBuzzo)
| [
Thiago Delgado Pinto](https://github.com/thiagodp)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=thiagodp "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=thiagodp "Documentation") [πŸ’‘](#example-thiagodp "Examples") [πŸ€”](#ideas-thiagodp "Ideas, Planning, & Feedback") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=thiagodp "Tests") | [
Ragnar Laud](https://github.com/xprn)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=xprn "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=xprn "Documentation") | [
Luiz AmΓ©rico](https://github.com/blikblum)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=blikblum "Code") | | [
Frederick Fogerty](https://github.com/frederickfogerty)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=frederickfogerty "Code") [πŸ€”](#ideas-frederickfogerty "Ideas, Planning, & Feedback") | [
Benjamin Kay](https://github.com/benjaminkay93)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=benjaminkay93 "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=benjaminkay93 "Documentation") | [
Gilles De Mey](https://demey.io)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=gillesdemey "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=gillesdemey "Documentation") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=gillesdemey "Tests") | [
Deniz Dogan](https://github.com/denizdogan)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=denizdogan "Code") | [
Mikey Powers](https://github.com/mvpowers)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=mvpowers "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=mvpowers "Documentation") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=mvpowers "Tests") | [
Tony Trinh](https://github.com/tony19)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=tony19 "Code") | [
Nikita Kurpas](https://github.com/NikitaKurpas)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=NikitaKurpas "Code") | -| [
Alcedo Nathaniel De Guzman Jr](https://twitter.com/natealcedo)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=natealcedo "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=natealcedo "Documentation") [πŸ’‘](#example-natealcedo "Examples") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=natealcedo "Tests") | [
Pete Hodgson](http://thepete.net)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=moredip "Code") | +| [
Alcedo Nathaniel De Guzman Jr](https://twitter.com/natealcedo)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=natealcedo "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=natealcedo "Documentation") [πŸ’‘](#example-natealcedo "Examples") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=natealcedo "Tests") | [
Pete Hodgson](http://thepete.net)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=moredip "Code") | [
Daniel Michalski](https://twitter.com/crazyPolishDan)
[πŸ’»](https://github.com/mattphillips/jest-extended/commits?author=danielpmichalski "Code") [πŸ“–](https://github.com/mattphillips/jest-extended/commits?author=danielpmichalski "Documentation") [⚠️](https://github.com/mattphillips/jest-extended/commits?author=danielpmichalski "Tests") | ## LICENSE From 0c2a35782bb9da4081b61ddb770e2f9ceda9fe77 Mon Sep 17 00:00:00 2001 From: Daniel Michalski <5457711+danielpmichalski@users.noreply.github.com> Date: Sat, 16 Feb 2019 22:43:55 +0100 Subject: [PATCH 11/13] Update .all-contributorsrc --- .all-contributorsrc | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/.all-contributorsrc b/.all-contributorsrc index a5356095..a5a750a9 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -454,12 +454,11 @@ { "login": "danielpmichalski", "name": "Daniel Michalski", - "avatar_url": "", - "profile": "https://github.com/danielpmichalski", + "avatar_url": "https://avatars2.githubusercontent.com/u/5457711?v=4", + "profile": "https://twitter.com/crazyPolishDan", "contributions": [ "code", "doc", - "example", "test" ] } From bb9411d298c8eab34ac23dfc57ebcfa02e6e3ff0 Mon Sep 17 00:00:00 2001 From: Daniel Michalski Date: Mon, 18 Feb 2019 16:22:45 +0100 Subject: [PATCH 12/13] improve test coverage --- src/matchers/toBeArrayWithFloats/index.js | 4 +- src/matchers/toBeArrayWithFloats/predicate.js | 38 +++++++++++++---- .../toBeArrayWithFloats/predicate.test.js | 41 +++++++++++++++++++ 3 files changed, 73 insertions(+), 10 deletions(-) diff --git a/src/matchers/toBeArrayWithFloats/index.js b/src/matchers/toBeArrayWithFloats/index.js index 4a02549d..00028612 100644 --- a/src/matchers/toBeArrayWithFloats/index.js +++ b/src/matchers/toBeArrayWithFloats/index.js @@ -5,12 +5,12 @@ export default { const pass = predicate(received, expected, precision); if (pass) { return { - message: () => `expected [${received}] not to be equal to [${expected}] with ${precision}-digit precision`, + message: `expected [${received}] not to be equal to [${expected}] with ${precision}-digit precision`, pass: true }; } else { return { - message: () => `expected [${received}] to be equal to [${expected}] with ${precision}-digit precision`, + message: `expected [${received}] to be equal to [${expected}] with ${precision}-digit precision`, pass: false }; } diff --git a/src/matchers/toBeArrayWithFloats/predicate.js b/src/matchers/toBeArrayWithFloats/predicate.js index 4db53a02..ee0a85b2 100644 --- a/src/matchers/toBeArrayWithFloats/predicate.js +++ b/src/matchers/toBeArrayWithFloats/predicate.js @@ -1,9 +1,20 @@ +const isUndefined = function(obj) { + return typeof obj === 'undefined'; +}; + +const someUndefined = function(...objects) { + let check = false; + objects.forEach(item => (check = check || isUndefined(item))); + return check; +}; + const areArrays = function(...objects) { - if (typeof objects === 'undefined') { + if (someUndefined(objects)) { return false; } + let allArrays = true; - objects.forEach(param => (allArrays = allArrays && Array.isArray(param))); + objects.forEach(item => (allArrays = allArrays && Array.isArray(item))); return allArrays; }; @@ -11,15 +22,26 @@ const cutDecimals = function(num, precision = 4) { return Number.parseFloat(Number.parseFloat(num).toFixed(precision)); }; -const arrayWithFloatsEquals = function(current, other, precision = 4) { - if (current.length !== other.length) { +const floatsEqual = function(float1, float2, precision = 4) { + let a = cutDecimals(float1, precision); + let b = cutDecimals(float2, precision); + + if (a === 0 && b === 0) { + return true; + } else if (a !== 0 && b === 0) { + return false; + } else { + return a / b === 1; + } +}; + +const arrayWithFloatsEquals = function(array1, array2, precision = 4) { + if (array1.length !== array2.length) { return false; } - for (let i = 0; i < current.length; i++) { - let first = cutDecimals(current[i], precision); - let second = cutDecimals(other[i], precision); - if (first / second !== 1) { + for (let i = 0; i < array1.length; i++) { + if (!floatsEqual(array1[i], array2[i], precision)) { return false; } } diff --git a/src/matchers/toBeArrayWithFloats/predicate.test.js b/src/matchers/toBeArrayWithFloats/predicate.test.js index 2786d5b7..88a742df 100644 --- a/src/matchers/toBeArrayWithFloats/predicate.test.js +++ b/src/matchers/toBeArrayWithFloats/predicate.test.js @@ -5,6 +5,14 @@ describe('toBeArrayWithFloats predicate - positive test cases', () => { expect(predicate([], [])).toBe(true); }); + test('returns true for arrays with zeroes', () => { + expect(predicate([0.0], [0.0])).toBe(true); + }); + + test('processes equal arrays with zeroes', () => { + expect(predicate([0.0, 1.0], [0.0, 1.0])).toBe(true); + }); + test('returns true for arrays with same floats with default precision', () => { expect(predicate([1.0001, 2.123], [1.00011, 2.123])).toBe(true); }); @@ -19,6 +27,39 @@ describe('toBeArrayWithFloats predicate - positive test cases', () => { }); describe('toBeArrayWithFloats predicate - negative test cases', () => { + test('returns false for undefined objects', () => { + expect(predicate(undefined, undefined)).toBe(false); + }); + + test('returns false for comparing an array to undefined objects', () => { + expect(predicate([], undefined)).toBe(false); + }); + + test('returns false for comparing an array to undefined objects no.2', () => { + expect(predicate(undefined, [])).toBe(false); + }); + + test('returns false when one value is non-array', () => { + expect(predicate([1.0], 'non-array')).toBe(false); + expect(predicate('non-array', [1.0])).toBe(false); + }); + + test('returns false for non-array values', () => { + expect(predicate('string', 5)).toBe(false); + }); + + test('processes non-equal arrays with zeroes', () => { + expect(predicate([0, 1.0], [0, 0])).toBe(false); + }); + + test('processes non-equal arrays with zeroes in 2nd array', () => { + expect(predicate([1.0], [0])).toBe(false); + }); + + test('returns false when comparing arrays of different size', () => { + expect(predicate([1.0], [1.0, 2.0])).toBe(false); + }); + test('returns false for arrays with different floats with default precision', () => { expect(predicate([1.1111], [1.1112])).toBe(false); }); From 5a84183fce6c66208d9a3d54baa648af542bc60b Mon Sep 17 00:00:00 2001 From: Daniel Michalski Date: Mon, 18 Feb 2019 16:30:51 +0100 Subject: [PATCH 13/13] fix test coverage --- src/matchers/toBeArrayWithFloats/predicate.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/matchers/toBeArrayWithFloats/predicate.js b/src/matchers/toBeArrayWithFloats/predicate.js index ee0a85b2..90de8583 100644 --- a/src/matchers/toBeArrayWithFloats/predicate.js +++ b/src/matchers/toBeArrayWithFloats/predicate.js @@ -2,7 +2,7 @@ const isUndefined = function(obj) { return typeof obj === 'undefined'; }; -const someUndefined = function(...objects) { +const someUndefined = function(objects) { let check = false; objects.forEach(item => (check = check || isUndefined(item))); return check; @@ -18,11 +18,11 @@ const areArrays = function(...objects) { return allArrays; }; -const cutDecimals = function(num, precision = 4) { +const cutDecimals = function(num, precision) { return Number.parseFloat(Number.parseFloat(num).toFixed(precision)); }; -const floatsEqual = function(float1, float2, precision = 4) { +const floatsEqual = function(float1, float2, precision) { let a = cutDecimals(float1, precision); let b = cutDecimals(float2, precision); @@ -35,7 +35,7 @@ const floatsEqual = function(float1, float2, precision = 4) { } }; -const arrayWithFloatsEquals = function(array1, array2, precision = 4) { +const arrayWithFloatsEquals = function(array1, array2, precision) { if (array1.length !== array2.length) { return false; }