Skip to content
Permalink
Browse files

Add assertNotEquals, assertArrayContains (denoland#246)

  • Loading branch information...
zekth authored and ry committed Mar 7, 2019
1 parent c677516 commit 899ab67cea12de812c041bcb5ea9ae7fd300d706
Showing with 122 additions and 1 deletion.
  1. +75 −0 testing/asserts.ts
  2. +47 −1 testing/asserts_test.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
@@ -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() {

0 comments on commit 899ab67

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.