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;
}
export class AssertionError extends Error {
constructor(message: string) {
super(message);
this.name = "AssertionError";
}
}
export function equal(c: unknown, d: unknown): boolean {
const seen = new Map();
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. */
export function assert(expr: boolean, msg = ""): void {
if (!expr) {
throw new Error(msg);
throw new AssertionError(msg);
}
}
@ -85,7 +92,7 @@ export function assertNotEquals(
if (!msg) {
msg = `actual: ${actualString} expected: ${expectedString}`;
}
throw new Error(msg);
throw new AssertionError(msg);
}
/**
@ -119,7 +126,7 @@ export function assertStrictEq(
if (!msg) {
msg = `actual: ${actualString} expected: ${expectedString}`;
}
throw new Error(msg);
throw new AssertionError(msg);
}
}
@ -142,7 +149,7 @@ export function assertStrContains(
if (!msg) {
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 += `missing: ${missing}`;
}
throw new Error(msg);
throw new AssertionError(msg);
}
/**
@ -204,7 +211,7 @@ export function assertMatch(
if (!msg) {
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 ? `: ${msg}` : "."
}`;
throw new Error(msg);
throw new AssertionError(msg);
}
if (msgIncludes) {
if (!e.message.includes(msgIncludes)) {
if (msgIncludes && !e.message.includes(msgIncludes)) {
msg = `Expected error message to include "${msgIncludes}", but got "${
e.message
}"${msg ? `: ${msg}` : "."}`;
throw new Error(msg);
}
throw new AssertionError(msg);
}
doesThrow = true;
}
if (!doesThrow) {
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 ? `: ${msg}` : "."
}`;
throw new Error(msg);
throw new AssertionError(msg);
}
if (msgIncludes) {
if (!e.message.includes(msgIncludes)) {
if (msgIncludes && !e.message.includes(msgIncludes)) {
msg = `Expected error message to include "${msgIncludes}", but got "${
e.message
}"${msg ? `: ${msg}` : "."}`;
throw new Error(msg);
}
throw new AssertionError(msg);
}
doesThrow = true;
}
if (!doesThrow) {
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. */
export function unimplemented(msg?: string): never {
throw new Error(msg || "unimplemented");
throw new AssertionError(msg || "unimplemented");
}
/** Use this to assert unreachable code. */
export function unreachable(): never {
throw new Error("unreachable");
throw new AssertionError("unreachable");
}

View file

@ -2,20 +2,19 @@
import {
assert,
equal,
assertNotEquals,
assertStrContains,
assertArrayContains,
assertMatch,
assertEquals,
assertThrows,
AssertionError,
equal,
fail,
unimplemented,
unreachable
} from "./asserts.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() {
assert(equal("world", "world"));
@ -49,6 +48,7 @@ test(function testingNotEquals() {
assertNotEquals("Raptor", "Raptor");
didThrow = false;
} catch (e) {
assert(e instanceof AssertionError);
didThrow = true;
}
assertEquals(didThrow, true);
@ -63,6 +63,7 @@ test(function testingAssertStringContains() {
assertStrContains("Denosaurus", "Raptor");
didThrow = false;
} catch (e) {
assert(e instanceof AssertionError);
didThrow = true;
}
assertEquals(didThrow, true);
@ -78,6 +79,7 @@ test(function testingArrayContains() {
assertArrayContains(fixtureObject, [{ deno: "node" }]);
didThrow = false;
} catch (e) {
assert(e instanceof AssertionError);
didThrow = true;
}
assertEquals(didThrow, true);
@ -92,6 +94,7 @@ test(function testingAssertStringContainsThrow() {
e.message ===
`actual: "Denosaurus from Jurassic" expected to contains: "Raptor"`
);
assert(e instanceof AssertionError);
didThrow = true;
}
assert(didThrow);
@ -110,6 +113,7 @@ test(function testingAssertStringMatchingThrows() {
e.message ===
`actual: "Denosaurus from Jurassic" expected to match: "/Raptor/"`
);
assert(e instanceof AssertionError);
didThrow = true;
}
assert(didThrow);
@ -121,6 +125,7 @@ test(function testingAssertsUnimplemented() {
unimplemented();
} catch (e) {
assert(e.message === "unimplemented");
assert(e instanceof AssertionError);
didThrow = true;
}
assert(didThrow);
@ -132,7 +137,19 @@ test(function testingAssertsUnreachable() {
unreachable();
} catch (e) {
assert(e.message === "unreachable");
assert(e instanceof AssertionError);
didThrow = true;
}
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,
assertStrictEq,
assertThrows,
assertThrowsAsync,
fail
assertThrowsAsync
} from "../testing/asserts.ts";
import "./format_test.ts";
import "./diff_test.ts";
import "./pretty_test.ts";
import "./asserts_test.ts";
test(function testingAssertFail() {
assertThrows(fail, Error, "Failed assertion.");
assertThrows(
() => {
fail("foo");
},
Error,
"Failed assertion: foo"
);
});
test(function testingAssertEqualActualUncoercable() {
let didThrow = false;
const a = Object.create(null);