rename(std/testing): rename assert*Contains to assert*Includes (#7951)

This commit renames two assertion functions to better align with JS API:
- assertStringContains -> assertStringIncludes
- assertArrayContains -> assertArrayIncludes
This commit is contained in:
Tim Reichen 2020-10-26 16:03:30 +01:00 committed by GitHub
parent 305a9c04ba
commit ae86cbb551
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 60 additions and 60 deletions

View file

@ -11,7 +11,7 @@
import { import {
assert, assert,
assertEquals, assertEquals,
assertStringContains, assertStringIncludes,
unitTest, unitTest,
} from "./test_util.ts"; } from "./test_util.ts";
import { stripColor } from "../../../std/fmt/colors.ts"; import { stripColor } from "../../../std/fmt/colors.ts";
@ -1703,5 +1703,5 @@ unitTest(function inspectProxy(): void {
unitTest(function inspectColors(): void { unitTest(function inspectColors(): void {
assertEquals(Deno.inspect(1), "1"); assertEquals(Deno.inspect(1), "1");
assertStringContains(Deno.inspect(1, { colors: true }), "\x1b["); assertStringIncludes(Deno.inspect(1, { colors: true }), "\x1b[");
}); });

View file

@ -2,7 +2,7 @@
import { import {
assert, assert,
assertEquals, assertEquals,
assertStringContains, assertStringIncludes,
unitTest, unitTest,
} from "./test_util.ts"; } from "./test_util.ts";
@ -155,7 +155,7 @@ unitTest(function formDataParamsArgumentsCheck(): void {
} }
} }
assertEquals(hasThrown, 2); assertEquals(hasThrown, 2);
assertStringContains( assertStringIncludes(
errMsg, errMsg,
`${method} requires at least 1 argument, but only 0 present`, `${method} requires at least 1 argument, but only 0 present`,
); );
@ -179,7 +179,7 @@ unitTest(function formDataParamsArgumentsCheck(): void {
} }
} }
assertEquals(hasThrown, 2); assertEquals(hasThrown, 2);
assertStringContains( assertStringIncludes(
errMsg, errMsg,
`${method} requires at least 2 arguments, but only 0 present`, `${method} requires at least 2 arguments, but only 0 present`,
); );
@ -199,7 +199,7 @@ unitTest(function formDataParamsArgumentsCheck(): void {
} }
} }
assertEquals(hasThrown, 2); assertEquals(hasThrown, 2);
assertStringContains( assertStringIncludes(
errMsg, errMsg,
`${method} requires at least 2 arguments, but only 1 present`, `${method} requires at least 2 arguments, but only 1 present`,
); );

View file

@ -2,7 +2,7 @@
import { import {
assert, assert,
assertEquals, assertEquals,
assertStringContains, assertStringIncludes,
unitTest, unitTest,
} from "./test_util.ts"; } from "./test_util.ts";
const { const {
@ -285,7 +285,7 @@ unitTest(function headerParamsArgumentsCheck(): void {
} }
} }
assertEquals(hasThrown, 2); assertEquals(hasThrown, 2);
assertStringContains( assertStringIncludes(
errMsg, errMsg,
`${method} requires at least 1 argument, but only 0 present`, `${method} requires at least 1 argument, but only 0 present`,
); );
@ -309,7 +309,7 @@ unitTest(function headerParamsArgumentsCheck(): void {
} }
} }
assertEquals(hasThrown, 2); assertEquals(hasThrown, 2);
assertStringContains( assertStringIncludes(
errMsg, errMsg,
`${method} requires at least 2 arguments, but only 0 present`, `${method} requires at least 2 arguments, but only 0 present`,
); );
@ -329,7 +329,7 @@ unitTest(function headerParamsArgumentsCheck(): void {
} }
} }
assertEquals(hasThrown, 2); assertEquals(hasThrown, 2);
assertStringContains( assertStringIncludes(
errMsg, errMsg,
`${method} requires at least 2 arguments, but only 1 present`, `${method} requires at least 2 arguments, but only 1 present`,
); );

View file

@ -2,7 +2,7 @@
import { import {
assert, assert,
assertEquals, assertEquals,
assertStringContains, assertStringIncludes,
assertThrows, assertThrows,
unitTest, unitTest,
} from "./test_util.ts"; } from "./test_util.ts";
@ -310,8 +310,8 @@ unitTest(
const decoder = new TextDecoder(); const decoder = new TextDecoder();
const text = decoder.decode(fileContents); const text = decoder.decode(fileContents);
assertStringContains(text, "error"); assertStringIncludes(text, "error");
assertStringContains(text, "output"); assertStringIncludes(text, "output");
}, },
); );

View file

@ -10,7 +10,7 @@ export {
assertMatch, assertMatch,
assertNotEquals, assertNotEquals,
assertStrictEquals, assertStrictEquals,
assertStringContains, assertStringIncludes,
assertThrows, assertThrows,
assertThrowsAsync, assertThrowsAsync,
fail, fail,

View file

@ -32,14 +32,14 @@ https://deno.land/std@$STD_VERSION/testing#usage to make testing easier:
```ts ```ts
import { import {
assertArrayContains, assertArrayIncludes,
assertEquals, assertEquals,
} from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts";
Deno.test("hello world", () => { Deno.test("hello world", () => {
const x = 1 + 2; const x = 1 + 2;
assertEquals(x, 3); assertEquals(x, 3);
assertArrayContains([1, 2, 3, 4, 5, 6], [3], "Expected 3 to be in the array"); assertArrayIncludes([1, 2, 3, 4, 5, 6], [3], "Expected 3 to be in the array");
}); });
``` ```

View file

@ -18,8 +18,8 @@ The assertions module provides 10 assertions:
- `assertEquals(actual: unknown, expected: unknown, msg?: string): void` - `assertEquals(actual: unknown, expected: unknown, msg?: string): void`
- `assertNotEquals(actual: unknown, expected: unknown, msg?: string): void` - `assertNotEquals(actual: unknown, expected: unknown, msg?: string): void`
- `assertStrictEquals(actual: unknown, expected: unknown, msg?: string): void` - `assertStrictEquals(actual: unknown, expected: unknown, msg?: string): void`
- `assertStringContains(actual: string, expected: string, msg?: string): void` - `assertStringIncludes(actual: string, expected: string, msg?: string): void`
- `assertArrayContains(actual: unknown[], expected: unknown[], msg?: string): void` - `assertArrayIncludes(actual: unknown[], expected: unknown[], msg?: string): void`
- `assertMatch(actual: string, expected: RegExp, msg?: string): void` - `assertMatch(actual: string, expected: RegExp, msg?: string): void`
- `assertNotMatch(actual: string, expected: RegExp, msg?: string): void` - `assertNotMatch(actual: string, expected: RegExp, msg?: string): void`
- `assertThrows(fn: () => void, ErrorClass?: Constructor, msgIncludes = "", msg?: string): Error` - `assertThrows(fn: () => void, ErrorClass?: Constructor, msgIncludes = "", msg?: string): Error`
@ -92,25 +92,25 @@ precise check against two primitive types.
### Contains ### Contains
There are two methods available to assert a value contains a value, There are two methods available to assert a value contains a value,
`assertStringContains()` and `assertArrayContains()`. `assertStringIncludes()` and `assertArrayIncludes()`.
The `assertStringContains()` assertion does a simple includes check on a string The `assertStringIncludes()` assertion does a simple includes check on a string
to see if it contains the expected string. to see if it contains the expected string.
```js ```js
Deno.test("Test Assert String Contains", () => { Deno.test("Test Assert String Contains", () => {
assertStringContains("Hello World", "Hello"); assertStringIncludes("Hello World", "Hello");
}); });
``` ```
The `assertArrayContains()` assertion is slightly more advanced and can find The `assertArrayIncludes()` assertion is slightly more advanced and can find
both a value within an array and an array of values within an array. both a value within an array and an array of values within an array.
```js ```js
Deno.test("Test Assert Array Contains", () => { Deno.test("Test Assert Array Contains", () => {
assertArrayContains([1, 2, 3], [1]); assertArrayIncludes([1, 2, 3], [1]);
assertArrayContains([1, 2, 3], [1, 2]); assertArrayIncludes([1, 2, 3], [1, 2]);
assertArrayContains(Array.from("Hello World"), Array.from("Hello")); assertArrayIncludes(Array.from("Hello World"), Array.from("Hello"));
}); });
``` ```

View file

@ -5,7 +5,7 @@ import { decode, encode } from "../encoding/utf8.ts";
import { import {
assert, assert,
assertEquals, assertEquals,
assertStringContains, assertStringIncludes,
} from "../testing/asserts.ts"; } from "../testing/asserts.ts";
import { dirname, fromFileUrl } from "../path/mod.ts"; import { dirname, fromFileUrl } from "../path/mod.ts";
@ -66,6 +66,6 @@ Deno.test("xevalCliSyntaxError", async function (): Promise<void> {
}); });
assertEquals(await p.status(), { code: 1, success: false }); assertEquals(await p.status(), { code: 1, success: false });
assertEquals(decode(await p.output()), ""); assertEquals(decode(await p.output()), "");
assertStringContains(decode(await p.stderrOutput()), "SyntaxError"); assertStringIncludes(decode(await p.stderrOutput()), "SyntaxError");
p.close(); p.close();
}); });

View file

@ -2,7 +2,7 @@
import { import {
assert, assert,
assertEquals, assertEquals,
assertStringContains, assertStringIncludes,
assertThrows, assertThrows,
assertThrowsAsync, assertThrowsAsync,
} from "../testing/asserts.ts"; } from "../testing/asserts.ts";
@ -231,7 +231,7 @@ for (const s of scenes) {
assert(p.stdout); assert(p.stdout);
const output = await p.output(); const output = await p.output();
p.close(); p.close();
assertStringContains(new TextDecoder().decode(output), s.output); assertStringIncludes(new TextDecoder().decode(output), s.output);
} catch (err) { } catch (err) {
await Deno.remove(testfolder, { recursive: true }); await Deno.remove(testfolder, { recursive: true });
throw err; throw err;

View file

@ -1,5 +1,5 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { assertEquals, assertStringContains } from "../testing/asserts.ts"; import { assertEquals, assertStringIncludes } from "../testing/asserts.ts";
import * as path from "../path/mod.ts"; import * as path from "../path/mod.ts";
import { exists, existsSync } from "./exists.ts"; import { exists, existsSync } from "./exists.ts";
@ -130,7 +130,7 @@ for (const s of scenes) {
const output = await p.output(); const output = await p.output();
p.close(); p.close();
assertStringContains(new TextDecoder().decode(output), s.output); assertStringIncludes(new TextDecoder().decode(output), s.output);
}); });
// done // done
} }

View file

@ -3,7 +3,7 @@ import { decode } from "../encoding/utf8.ts";
import { import {
assert, assert,
assertEquals, assertEquals,
assertStringContains, assertStringIncludes,
} from "../testing/asserts.ts"; } from "../testing/asserts.ts";
import { import {
fromFileUrl, fromFileUrl,
@ -126,7 +126,7 @@ Deno.test("expandGlobPermError", async function (): Promise<void> {
}); });
assertEquals(await p.status(), { code: 1, success: false }); assertEquals(await p.status(), { code: 1, success: false });
assertEquals(decode(await p.output()), ""); assertEquals(decode(await p.output()), "");
assertStringContains( assertStringIncludes(
decode(await p.stderrOutput()), decode(await p.stderrOutput()),
"Uncaught PermissionDenied", "Uncaught PermissionDenied",
); );

View file

@ -10,7 +10,7 @@ import {
assert, assert,
assertEquals, assertEquals,
assertMatch, assertMatch,
assertStringContains, assertStringIncludes,
assertThrowsAsync, assertThrowsAsync,
} from "../testing/asserts.ts"; } from "../testing/asserts.ts";
import { import {
@ -498,7 +498,7 @@ Deno.test({
const nread = await conn.read(res); const nread = await conn.read(res);
assert(nread !== null); assert(nread !== null);
const resStr = new TextDecoder().decode(res.subarray(0, nread)); const resStr = new TextDecoder().decode(res.subarray(0, nread));
assertStringContains(resStr, "/hello"); assertStringIncludes(resStr, "/hello");
server.close(); server.close();
await p; await p;
// Client connection should still be open, verify that // Client connection should still be open, verify that

View file

@ -3,7 +3,7 @@
import { import {
assert, assert,
assertEquals, assertEquals,
assertStringContains, assertStringIncludes,
} from "../testing/asserts.ts"; } from "../testing/asserts.ts";
import * as path from "../path/mod.ts"; import * as path from "../path/mod.ts";
@ -67,6 +67,6 @@ Deno.test("requireStack", function () {
try { try {
hello(); hello();
} catch (e) { } catch (e) {
assertStringContains(e.stack, "/tests/cjs/cjs_throw.js"); assertStringIncludes(e.stack, "/tests/cjs/cjs_throw.js");
} }
}); });

View file

@ -18,12 +18,12 @@ pretty-printed diff of failing assertion.
and `expected` are equal. and `expected` are equal.
- `assertStrictEquals()` - Compares `actual` and `expected` strictly, therefore - `assertStrictEquals()` - Compares `actual` and `expected` strictly, therefore
for non-primitives the values must reference the same instance. for non-primitives the values must reference the same instance.
- `assertStringContains()` - Make an assertion that `actual` contains - `assertStringIncludes()` - Make an assertion that `actual` includes
`expected`. `expected`.
- `assertMatch()` - Make an assertion that `actual` match RegExp `expected`. - `assertMatch()` - Make an assertion that `actual` match RegExp `expected`.
- `assertNotMatch()` - Make an assertion that `actual` not match RegExp - `assertNotMatch()` - Make an assertion that `actual` not match RegExp
`expected`. `expected`.
- `assertArrayContains()` - Make an assertion that `actual` array contains the - `assertArrayIncludes()` - Make an assertion that `actual` array includes the
`expected` values. `expected` values.
- `assertObjectMatch()` - Make an assertion that `actual` object match - `assertObjectMatch()` - Make an assertion that `actual` object match
`expected` subset object `expected` subset object

View file

@ -330,10 +330,10 @@ export function assertNotStrictEquals(
} }
/** /**
* Make an assertion that actual contains expected. If not * Make an assertion that actual includes expected. If not
* then thrown. * then thrown.
*/ */
export function assertStringContains( export function assertStringIncludes(
actual: string, actual: string,
expected: string, expected: string,
msg?: string, msg?: string,
@ -347,26 +347,26 @@ export function assertStringContains(
} }
/** /**
* Make an assertion that `actual` contains the `expected` values. * Make an assertion that `actual` includes the `expected` values.
* If not then an error will be thrown. * If not then an error will be thrown.
* *
* Type parameter can be specified to ensure values under comparison have the same type. * Type parameter can be specified to ensure values under comparison have the same type.
* For example: * For example:
*```ts *```ts
*assertArrayContains<number>([1, 2], [2]) *assertArrayIncludes<number>([1, 2], [2])
*``` *```
*/ */
export function assertArrayContains( export function assertArrayIncludes(
actual: ArrayLike<unknown>, actual: ArrayLike<unknown>,
expected: ArrayLike<unknown>, expected: ArrayLike<unknown>,
msg?: string, msg?: string,
): void; ): void;
export function assertArrayContains<T>( export function assertArrayIncludes<T>(
actual: ArrayLike<T>, actual: ArrayLike<T>,
expected: ArrayLike<T>, expected: ArrayLike<T>,
msg?: string, msg?: string,
): void; ): void;
export function assertArrayContains( export function assertArrayIncludes(
actual: ArrayLike<unknown>, actual: ArrayLike<unknown>,
expected: ArrayLike<unknown>, expected: ArrayLike<unknown>,
msg?: string, msg?: string,
@ -388,7 +388,7 @@ export function assertArrayContains(
return; return;
} }
if (!msg) { if (!msg) {
msg = `actual: "${_format(actual)}" expected to contain: "${ msg = `actual: "${_format(actual)}" expected to include: "${
_format(expected) _format(expected)
}"\nmissing: ${_format(missing)}`; }"\nmissing: ${_format(missing)}`;
} }

View file

@ -2,7 +2,7 @@
import { import {
_format, _format,
assert, assert,
assertArrayContains, assertArrayIncludes,
assertEquals, assertEquals,
AssertionError, AssertionError,
assertMatch, assertMatch,
@ -11,7 +11,7 @@ import {
assertNotStrictEquals, assertNotStrictEquals,
assertObjectMatch, assertObjectMatch,
assertStrictEquals, assertStrictEquals,
assertStringContains, assertStringIncludes,
assertThrows, assertThrows,
assertThrowsAsync, assertThrowsAsync,
equal, equal,
@ -161,12 +161,12 @@ Deno.test("testingNotEquals", function (): void {
}); });
Deno.test("testingAssertStringContains", function (): void { Deno.test("testingAssertStringContains", function (): void {
assertStringContains("Denosaurus", "saur"); assertStringIncludes("Denosaurus", "saur");
assertStringContains("Denosaurus", "Deno"); assertStringIncludes("Denosaurus", "Deno");
assertStringContains("Denosaurus", "rus"); assertStringIncludes("Denosaurus", "rus");
let didThrow; let didThrow;
try { try {
assertStringContains("Denosaurus", "Raptor"); assertStringIncludes("Denosaurus", "Raptor");
didThrow = false; didThrow = false;
} catch (e) { } catch (e) {
assert(e instanceof AssertionError); assert(e instanceof AssertionError);
@ -178,14 +178,14 @@ Deno.test("testingAssertStringContains", function (): void {
Deno.test("testingArrayContains", function (): void { Deno.test("testingArrayContains", function (): void {
const fixture = ["deno", "iz", "luv"]; const fixture = ["deno", "iz", "luv"];
const fixtureObject = [{ deno: "luv" }, { deno: "Js" }]; const fixtureObject = [{ deno: "luv" }, { deno: "Js" }];
assertArrayContains(fixture, ["deno"]); assertArrayIncludes(fixture, ["deno"]);
assertArrayContains(fixtureObject, [{ deno: "luv" }]); assertArrayIncludes(fixtureObject, [{ deno: "luv" }]);
assertArrayContains( assertArrayIncludes(
Uint8Array.from([1, 2, 3, 4]), Uint8Array.from([1, 2, 3, 4]),
Uint8Array.from([1, 2, 3]), Uint8Array.from([1, 2, 3]),
); );
assertThrows( assertThrows(
(): void => assertArrayContains(fixtureObject, [{ deno: "node" }]), (): void => assertArrayIncludes(fixtureObject, [{ deno: "node" }]),
AssertionError, AssertionError,
`actual: "[ `actual: "[
{ {
@ -194,7 +194,7 @@ Deno.test("testingArrayContains", function (): void {
{ {
deno: "Js", deno: "Js",
}, },
]" expected to contain: "[ ]" expected to include: "[
{ {
deno: "node", deno: "node",
}, },
@ -210,7 +210,7 @@ missing: [
Deno.test("testingAssertStringContainsThrow", function (): void { Deno.test("testingAssertStringContainsThrow", function (): void {
let didThrow = false; let didThrow = false;
try { try {
assertStringContains("Denosaurus from Jurassic", "Raptor"); assertStringIncludes("Denosaurus from Jurassic", "Raptor");
} catch (e) { } catch (e) {
assert( assert(
e.message === e.message ===
@ -715,7 +715,7 @@ Deno.test({
fn(): void { fn(): void {
assertEquals<string>("hello", "hello"); assertEquals<string>("hello", "hello");
assertNotEquals<number>(1, 2); assertNotEquals<number>(1, 2);
assertArrayContains<boolean>([true, false], [true]); assertArrayIncludes<boolean>([true, false], [true]);
const value = { x: 1 }; const value = { x: 1 };
assertStrictEquals<typeof value>(value, value); assertStrictEquals<typeof value>(value, value);
// eslint-disable-next-line @typescript-eslint/ban-types // eslint-disable-next-line @typescript-eslint/ban-types