-
-
Notifications
You must be signed in to change notification settings - Fork 123
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
Showing
1 changed file
with
84 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,91 @@ | ||
import { isMatching, P } from '../src'; | ||
import { isMatching, match, P } from '../src'; | ||
import { Equal, Expect } from '../src/types/helpers'; | ||
|
||
describe('Objects', () => { | ||
it('should work with symbols', () => { | ||
describe('symbols', () => { | ||
const symbolA = Symbol('symbol-a'); | ||
const symbolB = Symbol('symbol-b'); | ||
const obj: { [symbolA]: { [symbolB]: 'foo' | 'bar' } } = { | ||
[symbolA]: { [symbolB]: 'foo' }, | ||
}; | ||
if (isMatching({ [symbolA]: { [symbolB]: 'foo' } }, obj)) { | ||
type t = Expect<Equal<typeof obj, { [symbolA]: { [symbolB]: 'foo' } }>>; | ||
} else { | ||
throw new Error('Expected obj to match the foo pattern!'); | ||
} | ||
if (isMatching({ [symbolA]: { [symbolB]: 'bar' } }, obj)) { | ||
type t = Expect< | ||
Equal< | ||
typeof obj, | ||
{ [symbolA]: { [symbolB]: 'foo' } } & { | ||
[symbolA]: { [symbolB]: 'bar' }; | ||
} | ||
> | ||
>; | ||
throw new Error('Expected obj to not match the bar pattern!'); | ||
} | ||
const symbolC = Symbol('symbol-c'); | ||
type Input = { [symbolA]: { [symbolB]: 'foo' | 'bar' } }; | ||
|
||
it('should work with symbols', () => { | ||
const fn1 = (obj: Input) => { | ||
if (isMatching({ [symbolA]: { [symbolB]: 'foo' } }, obj)) { | ||
type t = Expect< | ||
Equal<typeof obj, { [symbolA]: { [symbolB]: 'foo' } }> | ||
>; | ||
} else { | ||
throw new Error('Expected obj to match the foo pattern!'); | ||
} | ||
}; | ||
|
||
const fn2 = (obj: Input) => { | ||
if (isMatching({ [symbolA]: { [symbolB]: 'bar' } }, obj)) { | ||
type t = Expect< | ||
Equal<typeof obj, { [symbolA]: { [symbolB]: 'bar' } }> | ||
>; | ||
throw new Error('Expected obj to not match the bar pattern!'); | ||
} | ||
}; | ||
|
||
fn1({ | ||
[symbolA]: { [symbolB]: 'foo' }, | ||
}); | ||
|
||
fn2({ | ||
[symbolA]: { [symbolB]: 'foo' }, | ||
}); | ||
}); | ||
|
||
it('narrowing inference should work', () => { | ||
const fn1 = (input: Input) => { | ||
return match(input) | ||
.with({ [symbolA]: P.select() }, (sel) => { | ||
type t = Expect<Equal<typeof sel, { [symbolB]: 'foo' | 'bar' }>>; | ||
return sel; | ||
}) | ||
.exhaustive(); | ||
}; | ||
|
||
expect(fn1({ [symbolA]: { [symbolB]: 'bar' } })).toEqual({ | ||
[symbolB]: 'bar', | ||
}); | ||
|
||
const fn2 = (input: Input | { [symbolC]: string }) => { | ||
return match(input) | ||
.with({ [symbolA]: P.any }, (sel) => { | ||
type t = Expect<Equal<typeof sel, Input>>; | ||
return sel; | ||
}) | ||
.with({ [symbolC]: P.select() }, (x) => x) | ||
.exhaustive(); | ||
}; | ||
|
||
expect(fn2({ [symbolC]: 'Hey' })).toEqual('Hey'); | ||
}); | ||
|
||
it('exhaustiveness checking should work', () => { | ||
const fn1 = (input: Input | { [symbolC]: string }) => { | ||
return match(input) | ||
.with({ [symbolA]: P.any }, (sel) => { | ||
type t = Expect<Equal<typeof sel, Input>>; | ||
return sel; | ||
}) | ||
.with({ [symbolC]: P.any }, () => '2') | ||
.exhaustive(); | ||
}; | ||
|
||
const fn2 = (input: Input | { [symbolC]: string }) => { | ||
return ( | ||
match(input) | ||
.with({ [symbolA]: P.any }, (sel) => { | ||
type t = Expect<Equal<typeof sel, Input>>; | ||
return sel; | ||
}) | ||
// @ts-expect-error | ||
.exhaustive() | ||
); | ||
}; | ||
}); | ||
}); | ||
}); |