1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-23 15:16:54 -05:00

Add assertNotEquals, assertArrayContains (denoland/deno_std#246)

Original: 899ab67cea
This commit is contained in:
Vincent LE GOFF 2019-03-07 15:08:19 +01:00 committed by Ryan Dahl
parent 31f684e71b
commit fd74b38d36
2 changed files with 122 additions and 1 deletions

View file

@ -52,6 +52,42 @@ export function assertEquals(
prettyAssertEqual(actual, expected, msg); 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 * Make an assertion that `actual` and `expected` are strictly equal. If
* not then throw. * 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 * Make an assertion that `actual` match RegExp `expected`. If not
* then thrown * then thrown

View file

@ -1,6 +1,14 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. // 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 { test } from "./mod.ts";
// import { assertEquals as prettyAssertEqual } from "./pretty.ts"; // import { assertEquals as prettyAssertEqual } from "./pretty.ts";
// import "./format_test.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() { test(function testingAssertStringContains() {
assertStrContains("Denosaurus", "saur"); assertStrContains("Denosaurus", "saur");
assertStrContains("Denosaurus", "Deno"); assertStrContains("Denosaurus", "Deno");
assertStrContains("Denosaurus", "rus"); 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() { test(function testingAssertStringContainsThrow() {