From e36edfdb3fd4709358a5f499f13cfe3d53c2b4f7 Mon Sep 17 00:00:00 2001 From: Vincent LE GOFF Date: Wed, 6 Mar 2019 22:39:50 +0100 Subject: Testing refactor (denoland/deno_std#240) Original: https://github.com/denoland/deno_std/commit/e1d5c00279132aa639030c6c6d9b4e308bd4775e --- testing/README.md | 73 ++++++++++--------------- testing/asserts.ts | 35 +++++++++++- testing/asserts_test.ts | 28 ++++++++-- testing/diff_test.ts | 23 ++++---- testing/format_test.ts | 141 ++++++++++++++++++++++++------------------------ testing/mod.ts | 62 --------------------- testing/pretty.ts | 4 +- testing/pretty_test.ts | 31 +++++------ testing/test.ts | 82 +++++++++++----------------- 9 files changed, 216 insertions(+), 263 deletions(-) (limited to 'testing') diff --git a/testing/README.md b/testing/README.md index cf78187b6..4c295e472 100644 --- a/testing/README.md +++ b/testing/README.md @@ -11,53 +11,38 @@ contains a `name` property and a `fn` property. When running tests and outputting the results, the name of the past function is used, or if the object is passed, the `name` property is used to identify the test. -The module also exports `assert`, `assertEqual`, and `equal`. - -`equal` is a deep comparision function, where `actual` and `expected` are -compared deeply, and if they vary, `equal` returns `false`. - -The export `assert` is a function, but it is also decorated with other useful -functions: +Asserts are exposed in `testing/asserts.ts` module. +- `equal` - Deep comparision function, where `actual` and `expected` are + compared deeply, and if they vary, `equal` returns `false`. - `assert()` - Expects a boolean value, throws if the value is `false`. -- `assert.equal()` - Uses the `equal` comparison and throws if the `actual` and +- `assertEq()` - Uses the `equal` comparison and throws if the `actual` and `expected` are not equal. -- `assert.strictEqual()` - Compares `actual` and `expected` strictly, therefore +- `assertStrictEq()` - Compares `actual` and `expected` strictly, therefore for non-primitives the values must reference the same instance. -- `assert.throws()` - Expects the passed `fn` to throw. If `fn` does not throw, +- `assertThrows()` - Expects the passed `fn` to throw. If `fn` does not throw, this function does. Also compares any errors thrown to an optional expected `Error` class and checks that the error `.message` includes an optional string. -- `assert.throwsAsync()` - Expects the passed `fn` to be async and throw (or +- `assertThrowsAsync()` - Expects the passed `fn` to be async and throw (or return a `Promise` that rejects). If the `fn` does not throw or reject, this function will throw asynchronously. Also compares any errors thrown to an optional expected `Error` class and checks that the error `.message` includes an optional string. -`assertEqual()` is the same as `assert.equal()` but maintained for backwards -compatibility. - `runTests()` executes the declared tests. Basic usage: ```ts -import { - runTests, - test, - assert, - equal -} from "https://deno.land/std/testing/mod.ts"; +import { runTests, test } from "https://deno.land/std/testing/mod.ts"; +import { assertEq } from "https://deno.land/std/testing/asserts.ts"; test({ name: "testing example", fn() { - assert(equal("world", "world")); - assert(!equal("hello", "world")); - assert(equal({ hello: "world" }, { hello: "world" })); - assert(!equal({ world: "hello" }, { hello: "world" })); - assert.equal("world", "world"); - assert.equal({ hello: "world" }, { hello: "world" }); + assertEq("world", "world")); + assertEq({ hello: "world" }, { hello: "world" })); } }); @@ -68,43 +53,39 @@ Short syntax (named function instead of object): ```ts test(function example() { - assert(equal("world", "world")); - assert(!equal("hello", "world")); - assert(equal({ hello: "world" }, { hello: "world" })); - assert(!equal({ world: "hello" }, { hello: "world" })); - assert.equal("world", "world"); - assert.equal({ hello: "world" }, { hello: "world" }); + assertEq("world", "world")); + assertEq({ hello: "world" }, { hello: "world" })); }); ``` -Using `assert.strictEqual()`: +Using `assertStrictEq()`: ```ts test(function isStrictlyEqual() { const a = {}; const b = a; - assert.strictEqual(a, b); + assertStrictEq(a, b); }); // This test fails test(function isNotStrictlyEqual() { const a = {}; const b = {}; - assert.strictEqual(a, b); + assertStrictEq(a, b); }); ``` -Using `assert.throws()`: +Using `assertThrows()`: ```ts test(function doesThrow() { - assert.throws(() => { + assertThrows(() => { throw new TypeError("hello world!"); }); - assert.throws(() => { + assertThrows(() => { throw new TypeError("hello world!"); }, TypeError); - assert.throws( + assertThrows( () => { throw new TypeError("hello world!"); }, @@ -115,37 +96,37 @@ test(function doesThrow() { // This test will not pass test(function fails() { - assert.throws(() => { + assertThrows(() => { console.log("Hello world"); }); }); ``` -Using `assert.throwsAsync()`: +Using `assertThrowsAsync()`: ```ts test(async function doesThrow() { - assert.throwsAsync(async () => { + assertThrowsAsync(async () => { throw new TypeError("hello world!"); }); - assert.throwsAsync(async () => { + assertThrowsAsync(async () => { throw new TypeError("hello world!"); }, TypeError); - assert.throwsAsync( + assertThrowsAsync( async () => { throw new TypeError("hello world!"); }, TypeError, "hello" ); - assert.throwsAsync(async () => { + assertThrowsAsync(async () => { return Promise.reject(new Error()); }); }); // This test will not pass test(async function fails() { - assert.throwsAsync(async () => { + assertThrowsAsync(async () => { console.log("Hello world"); }); }); diff --git a/testing/asserts.ts b/testing/asserts.ts index a2110b8d9..8932793d4 100644 --- a/testing/asserts.ts +++ b/testing/asserts.ts @@ -1,11 +1,38 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -import { assertEqual as prettyAssertEqual } from "./pretty.ts"; +import { assertEq as prettyAssertEqual } from "./pretty.ts"; interface Constructor { // eslint-disable-next-line @typescript-eslint/no-explicit-any new (...args: any[]): any; } +export function equal(c: unknown, d: unknown): boolean { + const seen = new Map(); + return (function compare(a: unknown, b: unknown) { + if (Object.is(a, b)) { + return true; + } + if (a && typeof a === "object" && b && typeof b === "object") { + if (seen.get(a) === b) { + return true; + } + if (Object.keys(a || {}).length !== Object.keys(b || {}).length) { + return false; + } + const merged = { ...a, ...b }; + for (const key in merged) { + type Key = keyof typeof merged; + if (!compare(a && a[key as Key], b && b[key as Key])) { + return false; + } + } + seen.set(a, b); + return true; + } + return false; + })(c, d); +} + /** Make an assertion, if not `true`, then throw. */ export function assert(expr: boolean, msg = ""): void { if (!expr) { @@ -17,7 +44,11 @@ export function assert(expr: boolean, msg = ""): void { * Make an assertion that `actual` and `expected` are equal, deeply. If not * deeply equal, then throw. */ -export function equal(actual: unknown, expected: unknown, msg?: string): void { +export function assertEq( + actual: unknown, + expected: unknown, + msg?: string +): void { prettyAssertEqual(actual, expected, msg); } diff --git a/testing/asserts_test.ts b/testing/asserts_test.ts index f4256d8f5..9dc70ad29 100644 --- a/testing/asserts_test.ts +++ b/testing/asserts_test.ts @@ -1,12 +1,34 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -import { assertStrContains, assertMatch } from "./asserts.ts"; -import { test, assert } from "./mod.ts"; -// import { assertEqual as prettyAssertEqual } from "./pretty.ts"; +import { assert, equal, assertStrContains, assertMatch } from "./asserts.ts"; +import { test } from "./mod.ts"; +// import { assertEq as prettyAssertEqual } from "./pretty.ts"; // import "./format_test.ts"; // import "./diff_test.ts"; // import "./pretty_test.ts"; +test(function testingEqual() { + assert(equal("world", "world")); + assert(!equal("hello", "world")); + assert(equal(5, 5)); + assert(!equal(5, 6)); + assert(equal(NaN, NaN)); + assert(equal({ hello: "world" }, { hello: "world" })); + assert(!equal({ world: "hello" }, { hello: "world" })); + assert( + equal( + { hello: "world", hi: { there: "everyone" } }, + { hello: "world", hi: { there: "everyone" } } + ) + ); + assert( + !equal( + { hello: "world", hi: { there: "everyone" } }, + { hello: "world", hi: { there: "everyone else" } } + ) + ); +}); + test(function testingAssertStringContains() { assertStrContains("Denosaurus", "saur"); assertStrContains("Denosaurus", "Deno"); diff --git a/testing/diff_test.ts b/testing/diff_test.ts index d2259ba35..cd71b25d8 100644 --- a/testing/diff_test.ts +++ b/testing/diff_test.ts @@ -1,17 +1,18 @@ import diff from "./diff.ts"; -import { test, assertEqual } from "./mod.ts"; +import { assertEq } from "../testing/asserts.ts"; +import { test } from "./mod.ts"; test({ name: "empty", fn() { - assertEqual(diff([], []), []); + assertEq(diff([], []), []); } }); test({ name: '"a" vs "b"', fn() { - assertEqual(diff(["a"], ["b"]), [ + assertEq(diff(["a"], ["b"]), [ { type: "removed", value: "a" }, { type: "added", value: "b" } ]); @@ -21,28 +22,28 @@ test({ test({ name: '"a" vs "a"', fn() { - assertEqual(diff(["a"], ["a"]), [{ type: "common", value: "a" }]); + assertEq(diff(["a"], ["a"]), [{ type: "common", value: "a" }]); } }); test({ name: '"a" vs ""', fn() { - assertEqual(diff(["a"], []), [{ type: "removed", value: "a" }]); + assertEq(diff(["a"], []), [{ type: "removed", value: "a" }]); } }); test({ name: '"" vs "a"', fn() { - assertEqual(diff([], ["a"]), [{ type: "added", value: "a" }]); + assertEq(diff([], ["a"]), [{ type: "added", value: "a" }]); } }); test({ name: '"a" vs "a, b"', fn() { - assertEqual(diff(["a"], ["a", "b"]), [ + assertEq(diff(["a"], ["a", "b"]), [ { type: "common", value: "a" }, { type: "added", value: "b" } ]); @@ -52,7 +53,7 @@ test({ test({ name: '"strength" vs "string"', fn() { - assertEqual(diff(Array.from("strength"), Array.from("string")), [ + assertEq(diff(Array.from("strength"), Array.from("string")), [ { type: "common", value: "s" }, { type: "common", value: "t" }, { type: "common", value: "r" }, @@ -69,7 +70,7 @@ test({ test({ name: '"strength" vs ""', fn() { - assertEqual(diff(Array.from("strength"), Array.from("")), [ + assertEq(diff(Array.from("strength"), Array.from("")), [ { type: "removed", value: "s" }, { type: "removed", value: "t" }, { type: "removed", value: "r" }, @@ -85,7 +86,7 @@ test({ test({ name: '"" vs "strength"', fn() { - assertEqual(diff(Array.from(""), Array.from("strength")), [ + assertEq(diff(Array.from(""), Array.from("strength")), [ { type: "added", value: "s" }, { type: "added", value: "t" }, { type: "added", value: "r" }, @@ -101,7 +102,7 @@ test({ test({ name: '"abc", "c" vs "abc", "bcd", "c"', fn() { - assertEqual(diff(["abc", "c"], ["abc", "bcd", "c"]), [ + assertEq(diff(["abc", "c"], ["abc", "bcd", "c"]), [ { type: "common", value: "abc" }, { type: "added", value: "bcd" }, { type: "common", value: "c" } diff --git a/testing/format_test.ts b/testing/format_test.ts index 7ca0235a5..23c5ca1c0 100644 --- a/testing/format_test.ts +++ b/testing/format_test.ts @@ -6,7 +6,8 @@ * LICENSE file in the root directory of this source tree. * */ -import { test, assertEqual } from "./mod.ts"; +import { test } from "./mod.ts"; +import { assertEq } from "../testing/asserts.ts"; import { format } from "./format.ts"; // eslint-disable-next-line @typescript-eslint/no-unused-vars,@typescript-eslint/no-explicit-any @@ -54,7 +55,7 @@ test({ name: "prints empty arguments", fn() { const val = returnArguments(); - assertEqual(format(val), "Arguments []"); + assertEq(format(val), "Arguments []"); } }); @@ -62,7 +63,7 @@ test({ name: "prints an empty array", fn() { const val: unknown[] = []; - assertEqual(format(val), "Array []"); + assertEq(format(val), "Array []"); } }); @@ -70,7 +71,7 @@ test({ name: "prints an array with items", fn() { const val = [1, 2, 3]; - assertEqual(format(val), "Array [\n 1,\n 2,\n 3,\n]"); + assertEq(format(val), "Array [\n 1,\n 2,\n 3,\n]"); } }); @@ -78,7 +79,7 @@ test({ name: "prints a empty typed array", fn() { const val = new Uint32Array(0); - assertEqual(format(val), "Uint32Array []"); + assertEq(format(val), "Uint32Array []"); } }); @@ -86,7 +87,7 @@ test({ name: "prints a typed array with items", fn() { const val = new Uint32Array(3); - assertEqual(format(val), "Uint32Array [\n 0,\n 0,\n 0,\n]"); + assertEq(format(val), "Uint32Array [\n 0,\n 0,\n 0,\n]"); } }); @@ -94,7 +95,7 @@ test({ name: "prints an array buffer", fn() { const val = new ArrayBuffer(3); - assertEqual(format(val), "ArrayBuffer []"); + assertEq(format(val), "ArrayBuffer []"); } }); @@ -102,7 +103,7 @@ test({ name: "prints a nested array", fn() { const val = [[1, 2, 3]]; - assertEqual( + assertEq( format(val), "Array [\n Array [\n 1,\n 2,\n 3,\n ],\n]" ); @@ -113,7 +114,7 @@ test({ name: "prints true", fn() { const val = true; - assertEqual(format(val), "true"); + assertEq(format(val), "true"); } }); @@ -121,7 +122,7 @@ test({ name: "prints false", fn() { const val = false; - assertEqual(format(val), "false"); + assertEq(format(val), "false"); } }); @@ -129,7 +130,7 @@ test({ name: "prints an error", fn() { const val = new Error(); - assertEqual(format(val), "[Error]"); + assertEq(format(val), "[Error]"); } }); @@ -137,7 +138,7 @@ test({ name: "prints a typed error with a message", fn() { const val = new TypeError("message"); - assertEqual(format(val), "[TypeError: message]"); + assertEq(format(val), "[TypeError: message]"); } }); @@ -146,7 +147,7 @@ test({ fn() { // tslint:disable-next-line:function-constructor const val = new Function(); - assertEqual(format(val), "[Function anonymous]"); + assertEq(format(val), "[Function anonymous]"); } }); @@ -159,7 +160,7 @@ test({ } // tslint:disable-next-line:no-empty f(() => {}); - assertEqual(format(val), "[Function anonymous]"); + assertEq(format(val), "[Function anonymous]"); } }); @@ -169,7 +170,7 @@ test({ // tslint:disable-next-line:no-empty const val = (): void => {}; const formatted = format(val); - assertEqual( + assertEq( formatted === "[Function anonymous]" || formatted === "[Function val]", true ); @@ -181,7 +182,7 @@ test({ fn() { // tslint:disable-next-line:no-empty const val = function named(): void {}; - assertEqual(format(val), "[Function named]"); + assertEq(format(val), "[Function named]"); } }); @@ -193,7 +194,7 @@ test({ yield 2; yield 3; }; - assertEqual(format(val), "[Function generate]"); + assertEq(format(val), "[Function generate]"); } }); @@ -202,7 +203,7 @@ test({ fn() { // tslint:disable-next-line:no-empty const val = function named(): void {}; - assertEqual( + assertEq( format(val, { printFunctionName: false }), @@ -215,7 +216,7 @@ test({ name: "prints Infinity", fn() { const val = Infinity; - assertEqual(format(val), "Infinity"); + assertEq(format(val), "Infinity"); } }); @@ -223,7 +224,7 @@ test({ name: "prints -Infinity", fn() { const val = -Infinity; - assertEqual(format(val), "-Infinity"); + assertEq(format(val), "-Infinity"); } }); @@ -231,7 +232,7 @@ test({ name: "prints an empty map", fn() { const val = new Map(); - assertEqual(format(val), "Map {}"); + assertEq(format(val), "Map {}"); } }); @@ -241,7 +242,7 @@ test({ const val = new Map(); val.set("prop1", "value1"); val.set("prop2", "value2"); - assertEqual( + assertEq( format(val), 'Map {\n "prop1" => "value1",\n "prop2" => "value2",\n}' ); @@ -287,7 +288,7 @@ test({ ' } => "object",', "}" ].join("\n"); - assertEqual(format(val), expected); + assertEq(format(val), expected); } }); @@ -295,7 +296,7 @@ test({ name: "prints NaN", fn() { const val = NaN; - assertEqual(format(val), "NaN"); + assertEq(format(val), "NaN"); } }); @@ -303,7 +304,7 @@ test({ name: "prints null", fn() { const val = null; - assertEqual(format(val), "null"); + assertEq(format(val), "null"); } }); @@ -311,7 +312,7 @@ test({ name: "prints a positive number", fn() { const val = 123; - assertEqual(format(val), "123"); + assertEq(format(val), "123"); } }); @@ -319,7 +320,7 @@ test({ name: "prints a negative number", fn() { const val = -123; - assertEqual(format(val), "-123"); + assertEq(format(val), "-123"); } }); @@ -327,7 +328,7 @@ test({ name: "prints zero", fn() { const val = 0; - assertEqual(format(val), "0"); + assertEq(format(val), "0"); } }); @@ -335,7 +336,7 @@ test({ name: "prints negative zero", fn() { const val = -0; - assertEqual(format(val), "-0"); + assertEq(format(val), "-0"); } }); @@ -343,7 +344,7 @@ test({ name: "prints a date", fn() { const val = new Date(10e11); - assertEqual(format(val), "2001-09-09T01:46:40.000Z"); + assertEq(format(val), "2001-09-09T01:46:40.000Z"); } }); @@ -351,7 +352,7 @@ test({ name: "prints an invalid date", fn() { const val = new Date(Infinity); - assertEqual(format(val), "Date { NaN }"); + assertEq(format(val), "Date { NaN }"); } }); @@ -359,7 +360,7 @@ test({ name: "prints an empty object", fn() { const val = {}; - assertEqual(format(val), "Object {}"); + assertEq(format(val), "Object {}"); } }); @@ -367,7 +368,7 @@ test({ name: "prints an object with properties", fn() { const val = { prop1: "value1", prop2: "value2" }; - assertEqual( + assertEq( format(val), 'Object {\n "prop1": "value1",\n "prop2": "value2",\n}' ); @@ -382,7 +383,7 @@ test({ val[Symbol("symbol1")] = "value2"; val[Symbol("symbol2")] = "value3"; val.prop = "value1"; - assertEqual( + assertEq( format(val), 'Object {\n "prop": "value1",\n Symbol(symbol1): "value2",\n Symbol(symbol2): "value3",\n}' ); @@ -401,7 +402,7 @@ test({ enumerable: false, value: false }); - assertEqual(format(val), 'Object {\n "enumerable": true,\n}'); + assertEq(format(val), 'Object {\n "enumerable": true,\n}'); } }); @@ -417,7 +418,7 @@ test({ enumerable: false, value: false }); - assertEqual(format(val), 'Object {\n "enumerable": true,\n}'); + assertEq(format(val), 'Object {\n "enumerable": true,\n}'); } }); @@ -425,7 +426,7 @@ test({ name: "prints an object with sorted properties", fn() { const val = { b: 1, a: 2 }; - assertEqual(format(val), 'Object {\n "a": 2,\n "b": 1,\n}'); + assertEq(format(val), 'Object {\n "a": 2,\n "b": 1,\n}'); } }); @@ -433,7 +434,7 @@ test({ name: "prints regular expressions from constructors", fn() { const val = new RegExp("regexp"); - assertEqual(format(val), "/regexp/"); + assertEq(format(val), "/regexp/"); } }); @@ -441,7 +442,7 @@ test({ name: "prints regular expressions from literals", fn() { const val = /regexp/gi; - assertEqual(format(val), "/regexp/gi"); + assertEq(format(val), "/regexp/gi"); } }); @@ -449,7 +450,7 @@ test({ name: "prints regular expressions {escapeRegex: false}", fn() { const val = /regexp\d/gi; - assertEqual(format(val), "/regexp\\d/gi"); + assertEq(format(val), "/regexp\\d/gi"); } }); @@ -457,7 +458,7 @@ test({ name: "prints regular expressions {escapeRegex: true}", fn() { const val = /regexp\d/gi; - assertEqual(format(val, { escapeRegex: true }), "/regexp\\\\d/gi"); + assertEq(format(val, { escapeRegex: true }), "/regexp\\\\d/gi"); } }); @@ -465,7 +466,7 @@ test({ name: "escapes regular expressions nested inside object", fn() { const obj = { test: /regexp\d/gi }; - assertEqual( + assertEq( format(obj, { escapeRegex: true }), 'Object {\n "test": /regexp\\\\d/gi,\n}' ); @@ -476,7 +477,7 @@ test({ name: "prints an empty set", fn() { const val = new Set(); - assertEqual(format(val), "Set {}"); + assertEq(format(val), "Set {}"); } }); @@ -486,7 +487,7 @@ test({ const val = new Set(); val.add("value1"); val.add("value2"); - assertEqual(format(val), 'Set {\n "value1",\n "value2",\n}'); + assertEq(format(val), 'Set {\n "value1",\n "value2",\n}'); } }); @@ -494,7 +495,7 @@ test({ name: "prints a string", fn() { const val = "string"; - assertEqual(format(val), '"string"'); + assertEq(format(val), '"string"'); } }); @@ -502,7 +503,7 @@ test({ name: "prints and escape a string", fn() { const val = "\"'\\"; - assertEqual(format(val), '"\\"\'\\\\"'); + assertEq(format(val), '"\\"\'\\\\"'); } }); @@ -510,15 +511,15 @@ test({ name: "doesn't escape string with {excapeString: false}", fn() { const val = "\"'\\n"; - assertEqual(format(val, { escapeString: false }), '""\'\\n"'); + assertEq(format(val, { escapeString: false }), '""\'\\n"'); } }); test({ name: "prints a string with escapes", fn() { - assertEqual(format('"-"'), '"\\"-\\""'); - assertEqual(format("\\ \\\\"), '"\\\\ \\\\\\\\"'); + assertEq(format('"-"'), '"\\"-\\""'); + assertEq(format("\\ \\\\"), '"\\\\ \\\\\\\\"'); } }); @@ -526,7 +527,7 @@ test({ name: "prints a multiline string", fn() { const val = ["line 1", "line 2", "line 3"].join("\n"); - assertEqual(format(val), '"' + val + '"'); + assertEq(format(val), '"' + val + '"'); } }); @@ -546,7 +547,7 @@ test({ }, type: "svg" }; - assertEqual( + assertEq( format(val), [ "Object {", @@ -572,7 +573,7 @@ test({ name: "prints a symbol", fn() { const val = Symbol("symbol"); - assertEqual(format(val), "Symbol(symbol)"); + assertEq(format(val), "Symbol(symbol)"); } }); @@ -580,7 +581,7 @@ test({ name: "prints undefined", fn() { const val = undefined; - assertEqual(format(val), "undefined"); + assertEq(format(val), "undefined"); } }); @@ -588,7 +589,7 @@ test({ name: "prints a WeakMap", fn() { const val = new WeakMap(); - assertEqual(format(val), "WeakMap {}"); + assertEq(format(val), "WeakMap {}"); } }); @@ -596,7 +597,7 @@ test({ name: "prints a WeakSet", fn() { const val = new WeakSet(); - assertEqual(format(val), "WeakSet {}"); + assertEq(format(val), "WeakSet {}"); } }); @@ -604,7 +605,7 @@ test({ name: "prints deeply nested objects", fn() { const val = { prop: { prop: { prop: "value" } } }; - assertEqual( + assertEq( format(val), 'Object {\n "prop": Object {\n "prop": Object {\n "prop": "value",\n },\n },\n}' ); @@ -617,7 +618,7 @@ test({ // eslint-disable-next-line @typescript-eslint/no-explicit-any const val: any = {}; val.prop = val; - assertEqual(format(val), 'Object {\n "prop": [Circular],\n}'); + assertEq(format(val), 'Object {\n "prop": [Circular],\n}'); } }); @@ -626,7 +627,7 @@ test({ fn() { const inner = {}; const val = { prop1: inner, prop2: inner }; - assertEqual( + assertEq( format(val), 'Object {\n "prop1": Object {},\n "prop2": Object {},\n}' ); @@ -636,14 +637,14 @@ test({ test({ name: "default implicit: 2 spaces", fn() { - assertEqual(format(createVal()), createExpected()); + assertEq(format(createVal()), createExpected()); } }); test({ name: "default explicit: 2 spaces", fn() { - assertEqual(format(createVal(), { indent: 2 }), createExpected()); + assertEq(format(createVal(), { indent: 2 }), createExpected()); } }); @@ -652,7 +653,7 @@ test({ name: "non-default: 0 spaces", fn() { const indent = 0; - assertEqual( + assertEq( format(createVal(), { indent }), createExpected().replace(/ {2}/g, " ".repeat(indent)) ); @@ -663,7 +664,7 @@ test({ name: "non-default: 4 spaces", fn() { const indent = 4; - assertEqual( + assertEq( format(createVal(), { indent }), createExpected().replace(/ {2}/g, " ".repeat(indent)) ); @@ -691,7 +692,7 @@ test({ "set non-empty": new Set(["value"]) } ]; - assertEqual( + assertEq( format(v, { maxDepth: 2 }), [ "Array [", @@ -719,7 +720,7 @@ test({ test({ name: "prints objects with no constructor", fn() { - assertEqual(format(Object.create(null)), "Object {}"); + assertEq(format(Object.create(null)), "Object {}"); } }); @@ -733,14 +734,14 @@ test({ ' "constructor": "constructor",', "}" ].join("\n"); - assertEqual(format(obj), expected); + assertEq(format(obj), expected); } }); test({ name: "calls toJSON and prints its return value", fn() { - assertEqual( + assertEq( format({ toJSON: () => ({ value: false }), value: true @@ -753,7 +754,7 @@ test({ test({ name: "calls toJSON and prints an internal representation.", fn() { - assertEqual( + assertEq( format({ toJSON: () => "[Internal Object]", value: true @@ -766,7 +767,7 @@ test({ test({ name: "calls toJSON only on functions", fn() { - assertEqual( + assertEq( format({ toJSON: false, value: true @@ -779,7 +780,7 @@ test({ test({ name: "does not call toJSON recursively", fn() { - assertEqual( + assertEq( format({ toJSON: () => ({ toJSON: () => ({ value: true }) }), value: false @@ -795,6 +796,6 @@ test({ const set = new Set([1]); // eslint-disable-next-line @typescript-eslint/no-explicit-any (set as any).toJSON = () => "map"; - assertEqual(format(set), '"map"'); + assertEq(format(set), '"map"'); } }); diff --git a/testing/mod.ts b/testing/mod.ts index c9cdde0d2..09e183796 100644 --- a/testing/mod.ts +++ b/testing/mod.ts @@ -1,68 +1,6 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. import { green, red } from "../colors/mod.ts"; -import { assertEqual as prettyAssertEqual } from "./pretty.ts"; -import { - assert as assertImport, - equal as AssertEqual, - assertStrictEq, - assertStrContains, - assertMatch, - fail, - assertThrows, - assertThrowsAsync -} from "./asserts.ts"; - -export function equal(c: unknown, d: unknown): boolean { - const seen = new Map(); - return (function compare(a: unknown, b: unknown) { - if (Object.is(a, b)) { - return true; - } - if (a && typeof a === "object" && b && typeof b === "object") { - if (seen.get(a) === b) { - return true; - } - if (Object.keys(a || {}).length !== Object.keys(b || {}).length) { - return false; - } - const merged = { ...a, ...b }; - for (const key in merged) { - type Key = keyof typeof merged; - if (!compare(a && a[key as Key], b && b[key as Key])) { - return false; - } - } - seen.set(a, b); - return true; - } - return false; - })(c, d); -} - -const assertions = { - assert: assertImport, - equal: AssertEqual, - strictEqual: assertStrictEq, - assertStrContains: assertStrContains, - assertMatch: assertMatch, - fail: fail, - throws: assertThrows, - throwsAsync: assertThrowsAsync -}; - -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; - -/** - * Alias to pretty.assertEqual - * @deprecated - */ -export const assertEqual = prettyAssertEqual; export type TestFunction = () => void | Promise; diff --git a/testing/pretty.ts b/testing/pretty.ts index 05fd86b6f..fb2ae122e 100644 --- a/testing/pretty.ts +++ b/testing/pretty.ts @@ -1,6 +1,6 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -import { equal } from "./mod.ts"; +import { equal } from "./asserts.ts"; import { red, green, white, gray, bold } from "../colors/mod.ts"; import diff, { DiffType, DiffResult } from "./diff.ts"; import { format } from "./format.ts"; @@ -55,7 +55,7 @@ function buildMessage(diffResult: ReadonlyArray>): string[] { return messages; } -export function assertEqual( +export function assertEq( actual: unknown, expected: unknown, msg?: string diff --git a/testing/pretty_test.ts b/testing/pretty_test.ts index 89b35c088..0262bfcfd 100644 --- a/testing/pretty_test.ts +++ b/testing/pretty_test.ts @@ -1,8 +1,9 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -import { test, assert } from "./mod.ts"; +import { test } from "./mod.ts"; import { red, green, white, gray, bold } from "../colors/mod.ts"; -import { assertEqual } from "./pretty.ts"; +import { assertEq } from "./pretty.ts"; +import { assertThrows } from "./asserts.ts"; const createHeader = (): string[] => [ "", @@ -18,19 +19,19 @@ const removed: (s: string) => string = (s: string): string => red(bold(s)); test({ name: "pass case", fn() { - assertEqual({ a: 10 }, { a: 10 }); - assertEqual(true, true); - assertEqual(10, 10); - assertEqual("abc", "abc"); - assertEqual({ a: 10, b: { c: "1" } }, { a: 10, b: { c: "1" } }); + assertEq({ a: 10 }, { a: 10 }); + assertEq(true, true); + assertEq(10, 10); + assertEq("abc", "abc"); + assertEq({ a: 10, b: { c: "1" } }, { a: 10, b: { c: "1" } }); } }); test({ name: "failed with number", fn() { - assert.throws( - () => assertEqual(1, 2), + assertThrows( + () => assertEq(1, 2), Error, [...createHeader(), removed(`- 1`), added(`+ 2`), ""].join("\n") ); @@ -40,8 +41,8 @@ test({ test({ name: "failed with number vs string", fn() { - assert.throws( - () => assertEqual(1, "1"), + assertThrows( + () => assertEq(1, "1"), Error, [...createHeader(), removed(`- 1`), added(`+ "1"`)].join("\n") ); @@ -51,8 +52,8 @@ test({ test({ name: "failed with array", fn() { - assert.throws( - () => assertEqual([1, "2", 3], ["1", "2", 3]), + assertThrows( + () => assertEq([1, "2", 3], ["1", "2", 3]), Error, [ ...createHeader(), @@ -71,8 +72,8 @@ test({ test({ name: "failed with object", fn() { - assert.throws( - () => assertEqual({ a: 1, b: "2", c: 3 }, { a: 1, b: 2, c: [3] }), + assertThrows( + () => assertEq({ a: 1, b: "2", c: 3 }, { a: 1, b: 2, c: [3] }), Error, [ ...createHeader(), diff --git a/testing/test.ts b/testing/test.ts index bdebce431..e3fc5c431 100644 --- a/testing/test.ts +++ b/testing/test.ts @@ -1,45 +1,23 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -import { test, assert, assertEqual, equal, runIfMain } from "./mod.ts"; -import { assertEqual as prettyAssertEqual } from "./pretty.ts"; +import { test, runIfMain } from "./mod.ts"; +import { + assert, + assertEq, + assertStrictEq, + assertThrows, + assertThrowsAsync, + fail +} from "../testing/asserts.ts"; import "./format_test.ts"; import "./diff_test.ts"; import "./pretty_test.ts"; import "./asserts_test.ts"; -test(function testingEqual() { - assert(equal("world", "world")); - assert(!equal("hello", "world")); - assert(equal(5, 5)); - assert(!equal(5, 6)); - assert(equal(NaN, NaN)); - assert(equal({ hello: "world" }, { hello: "world" })); - assert(!equal({ world: "hello" }, { hello: "world" })); - assert( - equal( - { hello: "world", hi: { there: "everyone" } }, - { hello: "world", hi: { there: "everyone" } } - ) - ); - assert( - !equal( - { hello: "world", hi: { there: "everyone" } }, - { hello: "world", hi: { there: "everyone else" } } - ) - ); -}); - -test(function testingAssertEqual() { - const a = Object.create(null); - a.b = "foo"; - assert.equal(a, a); - assert(assertEqual === prettyAssertEqual); -}); - test(function testingAssertFail() { - assert.throws(assert.fail, Error, "Failed assertion."); - assert.throws( + assertThrows(fail, Error, "Failed assertion."); + assertThrows( () => { - assert.fail("foo"); + fail("foo"); }, Error, "Failed assertion: foo" @@ -50,7 +28,7 @@ test(function testingAssertEqualActualUncoercable() { let didThrow = false; const a = Object.create(null); try { - assert.equal(a, "bar"); + assertEq(a, "bar"); } catch (e) { didThrow = true; } @@ -61,7 +39,7 @@ test(function testingAssertEqualExpectedUncoercable() { let didThrow = false; const a = Object.create(null); try { - assert.equal("bar", a); + assertStrictEq("bar", a); } catch (e) { didThrow = true; } @@ -71,7 +49,7 @@ test(function testingAssertEqualExpectedUncoercable() { test(function testingAssertStrictEqual() { const a = {}; const b = a; - assert.strictEqual(a, b); + assertStrictEq(a, b); }); test(function testingAssertNotStrictEqual() { @@ -79,7 +57,7 @@ test(function testingAssertNotStrictEqual() { const a = {}; const b = {}; try { - assert.strictEqual(a, b); + assertStrictEq(a, b); } catch (e) { assert(e.message === "actual: [object Object] expected: [object Object]"); didThrow = true; @@ -89,7 +67,7 @@ test(function testingAssertNotStrictEqual() { test(function testingDoesThrow() { let count = 0; - assert.throws(() => { + assertThrows(() => { count++; throw new Error(); }); @@ -100,7 +78,7 @@ test(function testingDoesNotThrow() { let count = 0; let didThrow = false; try { - assert.throws(() => { + assertThrows(() => { count++; console.log("Hello world"); }); @@ -114,7 +92,7 @@ test(function testingDoesNotThrow() { test(function testingThrowsErrorType() { let count = 0; - assert.throws(() => { + assertThrows(() => { count++; throw new TypeError(); }, TypeError); @@ -125,7 +103,7 @@ test(function testingThrowsNotErrorType() { let count = 0; let didThrow = false; try { - assert.throws(() => { + assertThrows(() => { count++; throw new TypeError(); }, RangeError); @@ -139,7 +117,7 @@ test(function testingThrowsNotErrorType() { test(function testingThrowsMsgIncludes() { let count = 0; - assert.throws( + assertThrows( () => { count++; throw new TypeError("Hello world!"); @@ -154,7 +132,7 @@ test(function testingThrowsMsgNotIncludes() { let count = 0; let didThrow = false; try { - assert.throws( + assertThrows( () => { count++; throw new TypeError("Hello world!"); @@ -175,7 +153,7 @@ test(function testingThrowsMsgNotIncludes() { test(async function testingDoesThrowAsync() { let count = 0; - await assert.throwsAsync(async () => { + await assertThrowsAsync(async () => { count++; throw new Error(); }); @@ -184,7 +162,7 @@ test(async function testingDoesThrowAsync() { test(async function testingDoesReject() { let count = 0; - await assert.throwsAsync(() => { + await assertThrowsAsync(() => { count++; return Promise.reject(new Error()); }); @@ -195,7 +173,7 @@ test(async function testingDoesNotThrowAsync() { let count = 0; let didThrow = false; try { - await assert.throwsAsync(async () => { + await assertThrowsAsync(async () => { count++; console.log("Hello world"); }); @@ -211,7 +189,7 @@ test(async function testingDoesNotRejectAsync() { let count = 0; let didThrow = false; try { - await assert.throwsAsync(() => { + await assertThrowsAsync(() => { count++; console.log("Hello world"); return Promise.resolve(); @@ -226,7 +204,7 @@ test(async function testingDoesNotRejectAsync() { test(async function testingThrowsAsyncErrorType() { let count = 0; - await assert.throwsAsync(async () => { + await assertThrowsAsync(async () => { count++; throw new TypeError(); }, TypeError); @@ -237,7 +215,7 @@ test(async function testingThrowsAsyncNotErrorType() { let count = 0; let didThrow = false; try { - await assert.throwsAsync(async () => { + await assertThrowsAsync(async () => { count++; throw new TypeError(); }, RangeError); @@ -251,7 +229,7 @@ test(async function testingThrowsAsyncNotErrorType() { test(async function testingThrowsAsyncMsgIncludes() { let count = 0; - await assert.throwsAsync( + await assertThrowsAsync( async () => { count++; throw new TypeError("Hello world!"); @@ -266,7 +244,7 @@ test(async function testingThrowsAsyncMsgNotIncludes() { let count = 0; let didThrow = false; try { - await assert.throwsAsync( + await assertThrowsAsync( async () => { count++; throw new TypeError("Hello world!"); -- cgit v1.2.3