summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKitson Kelly <me@kitsonkelly.com>2019-01-22 16:09:51 +1000
committerRyan Dahl <ry@tinyclouds.org>2019-01-22 08:34:35 -0600
commit8d474a7911d6b36aec222f08ef18d88baabe2dad (patch)
tree68b161e3c0288685c1f67857b6bb345376bb5f82
parent41829f3e2bd0b1c3af8b508dfafb1b5a17fe59b6 (diff)
Make testing more matainable
Original: https://github.com/denoland/deno_std/commit/2f0ca2242f6db66acc5f8939e99615483b1d496c
-rw-r--r--testing/mod.ts189
1 files changed, 97 insertions, 92 deletions
diff --git a/testing/mod.ts b/testing/mod.ts
index 1de5f55bc..0ac2c4ea4 100644
--- a/testing/mod.ts
+++ b/testing/mod.ts
@@ -2,123 +2,128 @@
// Do not add imports in this file in order to be compatible with Node.
-export function assertEqual(actual: unknown, expected: unknown, msg?: string) {
- if (!equal(actual, expected)) {
- 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);
- }
-}
-
interface Constructor {
new (...args: any[]): any;
}
-interface Assert {
- /** Make an assertion, if not true, then throw. */
- (expr: boolean, msg?: string): void;
+const assertions = {
+ /** Make an assertion, if not `true`, then throw. */
+ assert(expr: boolean, msg = ""): void {
+ if (!expr) {
+ throw new Error(msg);
+ }
+ },
/** Make an assertion that `actual` and `expected` are equal, deeply. If not
* deeply equal, then throw.
*/
- equal(actual: unknown, expected: unknown, msg?: string): void;
+ equal(actual: unknown, expected: unknown, msg?: string): void {
+ if (!equal(actual, expected)) {
+ 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
* not then throw.
*/
- strictEqual(actual: unknown, expected: unknown, msg?: string): void;
+ strictEqual(actual: unknown, expected: unknown, msg = ""): void {
+ if (actual !== expected) {
+ 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(
+ "strictEqual failed. actual =",
+ actualString,
+ "expected =",
+ expectedString
+ );
+ if (!msg) {
+ msg = `actual: ${actualString} expected: ${expectedString}`;
+ }
+ throw new Error(msg);
+ }
+ },
/** Executes a function, expecting it to throw. If it does not, then it
* throws. An error class and a string that should be included in the
* error message can also be asserted.
*/
- throws(fn: () => void, errorClass?: Constructor, msgIncludes?: string): void;
-}
-
-export const assert = function assert(expr: boolean, msg = "") {
- if (!expr) {
- throw new Error(msg);
- }
-} as Assert;
-
-assert.equal = assertEqual;
-assert.strictEqual = function strictEqual(actual, expected, msg = "") {
- if (actual !== expected) {
- let actualString: string;
- let expectedString: string;
+ throws(
+ fn: () => void,
+ ErrorClass?: Constructor,
+ msgIncludes = "",
+ msg = ""
+ ): void {
+ let doesThrow = false;
try {
- actualString = String(actual);
+ fn();
} catch (e) {
- actualString = "[Cannot display]";
- }
- try {
- expectedString = String(expected);
- } catch (e) {
- expectedString = "[Cannot display]";
- }
- console.error(
- "strictEqual failed. actual =",
- actualString,
- "expected =",
- expectedString
- );
- if (!msg) {
- msg = `actual: ${actualString} expected: ${expectedString}`;
- }
- throw new Error(msg);
- }
-};
-assert.throws = function throws(
- fn,
- ErrorClass: Constructor,
- msgIncludes = "",
- msg = ""
-) {
- let doesThrow = false;
- try {
- fn();
- } catch (e) {
- if (ErrorClass && !(Object.getPrototypeOf(e) === ErrorClass.prototype)) {
- msg = `Expected error to be instance of "${ErrorClass.name}"${
- msg ? `: ${msg}` : "."
- }`;
- throw new Error(msg);
- }
- if (msgIncludes) {
- if (!e.message.includes(msgIncludes)) {
- msg = `Expected error message to include "${msgIncludes}", but got "${
- e.message
- }"${msg ? `: ${msg}` : "."}`;
+ if (ErrorClass && !(Object.getPrototypeOf(e) === ErrorClass.prototype)) {
+ msg = `Expected error to be instance of "${ErrorClass.name}"${
+ msg ? `: ${msg}` : "."
+ }`;
throw new Error(msg);
}
+ if (msgIncludes) {
+ if (!e.message.includes(msgIncludes)) {
+ msg = `Expected error message to include "${msgIncludes}", but got "${
+ e.message
+ }"${msg ? `: ${msg}` : "."}`;
+ throw new Error(msg);
+ }
+ }
+ doesThrow = true;
+ }
+ if (!doesThrow) {
+ msg = `Expected function to throw${msg ? `: ${msg}` : "."}`;
+ throw new Error(msg);
}
- doesThrow = true;
- }
- if (!doesThrow) {
- msg = `Expected function to throw${msg ? `: ${msg}` : "."}`;
- throw new Error(msg);
}
};
+type Assert = typeof assertions.assert & typeof assertions;
+
+// Decorate assertions.assert with all the assertions
+Object.assign(assertions.assert, assertions);
+
+export const assert = assertions.assert as Assert;
+
+/**
+ * An alias to assert.equal
+ * @deprecated
+ */
+export const assertEqual = assert.equal;
+
export function equal(c: unknown, d: unknown): boolean {
const seen = new Map();
return (function compare(a: unknown, b: unknown) {