diff --git a/std/_util/assert.ts b/std/_util/assert.ts new file mode 100644 index 0000000000..d797591fe0 --- /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 0000000000..38aeae91b3 --- /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 9034d89bdf..ca1f0aba11 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, diff --git a/std/archive/tar.ts b/std/archive/tar.ts index 225bcb7693..52af7bd694 100644 --- a/std/archive/tar.ts +++ b/std/archive/tar.ts @@ -28,7 +28,7 @@ */ import { MultiReader } from "../io/readers.ts"; import { BufReader } from "../io/bufio.ts"; -import { assert } from "../testing/asserts.ts"; +import { assert } from "../_util/assert.ts"; const recordSize = 512; const ustar = "ustar\u000000"; diff --git a/std/datetime/mod.ts b/std/datetime/mod.ts index 258388397d..7503eccb43 100644 --- a/std/datetime/mod.ts +++ b/std/datetime/mod.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 type DateFormat = "mm-dd-yyyy" | "dd-mm-yyyy" | "yyyy-mm-dd"; diff --git a/std/encoding/csv.ts b/std/encoding/csv.ts index 3e7164cbb9..5af0545300 100644 --- a/std/encoding/csv.ts +++ b/std/encoding/csv.ts @@ -7,7 +7,7 @@ import { BufReader } from "../io/bufio.ts"; import { TextProtoReader } from "../textproto/mod.ts"; import { StringReader } from "../io/readers.ts"; -import { assert } from "../testing/asserts.ts"; +import { assert } from "../_util/assert.ts"; const INVALID_RUNE = ["\r", "\n", '"']; diff --git a/std/encoding/toml.ts b/std/encoding/toml.ts index 7e44bdc18d..8a0715ab68 100644 --- a/std/encoding/toml.ts +++ b/std/encoding/toml.ts @@ -1,6 +1,6 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. import { deepAssign } from "../_util/deep_assign.ts"; -import { assert } from "../testing/asserts.ts"; +import { assert } from "../_util/assert.ts"; class KeyValuePair { constructor(public key: string, public value: unknown) {} diff --git a/std/flags/mod.ts b/std/flags/mod.ts index e3680087de..5c8fcc0d91 100644 --- a/std/flags/mod.ts +++ b/std/flags/mod.ts @@ -1,6 +1,6 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { assert } from "../testing/asserts.ts"; +import { assert } from "../_util/assert.ts"; export interface Args { /** Contains all the arguments that didn't have an option associated with diff --git a/std/fs/copy.ts b/std/fs/copy.ts index d45ac17c95..269340e850 100644 --- a/std/fs/copy.ts +++ b/std/fs/copy.ts @@ -2,7 +2,7 @@ import * as path from "../path/mod.ts"; import { ensureDir, ensureDirSync } from "./ensure_dir.ts"; import { isSubdir, getFileInfoType } from "./_util.ts"; -import { assert } from "../testing/asserts.ts"; +import { assert } from "../_util/assert.ts"; const isWindows = Deno.build.os === "windows"; diff --git a/std/fs/expand_glob.ts b/std/fs/expand_glob.ts index e5abdabcf3..4b7b118854 100644 --- a/std/fs/expand_glob.ts +++ b/std/fs/expand_glob.ts @@ -14,7 +14,7 @@ import { walk, walkSync, } from "./walk.ts"; -import { assert } from "../testing/asserts.ts"; +import { assert } from "../_util/assert.ts"; const { cwd } = Deno; type FileInfo = Deno.FileInfo; diff --git a/std/fs/walk.ts b/std/fs/walk.ts index ccd5097a49..553e52b2ec 100644 --- a/std/fs/walk.ts +++ b/std/fs/walk.ts @@ -1,7 +1,7 @@ // Documentation and interface for walk were adapted from Go // https://golang.org/pkg/path/filepath/#Walk // Copyright 2009 The Go Authors. All rights reserved. BSD license. -import { unimplemented, assert } from "../testing/asserts.ts"; +import { assert } from "../_util/assert.ts"; import { basename, join, normalize } from "../path/mod.ts"; const { readDir, readDirSync, stat, statSync } = Deno; @@ -107,7 +107,7 @@ export async function* walk( if (entry.isSymlink) { if (followSymlinks) { // TODO(ry) Re-enable followSymlinks. - unimplemented(); + throw new Error("unimplemented"); } else { continue; } @@ -159,7 +159,7 @@ export function* walkSync( for (const entry of readDirSync(root)) { if (entry.isSymlink) { if (followSymlinks) { - unimplemented(); + throw new Error("unimplemented"); } else { continue; } diff --git a/std/http/_io.ts b/std/http/_io.ts index 82954cceec..1db0fc2924 100644 --- a/std/http/_io.ts +++ b/std/http/_io.ts @@ -1,6 +1,6 @@ import { BufReader, BufWriter } from "../io/bufio.ts"; import { TextProtoReader } from "../textproto/mod.ts"; -import { assert } from "../testing/asserts.ts"; +import { assert } from "../_util/assert.ts"; import { encoder } from "../encoding/utf8.ts"; import { ServerRequest, Response } from "./server.ts"; import { STATUS_TEXT } from "./http_status.ts"; diff --git a/std/http/cookie.ts b/std/http/cookie.ts index a138eb3505..88a71626c9 100644 --- a/std/http/cookie.ts +++ b/std/http/cookie.ts @@ -2,7 +2,7 @@ // Structured similarly to Go's cookie.go // https://github.com/golang/go/blob/master/src/net/http/cookie.go import { ServerRequest, Response } from "./server.ts"; -import { assert } from "../testing/asserts.ts"; +import { assert } from "../_util/assert.ts"; import { toIMF } from "../datetime/mod.ts"; export interface Cookies { diff --git a/std/http/file_server.ts b/std/http/file_server.ts index 7614c3ab2f..66babfd570 100755 --- a/std/http/file_server.ts +++ b/std/http/file_server.ts @@ -10,7 +10,7 @@ const { args, stat, readDir, open, exit } = Deno; import { posix, extname } from "../path/mod.ts"; import { listenAndServe, ServerRequest, Response } from "./server.ts"; import { parse } from "../flags/mod.ts"; -import { assert } from "../testing/asserts.ts"; +import { assert } from "../_util/assert.ts"; interface EntryInfo { mode: string; diff --git a/std/http/server.ts b/std/http/server.ts index faf5da6af1..3cc95a9e49 100644 --- a/std/http/server.ts +++ b/std/http/server.ts @@ -1,7 +1,7 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. import { encode } from "../encoding/utf8.ts"; import { BufReader, BufWriter } from "../io/bufio.ts"; -import { assert } from "../testing/asserts.ts"; +import { assert } from "../_util/assert.ts"; import { deferred, Deferred, MuxAsyncIterator } from "../async/mod.ts"; import { bodyReader, diff --git a/std/io/bufio.ts b/std/io/bufio.ts index 5c005672a3..4ebec13e1d 100644 --- a/std/io/bufio.ts +++ b/std/io/bufio.ts @@ -7,7 +7,7 @@ type Reader = Deno.Reader; type Writer = Deno.Writer; type WriterSync = Deno.WriterSync; import { charCode, copyBytes } from "./util.ts"; -import { assert } from "../testing/asserts.ts"; +import { assert } from "../_util/assert.ts"; const DEFAULT_BUF_SIZE = 4096; const MIN_BUF_SIZE = 16; diff --git a/std/io/ioutil.ts b/std/io/ioutil.ts index aff9f05e2c..3d9e72b569 100644 --- a/std/io/ioutil.ts +++ b/std/io/ioutil.ts @@ -2,7 +2,7 @@ import { BufReader } from "./bufio.ts"; type Reader = Deno.Reader; type Writer = Deno.Writer; -import { assert } from "../testing/asserts.ts"; +import { assert } from "../_util/assert.ts"; const DEFAULT_BUFFER_SIZE = 32 * 1024; diff --git a/std/log/mod.ts b/std/log/mod.ts index 983e824015..e596c81ba8 100644 --- a/std/log/mod.ts +++ b/std/log/mod.ts @@ -7,7 +7,7 @@ import { FileHandler, RotatingFileHandler, } from "./handlers.ts"; -import { assert } from "../testing/asserts.ts"; +import { assert } from "../_util/assert.ts"; import { LevelName } from "./levels.ts"; export { LogLevels } from "./levels.ts"; diff --git a/std/mime/multipart.ts b/std/mime/multipart.ts index 5d9ee7cf9c..73a6544b5b 100644 --- a/std/mime/multipart.ts +++ b/std/mime/multipart.ts @@ -12,7 +12,7 @@ import { extname } from "../path/mod.ts"; import { tempFile } from "../io/util.ts"; import { BufReader, BufWriter } from "../io/bufio.ts"; import { encoder } from "../encoding/utf8.ts"; -import { assertStrictEquals, assert } from "../testing/asserts.ts"; +import { assert } from "../_util/assert.ts"; import { TextProtoReader } from "../textproto/mod.ts"; import { hasOwnProperty } from "../_util/has_own_property.ts"; @@ -178,7 +178,7 @@ class PartReader implements Reader, Closer { ); if (this.n === 0) { // Force buffered I/O to read more into buffer. - assertStrictEquals(eof, false); + assert(eof === false); peekLength++; } } @@ -190,7 +190,7 @@ class PartReader implements Reader, Closer { const nread = min(p.length, this.n); const buf = p.subarray(0, nread); const r = await br.readFull(buf); - assertStrictEquals(r, buf); + assert(r === buf); this.n -= nread; this.total += nread; return nread; diff --git a/std/node/_fs/_fs_dir.ts b/std/node/_fs/_fs_dir.ts index a39422ce06..7f2085b3b1 100644 --- a/std/node/_fs/_fs_dir.ts +++ b/std/node/_fs/_fs_dir.ts @@ -1,5 +1,5 @@ import Dirent from "./_fs_dirent.ts"; -import { assert } from "../../testing/asserts.ts"; +import { assert } from "../../_util/assert.ts"; export default class Dir { private dirPath: string | Uint8Array; diff --git a/std/node/events.ts b/std/node/events.ts index aa62e4d432..cb9acb0f5a 100644 --- a/std/node/events.ts +++ b/std/node/events.ts @@ -22,7 +22,7 @@ // USE OR OTHER DEALINGS IN THE SOFTWARE. import { validateIntegerRange } from "./util.ts"; -import { assert } from "../testing/asserts.ts"; +import { assert } from "../_util/assert.ts"; export interface WrappedFunction extends Function { listener: Function; diff --git a/std/node/module.ts b/std/node/module.ts index 8d0160f78a..daa01d2b24 100644 --- a/std/node/module.ts +++ b/std/node/module.ts @@ -31,7 +31,7 @@ import * as nodeEvents from "./events.ts"; import * as nodeQueryString from "./querystring.ts"; import * as path from "../path/mod.ts"; -import { assert } from "../testing/asserts.ts"; +import { assert } from "../_util/assert.ts"; import { pathToFileURL, fileURLToPath } from "./url.ts"; const CHAR_FORWARD_SLASH = "/".charCodeAt(0); diff --git a/std/path/glob.ts b/std/path/glob.ts index 34fc213f62..c7d23b3441 100644 --- a/std/path/glob.ts +++ b/std/path/glob.ts @@ -4,7 +4,7 @@ import { SEP, SEP_PATTERN } from "./separator.ts"; import { globrex } from "./_globrex.ts"; import { join, normalize } from "./mod.ts"; -import { assert } from "../testing/asserts.ts"; +import { assert } from "../_util/assert.ts"; export interface GlobOptions { extended?: boolean; diff --git a/std/path/win32.ts b/std/path/win32.ts index f556c5b736..8e438a79c3 100644 --- a/std/path/win32.ts +++ b/std/path/win32.ts @@ -17,7 +17,7 @@ import { normalizeString, _format, } from "./_util.ts"; -import { assert } from "../testing/asserts.ts"; +import { assert } from "../_util/assert.ts"; export const sep = "\\"; export const delimiter = ";"; diff --git a/std/uuid/v5.ts b/std/uuid/v5.ts index 3c04873fec..f982d9745a 100644 --- a/std/uuid/v5.ts +++ b/std/uuid/v5.ts @@ -8,7 +8,7 @@ import { } from "./_common.ts"; import { Sha1 } from "../hash/sha1.ts"; import { isString } from "../node/util.ts"; -import { assert } from "../testing/asserts.ts"; +import { assert } from "../_util/assert.ts"; const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i; diff --git a/std/ws/mod.ts b/std/ws/mod.ts index f98e70d928..f58828aea8 100644 --- a/std/ws/mod.ts +++ b/std/ws/mod.ts @@ -8,7 +8,7 @@ import { Sha1 } from "../hash/sha1.ts"; import { writeResponse } from "../http/_io.ts"; import { TextProtoReader } from "../textproto/mod.ts"; import { Deferred, deferred } from "../async/deferred.ts"; -import { assert } from "../testing/asserts.ts"; +import { assert } from "../_util/assert.ts"; import { concat } from "../bytes/mod.ts"; import Conn = Deno.Conn; import Writer = Deno.Writer;