-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1bf8163
commit 1d8afc7
Showing
53 changed files
with
2,962 additions
and
2,448 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { expect as onyxExpect } from '../src/Expectation' | ||
|
||
describe('toBe', () => { | ||
const pass: any[] = [ | ||
[1, 1, true], | ||
['1', '1', true], | ||
[null, null, true], | ||
[undefined, undefined, true], | ||
] | ||
|
||
const failMsg = 'onyxToBe failed' | ||
|
||
const fail: any[] = [ | ||
['1', 1, failMsg], | ||
[{ a: 1 }, { b: 2 }, failMsg], | ||
[{ a: 1 }, { a: 1 }, failMsg], | ||
[[1, 2], [2, 3], failMsg], | ||
[[], [], failMsg], | ||
[{}, {}, failMsg], | ||
] | ||
|
||
test.each(pass)('toBe(%p, %p) should return true', (a, b, expected) => { | ||
expect(onyxExpect(a).onyxToBe(b)).toBe(expected) | ||
}) | ||
|
||
test.each(fail)('toBe(%p, %p) should throw an expect error', (a, b, expected: string) => { | ||
expect(() => onyxExpect(a).onyxToBe(b)).toThrowError(expected) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { expect as onyxExpect } from '../src/Expectation' | ||
|
||
describe('toBe', () => { | ||
const pass: any[] = [ | ||
[[1, 2, 3], 3, true], | ||
['1', 1, true], | ||
// [{ a: 1, b: 2, c: 3 }, 3, true], | ||
// [(() => [1, 2, 3, 4, 5])(), 5, true], | ||
// [() => [1, 2, 3, 4, 5], 5, true], | ||
] | ||
|
||
const failMsg = 'onyxToHaveLength failed' | ||
|
||
const fail: any[] = [ | ||
['1', 0, failMsg], | ||
[true, { b: 2 }, failMsg], | ||
[false, { a: 1 }, failMsg], | ||
[[1, 2], [2, 3], failMsg], | ||
[[], [], failMsg], | ||
[{}, {}, failMsg], | ||
] | ||
|
||
test.each(pass)('toHaveLength(%p, %p) should return true', (a, b: number, expected) => { | ||
expect(onyxExpect(a).onyxToHaveLength(b)).toBe(expected) | ||
}) | ||
|
||
test.each(fail)('toHaveLength(%p, %p) should throw an expect error', (a, b: number, expected: string) => { | ||
expect(() => onyxExpect(a).onyxToHaveLength(b)).toThrowError(expected) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { MatcherResult, MatcherFn } from './' | ||
|
||
export type ToBe = MatcherFn<'toBe'> | ||
|
||
export const toBe: ToBe = function (received, expected) { | ||
|
||
return { | ||
actual: received, | ||
expected, | ||
message: '', | ||
name: 'toBe', | ||
passed: true, | ||
result: false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const toHaveLength = (actual: unknown, expected: unknown) => { | ||
|
||
} | ||
|
||
export default toHaveLength |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { AnyMatchers, matchers, onyx } from './matchers' | ||
|
||
type OmitFirstArg<F> = F extends (x: any, ...args: infer P) => infer R ? (...args: P) => R : never | ||
type Expectations<M extends AnyMatchers> = { | ||
[K in keyof M]: OmitFirstArg<M[K]> | ||
} | ||
|
||
export function expectations<M extends AnyMatchers = AnyMatchers>( | ||
currentMatchers: M, | ||
expectation: any, | ||
not = false, | ||
): Expectations<M> { | ||
const entries = Object.entries(currentMatchers) | ||
.map(([key, value]) => [ | ||
key, | ||
(...args: any[]): boolean => { | ||
const result = value(expectation, ...args) | ||
if (result === not) { throw new ExpectError(`${not ? 'not.' : ''}${key} failed`) } // TODO diff | ||
return result | ||
}, | ||
]) | ||
return Object.assign({}, ...Array.from(entries, ([k, v]: any[]) => ({[k]: v}) )) | ||
} | ||
|
||
type NegatedExpectations<M extends AnyMatchers> = Expectations<M> & { | ||
not: Expectations<M> | ||
} | ||
|
||
export default function expect<M extends onyx.Matchers = onyx.Matchers>( | ||
expectation: any, | ||
): NegatedExpectations<M> { | ||
return { | ||
...expectations<M>(matchers as M, expectation, false), | ||
not: expectations<M>(matchers as M, expectation, true ), | ||
} | ||
} | ||
|
||
export class ExpectError extends Error { | ||
public constructor(message: string) { | ||
super(message) | ||
this.name = 'ExpectError' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { matchers, AnyMatchers, extendMatchers, onyx } from './matchers' | ||
export { default, ExpectError } from './expect' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { AnyMatchers, ExpectError } from './' | ||
|
||
type OmitFirstArg<A> = A extends (x: any, ...args: infer P) => infer R ? (...args: P) => R : never | ||
|
||
type Expectations<M extends AnyMatchers> = { [K in keyof M]: OmitFirstArg<M[K]> } | ||
|
||
export type NegatedExpectations<M extends AnyMatchers> = Expectations<M> & { | ||
not: Expectations<M> | ||
} | ||
|
||
|
||
export type ExpectResult<A, E> = { | ||
matcher: string | ||
error?: ExpectError | ||
status: ExpectStatus | ||
actual: A | ||
expected: E | ||
} | ||
|
||
enum ExpectStatus { | ||
PASS = 'Pass', | ||
FAIL = 'Fail', | ||
} | ||
|
||
const _expectFail = <F, P = never> (fail: F): ExpectationResult<F, P> => ({ _status: ExpectStatus.FAIL, fail }) | ||
const _expectPass = <P, F = never> (pass: P): ExpectationResult<F, P> => ({ _status: ExpectStatus.PASS, pass }) | ||
|
||
export const expectPass: <F = never, P = never> (pass: P) => ExpectationResult<F, P> = _expectPass | ||
export const expectFail: <F = never, P = never> (fail: F) => ExpectationResult<F, P> = _expectFail | ||
|
||
interface IExpectFail<F> { | ||
readonly _status: ExpectStatus.FAIL | ||
readonly fail: F | ||
} | ||
|
||
interface IExpectPass<P> { | ||
readonly _status: ExpectStatus.PASS | ||
readonly pass: P | ||
} | ||
|
||
type ExpectationResult<F, P> = IExpectFail<F> | IExpectPass<P> |
Oops, something went wrong.