1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-28 10:09:20 -05:00

Use AssertionError instead of Error in testing (#254)

This commit is contained in:
Vincent LE GOFF 2019-03-08 22:04:43 +01:00 committed by Ryan Dahl
parent 84f9fe0649
commit d4088a1920
3 changed files with 52 additions and 44 deletions

View file

@ -6,6 +6,13 @@ interface Constructor {
new (...args: any[]): any; new (...args: any[]): any;
} }
export class AssertionError extends Error {
constructor(message: string) {
super(message);
this.name = "AssertionError";
}
}
export function equal(c: unknown, d: unknown): boolean { export function equal(c: unknown, d: unknown): boolean {
const seen = new Map(); const seen = new Map();
return (function compare(a: unknown, b: unknown) { return (function compare(a: unknown, b: unknown) {
@ -36,7 +43,7 @@ export function equal(c: unknown, d: unknown): boolean {
/** Make an assertion, if not `true`, then throw. */ /** Make an assertion, if not `true`, then throw. */
export function assert(expr: boolean, msg = ""): void { export function assert(expr: boolean, msg = ""): void {
if (!expr) { if (!expr) {
throw new Error(msg); throw new AssertionError(msg);
} }
} }
@ -85,7 +92,7 @@ export function assertNotEquals(
if (!msg) { if (!msg) {
msg = `actual: ${actualString} expected: ${expectedString}`; msg = `actual: ${actualString} expected: ${expectedString}`;
} }
throw new Error(msg); throw new AssertionError(msg);
} }
/** /**
@ -119,7 +126,7 @@ export function assertStrictEq(
if (!msg) { if (!msg) {
msg = `actual: ${actualString} expected: ${expectedString}`; msg = `actual: ${actualString} expected: ${expectedString}`;
} }
throw new Error(msg); throw new AssertionError(msg);
} }
} }
@ -142,7 +149,7 @@ export function assertStrContains(
if (!msg) { if (!msg) {
msg = `actual: "${actual}" expected to contains: "${expected}"`; msg = `actual: "${actual}" expected to contains: "${expected}"`;
} }
throw new Error(msg); throw new AssertionError(msg);
} }
} }
@ -182,7 +189,7 @@ export function assertArrayContains(
msg += "\n"; msg += "\n";
msg += `missing: ${missing}`; msg += `missing: ${missing}`;
} }
throw new Error(msg); throw new AssertionError(msg);
} }
/** /**
@ -204,7 +211,7 @@ export function assertMatch(
if (!msg) { if (!msg) {
msg = `actual: "${actual}" expected to match: "${expected}"`; msg = `actual: "${actual}" expected to match: "${expected}"`;
} }
throw new Error(msg); throw new AssertionError(msg);
} }
} }
@ -234,21 +241,19 @@ export function assertThrows(
msg = `Expected error to be instance of "${ErrorClass.name}"${ msg = `Expected error to be instance of "${ErrorClass.name}"${
msg ? `: ${msg}` : "." msg ? `: ${msg}` : "."
}`; }`;
throw new Error(msg); throw new AssertionError(msg);
} }
if (msgIncludes) { if (msgIncludes && !e.message.includes(msgIncludes)) {
if (!e.message.includes(msgIncludes)) { msg = `Expected error message to include "${msgIncludes}", but got "${
msg = `Expected error message to include "${msgIncludes}", but got "${ e.message
e.message }"${msg ? `: ${msg}` : "."}`;
}"${msg ? `: ${msg}` : "."}`; throw new AssertionError(msg);
throw new Error(msg);
}
} }
doesThrow = true; doesThrow = true;
} }
if (!doesThrow) { if (!doesThrow) {
msg = `Expected function to throw${msg ? `: ${msg}` : "."}`; msg = `Expected function to throw${msg ? `: ${msg}` : "."}`;
throw new Error(msg); throw new AssertionError(msg);
} }
} }
@ -266,30 +271,28 @@ export async function assertThrowsAsync(
msg = `Expected error to be instance of "${ErrorClass.name}"${ msg = `Expected error to be instance of "${ErrorClass.name}"${
msg ? `: ${msg}` : "." msg ? `: ${msg}` : "."
}`; }`;
throw new Error(msg); throw new AssertionError(msg);
} }
if (msgIncludes) { if (msgIncludes && !e.message.includes(msgIncludes)) {
if (!e.message.includes(msgIncludes)) { msg = `Expected error message to include "${msgIncludes}", but got "${
msg = `Expected error message to include "${msgIncludes}", but got "${ e.message
e.message }"${msg ? `: ${msg}` : "."}`;
}"${msg ? `: ${msg}` : "."}`; throw new AssertionError(msg);
throw new Error(msg);
}
} }
doesThrow = true; doesThrow = true;
} }
if (!doesThrow) { if (!doesThrow) {
msg = `Expected function to throw${msg ? `: ${msg}` : "."}`; msg = `Expected function to throw${msg ? `: ${msg}` : "."}`;
throw new Error(msg); throw new AssertionError(msg);
} }
} }
/** Use this to stub out methods that will throw when invoked. */ /** Use this to stub out methods that will throw when invoked. */
export function unimplemented(msg?: string): never { export function unimplemented(msg?: string): never {
throw new Error(msg || "unimplemented"); throw new AssertionError(msg || "unimplemented");
} }
/** Use this to assert unreachable code. */ /** Use this to assert unreachable code. */
export function unreachable(): never { export function unreachable(): never {
throw new Error("unreachable"); throw new AssertionError("unreachable");
} }

View file

@ -2,20 +2,19 @@
import { import {
assert, assert,
equal,
assertNotEquals, assertNotEquals,
assertStrContains, assertStrContains,
assertArrayContains, assertArrayContains,
assertMatch, assertMatch,
assertEquals, assertEquals,
assertThrows,
AssertionError,
equal,
fail,
unimplemented, unimplemented,
unreachable unreachable
} from "./asserts.ts"; } from "./asserts.ts";
import { test } from "./mod.ts"; import { test } from "./mod.ts";
// import { assertEquals as prettyAssertEqual } from "./pretty.ts";
// import "./format_test.ts";
// import "./diff_test.ts";
// import "./pretty_test.ts";
test(function testingEqual() { test(function testingEqual() {
assert(equal("world", "world")); assert(equal("world", "world"));
@ -49,6 +48,7 @@ test(function testingNotEquals() {
assertNotEquals("Raptor", "Raptor"); assertNotEquals("Raptor", "Raptor");
didThrow = false; didThrow = false;
} catch (e) { } catch (e) {
assert(e instanceof AssertionError);
didThrow = true; didThrow = true;
} }
assertEquals(didThrow, true); assertEquals(didThrow, true);
@ -63,6 +63,7 @@ test(function testingAssertStringContains() {
assertStrContains("Denosaurus", "Raptor"); assertStrContains("Denosaurus", "Raptor");
didThrow = false; didThrow = false;
} catch (e) { } catch (e) {
assert(e instanceof AssertionError);
didThrow = true; didThrow = true;
} }
assertEquals(didThrow, true); assertEquals(didThrow, true);
@ -78,6 +79,7 @@ test(function testingArrayContains() {
assertArrayContains(fixtureObject, [{ deno: "node" }]); assertArrayContains(fixtureObject, [{ deno: "node" }]);
didThrow = false; didThrow = false;
} catch (e) { } catch (e) {
assert(e instanceof AssertionError);
didThrow = true; didThrow = true;
} }
assertEquals(didThrow, true); assertEquals(didThrow, true);
@ -92,6 +94,7 @@ test(function testingAssertStringContainsThrow() {
e.message === e.message ===
`actual: "Denosaurus from Jurassic" expected to contains: "Raptor"` `actual: "Denosaurus from Jurassic" expected to contains: "Raptor"`
); );
assert(e instanceof AssertionError);
didThrow = true; didThrow = true;
} }
assert(didThrow); assert(didThrow);
@ -110,6 +113,7 @@ test(function testingAssertStringMatchingThrows() {
e.message === e.message ===
`actual: "Denosaurus from Jurassic" expected to match: "/Raptor/"` `actual: "Denosaurus from Jurassic" expected to match: "/Raptor/"`
); );
assert(e instanceof AssertionError);
didThrow = true; didThrow = true;
} }
assert(didThrow); assert(didThrow);
@ -121,6 +125,7 @@ test(function testingAssertsUnimplemented() {
unimplemented(); unimplemented();
} catch (e) { } catch (e) {
assert(e.message === "unimplemented"); assert(e.message === "unimplemented");
assert(e instanceof AssertionError);
didThrow = true; didThrow = true;
} }
assert(didThrow); assert(didThrow);
@ -132,7 +137,19 @@ test(function testingAssertsUnreachable() {
unreachable(); unreachable();
} catch (e) { } catch (e) {
assert(e.message === "unreachable"); assert(e.message === "unreachable");
assert(e instanceof AssertionError);
didThrow = true; didThrow = true;
} }
assert(didThrow); assert(didThrow);
}); });
test(function testingAssertFail() {
assertThrows(fail, AssertionError, "Failed assertion.");
assertThrows(
() => {
fail("foo");
},
AssertionError,
"Failed assertion: foo"
);
});

View file

@ -5,25 +5,13 @@ import {
assertEquals, assertEquals,
assertStrictEq, assertStrictEq,
assertThrows, assertThrows,
assertThrowsAsync, assertThrowsAsync
fail
} from "../testing/asserts.ts"; } from "../testing/asserts.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";
import "./asserts_test.ts"; import "./asserts_test.ts";
test(function testingAssertFail() {
assertThrows(fail, Error, "Failed assertion.");
assertThrows(
() => {
fail("foo");
},
Error,
"Failed assertion: foo"
);
});
test(function testingAssertEqualActualUncoercable() { test(function testingAssertEqualActualUncoercable() {
let didThrow = false; let didThrow = false;
const a = Object.create(null); const a = Object.create(null);