Skip to content

Commit

Permalink
feat: Add isNumber matcher
Browse files Browse the repository at this point in the history
```typescript
const fn = mock<(x: number) => number>();
when(fn(It.isNumber()).returns(42);

instance(fn)(20.5) === 42
instance(fn)(NaN) // throws
```
  • Loading branch information
NiGhTTraX committed May 28, 2020
1 parent 13319a8 commit cc5b0a8
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export function isMatcher(f: any): f is Matcher<any> {
* @example
* const fn = mock<(x: number, y: string) => number>();
* when(fn(It.isAny(), It.isAny()).thenReturn(1);
*
* instance(fn)(23, 'foobar') === 1
*/
const isAny = (): Matcher<any> => ({
Expand All @@ -57,6 +58,8 @@ const isAny = (): Matcher<any> => ({
* @example
* const fn = mock<(x: number) => number>();
* when(fn(It.matches(x => x >= 0)).returns(42);
*
* instance(fn)(2) === 42
* instance(fn)(-1) // throws
*/
const matches = <T>(cb: (arg: T) => boolean): Matcher<T> =>
Expand All @@ -77,6 +80,7 @@ const matches = <T>(cb: (arg: T) => boolean): Matcher<T> =>
* @example
* const fn = mock<(foo: { x: number, y: number }) => number>();
* when(fn(It.isObjectContaining({ x: 23 }).returns(42);
*
* instance(fn)({ x: 100, y: 200 }) // throws
* instance(fn)({ x: 23, y: 200 }) // returns 42
*/
Expand All @@ -91,6 +95,23 @@ const isObjectContaining = <T extends object, K extends DeepPartial<T>>(
},
} as any);

/**
* Match any number.
*
* @example
* const fn = mock<(x: number) => number>();
* when(fn(It.isNumber()).returns(42);
*
* instance(fn)(20.5) === 42
* instance(fn)(NaN) // throws
*/
const isNumber = (): Matcher<number> =>
({
__isMatcher: true,
matches: (arg: any) => typeof arg === 'number' && !Number.isNaN(arg),
toJSON: () => 'isNumber',
} as any);

/**
* Contains argument matchers that can be used to ignore arguments in an
* expectation or to match complex arguments.
Expand All @@ -100,4 +121,5 @@ export const It = {
isAny,
matches,
isObjectContaining,
isNumber,
};
46 changes: 46 additions & 0 deletions tests/matcher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,52 @@ describe('It', () => {
});
});

describe('isNumber', () => {
it('should match 0', () => {
expect(It.isNumber().matches(0)).toBeTruthy();
});

it('should match positive integers', () => {
expect(It.isNumber().matches(23)).toBeTruthy();
});

it('should match negative integers', () => {
expect(It.isNumber().matches(-23)).toBeTruthy();
});

it('should match positive floats', () => {
expect(It.isNumber().matches(10.5)).toBeTruthy();
});

it('should match negative floats', () => {
expect(It.isNumber().matches(-10.5)).toBeTruthy();
});

it('should math positive scientific notation', () => {
expect(It.isNumber().matches(10e2)).toBeTruthy();
});

it('should math negative scientific notation', () => {
expect(It.isNumber().matches(-10e2)).toBeTruthy();
});

it('should not match strings', () => {
expect(It.isNumber().matches('foo')).toBeFalsy();
});

it('should not match strings numbers', () => {
expect(It.isNumber().matches('10')).toBeFalsy();
});

it('should not match strings containing numbers', () => {
expect(It.isNumber().matches('10foo')).toBeFalsy();
});

it('should not match NaN', () => {
expect(It.isNumber().matches(NaN)).toBeFalsy();
});
});

describe('matches', () => {
it('should support custom predicates', () => {
expect(It.matches(() => true).matches(':irrelevant:')).toBeTruthy();
Expand Down

0 comments on commit cc5b0a8

Please sign in to comment.