diff options
Diffstat (limited to 'std/_util')
-rw-r--r-- | std/_util/assert.ts | 15 | ||||
-rw-r--r-- | std/_util/assert_test.ts | 32 | ||||
-rw-r--r-- | std/_util/deep_assign.ts | 2 |
3 files changed, 48 insertions, 1 deletions
diff --git a/std/_util/assert.ts b/std/_util/assert.ts new file mode 100644 index 000000000..d797591fe --- /dev/null +++ b/std/_util/assert.ts @@ -0,0 +1,15 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +export class DenoStdInternalError extends Error { + constructor(message: string) { + super(message); + this.name = "DenoStdInternalError"; + } +} + +/** Make an assertion, if not `true`, then throw. */ +export function assert(expr: unknown, msg = ""): asserts expr { + if (!expr) { + throw new DenoStdInternalError(msg); + } +} diff --git a/std/_util/assert_test.ts b/std/_util/assert_test.ts new file mode 100644 index 000000000..38aeae91b --- /dev/null +++ b/std/_util/assert_test.ts @@ -0,0 +1,32 @@ +import { assert, DenoStdInternalError } from "./assert.ts"; +import { assertThrows } from "../testing/asserts.ts"; + +const { test } = Deno; + +test({ + name: "assert valid scenario", + fn(): void { + assert(true); + }, +}); + +test({ + name: "assert invalid scenario, no message", + fn(): void { + assertThrows(() => { + assert(false); + }, DenoStdInternalError); + }, +}); +test({ + name: "assert invalid scenario, with message", + fn(): void { + assertThrows( + () => { + assert(false, "Oops! Should be true"); + }, + DenoStdInternalError, + "Oops! Should be true" + ); + }, +}); diff --git a/std/_util/deep_assign.ts b/std/_util/deep_assign.ts index 9034d89bd..ca1f0aba1 100644 --- a/std/_util/deep_assign.ts +++ b/std/_util/deep_assign.ts @@ -1,5 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { assert } from "../testing/asserts.ts"; +import { assert } from "../_util/assert.ts"; export function deepAssign( target: Record<string, unknown>, |