-
-
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.
- Loading branch information
1 parent
eb9f47b
commit d9b495b
Showing
8 changed files
with
175 additions
and
11 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
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,22 @@ | ||
// Copyright 2021-Present the Unitest authors. All rights reserved. MIT license. | ||
import { equality } from "../helper/equal.ts"; | ||
import { isNil } from "../deps.ts"; | ||
import type { Equality } from "../helper/equal.ts"; | ||
|
||
/** equal to anything */ | ||
class Anything implements Equality { | ||
[equality](actual: unknown): boolean { | ||
return !isNil(actual); | ||
} | ||
|
||
toString(): string { | ||
return "except undefined or null"; | ||
} | ||
} | ||
|
||
/** make Anything */ | ||
function anything(): Anything { | ||
return new Anything(); | ||
} | ||
|
||
export { Anything, anything }; |
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,25 @@ | ||
// Copyright 2021-Present the Unitest authors. All rights reserved. MIT license. | ||
import { Anything, anything } from "./anything.ts"; | ||
import { equality } from "../helper/equal.ts"; | ||
import { assertEquals, assertExists } from "../dev_deps.ts"; | ||
|
||
Deno.test({ | ||
name: "Anything", | ||
fn: () => { | ||
const anything = new Anything(); | ||
assertExists(anything.toString); | ||
assertExists(anything[equality]); | ||
|
||
assertEquals(anything.toString(), "except undefined or null"); | ||
assertEquals(anything[equality](undefined), false); | ||
assertEquals(anything[equality](null), false); | ||
assertEquals(anything[equality]({}), true); | ||
}, | ||
}); | ||
|
||
Deno.test({ | ||
name: "anything", | ||
fn: () => { | ||
assertEquals(anything(), new Anything()); | ||
}, | ||
}); |
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,38 @@ | ||
// Copyright 2021-Present the Unitest authors. All rights reserved. MIT license. | ||
import { equal as eq, isObject } from "../deps.ts"; | ||
|
||
/** Symbol for equality */ | ||
const equality = Symbol.for("equality"); | ||
|
||
interface Equality { | ||
[equality]: (actual: unknown) => boolean; | ||
toString: () => string; | ||
} | ||
|
||
/** Whatever argument is `Equality` or not */ | ||
function isEquality(value: object): value is Equality { | ||
return equality in value; | ||
} | ||
|
||
/** safe stringify for `Equality` */ | ||
function stringifyEquality<T>(value: T): T | string { | ||
if (isObject(value) && isEquality(value)) { | ||
return value.toString(); | ||
} | ||
|
||
return value; | ||
} | ||
|
||
/** Deep equality comparison used in assertions */ | ||
function equal( | ||
a: unknown, | ||
b: unknown, | ||
): boolean { | ||
if (isObject(b) && isEquality(b)) { | ||
return b[equality](a); | ||
} | ||
return eq(a, b); | ||
} | ||
|
||
export { equal, equality, isEquality, stringifyEquality }; | ||
export type { Equality }; |
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,67 @@ | ||
// Copyright 2021-Present the Unitest authors. All rights reserved. MIT license. | ||
import { equal, equality, isEquality, stringifyEquality } from "./equal.ts"; | ||
import { anything } from "../expect/anything.ts"; | ||
import { fn } from "../mock/fn.ts"; | ||
import { assertEquals } from "../dev_deps.ts"; | ||
|
||
Deno.test("equality", () => assertEquals(equality, Symbol.for("equality"))); | ||
|
||
Deno.test("isEquality", () => { | ||
const table: [ | ||
...Parameters<typeof isEquality>, | ||
ReturnType<typeof isEquality>, | ||
][] = [ | ||
[{}, false], | ||
[[], false], | ||
[{ | ||
[equality]: true, | ||
}, true], | ||
[{ | ||
[equality]: () => true, | ||
}, true], | ||
]; | ||
|
||
table.forEach(([value, result]) => assertEquals(isEquality(value), result)); | ||
}); | ||
|
||
Deno.test("equal", () => { | ||
const table: [ | ||
...Parameters<typeof equal>, | ||
ReturnType<typeof equal>, | ||
][] = [ | ||
[null, anything(), false], | ||
[{}, anything(), true], | ||
[{}, {}, true], | ||
]; | ||
|
||
table.forEach(([a, b, result]) => assertEquals(equal(a, b), result)); | ||
|
||
const mock = fn(); | ||
equal({}, { | ||
[equality]: mock, | ||
}); | ||
|
||
assertEquals(mock.mock.calls.length, 1); | ||
}); | ||
|
||
Deno.test("stringifyEquality", () => { | ||
const table: [ | ||
...Parameters<typeof stringifyEquality>, | ||
ReturnType<typeof stringifyEquality>, | ||
][] = [ | ||
[false, false], | ||
[[], []], | ||
[{ | ||
[equality]: true, | ||
toString: () => "equal", | ||
}, "equal"], | ||
[{ | ||
[equality]: true, | ||
toString: () => "any", | ||
}, "any"], | ||
]; | ||
|
||
table.forEach(([value, result]) => | ||
assertEquals(stringifyEquality(value), result) | ||
); | ||
}); |
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