-
-
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.
feat(matcher): add
toContainEntry
matcher
- Loading branch information
1 parent
a91338f
commit e43e419
Showing
6 changed files
with
72 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright 2021-Present the Unitest authors. All rights reserved. MIT license. | ||
// This module is browser compatible. | ||
|
||
import { has, prop } from "./utils.ts"; | ||
import { equal } from "../deps.ts"; | ||
import type { MatchResult } from "./types.ts"; | ||
|
||
function predict( | ||
actual: object, | ||
[key, value]: [PropertyKey, unknown], | ||
): boolean { | ||
const _has = has(key, actual); | ||
return _has && equal(prop(key, actual), value); | ||
} | ||
|
||
function toContainEntry( | ||
actual: object, | ||
expected: [PropertyKey, unknown], | ||
): MatchResult { | ||
return { | ||
pass: predict(actual, expected), | ||
actual: Object.entries(actual), | ||
actualHint: "Actual entry:", | ||
expectedHint: "Expected to contain:", | ||
expected, | ||
}; | ||
} | ||
|
||
export { predict, toContainEntry }; |
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,39 @@ | ||
// Copyright 2021-Present the Unitest authors. All rights reserved. MIT license. | ||
|
||
import { assertEquals, assertFail, assertSuccess } from "../dev_deps.ts"; | ||
import { predict, toContainEntry } from "./to_contain_entry.ts"; | ||
|
||
Deno.test({ | ||
name: "predict", | ||
fn: () => { | ||
const table: [...Parameters<typeof predict>, ReturnType<typeof predict>][] = | ||
[ | ||
[{}, ["", {}], false], | ||
[{ a: 1 }, ["a", 1], true], | ||
[{ a: 1, b: 2 }, ["a", 2], false], | ||
[{ a: 1, b: 2 }, [1, 2], false], | ||
[{ a: 1, [Symbol.for("test")]: 2 }, [Symbol.for("test"), 2], true], | ||
[{ a: {}, b: [] }, ["b", []], true], | ||
]; | ||
|
||
table.forEach(([actual, expected, result]) => | ||
assertEquals(predict(actual, expected), result) | ||
); | ||
}, | ||
}); | ||
|
||
Deno.test({ | ||
name: "toContainEntry", | ||
fn: () => { | ||
assertSuccess(toContainEntry({ "": {} }, ["", {}])); | ||
assertFail(toContainEntry({}, ["", ""])); | ||
|
||
assertEquals(toContainEntry({ a: 1, b: 2, c: 3 }, ["abc", "def"]), { | ||
pass: false, | ||
expected: ["abc", "def"], | ||
expectedHint: "Expected to contain:", | ||
actual: [["a", 1], ["b", 2], ["c", 3]], | ||
actualHint: "Actual entry:", | ||
}); | ||
}, | ||
}); |
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