mirror of
https://github.com/denoland/std.git
synced 2024-11-22 04:59:05 +00:00
feat(assert): add inequality asserts (#3496)
This commit is contained in:
parent
ff192484b4
commit
10f1323ef7
15
assert/assert_greater.ts
Normal file
15
assert/assert_greater.ts
Normal 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}`);
|
||||||
|
}
|
17
assert/assert_greater_or_equal.ts
Normal file
17
assert/assert_greater_or_equal.ts
Normal 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}`,
|
||||||
|
);
|
||||||
|
}
|
9
assert/assert_greater_or_equal_test.ts
Normal file
9
assert/assert_greater_or_equal_test.ts
Normal 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));
|
||||||
|
});
|
10
assert/assert_greater_test.ts
Normal file
10
assert/assert_greater_test.ts
Normal 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
15
assert/assert_less.ts
Normal 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}`);
|
||||||
|
}
|
17
assert/assert_less_or_equal.ts
Normal file
17
assert/assert_less_or_equal.ts
Normal 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}`,
|
||||||
|
);
|
||||||
|
}
|
9
assert/assert_less_or_equal_test.ts
Normal file
9
assert/assert_less_or_equal_test.ts
Normal 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));
|
||||||
|
});
|
10
assert/assert_less_test.ts
Normal file
10
assert/assert_less_test.ts
Normal 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));
|
||||||
|
});
|
@ -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";
|
||||||
|
Loading…
Reference in New Issue
Block a user