mirror of
https://github.com/denoland/deno.git
synced 2024-11-29 16:30:56 -05:00
parent
6545e5bde9
commit
6a783ea179
36 changed files with 275 additions and 66 deletions
|
@ -8,11 +8,12 @@ for Deno.
|
||||||
| Collection | Description |
|
| Collection | Description |
|
||||||
| --------------------- | --------------------------------------------------------------- |
|
| --------------------- | --------------------------------------------------------------- |
|
||||||
| [colors](./colors/) | Modules that generate ANSI color codes for the console. |
|
| [colors](./colors/) | Modules that generate ANSI color codes for the console. |
|
||||||
| [net](./net/) | A framework for creating HTTP/HTTPS servers inspired by GoLang. |
|
|
||||||
| [path](./path/) | File path manipulation. |
|
|
||||||
| [flags](./flags/) | Command line arguments parser. |
|
| [flags](./flags/) | Command line arguments parser. |
|
||||||
| [logging](./logging/) | Command line logging |
|
| [logging](./logging/) | Command line logging |
|
||||||
| [mkdirp](./mkdirp/) | Make directory branches. |
|
| [mkdirp](./mkdirp/) | Make directory branches. |
|
||||||
|
| [net](./net/) | A framework for creating HTTP/HTTPS servers inspired by GoLang. |
|
||||||
|
| [path](./path/) | File path manipulation. |
|
||||||
|
| [testing](./testing/) | Testing |
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { assertEqual, test } from "https://deno.land/x/testing/testing.ts";
|
import { assertEqual, test } from "../testing/mod.ts";
|
||||||
import { color } from "main.ts";
|
import { color } from "main.ts";
|
||||||
import "example.ts";
|
import "example.ts";
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { run } from "deno";
|
import { run } from "deno";
|
||||||
import { test, assertEqual } from "https://deno.land/x/testing/testing.ts";
|
import { test, assertEqual } from "../testing/mod.ts";
|
||||||
|
|
||||||
/** Example of how to do basic tests */
|
/** Example of how to do basic tests */
|
||||||
test(function t1() {
|
test(function t1() {
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { test, assertEqual } from "https://deno.land/x/testing/testing.ts";
|
import { test, assertEqual } from "../../testing/mod.ts";
|
||||||
import { parse } from "../index.ts";
|
import { parse } from "../index.ts";
|
||||||
|
|
||||||
// flag boolean true (default all --args to boolean)
|
// flag boolean true (default all --args to boolean)
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { test, assertEqual } from "https://deno.land/x/testing/testing.ts";
|
import { test, assertEqual } from "../../testing/mod.ts";
|
||||||
import { parse } from "../index.ts";
|
import { parse } from "../index.ts";
|
||||||
|
|
||||||
test(function flagBooleanDefaultFalse() {
|
test(function flagBooleanDefaultFalse() {
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { test, assertEqual } from "https://deno.land/x/testing/testing.ts";
|
import { test, assertEqual } from "../../testing/mod.ts";
|
||||||
import { parse } from "../index.ts";
|
import { parse } from "../index.ts";
|
||||||
|
|
||||||
test(function hyphen() {
|
test(function hyphen() {
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { test, assertEqual } from "https://deno.land/x/testing/testing.ts";
|
import { test, assertEqual } from "../../testing/mod.ts";
|
||||||
import { parse } from "../index.ts";
|
import { parse } from "../index.ts";
|
||||||
|
|
||||||
test(function booleanDefaultTrue() {
|
test(function booleanDefaultTrue() {
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { test, assertEqual } from "https://deno.land/x/testing/testing.ts";
|
import { test, assertEqual } from "../../testing/mod.ts";
|
||||||
import { parse } from "../index.ts";
|
import { parse } from "../index.ts";
|
||||||
|
|
||||||
test(function dottedAlias() {
|
test(function dottedAlias() {
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { test, assertEqual } from "https://deno.land/x/testing/testing.ts";
|
import { test, assertEqual } from "../../testing/mod.ts";
|
||||||
import { parse } from "../index.ts";
|
import { parse } from "../index.ts";
|
||||||
|
|
||||||
test(function short() {
|
test(function short() {
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { test, assertEqual } from "https://deno.land/x/testing/testing.ts";
|
import { test, assertEqual } from "../../testing/mod.ts";
|
||||||
import { parse } from "../index.ts";
|
import { parse } from "../index.ts";
|
||||||
|
|
||||||
test(function longOpts() {
|
test(function longOpts() {
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { test, assertEqual } from "https://deno.land/x/testing/testing.ts";
|
import { test, assertEqual } from "../../testing/mod.ts";
|
||||||
import { parse } from "../index.ts";
|
import { parse } from "../index.ts";
|
||||||
|
|
||||||
test(function nums() {
|
test(function nums() {
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { test, assertEqual } from "https://deno.land/x/testing/testing.ts";
|
import { test, assertEqual } from "../../testing/mod.ts";
|
||||||
import { parse } from "../index.ts";
|
import { parse } from "../index.ts";
|
||||||
|
|
||||||
test(function _arseArgs() {
|
test(function _arseArgs() {
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { test, assertEqual } from "https://deno.land/x/testing/testing.ts";
|
import { test, assertEqual } from "../../testing/mod.ts";
|
||||||
import { parse } from "../index.ts";
|
import { parse } from "../index.ts";
|
||||||
|
|
||||||
test(function numbericShortArgs() {
|
test(function numbericShortArgs() {
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { test, assertEqual } from "https://deno.land/x/testing/testing.ts";
|
import { test, assertEqual } from "../../testing/mod.ts";
|
||||||
import { parse } from "../index.ts";
|
import { parse } from "../index.ts";
|
||||||
|
|
||||||
// stops parsing on the first non-option when stopEarly is set
|
// stops parsing on the first non-option when stopEarly is set
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { test, assertEqual } from "https://deno.land/x/testing/testing.ts";
|
import { test, assertEqual } from "../../testing/mod.ts";
|
||||||
import { parse } from "../index.ts";
|
import { parse } from "../index.ts";
|
||||||
|
|
||||||
test(function booleanAndAliasIsNotUnknown() {
|
test(function booleanAndAliasIsNotUnknown() {
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { test, assertEqual } from "https://deno.land/x/testing/testing.ts";
|
import { test, assertEqual } from "../../testing/mod.ts";
|
||||||
import { parse } from "../index.ts";
|
import { parse } from "../index.ts";
|
||||||
|
|
||||||
test(function whitespaceShouldBeWhitespace() {
|
test(function whitespaceShouldBeWhitespace() {
|
||||||
|
|
|
@ -14,25 +14,25 @@ log.critical("500 Internal server error");
|
||||||
await log.setup({
|
await log.setup({
|
||||||
handlers: {
|
handlers: {
|
||||||
console: new log.handlers.ConsoleHandler("DEBUG"),
|
console: new log.handlers.ConsoleHandler("DEBUG"),
|
||||||
file: new log.handlers.FileHandler("WARNING", "./log.txt"),
|
file: new log.handlers.FileHandler("WARNING", "./log.txt")
|
||||||
},
|
},
|
||||||
|
|
||||||
loggers: {
|
loggers: {
|
||||||
default: {
|
default: {
|
||||||
level: "DEBUG",
|
level: "DEBUG",
|
||||||
handlers: ["console", "file"],
|
handlers: ["console", "file"]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// get configured logger
|
// get configured logger
|
||||||
const logger = log.getLogger("default");
|
const logger = log.getLogger("default");
|
||||||
logger.debug("fizz") // <- logs to `console`, because `file` handler requires 'WARNING' level
|
logger.debug("fizz"); // <- logs to `console`, because `file` handler requires 'WARNING' level
|
||||||
logger.warning("buzz") // <- logs to both `console` and `file` handlers
|
logger.warning("buzz"); // <- logs to both `console` and `file` handlers
|
||||||
|
|
||||||
// if you try to use a logger that hasn't been configured
|
// if you try to use a logger that hasn't been configured
|
||||||
// you're good to go, it gets created automatically with level set to 0
|
// you're good to go, it gets created automatically with level set to 0
|
||||||
// so no message is logged
|
// so no message is logged
|
||||||
const unknownLogger = log.getLogger("mystery");
|
const unknownLogger = log.getLogger("mystery");
|
||||||
unknownLogger.info("foobar") // no-op
|
unknownLogger.info("foobar"); // no-op
|
||||||
```
|
```
|
|
@ -1,6 +1,5 @@
|
||||||
import { remove, open, readAll } from "deno";
|
import { remove, open, readAll } from "deno";
|
||||||
import { assertEqual, test } from "https://deno.land/x/testing/testing.ts";
|
import { assertEqual, test } from "../testing/mod.ts";
|
||||||
|
|
||||||
import * as log from "index.ts";
|
import * as log from "index.ts";
|
||||||
import { FileHandler } from "./handlers.ts";
|
import { FileHandler } from "./handlers.ts";
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { cwd, lstat, makeTempDirSync, removeAll, FileInfo } from "deno";
|
import { cwd, lstat, makeTempDirSync, removeAll, FileInfo } from "deno";
|
||||||
import { test, assert } from "https://deno.land/x/testing/testing.ts";
|
import { test, assert } from "../testing/mod.ts";
|
||||||
import { mkdirp } from "./mkdirp.ts";
|
import { mkdirp } from "./mkdirp.ts";
|
||||||
|
|
||||||
let root: string = `${cwd()}/${Date.now()}`; //makeTempDirSync();
|
let root: string = `${cwd()}/${Date.now()}`; //makeTempDirSync();
|
||||||
|
|
|
@ -8,7 +8,7 @@ import {
|
||||||
test,
|
test,
|
||||||
assert,
|
assert,
|
||||||
assertEqual
|
assertEqual
|
||||||
} from "https://deno.land/x/testing/testing.ts";
|
} from "../testing/mod.ts";
|
||||||
import { BufReader, BufState, BufWriter } from "./bufio.ts";
|
import { BufReader, BufState, BufWriter } from "./bufio.ts";
|
||||||
import * as iotest from "./iotest.ts";
|
import * as iotest from "./iotest.ts";
|
||||||
import { charCode, copyBytes, stringsReader } from "./util.ts";
|
import { charCode, copyBytes, stringsReader } from "./util.ts";
|
||||||
|
|
|
@ -4,7 +4,7 @@ import {
|
||||||
test,
|
test,
|
||||||
assert,
|
assert,
|
||||||
assertEqual
|
assertEqual
|
||||||
} from "https://deno.land/x/testing/testing.ts";
|
} from "../testing/mod.ts";
|
||||||
|
|
||||||
// Promise to completeResolve when all tests completes
|
// Promise to completeResolve when all tests completes
|
||||||
let completeResolve;
|
let completeResolve;
|
||||||
|
|
|
@ -10,7 +10,7 @@ import {
|
||||||
test,
|
test,
|
||||||
assert,
|
assert,
|
||||||
assertEqual
|
assertEqual
|
||||||
} from "https://deno.land/x/testing/testing.ts";
|
} from "../testing/mod.ts";
|
||||||
import {
|
import {
|
||||||
listenAndServe,
|
listenAndServe,
|
||||||
ServerRequest,
|
ServerRequest,
|
||||||
|
|
|
@ -10,7 +10,7 @@ import {
|
||||||
test,
|
test,
|
||||||
assert,
|
assert,
|
||||||
assertEqual
|
assertEqual
|
||||||
} from "https://deno.land/x/testing/testing.ts";
|
} from "../testing/mod.ts";
|
||||||
|
|
||||||
function reader(s: string): TextProtoReader {
|
function reader(s: string): TextProtoReader {
|
||||||
return new TextProtoReader(new BufReader(stringsReader(s)));
|
return new TextProtoReader(new BufReader(stringsReader(s)));
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
// Copyright the Browserify authors. MIT License.
|
// Copyright the Browserify authors. MIT License.
|
||||||
// Ported from https://github.com/browserify/path-browserify/
|
// Ported from https://github.com/browserify/path-browserify/
|
||||||
|
|
||||||
import { test, assertEqual } from "https://deno.land/x/testing/testing.ts";
|
import { test, assertEqual } from "../testing/mod.ts";
|
||||||
import * as path from "./index.ts";
|
import * as path from "./index.ts";
|
||||||
|
|
||||||
test(function basename() {
|
test(function basename() {
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
// Copyright the Browserify authors. MIT License.
|
// Copyright the Browserify authors. MIT License.
|
||||||
// Ported from https://github.com/browserify/path-browserify/
|
// Ported from https://github.com/browserify/path-browserify/
|
||||||
|
|
||||||
import { test, assertEqual } from "https://deno.land/x/testing/testing.ts";
|
import { test, assertEqual } from "../testing/mod.ts";
|
||||||
import * as path from "./index.ts";
|
import * as path from "./index.ts";
|
||||||
|
|
||||||
test(function dirname() {
|
test(function dirname() {
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
// Copyright the Browserify authors. MIT License.
|
// Copyright the Browserify authors. MIT License.
|
||||||
// Ported from https://github.com/browserify/path-browserify/
|
// Ported from https://github.com/browserify/path-browserify/
|
||||||
|
|
||||||
import { test, assertEqual } from "https://deno.land/x/testing/testing.ts";
|
import { test, assertEqual } from "../testing/mod.ts";
|
||||||
import * as path from "./index.ts";
|
import * as path from "./index.ts";
|
||||||
|
|
||||||
const slashRE = /\//g;
|
const slashRE = /\//g;
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
// Copyright the Browserify authors. MIT License.
|
// Copyright the Browserify authors. MIT License.
|
||||||
// Ported from https://github.com/browserify/path-browserify/
|
// Ported from https://github.com/browserify/path-browserify/
|
||||||
|
|
||||||
import { test, assertEqual } from "https://deno.land/x/testing/testing.ts";
|
import { test, assertEqual } from "../testing/mod.ts";
|
||||||
import * as path from "./index.ts";
|
import * as path from "./index.ts";
|
||||||
|
|
||||||
test(function isAbsolute() {
|
test(function isAbsolute() {
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { test, assertEqual } from "https://deno.land/x/testing/testing.ts";
|
import { test, assertEqual } from "../testing/mod.ts";
|
||||||
import * as path from "./index.ts";
|
import * as path from "./index.ts";
|
||||||
|
|
||||||
const backslashRE = /\\/g;
|
const backslashRE = /\\/g;
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
// Copyright the Browserify authors. MIT License.
|
// Copyright the Browserify authors. MIT License.
|
||||||
// Ported from https://github.com/browserify/path-browserify/
|
// Ported from https://github.com/browserify/path-browserify/
|
||||||
|
|
||||||
import { test, assertEqual } from "https://deno.land/x/testing/testing.ts";
|
import { test, assertEqual } from "../testing/mod.ts";
|
||||||
import * as path from "./index.ts";
|
import * as path from "./index.ts";
|
||||||
|
|
||||||
const winPaths = [
|
const winPaths = [
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
// Copyright the Browserify authors. MIT License.
|
// Copyright the Browserify authors. MIT License.
|
||||||
// Ported from https://github.com/browserify/path-browserify/
|
// Ported from https://github.com/browserify/path-browserify/
|
||||||
|
|
||||||
import { test, assertEqual } from "https://deno.land/x/testing/testing.ts";
|
import { test, assertEqual } from "../testing/mod.ts";
|
||||||
import * as path from "./index.ts";
|
import * as path from "./index.ts";
|
||||||
|
|
||||||
const relativeTests = {
|
const relativeTests = {
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
// Copyright the Browserify authors. MIT License.
|
// Copyright the Browserify authors. MIT License.
|
||||||
// Ported from https://github.com/browserify/path-browserify/
|
// Ported from https://github.com/browserify/path-browserify/
|
||||||
|
|
||||||
import { test, assertEqual } from "https://deno.land/x/testing/testing.ts";
|
import { test, assertEqual } from "../testing/mod.ts";
|
||||||
import * as path from "./index.ts";
|
import * as path from "./index.ts";
|
||||||
import { cwd } from "deno";
|
import { cwd } from "deno";
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
// Copyright the Browserify authors. MIT License.
|
// Copyright the Browserify authors. MIT License.
|
||||||
// Ported from https://github.com/browserify/path-browserify/
|
// Ported from https://github.com/browserify/path-browserify/
|
||||||
|
|
||||||
import { test, assertEqual } from "https://deno.land/x/testing/testing.ts";
|
import { test, assertEqual } from "../testing/mod.ts";
|
||||||
import * as path from "./index.ts";
|
import * as path from "./index.ts";
|
||||||
import { cwd } from "deno";
|
import { cwd } from "deno";
|
||||||
|
|
||||||
|
|
28
test.ts
28
test.ts
|
@ -1,27 +1,14 @@
|
||||||
#!/usr/bin/env deno --allow-run --allow-net --allow-write
|
#!/usr/bin/env deno --allow-run --allow-net --allow-write
|
||||||
import { run } from "deno";
|
import { run } from "deno";
|
||||||
|
|
||||||
// colors tests
|
|
||||||
import "colors/main_test.ts";
|
import "colors/main_test.ts";
|
||||||
|
import "examples/test.ts";
|
||||||
// flags tests
|
|
||||||
import "flags/test.ts";
|
import "flags/test.ts";
|
||||||
|
import "logging/test.ts";
|
||||||
// net tests
|
import "mkdirp/test.ts";
|
||||||
import "net/bufio_test.ts";
|
import "net/bufio_test.ts";
|
||||||
import "net/http_test.ts";
|
import "net/http_test.ts";
|
||||||
import "net/textproto_test.ts";
|
import "net/textproto_test.ts";
|
||||||
import "examples/test.ts";
|
|
||||||
import { runTests, completePromise } from "net/file_server_test.ts";
|
|
||||||
|
|
||||||
// logging tests
|
|
||||||
import "logging/test.ts";
|
|
||||||
|
|
||||||
// file server test
|
|
||||||
const fileServer = run({
|
|
||||||
args: ["deno", "--allow-net", "net/file_server.ts", ".", "--cors"]
|
|
||||||
});
|
|
||||||
// path test
|
|
||||||
import "path/basename_test.ts";
|
import "path/basename_test.ts";
|
||||||
import "path/dirname_test.ts";
|
import "path/dirname_test.ts";
|
||||||
import "path/extname_test.ts";
|
import "path/extname_test.ts";
|
||||||
|
@ -31,11 +18,14 @@ import "path/parse_format_test.ts";
|
||||||
import "path/relative_test.ts";
|
import "path/relative_test.ts";
|
||||||
import "path/resolve_test.ts";
|
import "path/resolve_test.ts";
|
||||||
import "path/zero_length_strings_test.ts";
|
import "path/zero_length_strings_test.ts";
|
||||||
|
import "testing/test.ts";
|
||||||
|
|
||||||
// mkdirp tests
|
import { runTests, completePromise } from "net/file_server_test.ts";
|
||||||
import "mkdirp/test.ts";
|
|
||||||
|
const fileServer = run({
|
||||||
|
args: ["deno", "--allow-net", "net/file_server.ts", ".", "--cors"]
|
||||||
|
});
|
||||||
|
|
||||||
// I am also too lazy to do this properly LOL
|
|
||||||
runTests(new Promise(res => setTimeout(res, 5000)));
|
runTests(new Promise(res => setTimeout(res, 5000)));
|
||||||
(async () => {
|
(async () => {
|
||||||
await completePromise;
|
await completePromise;
|
||||||
|
|
162
testing/mod.ts
Normal file
162
testing/mod.ts
Normal file
|
@ -0,0 +1,162 @@
|
||||||
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
||||||
|
|
||||||
|
// Do not add imports in this file in order to be compatible with Node.
|
||||||
|
|
||||||
|
export function assertEqual(actual: unknown, expected: unknown, msg?: string) {
|
||||||
|
if (!equal(actual, expected)) {
|
||||||
|
let actualString: string;
|
||||||
|
let expectedString: string;
|
||||||
|
try {
|
||||||
|
actualString = String(actual);
|
||||||
|
} catch (e) {
|
||||||
|
actualString = "[Cannot display]";
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
expectedString = String(expected);
|
||||||
|
} catch (e) {
|
||||||
|
expectedString = "[Cannot display]";
|
||||||
|
}
|
||||||
|
console.error(
|
||||||
|
"assertEqual failed. actual =",
|
||||||
|
actualString,
|
||||||
|
"expected =",
|
||||||
|
expectedString
|
||||||
|
);
|
||||||
|
if (!msg) {
|
||||||
|
msg = `actual: ${actualString} expected: ${expectedString}`;
|
||||||
|
}
|
||||||
|
throw new Error(msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function assert(expr: boolean, msg = "") {
|
||||||
|
if (!expr) {
|
||||||
|
throw new Error(msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO(ry) Use unknown here for parameters types.
|
||||||
|
// tslint:disable-next-line:no-any
|
||||||
|
export function equal(c: any, d: any): boolean {
|
||||||
|
const seen = new Map();
|
||||||
|
return (function compare(a, b) {
|
||||||
|
if (Object.is(a, b)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (a && typeof a === "object" && b && typeof b === "object") {
|
||||||
|
if (seen.get(a) === b) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (Object.keys(a).length !== Object.keys(b).length) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for (const key in { ...a, ...b }) {
|
||||||
|
if (!compare(a[key], b[key])) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
seen.set(a, b);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
})(c, d);
|
||||||
|
}
|
||||||
|
|
||||||
|
export type TestFunction = () => void | Promise<void>;
|
||||||
|
|
||||||
|
export interface TestDefinition {
|
||||||
|
fn: TestFunction;
|
||||||
|
name: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const exitOnFail = true;
|
||||||
|
|
||||||
|
let filterRegExp: RegExp | null;
|
||||||
|
const tests: TestDefinition[] = [];
|
||||||
|
|
||||||
|
let filtered = 0;
|
||||||
|
const ignored = 0;
|
||||||
|
const measured = 0;
|
||||||
|
|
||||||
|
// Must be called before any test() that needs to be filtered.
|
||||||
|
export function setFilter(s: string): void {
|
||||||
|
filterRegExp = new RegExp(s, "i");
|
||||||
|
}
|
||||||
|
|
||||||
|
export function test(t: TestDefinition | TestFunction): void {
|
||||||
|
const fn: TestFunction = typeof t === "function" ? t : t.fn;
|
||||||
|
const name: string = t.name;
|
||||||
|
|
||||||
|
if (!name) {
|
||||||
|
throw new Error("Test function may not be anonymous");
|
||||||
|
}
|
||||||
|
if (filter(name)) {
|
||||||
|
tests.push({ fn, name });
|
||||||
|
} else {
|
||||||
|
filtered++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function filter(name: string): boolean {
|
||||||
|
if (filterRegExp) {
|
||||||
|
return filterRegExp.test(name);
|
||||||
|
} else {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const RESET = "\x1b[0m";
|
||||||
|
const FG_RED = "\x1b[31m";
|
||||||
|
const FG_GREEN = "\x1b[32m";
|
||||||
|
|
||||||
|
function red_failed() {
|
||||||
|
return FG_RED + "FAILED" + RESET;
|
||||||
|
}
|
||||||
|
|
||||||
|
function green_ok() {
|
||||||
|
return FG_GREEN + "ok" + RESET;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function runTests() {
|
||||||
|
let passed = 0;
|
||||||
|
let failed = 0;
|
||||||
|
|
||||||
|
console.log("running", tests.length, "tests");
|
||||||
|
for (let i = 0; i < tests.length; i++) {
|
||||||
|
const { fn, name } = tests[i];
|
||||||
|
let result = green_ok();
|
||||||
|
console.log("test", name);
|
||||||
|
try {
|
||||||
|
await fn();
|
||||||
|
passed++;
|
||||||
|
} catch (e) {
|
||||||
|
result = red_failed();
|
||||||
|
console.error((e && e.stack) || e);
|
||||||
|
failed++;
|
||||||
|
if (exitOnFail) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// TODO Do this on the same line as test name is printed.
|
||||||
|
console.log("...", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Attempting to match the output of Rust's test runner.
|
||||||
|
const result = failed > 0 ? red_failed() : green_ok();
|
||||||
|
console.log(
|
||||||
|
`\ntest result: ${result}. ${passed} passed; ${failed} failed; ` +
|
||||||
|
`${ignored} ignored; ${measured} measured; ${filtered} filtered out\n`
|
||||||
|
);
|
||||||
|
|
||||||
|
if (failed === 0) {
|
||||||
|
// All good.
|
||||||
|
} else {
|
||||||
|
// Use setTimeout to avoid the error being ignored due to unhandled
|
||||||
|
// promise rejections being swallowed.
|
||||||
|
setTimeout(() => {
|
||||||
|
throw new Error(`There were ${failed} test failures.`);
|
||||||
|
}, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
setTimeout(runTests, 0);
|
57
testing/test.ts
Normal file
57
testing/test.ts
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
||||||
|
|
||||||
|
import { test, assert, assertEqual, equal } from "./mod.ts";
|
||||||
|
|
||||||
|
test(function testingEqual() {
|
||||||
|
assert(equal("world", "world"));
|
||||||
|
assert(!equal("hello", "world"));
|
||||||
|
assert(equal(5, 5));
|
||||||
|
assert(!equal(5, 6));
|
||||||
|
assert(equal(NaN, NaN));
|
||||||
|
assert(equal({ hello: "world" }, { hello: "world" }));
|
||||||
|
assert(!equal({ world: "hello" }, { hello: "world" }));
|
||||||
|
assert(
|
||||||
|
equal(
|
||||||
|
{ hello: "world", hi: { there: "everyone" } },
|
||||||
|
{ hello: "world", hi: { there: "everyone" } }
|
||||||
|
)
|
||||||
|
);
|
||||||
|
assert(
|
||||||
|
!equal(
|
||||||
|
{ hello: "world", hi: { there: "everyone" } },
|
||||||
|
{ hello: "world", hi: { there: "everyone else" } }
|
||||||
|
)
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test(function testingAssertEqual() {
|
||||||
|
const a = Object.create(null);
|
||||||
|
a.b = "foo";
|
||||||
|
assertEqual(a, a);
|
||||||
|
});
|
||||||
|
|
||||||
|
test(function testingAssertEqualActualUncoercable() {
|
||||||
|
let didThrow = false;
|
||||||
|
const a = Object.create(null);
|
||||||
|
try {
|
||||||
|
assertEqual(a, "bar");
|
||||||
|
} catch (e) {
|
||||||
|
didThrow = true;
|
||||||
|
console.log(e.message);
|
||||||
|
assert(e.message === "actual: [Cannot display] expected: bar");
|
||||||
|
}
|
||||||
|
assert(didThrow);
|
||||||
|
});
|
||||||
|
|
||||||
|
test(function testingAssertEqualExpectedUncoercable() {
|
||||||
|
let didThrow = false;
|
||||||
|
const a = Object.create(null);
|
||||||
|
try {
|
||||||
|
assertEqual("bar", a);
|
||||||
|
} catch (e) {
|
||||||
|
didThrow = true;
|
||||||
|
console.log(e.message);
|
||||||
|
assert(e.message === "actual: bar expected: [Cannot display]");
|
||||||
|
}
|
||||||
|
assert(didThrow);
|
||||||
|
});
|
Loading…
Reference in a new issue