Skip to content

Commit

Permalink
feat(assert): add inequality asserts (#3496)
Browse files Browse the repository at this point in the history
  • Loading branch information
ratson authored Aug 30, 2023
1 parent ff19248 commit 10f1323
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 0 deletions.
15 changes: 15 additions & 0 deletions assert/assert_greater.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 { 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 changes: 17 additions & 0 deletions assert/assert_greater_or_equal.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 9 additions & 0 deletions assert/assert_greater_or_equal_test.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 10 additions & 0 deletions assert/assert_greater_test.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 15 additions & 0 deletions assert/assert_less.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 { 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 changes: 17 additions & 0 deletions assert/assert_less_or_equal.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 9 additions & 0 deletions assert/assert_less_or_equal_test.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 10 additions & 0 deletions assert/assert_less_test.ts
Original file line number Diff line number Diff line change
@@ -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));
});
4 changes: 4 additions & 0 deletions assert/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ export * from "./assert_array_includes.ts";
export * from "./assert_equals.ts";
export * from "./assert_exists.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_is_error.ts";
export * from "./assert_less_or_equal.ts";
export * from "./assert_less.ts";
export * from "./assert_match.ts";
export * from "./assert_not_equals.ts";
export * from "./assert_not_instance_of.ts";
Expand Down

0 comments on commit 10f1323

Please sign in to comment.