Skip to content

Commit

Permalink
feat: support indexing when checking against array. (#36)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
unional authored Jan 14, 2018
1 parent 20ab9ef commit 9163af2
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 13 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
19 changes: 19 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 0 additions & 8 deletions src/createSatisfier.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
27 changes: 27 additions & 0 deletions src/createSatisfier.test.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { test } from 'ava'
import { AssertOrder } from 'assertron'

import { createSatisfier } from './index'

Expand Down Expand Up @@ -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'])
})
12 changes: 7 additions & 5 deletions src/createSatisfier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ export function createSatisfier<T extends Struct = Struct>(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
Expand All @@ -38,7 +40,7 @@ export function createSatisfier<T extends Struct = Struct>(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') {
Expand All @@ -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,
Expand Down

0 comments on commit 9163af2

Please sign in to comment.