diff options
| author | Chris Knight <cknight1234@gmail.com> | 2020-06-07 14:20:33 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-06-07 09:20:33 -0400 |
| commit | 3ef94c5473ac77366d009c7a7c665f695670f886 (patch) | |
| tree | 0baa502ca705e9e0a38e013def65f1ab545e643b /std/_util | |
| parent | adffbacfe49aac480fc34e78035200ab9602443c (diff) | |
refactor(std): remove testing dependencies from non-test code (#5838)
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>, |
