Skip to content

Commit

Permalink
fix: add support for null array. (#23)
Browse files Browse the repository at this point in the history
* fix: add support for null array.

* test: improve coverage
  • Loading branch information
unional authored Jan 10, 2018
1 parent 97fc092 commit 4bfe226
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
12 changes: 12 additions & 0 deletions src/createSatisfier.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,15 @@ test('actual should be a complete struct', t => {
// t.true(s.test({ a: 1 }))
t.true(s.test({ a: 1, b: 'b' }))
})

test('expect null', t => {
t.true(createSatisfier(null).test(null))
})
test('array with number', t => {
t.true(createSatisfier([1, 2]).test([1, 2]))
})

test('array with null', t => {
t.true(createSatisfier([null]).test([null]))
t.false(createSatisfier([null]).test([1]))
})
13 changes: 11 additions & 2 deletions src/createSatisfier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Struct, Expectation, SatisfierExec } from './interfaces'
* creates a satisfier
* @param expectation All properties can be a value which will be compared to the same property in `actual`, RegExp, or a predicate function that will be used to check against the property.
*/
export function createSatisfier<T extends Struct>(expectation: Expectation<T>): {
export function createSatisfier<T extends Struct = Struct>(expectation: Expectation<T>): {
test: (actual: T) => boolean;
exec: (actual: T) => SatisfierExec[] | undefined;
} {
Expand Down Expand Up @@ -50,6 +50,14 @@ function detectDiff(actual, expected, path: string[] = []) {
})
}
}
else if (expected === null) {
if (expected !== actual)
diff.push({
path,
expected,
actual
})
}
else if (expectedType === 'boolean' || expectedType === 'number' || expectedType === 'string' || actual === undefined) {
if (expected !== actual)
diff.push({
Expand Down Expand Up @@ -82,10 +90,11 @@ function detectDiff(actual, expected, path: string[] = []) {
expected,
actual
})
else
else {
Object.keys(expected).forEach(k => {
diff.push(...detectDiff(actual[k], expected[k], path.concat([k])))
})
}
}
return diff
}
2 changes: 1 addition & 1 deletion src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export type ExpectationHash<T extends Struct = Struct> = {
[P in keyof T]: ExpectationNode<T[P]> | ExpectationNode<T[P]>[];
}

export type Struct = StructNode | StructHash | (StructNode | StructHash)[]
export type Struct = null | StructNode | StructHash | (StructNode | StructHash)[]

export type StructNode = boolean | number | string | object

Expand Down

0 comments on commit 4bfe226

Please sign in to comment.