From 27e906d942848509258b19710938126db4086c5b Mon Sep 17 00:00:00 2001 From: Homa Wong Date: Mon, 31 Dec 2018 02:15:48 -0800 Subject: [PATCH] fix: support NaN (#64) --- src/createSatisfier.spec.ts | 19 +++++++++++++++++-- src/createSatisfier.ts | 14 +++++++++----- src/some.spec.ts | 4 ++++ 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/createSatisfier.spec.ts b/src/createSatisfier.spec.ts index 90b230d..7ee6f95 100644 --- a/src/createSatisfier.spec.ts +++ b/src/createSatisfier.spec.ts @@ -3,7 +3,6 @@ import a from 'assertron'; import { createSatisfier } from './index'; import { assertExec, assertRegExp } from './testUtil'; - test('support generics', () => { const s = createSatisfier<{ a: number }>({ a: 1 }) t(s.test({ a: 1 })) @@ -50,8 +49,24 @@ test('array with null', () => { a.false(createSatisfier([null]).test([1])) }) -describe('exec', () => { +test('NaN satisfies NaN', () => { + t(createSatisfier(NaN).test(NaN)) +}) + +test('NaN not satisfies others', () => { + a.false(createSatisfier(NaN).test(`a`)) + a.false(createSatisfier(NaN).test(true)) + a.false(createSatisfier(NaN).test(1)) + a.false(createSatisfier(NaN).test([])) + a.false(createSatisfier(NaN).test({})) +}) + +test('array with NaN', () => { + t(createSatisfier([NaN]).test([NaN])) + a.false(createSatisfier([NaN]).test([1])) +}) +describe('exec', () => { test('undefined should match anything', () => { t.strictEqual(createSatisfier(undefined).exec(undefined), undefined) t.strictEqual(createSatisfier(undefined).exec({}), undefined) diff --git a/src/createSatisfier.ts b/src/createSatisfier.ts index 9875685..5323f7a 100644 --- a/src/createSatisfier.ts +++ b/src/createSatisfier.ts @@ -46,6 +46,9 @@ export function createSatisfier(expectation: any): Satisfier { else if (typeof expectation === 'function') { diff.push(...detectDiff(actual, expectation)) } + else if (actual.length === 0) { + diff.push({ path: [], expected: expectation, actual }) + } else { actual.forEach((a, i) => { diff.push(...detectDiff(a, expectation, [`[${i}]`], i)) @@ -85,13 +88,14 @@ function detectDiff(actual, expected, path: string[] = [], index?: number) { actual }) } + else if (expectedType === 'number' && typeof actual === 'number') { + if (isNaN(expected) && isNaN(actual)) return diff + if (expected !== actual) + diff.push({ path, expected, actual }) + } else if (expectedType === 'boolean' || expectedType === 'number' || expectedType === 'string' || actual === undefined) { if (expected !== actual) - diff.push({ - path, - expected, - actual - }) + diff.push({ path, expected, actual }) } else if (expected instanceof ArrayEntryExpectation) { const d = expected.exec(actual, path) diff --git a/src/some.spec.ts b/src/some.spec.ts index 347b6b5..9ec2b16 100644 --- a/src/some.spec.ts +++ b/src/some.spec.ts @@ -27,3 +27,7 @@ test('array with more than one match returns true', () => { test('tersify()', () => { t.strictEqual(some({ a: 1 }).tersify(), 'some({ a: 1 })') }) + +test('match second', () => { + a.satisfy(['first', 'second'], some('second')) +})