From 9163af201f154f9198e398be24a7c0edd7fe8773 Mon Sep 17 00:00:00 2001 From: Homa Wong Date: Sat, 13 Jan 2018 23:43:35 -0800 Subject: [PATCH] feat: support indexing when checking against array. (#36) * feat: support indexing when checking against array. Check using undefined is removed. undefined in expectation will be treated as "don't care" * chore: suppress ts error Although the type say it cannot be undefined, it actually can. I don't put undefined as a valid value for expectation because it will screw up all code completoion. * chore: add assertron as devDeps --- README.md | 12 ++++++++++++ package-lock.json | 19 +++++++++++++++++++ package.json | 1 + src/createSatisfier.spec.ts | 8 -------- src/createSatisfier.test.spec.ts | 27 +++++++++++++++++++++++++++ src/createSatisfier.ts | 12 +++++++----- 6 files changed, 66 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 214beec..29c1181 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,18 @@ createSatisfier({ a: /boo/ }).exec({ a: 'foo' }) createSatisfier({ a: a => a === 1 }).exec({ a: 2 }) ``` +## test against array + +When testing against array, you can use `undefined` to skip over entries that you don't care. + +If you want to test against undefined explicitly, use predicate function. + +```ts +import { createSatisfier } from 'satisfier' + +createSatisfier([undefined, 1]).test(['...anything...', 1]) +``` + ## Build in predicates There are a few predicates shipped in the package for convenience. diff --git a/package-lock.json b/package-lock.json index 9dd97e0..89fbfea 100644 --- a/package-lock.json +++ b/package-lock.json @@ -480,6 +480,16 @@ "integrity": "sha1-8S4PPF13sLHN2RRpQuTpbB5N1SU=", "dev": true }, + "assertron": { + "version": "3.3.10", + "resolved": "https://registry.npmjs.org/assertron/-/assertron-3.3.10.tgz", + "integrity": "sha512-x0DuWD4weCMlkEMSA1FEPIIcW3xggvzxNGePvJ7xFU77CCiPnpj7FZ/dFUsIb5QUr7w4Vl8Rg4aNg8nXGFL8uA==", + "dev": true, + "requires": { + "satisfier": "3.0.3", + "tersify": "1.2.0" + } + }, "async": { "version": "2.6.0", "resolved": "https://registry.npmjs.org/async/-/async-2.6.0.tgz", @@ -7621,6 +7631,15 @@ "integrity": "sha512-kKvNJn6Mm93gAczWVJg7wH+wGYWNrDHdWvpUmHyEsgCtIwwo3bqPtV4tR5tuPaUhTOo/kvhVwd8XwwOllGYkbg==", "dev": true }, + "satisfier": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/satisfier/-/satisfier-3.0.3.tgz", + "integrity": "sha512-DxqLBwm37uXMMcAEpMSGp7RNbLu6zjgbVqvC/EZQNA9Efmb72+fatMcreEpG6FOObwdRx/kb9qcIiE8msaX5Pw==", + "dev": true, + "requires": { + "tersify": "1.2.0" + } + }, "semantic-release": { "version": "12.2.2", "resolved": "https://registry.npmjs.org/semantic-release/-/semantic-release-12.2.2.tgz", diff --git a/package.json b/package.json index 9330b01..1c15952 100644 --- a/package.json +++ b/package.json @@ -60,6 +60,7 @@ }, "devDependencies": { "@types/node": "^8.5.8", + "assertron": "^3.3.10", "ava": "^0.24.0", "dependency-check": "^2.10.0", "eslint": "^4.15.0", diff --git a/src/createSatisfier.spec.ts b/src/createSatisfier.spec.ts index b025010..beeac5b 100644 --- a/src/createSatisfier.spec.ts +++ b/src/createSatisfier.spec.ts @@ -32,14 +32,6 @@ test('actual should be a complete struct', t => { t.true(s.test({ a: 1, b: 'b' })) }) -test('expect [undefined]', t => { - t.true(createSatisfier([undefined]).test([undefined])) -}) - -test('expect [undefined] should work with [null]', t => { - t.false(createSatisfier([undefined]).test([null])) -}) - test('expect array and test against non-array', t => { const s = createSatisfier([1]) t.false(s.test(null)) diff --git a/src/createSatisfier.test.spec.ts b/src/createSatisfier.test.spec.ts index f7b5da5..1c1934b 100644 --- a/src/createSatisfier.test.spec.ts +++ b/src/createSatisfier.test.spec.ts @@ -1,4 +1,5 @@ import { test } from 'ava' +import { AssertOrder } from 'assertron' import { createSatisfier } from './index' @@ -33,3 +34,29 @@ test('mismatch value fails', t => { t.false(createSatisfier({ a: [1, true, 'a'] }).test({ a: [1, true, 'b'] })) t.false(createSatisfier({ a: { b: 1 } }).test({ a: { b: 2 } })) }) + +test('undefined expectation are ignored', t => { + const s = createSatisfier([undefined, 1]) + t.true(s.test([undefined, 1])) + t.true(s.test([null, 1])) + t.true(s.test([1, 1])) + t.true(s.test(['a', 1])) + t.true(s.test([true, 1])) + t.true(s.test([{ a: 1 }, 1])) + t.true(s.test([[1, 2], 1])) +}) + +test('index', t => { + const order = new AssertOrder() + createSatisfier((e, i) => { + const step = order.any([1, 2]) + if (step === 1) { + t.is(e, 'a') + t.is(i, 0) + } + if (step === 2) { + t.is(e, 'b') + t.is(i, 1) + } + }).test(['a', 'b']) +}) diff --git a/src/createSatisfier.ts b/src/createSatisfier.ts index 31bf6ee..67095b9 100644 --- a/src/createSatisfier.ts +++ b/src/createSatisfier.ts @@ -18,13 +18,15 @@ export function createSatisfier(expectation: Expectat if (Array.isArray(actual)) { const diff: SatisfierExec[] = [] if (Array.isArray(expectation)) { - expectation.forEach((e, i) => { - diff.push(...detectDiff(actual[i], e, [`[${i}]`])) + expectation.forEach((e: any, i) => { + if (e === undefined) + return + diff.push(...detectDiff(actual[i], e, [`[${i}]`], i)) }) } else { actual.forEach((a, i) => { - diff.push(...detectDiff(a, expectation, [`[${i}]`])) + diff.push(...detectDiff(a, expectation, [`[${i}]`], i)) }) } return diff.length === 0 ? undefined : diff @@ -38,7 +40,7 @@ export function createSatisfier(expectation: Expectat } } -function detectDiff(actual, expected, path: string[] = []) { +function detectDiff(actual, expected, path: string[] = [], index?: number) { const diff: SatisfierExec[] = [] const expectedType = typeof expected if (expectedType === 'function') { @@ -47,7 +49,7 @@ function detectDiff(actual, expected, path: string[] = []) { diff.push(...detectDiff(a, expected, path.concat([`[${i}]`]))) }) } - else if (!(expected as Function)(actual)) { + else if (!(expected as Function)(actual, index)) { diff.push({ path, expected,