Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add support for null array. #23

Merged
merged 2 commits into from
Jan 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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