Skip to content

Commit

Permalink
fix: support NaN (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
unional authored Dec 31, 2018
1 parent d31d560 commit 27e906d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
19 changes: 17 additions & 2 deletions src/createSatisfier.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }))
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 9 additions & 5 deletions src/createSatisfier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ export function createSatisfier<T = any>(expectation: any): Satisfier<T> {
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))
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions src/some.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
})

0 comments on commit 27e906d

Please sign in to comment.