Skip to content

Commit

Permalink
feat(matcher): add toContainAnyValues matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed Nov 27, 2021
1 parent 055f965 commit a91338f
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 3 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ Released under the [MIT](./LICENSE) license
- toContainAllKeys
- toContainValues
- toContainAllValues
- toContainAnyValues
- toContainEntry
- toContainEntries
- toContainAllEntries
Expand Down
1 change: 1 addition & 0 deletions matcher/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,4 @@ export { toBeHexColor } from "./to_be_hex_color.ts";
export { toIncludeAllMembers } from "./to_include_all_members.ts";
export { toContainAnyKeys } from "./to_contain_any_keys.ts";
export { toContainValue } from "./to_contain_value.ts";
export { toContainAnyValues } from "./to_contain_any_values.ts";
2 changes: 2 additions & 0 deletions matcher/preset.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright 2021-Present the Unitest authors. All rights reserved. MIT license.
// This module is browser compatible.
import { toContainAnyValues } from "./to_contain_any_values.ts";
import { toContainValue } from "./to_contain_value.ts";
import { toContainAnyKeys } from "./to_contain_any_keys.ts";
import { toIncludeAllMembers } from "./to_include_all_members.ts";
Expand Down Expand Up @@ -122,6 +123,7 @@ const jestMatcherMap = {
* @see https://github.com/jest-community/jest-extended
*/
const jestExtendedMatcherMap = {
toContainAnyValues,
toContainValue,
toContainAnyKeys,
toIncludeAllMembers,
Expand Down
19 changes: 19 additions & 0 deletions matcher/to_contain_any_values.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2021-Present the Unitest authors. All rights reserved. MIT license.
// This module is browser compatible.

import { containSome } from "./utils.ts";
import type { MatchResult } from "./types.ts";

function toContainAnyValues(actual: object, expected: unknown[]): MatchResult {
const actualValue = Object.values(actual);

return {
pass: containSome(actualValue, expected),
actual: actualValue,
actualHint: "Actual values:",
expected,
expectedHint: "Expected to contain any of values:",
};
}

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

import { assertEquals, assertFail, assertSuccess } from "../dev_deps.ts";
import { toContainAnyValues } from "./to_contain_any_values.ts";

Deno.test({
name: "toContainAnyValues",
fn: () => {
assertSuccess(toContainAnyValues({ a: 1, b: 2 }, [0, 2]));
assertFail(toContainAnyValues({}, []));

assertEquals(toContainAnyValues({ a: 1, b: 2, c: 3 }, [4, 5, 6]), {
pass: false,
actual: [1, 2, 3],
actualHint: "Actual values:",
expected: [4, 5, 6],
expectedHint: "Expected to contain any of values:",
});
},
});
6 changes: 5 additions & 1 deletion matcher/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ function contains(array: unknown[], value: unknown): boolean {
return array.some((v) => equal(v, value));
}

function containSome(target: unknown[], part: unknown[]): boolean {
return part.some((value) => contains(target, value));
}

function has(
key: PropertyKey,
object: object,
Expand Down Expand Up @@ -62,4 +66,4 @@ function hasPath(
return false;
}

export { contains, has, hasPath, propPath, takeLast };
export { contains, containSome, has, hasPath, propPath, takeLast };
24 changes: 23 additions & 1 deletion matcher/utils_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2021-Present the Unitest authors. All rights reserved. MIT license.
import { assertEquals } from "../dev_deps.ts";
import { contains, hasPath, propPath } from "./utils.ts";
import { contains, containSome, hasPath, propPath } from "./utils.ts";
import { stringify } from "../helper/format.ts";

Deno.test({
Expand Down Expand Up @@ -45,6 +45,28 @@ Deno.test({
},
});

Deno.test({
name: "containSome",
fn: () => {
const table: [
...Parameters<typeof containSome>,
ReturnType<typeof containSome>,
][] = [
[[], [], false],
[[], [1], false],
[[1, 2, 3], [2], true],
[[1, 2, 3], [2, 3], true],
[[1, 2, 3], [{}, [], 1], true],
];
table.forEach(([target, part, result]) => {
assertEquals(
containSome(target, part),
result,
);
});
},
});

Deno.test({
name: "propPath",
fn: () => {
Expand Down
1 change: 1 addition & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export { toEqualCaseInsensitive } from "./matcher/to_equal_case_insensitive.ts";
export { toIncludeAllMembers } from "./matcher/to_include_all_members.ts";
export { toContainAnyKeys } from "./matcher/to_contain_any_keys.ts";
export { toContainValue } from "./matcher/to_contain_value.ts";
export { toContainAnyValues } from "./matcher/to_contain_any_values.ts";
export { toBeHexColor } from "./matcher/to_be_hex_color.ts";
export { toBeDateString } from "./matcher/to_be_date_string.ts";
export { toBeSealed } from "./matcher/to_be_sealed.ts";
Expand Down

0 comments on commit a91338f

Please sign in to comment.