-
Notifications
You must be signed in to change notification settings - Fork 632
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(assert): add inequality asserts (#3496)
- Loading branch information
Showing
9 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters