mirror of
https://github.com/denoland/deno.git
synced 2024-11-23 15:16:54 -05:00
Use pretty assertEqual in testing (denoland/deno_std#234)
Original: 8fb9f709a6
This commit is contained in:
parent
17663c1232
commit
39fde3a454
3 changed files with 14 additions and 32 deletions
|
@ -1,6 +1,7 @@
|
||||||
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
||||||
|
|
||||||
import { green, red } from "../colors/mod.ts";
|
import { green, red } from "../colors/mod.ts";
|
||||||
|
import { assertEqual as prettyAssertEqual } from "./pretty.ts";
|
||||||
|
|
||||||
interface Constructor {
|
interface Constructor {
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
|
@ -46,30 +47,7 @@ const assertions = {
|
||||||
* deeply equal, then throw.
|
* deeply equal, then throw.
|
||||||
*/
|
*/
|
||||||
equal(actual: unknown, expected: unknown, msg?: string): void {
|
equal(actual: unknown, expected: unknown, msg?: string): void {
|
||||||
if (!equal(actual, expected)) {
|
prettyAssertEqual(actual, expected, msg);
|
||||||
let actualString: string;
|
|
||||||
let expectedString: string;
|
|
||||||
try {
|
|
||||||
actualString = String(actual);
|
|
||||||
} catch (e) {
|
|
||||||
actualString = "[Cannot display]";
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
expectedString = String(expected);
|
|
||||||
} catch (e) {
|
|
||||||
expectedString = "[Cannot display]";
|
|
||||||
}
|
|
||||||
console.error(
|
|
||||||
"assertEqual failed. actual =",
|
|
||||||
actualString,
|
|
||||||
"expected =",
|
|
||||||
expectedString
|
|
||||||
);
|
|
||||||
if (!msg) {
|
|
||||||
msg = `actual: ${actualString} expected: ${expectedString}`;
|
|
||||||
}
|
|
||||||
throw new Error(msg);
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/** Make an assertion that `actual` and `expected` are strictly equal. If
|
/** Make an assertion that `actual` and `expected` are strictly equal. If
|
||||||
|
@ -187,10 +165,10 @@ Object.assign(assertions.assert, assertions);
|
||||||
export const assert = assertions.assert as Assert;
|
export const assert = assertions.assert as Assert;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An alias to assert.equal
|
* Alias to pretty.assertEqual
|
||||||
* @deprecated
|
* @deprecated
|
||||||
*/
|
*/
|
||||||
export const assertEqual = assert.equal;
|
export const assertEqual = prettyAssertEqual;
|
||||||
|
|
||||||
export type TestFunction = () => void | Promise<void>;
|
export type TestFunction = () => void | Promise<void>;
|
||||||
|
|
||||||
|
|
|
@ -55,7 +55,11 @@ function buildMessage(diffResult: ReadonlyArray<DiffResult<string>>): string[] {
|
||||||
return messages;
|
return messages;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function assertEqual(actual: unknown, expected: unknown): void {
|
export function assertEqual(
|
||||||
|
actual: unknown,
|
||||||
|
expected: unknown,
|
||||||
|
msg?: string
|
||||||
|
): void {
|
||||||
if (equal(actual, expected)) {
|
if (equal(actual, expected)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -71,5 +75,8 @@ export function assertEqual(actual: unknown, expected: unknown): void {
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
message = `\n${red(CAN_NOT_DISPLAY)} + \n\n`;
|
message = `\n${red(CAN_NOT_DISPLAY)} + \n\n`;
|
||||||
}
|
}
|
||||||
|
if (msg) {
|
||||||
|
message = msg;
|
||||||
|
}
|
||||||
throw new Error(message);
|
throw new Error(message);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
||||||
|
|
||||||
import { test, assert, assertEqual, equal, runIfMain } from "./mod.ts";
|
import { test, assert, assertEqual, equal, runIfMain } from "./mod.ts";
|
||||||
|
import { assertEqual as prettyAssertEqual } from "./pretty.ts";
|
||||||
import "./format_test.ts";
|
import "./format_test.ts";
|
||||||
import "./diff_test.ts";
|
import "./diff_test.ts";
|
||||||
import "./pretty_test.ts";
|
import "./pretty_test.ts";
|
||||||
|
@ -31,7 +32,7 @@ test(function testingAssertEqual() {
|
||||||
const a = Object.create(null);
|
const a = Object.create(null);
|
||||||
a.b = "foo";
|
a.b = "foo";
|
||||||
assert.equal(a, a);
|
assert.equal(a, a);
|
||||||
assert(assertEqual === assert.equal);
|
assert(assertEqual === prettyAssertEqual);
|
||||||
});
|
});
|
||||||
|
|
||||||
test(function testingAssertFail() {
|
test(function testingAssertFail() {
|
||||||
|
@ -52,8 +53,6 @@ test(function testingAssertEqualActualUncoercable() {
|
||||||
assert.equal(a, "bar");
|
assert.equal(a, "bar");
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
didThrow = true;
|
didThrow = true;
|
||||||
console.log(e.message);
|
|
||||||
assert(e.message === "actual: [Cannot display] expected: bar");
|
|
||||||
}
|
}
|
||||||
assert(didThrow);
|
assert(didThrow);
|
||||||
});
|
});
|
||||||
|
@ -65,8 +64,6 @@ test(function testingAssertEqualExpectedUncoercable() {
|
||||||
assert.equal("bar", a);
|
assert.equal("bar", a);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
didThrow = true;
|
didThrow = true;
|
||||||
console.log(e.message);
|
|
||||||
assert(e.message === "actual: bar expected: [Cannot display]");
|
|
||||||
}
|
}
|
||||||
assert(didThrow);
|
assert(didThrow);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue