summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVincent LE GOFF <g_n_s@hotmail.fr>2019-03-07 15:08:19 +0100
committerRyan Dahl <ry@tinyclouds.org>2019-03-07 09:08:19 -0500
commitfd74b38d361db4a251621d486b69473a4cc13f24 (patch)
tree1897445b1612ef2f28e4cb4f6db8347186805e1b
parent31f684e71b5c5b0b22e57b0e8bdc50356f8eea09 (diff)
Add assertNotEquals, assertArrayContains (denoland/deno_std#246)
Original: https://github.com/denoland/deno_std/commit/899ab67cea12de812c041bcb5ea9ae7fd300d706
-rw-r--r--testing/asserts.ts75
-rw-r--r--testing/asserts_test.ts48
2 files changed, 122 insertions, 1 deletions
diff --git a/testing/asserts.ts b/testing/asserts.ts
index 883632b1e..233219b6a 100644
--- a/testing/asserts.ts
+++ b/testing/asserts.ts
@@ -53,6 +53,42 @@ export function assertEquals(
}
/**
+ * 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.
*/
@@ -111,6 +147,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 fb26d9cd2..6227543e5 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() {