diff --git a/testing/asserts.ts b/testing/asserts.ts index 883632b1eb..233219b6a7 100644 --- a/testing/asserts.ts +++ b/testing/asserts.ts @@ -52,6 +52,42 @@ export function assertEquals( prettyAssertEqual(actual, expected, msg); } +/** + * Make an assertion that `actual` and `expected` are not equal, deeply. + * If not then throw. + */ +export function assertNotEquals( + actual: unknown, + expected: unknown, + msg?: string +): void { + if (!equal(actual, expected)) { + return; + } + 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( + "Not Equals 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. @@ -110,6 +146,45 @@ export function assertStrContains( } } +/** + * Make an assertion that `actual` contains the `expected` values + * If not then thrown. + */ +export function assertArrayContains( + actual: unknown[], + expected: unknown[], + msg?: string +) { + let missing = []; + for (let i = 0; i < expected.length; i++) { + let found = false; + for (let j = 0; j < actual.length; j++) { + if (equal(expected[i], actual[j])) { + found = true; + break; + } + } + if (!found) { + missing.push(expected[i]); + } + } + if (missing.length === 0) { + return; + } + console.error( + "assertArrayContains failed. actual=", + actual, + "not containing ", + expected + ); + if (!msg) { + msg = `actual: "${actual}" expected to contains: "${expected}"`; + msg += "\n"; + msg += `missing: ${missing}`; + } + throw new Error(msg); +} + /** * Make an assertion that `actual` match RegExp `expected`. If not * then thrown diff --git a/testing/asserts_test.ts b/testing/asserts_test.ts index fb26d9cd2a..6227543e50 100644 --- a/testing/asserts_test.ts +++ b/testing/asserts_test.ts @@ -1,6 +1,14 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -import { assert, equal, assertStrContains, assertMatch } from "./asserts.ts"; +import { + assert, + equal, + assertNotEquals, + assertStrContains, + assertArrayContains, + assertMatch, + assertEquals +} from "./asserts.ts"; import { test } from "./mod.ts"; // import { assertEquals as prettyAssertEqual } from "./pretty.ts"; // import "./format_test.ts"; @@ -29,10 +37,48 @@ test(function testingEqual() { ); }); +test(function testingNotEquals() { + const a = { foo: "bar" }; + const b = { bar: "foo" }; + assertNotEquals(a, b); + assertNotEquals("Denosaurus", "Tyrannosaurus"); + let didThrow; + try { + assertNotEquals("Raptor", "Raptor"); + didThrow = false; + } catch (e) { + didThrow = true; + } + assertEquals(didThrow, true); +}); + test(function testingAssertStringContains() { assertStrContains("Denosaurus", "saur"); assertStrContains("Denosaurus", "Deno"); assertStrContains("Denosaurus", "rus"); + let didThrow; + try { + assertStrContains("Denosaurus", "Raptor"); + didThrow = false; + } catch (e) { + didThrow = true; + } + assertEquals(didThrow, true); +}); + +test(function testingArrayContains() { + const fixture = ["deno", "iz", "luv"]; + const fixtureObject = [{ deno: "luv" }, { deno: "Js" }]; + assertArrayContains(fixture, ["deno"]); + assertArrayContains(fixtureObject, [{ deno: "luv" }]); + let didThrow; + try { + assertArrayContains(fixtureObject, [{ deno: "node" }]); + didThrow = false; + } catch (e) { + didThrow = true; + } + assertEquals(didThrow, true); }); test(function testingAssertStringContainsThrow() {