Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add std/expect #3814

Merged
merged 17 commits into from
Nov 21, 2023
Next Next commit
feat: add expect syntax by wrapping assert syntax
Co-authored-by: Thomas Cruveilher <[email protected]>
kt3k and Sorikairox committed Nov 16, 2023
commit 6de4de6a1810953d2f5a74a3f3dc4c7e3ac84435
2 changes: 1 addition & 1 deletion assert/assert_instance_of.ts
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
import { AssertionError } from "./assertion_error.ts";

// deno-lint-ignore no-explicit-any
type AnyConstructor = new (...args: any[]) => any;
export type AnyConstructor = new (...args: any[]) => any;
type GetConstructorType<T extends AnyConstructor> = T extends // deno-lint-ignore no-explicit-any
new (...args: any) => infer C ? C
: never;
2 changes: 2 additions & 0 deletions testing/_matchers/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
export { toEqual } from "./to_equal.ts";
22 changes: 22 additions & 0 deletions testing/_matchers/to__strict_equal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

import { MatcherContext, MatchResult } from "../_types.ts";
import {
assertNotStrictEquals,
assertStrictEquals,
} from "../../testing/asserts.ts";

/* Similar to assertStrictEquals */
export function toStrictEqual(
context: MatcherContext,
expected: unknown,
): MatchResult {
if (context.isNot) {
return assertNotStrictEquals(
context.value,
expected,
context.customMessage,
);
}
return assertStrictEquals(context.value, expected, context.customMessage);
}
15 changes: 15 additions & 0 deletions testing/_matchers/to_be.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

import { MatcherContext, MatchResult } from "../_types.ts";
import {
assertNotStrictEquals,
assertStrictEquals,
} from "../../testing/asserts.ts";

/* Similar to assertStrictEquals and assertNotStrictEquals*/
export function toBe(context: MatcherContext, expect: unknown): MatchResult {
if (context.isNot) {
return assertNotStrictEquals(context.value, expect, context.customMessage);
}
return assertStrictEquals(context.value, expect, context.customMessage);
}
34 changes: 34 additions & 0 deletions testing/_matchers/to_be_close_to.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

import { MatcherContext, MatchResult } from "../_types.ts";
import { assertAlmostEquals } from "../../assert/assert_almost_equals.ts";
import { AssertionError } from "../../assert/assertion_error.ts";

/* Similar to assertStrictEquals(value, undefined) and assertNotStrictEquals(value, undefined)*/
export function toBeCloseTo(
context: MatcherContext,
expected: number,
tolerance = 1e-7,
): MatchResult {
if (context.isNot) {
const actual = Number(context.value);
const delta = Math.abs(expected - actual);
if (delta > tolerance) {
return;
}
const msgSuffix = context.customMessage
? `: ${context.customMessage}`
: ".";
const f = (n: number) => Number.isInteger(n) ? n : n.toExponential();
throw new AssertionError(
`Expected actual: "${f(actual)}" to NOT be close to "${f(expected)}": \
delta "${f(delta)}" is greater than "${f(tolerance)}"${msgSuffix}`,
);
}
return assertAlmostEquals(
Number(context.value),
expected,
tolerance,
context.customMessage,
);
}
15 changes: 15 additions & 0 deletions testing/_matchers/to_be_defined.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

import { MatcherContext, MatchResult } from "../_types.ts";
import {
assertNotStrictEquals,
assertStrictEquals,
} from "../../testing/asserts.ts";

/* Similar to assertStrictEquals(value, undefined) and assertNotStrictEquals(value, undefined)*/
export function toBeDefined(context: MatcherContext): MatchResult {
if (context.isNot) {
return assertStrictEquals(context.value, undefined, context.customMessage);
}
return assertNotStrictEquals(context.value, undefined, context.customMessage);
}
23 changes: 23 additions & 0 deletions testing/_matchers/to_be_falsy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

import { MatcherContext, MatchResult } from "../_types.ts";
import { AssertionError } from "../../assert/assertion_error.ts";

/* Similar to assertEqual(!!value) */
export function toBeTruthy(
context: MatcherContext,
): MatchResult {
const isFalsy = !(context.value);
if (context.isNot) {
if (isFalsy) {
throw new AssertionError(
`Expected ${context.value} to NOT be falsy`,
);
}
}
if (!isFalsy) {
throw new AssertionError(
`Expected ${context.value} to be falsy`,
);
}
}
24 changes: 24 additions & 0 deletions testing/_matchers/to_be_greater.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

import { MatcherContext, MatchResult } from "../_types.ts";
import { AssertionError } from "../../assert/assertion_error.ts";

/* Similar to assertEqual */
export function toBeGreaterThan(
context: MatcherContext,
expected: number,
): MatchResult {
const isGreater = Number(context.value) > Number(expected);
if (context.isNot) {
if (isGreater) {
throw new AssertionError(
`Expected ${context.value} to NOT be greater than ${expected}`,
);
}
}
if (!isGreater) {
throw new AssertionError(
`Expected ${context.value} to be greater than ${expected}`,
);
}
}
24 changes: 24 additions & 0 deletions testing/_matchers/to_be_greater_or_equal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

import { MatcherContext, MatchResult } from "../_types.ts";
import { AssertionError } from "../../assert/assertion_error.ts";

/* Similar to assertEqual */
export function toBeGreaterThanOrEqual(
context: MatcherContext,
expected: number,
): MatchResult {
const isGreaterOrEqual = Number(context.value) >= Number(expected);
if (context.isNot) {
if (isGreaterOrEqual) {
throw new AssertionError(
`Expected ${context.value} to NOT be greater than or equal ${expected}`,
);
}
}
if (!isGreaterOrEqual) {
throw new AssertionError(
`Expected ${context.value} to be greater than or equal ${expected}`,
);
}
}
19 changes: 19 additions & 0 deletions testing/_matchers/to_be_instance_of.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

import { MatcherContext, MatchResult } from "../_types.ts";
import {
assertInstanceOf,
assertNotInstanceOf,
} from "../../testing/asserts.ts";
import { AnyConstructor } from "../../assert/assert_instance_of.ts";

/* Similar to assertInstanceOf(value, null) and assertNotInstanceOf(value, null)*/
export function toBeInstanceOf<T extends AnyConstructor>(
context: MatcherContext,
expected: T,
): MatchResult {
if (context.isNot) {
return assertNotInstanceOf(context.value, expected);
}
return assertInstanceOf(context.value, expected);
}
24 changes: 24 additions & 0 deletions testing/_matchers/to_be_less.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

import { MatcherContext, MatchResult } from "../_types.ts";
import { AssertionError } from "../../assert/assertion_error.ts";

/* Similar to assertEqual */
export function toBeLowerThan(
context: MatcherContext,
expected: number,
): MatchResult {
const isLower = Number(context.value) < Number(expected);
if (context.isNot) {
if (isLower) {
throw new AssertionError(
`Expected ${context.value} to NOT be lower than ${expected}`,
);
}
}
if (!isLower) {
throw new AssertionError(
`Expected ${context.value} to be lower than ${expected}`,
);
}
}
24 changes: 24 additions & 0 deletions testing/_matchers/to_be_less_or_equal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

import { MatcherContext, MatchResult } from "../_types.ts";
import { AssertionError } from "../../assert/assertion_error.ts";

/* Similar to assertEqual */
export function toBeLessThanOrEqual(
context: MatcherContext,
expected: number,
): MatchResult {
const isLower = Number(context.value) <= Number(expected);
if (context.isNot) {
if (isLower) {
throw new AssertionError(
`Expected ${context.value} to NOT be lower than or equal ${expected}`,
);
}
}
if (!isLower) {
throw new AssertionError(
`Expected ${context.value} to be lower than or equal ${expected}`,
);
}
}
20 changes: 20 additions & 0 deletions testing/_matchers/to_be_nan.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

import { MatcherContext, MatchResult } from "../_types.ts";
import { assertEquals, assertNotEquals } from "../../testing/asserts.ts";

/* Similar to assertStrictEquals(isNaN(context.value as number), true) and assertNotStrictEquals(isNaN(context.value as number), true)*/
export function toBeNan(context: MatcherContext): MatchResult {
if (context.isNot) {
return assertNotEquals(
isNaN(Number(context.value)),
true,
context.customMessage || `Expected ${context.value} to not be NaN`,
);
}
return assertEquals(
isNaN(Number(context.value)),
true,
context.customMessage || `Expected ${context.value} to be NaN`,
);
}
23 changes: 23 additions & 0 deletions testing/_matchers/to_be_null.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

import { MatcherContext, MatchResult } from "../_types.ts";
import {
assertNotStrictEquals,
assertStrictEquals,
} from "../../testing/asserts.ts";

/* Similar to assertStrictEquals(value, null) and assertNotStrictEquals(value, null)*/
export function toBeNull(context: MatcherContext): MatchResult {
if (context.isNot) {
return assertNotStrictEquals(
context.value as number,
null,
context.customMessage || `Expected ${context.value} to not be null`,
);
}
return assertStrictEquals(
context.value as number,
null,
context.customMessage || `Expected ${context.value} to be null`,
);
}
23 changes: 23 additions & 0 deletions testing/_matchers/to_be_truthy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

import { MatcherContext, MatchResult } from "../_types.ts";
import { AssertionError } from "../../assert/assertion_error.ts";

/* Similar to assertEqual(!!value) */
export function toBeTruthy(
context: MatcherContext,
): MatchResult {
const isTruthy = !!(context.value);
if (context.isNot) {
if (isTruthy) {
throw new AssertionError(
`Expected ${context.value} to NOT be truthy`,
);
}
}
if (!isTruthy) {
throw new AssertionError(
`Expected ${context.value} to be truthy`,
);
}
}
19 changes: 19 additions & 0 deletions testing/_matchers/to_be_undefined.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

import { MatcherContext, MatchResult } from "../_types.ts";
import {
assertNotStrictEquals,
assertStrictEquals,
} from "../../testing/asserts.ts";

/* Similar to assertStrictEquals(value, undefined) and assertNotStrictEquals(value, undefined)*/
export function toBeUndefined(context: MatcherContext): MatchResult {
if (context.isNot) {
return assertNotStrictEquals(
context.value,
undefined,
context.customMessage,
);
}
return assertStrictEquals(context.value, undefined, context.customMessage);
}
16 changes: 16 additions & 0 deletions testing/_matchers/to_equal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

import { MatcherContext, MatchResult } from "../_types.ts";
import { assertNotEquals } from "../../assert/assert_not_equals.ts";
import { assertEquals } from "../../assert/assert_equals.ts";

/* Similar to assertEqual */
export function toEqual(
context: MatcherContext,
expected: unknown,
): MatchResult {
if (context.isNot) {
return assertNotEquals(context.value, expected, context.customMessage);
}
return assertEquals(context.value, expected, context.customMessage);
}
Loading