Skip to content

Commit

Permalink
feat(modifier): add number pre modifier
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed Dec 20, 2021
1 parent 1d61e93 commit 8ce5f1e
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 1 deletion.
15 changes: 15 additions & 0 deletions _e2e/expect_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
fn,
jestExtendedMatcherMap,
not,
number,
resolves,
string,
test,
Expand Down Expand Up @@ -1229,3 +1230,17 @@ test("passes when stringified value to be", async () => {
"test",
);
});

test("passes when numberized value to be", () => {
const expect = defineExpect({
matcherMap: {
toBe,
},
modifierMap: {
number,
},
});

expect("").number.toBe(0);
expect("test").number.toBe(NaN);
});
1 change: 1 addition & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export { resolves } from "./modifier/resolves.ts";
export { trim } from "./modifier/trim.ts";
export { debug } from "./modifier/debug.ts";
export { string } from "./modifier/string.ts";
export * from "./modifier/number.ts";
export * from "./modifier/preset.ts";

export * from "./modifier/types.ts";
Expand Down
33 changes: 32 additions & 1 deletion modifier/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ import type { PreModifierContext } from "https://deno.land/x/unitest@$VERSION/mo

type PreModifier = {
type: "pre";
fn: (actual: unknown, context: PreModifierContext) => { actual: unknown };
fn: (
actual: unknown,
context: PreModifierContext,
) => { actual: unknown } | Promise<{ actual: unknown }>;
};
```

Expand Down Expand Up @@ -130,6 +133,34 @@ test("passes when stringified value to be", () => {
});
```

### number

Use `.number` to convert any `actual` to `number`. Internally, the `Number`
constructor is used.

```ts
import {
defineExpect,
number,
test,
toBe,
} from "https://deno.land/x/unitest@$VERSION/mod.ts";

const expect = defineExpect({
matcherMap: {
toBe,
},
modifierMap: {
number,
},
});

test("passes when numberized value to be", () => {
expect("").number.toBe(0);
expect("test").number.toBe(NaN);
});
```

## post modifier

Post modifier is an object that is executed after a match. It interrupts the
Expand Down
1 change: 1 addition & 0 deletions modifier/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export { resolves } from "./resolves.ts";
export { trim } from "./trim.ts";
export { debug } from "./debug.ts";
export * from "./string.ts";
export * from "./number.ts";
export * from "./preset.ts";
36 changes: 36 additions & 0 deletions modifier/number.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2021-Present the Unitest authors. All rights reserved. MIT license.
// This module is browser compatible.

import type { PreModifier } from "./types.ts";

/** Use `.number` to convert any `actual` to `number`. Internally, the `Number`
* constructor is used.
* ```ts
* import {
* defineExpect,
* number,
* test,
* toBe,
* } from "https://deno.land/x/unitest@$VERSION/mod.ts";
*
* const expect = defineExpect({
* matcherMap: {
* toBe,
* },
* modifierMap: {
* number,
* },
* });
*
* test("passes when numberized value to be", () => {
* expect("").number.toBe(0);
* expect("test").number.toBe(NaN);
* });
* ```
*/
const number: PreModifier<unknown, { actual: number }> = {
type: "pre",
fn: (actual) => ({ actual: Number(actual) }),
};

export { number };
14 changes: 14 additions & 0 deletions modifier/number_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2021-Present the Unitest authors. All rights reserved. MIT license.

import { number } from "./number.ts";
import { assertEquals } from "../dev_deps.ts";
import type { PreModifierContext } from "./types.ts";

Deno.test("number", () => {
const context = {} as PreModifierContext;
assertEquals(number.type, "pre");
assertEquals(number.fn("", context), { actual: 0 });
assertEquals(number.fn("test", context), { actual: NaN });
assertEquals(number.fn(1, context), { actual: 1 });
assertEquals(number.fn(1n, context), { actual: 1 });
});

0 comments on commit 8ce5f1e

Please sign in to comment.