summaryrefslogtreecommitdiff
path: root/testing/asserts.ts
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 /testing/asserts.ts
parent31f684e71b5c5b0b22e57b0e8bdc50356f8eea09 (diff)
Add assertNotEquals, assertArrayContains (denoland/deno_std#246)
Original: https://github.com/denoland/deno_std/commit/899ab67cea12de812c041bcb5ea9ae7fd300d706
Diffstat (limited to 'testing/asserts.ts')
-rw-r--r--testing/asserts.ts75
1 files changed, 75 insertions, 0 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
*/