feat(assert): add inequality asserts (#3496)

This commit is contained in:
(◕ᴥ◕) 2023-08-30 13:35:32 +08:00 committed by GitHub
parent ff192484b4
commit 10f1323ef7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 106 additions and 0 deletions

15
assert/assert_greater.ts Normal file
View File

@ -0,0 +1,15 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
import { format } from "./_format.ts";
import { AssertionError } from "./assertion_error.ts";
/**
* Make an assertion that `actual` is greater than `expected`.
* If not then throw.
*/
export function assertGreater<T>(actual: T, expected: T, msg?: string) {
if (actual > expected) return;
const actualString = format(actual);
const expectedString = format(expected);
throw new AssertionError(msg ?? `Expect ${actualString} > ${expectedString}`);
}

View File

@ -0,0 +1,17 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
import { format } from "./_format.ts";
import { AssertionError } from "./assertion_error.ts";
/**
* Make an assertion that `actual` is greater than or equal to `expected`.
* If not then throw.
*/
export function assertGreaterOrEqual<T>(actual: T, expected: T, msg?: string) {
if (actual >= expected) return;
const actualString = format(actual);
const expectedString = format(expected);
throw new AssertionError(
msg ?? `Expect ${actualString} >= ${expectedString}`,
);
}

View File

@ -0,0 +1,9 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
import { assertGreaterOrEqual, assertThrows } from "./mod.ts";
Deno.test("assertGreaterOrEqual", () => {
assertGreaterOrEqual(2, 1);
assertGreaterOrEqual(1n, 1n);
assertThrows(() => assertGreaterOrEqual(1, 2));
});

View File

@ -0,0 +1,10 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
import { assertGreater, assertThrows } from "./mod.ts";
Deno.test("assertGreater", () => {
assertGreater(2, 1);
assertGreater(2n, 1n);
assertGreater(1.1, 1);
assertThrows(() => assertGreater(1, 2));
});

15
assert/assert_less.ts Normal file
View File

@ -0,0 +1,15 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
import { format } from "./_format.ts";
import { AssertionError } from "./assertion_error.ts";
/**
* Make an assertion that `actual` is less than `expected`.
* If not then throw.
*/
export function assertLess<T>(actual: T, expected: T, msg?: string) {
if (actual < expected) return;
const actualString = format(actual);
const expectedString = format(expected);
throw new AssertionError(msg ?? `Expect ${actualString} < ${expectedString}`);
}

View File

@ -0,0 +1,17 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
import { format } from "./_format.ts";
import { AssertionError } from "./assertion_error.ts";
/**
* Make an assertion that `actual` is less than or equal to `expected`.
* If not then throw.
*/
export function assertLessOrEqual<T>(actual: T, expected: T, msg?: string) {
if (actual <= expected) return;
const actualString = format(actual);
const expectedString = format(expected);
throw new AssertionError(
msg ?? `Expect ${actualString} <= ${expectedString}`,
);
}

View File

@ -0,0 +1,9 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
import { assertLessOrEqual, assertThrows } from "./mod.ts";
Deno.test("assertLessOrEqual", () => {
assertLessOrEqual(1, 2);
assertLessOrEqual(1n, 1n);
assertThrows(() => assertLessOrEqual(2, 1));
});

View File

@ -0,0 +1,10 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
import { assertLess, assertThrows } from "./mod.ts";
Deno.test("assertLess", () => {
assertLess(1, 2);
assertLess(1n, 2n);
assertLess(1, 1.1);
assertThrows(() => assertLess(2, 1));
});

View File

@ -15,8 +15,12 @@ export * from "./assert_array_includes.ts";
export * from "./assert_equals.ts"; export * from "./assert_equals.ts";
export * from "./assert_exists.ts"; export * from "./assert_exists.ts";
export * from "./assert_false.ts"; export * from "./assert_false.ts";
export * from "./assert_greater_or_equal.ts";
export * from "./assert_greater.ts";
export * from "./assert_instance_of.ts"; export * from "./assert_instance_of.ts";
export * from "./assert_is_error.ts"; export * from "./assert_is_error.ts";
export * from "./assert_less_or_equal.ts";
export * from "./assert_less.ts";
export * from "./assert_match.ts"; export * from "./assert_match.ts";
export * from "./assert_not_equals.ts"; export * from "./assert_not_equals.ts";
export * from "./assert_not_instance_of.ts"; export * from "./assert_not_instance_of.ts";