1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-28 10:09:20 -05:00

Add testing module

Original: 61fdae51a7
This commit is contained in:
Ryan Dahl 2019-01-01 22:46:17 -05:00
parent 6545e5bde9
commit 6a783ea179
36 changed files with 275 additions and 66 deletions

View file

@ -8,11 +8,12 @@ for Deno.
| Collection | Description |
| --------------------- | --------------------------------------------------------------- |
| [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. |
| [logging](./logging/) | Command line logging |
| [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 |
---

View file

@ -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 "example.ts";

View file

@ -1,5 +1,5 @@
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 */
test(function t1() {

View file

@ -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";
// flag boolean true (default all --args to boolean)

View file

@ -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";
test(function flagBooleanDefaultFalse() {

View file

@ -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";
test(function hyphen() {

View file

@ -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";
test(function booleanDefaultTrue() {

View file

@ -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";
test(function dottedAlias() {

View file

@ -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";
test(function short() {

View file

@ -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";
test(function longOpts() {

View file

@ -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";
test(function nums() {

View file

@ -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";
test(function _arseArgs() {

View file

@ -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";
test(function numbericShortArgs() {

View file

@ -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";
// stops parsing on the first non-option when stopEarly is set

View file

@ -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";
test(function booleanAndAliasIsNotUnknown() {

View file

@ -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";
test(function whitespaceShouldBeWhitespace() {

View file

@ -12,27 +12,27 @@ log.critical("500 Internal server error");
// configure as needed
await log.setup({
handlers: {
console: new log.handlers.ConsoleHandler("DEBUG"),
file: new log.handlers.FileHandler("WARNING", "./log.txt"),
},
handlers: {
console: new log.handlers.ConsoleHandler("DEBUG"),
file: new log.handlers.FileHandler("WARNING", "./log.txt")
},
loggers: {
default: {
level: "DEBUG",
handlers: ["console", "file"],
}
loggers: {
default: {
level: "DEBUG",
handlers: ["console", "file"]
}
}
});
// get configured logger
const logger = log.getLogger("default");
logger.debug("fizz") // <- logs to `console`, because `file` handler requires 'WARNING' level
logger.warning("buzz") // <- logs to both `console` and `file` handlers
logger.debug("fizz"); // <- logs to `console`, because `file` handler requires 'WARNING' level
logger.warning("buzz"); // <- logs to both `console` and `file` handlers
// 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
// so no message is logged
const unknownLogger = log.getLogger("mystery");
unknownLogger.info("foobar") // no-op
```
unknownLogger.info("foobar"); // no-op
```

View file

@ -1,6 +1,5 @@
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 { FileHandler } from "./handlers.ts";

View file

@ -1,5 +1,5 @@
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";
let root: string = `${cwd()}/${Date.now()}`; //makeTempDirSync();

View file

@ -8,7 +8,7 @@ import {
test,
assert,
assertEqual
} from "https://deno.land/x/testing/testing.ts";
} from "../testing/mod.ts";
import { BufReader, BufState, BufWriter } from "./bufio.ts";
import * as iotest from "./iotest.ts";
import { charCode, copyBytes, stringsReader } from "./util.ts";

View file

@ -82,4 +82,4 @@
".yml": "text/yaml",
".yaml": "text/yaml",
".zip": "application/zip"
}
}

View file

@ -4,7 +4,7 @@ import {
test,
assert,
assertEqual
} from "https://deno.land/x/testing/testing.ts";
} from "../testing/mod.ts";
// Promise to completeResolve when all tests completes
let completeResolve;

View file

@ -10,7 +10,7 @@ import {
test,
assert,
assertEqual
} from "https://deno.land/x/testing/testing.ts";
} from "../testing/mod.ts";
import {
listenAndServe,
ServerRequest,

View file

@ -10,7 +10,7 @@ import {
test,
assert,
assertEqual
} from "https://deno.land/x/testing/testing.ts";
} from "../testing/mod.ts";
function reader(s: string): TextProtoReader {
return new TextProtoReader(new BufReader(stringsReader(s)));

View file

@ -1,7 +1,7 @@
// Copyright the Browserify authors. MIT License.
// 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";
test(function basename() {

View file

@ -1,7 +1,7 @@
// Copyright the Browserify authors. MIT License.
// 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";
test(function dirname() {

View file

@ -1,7 +1,7 @@
// Copyright the Browserify authors. MIT License.
// 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";
const slashRE = /\//g;

View file

@ -1,7 +1,7 @@
// Copyright the Browserify authors. MIT License.
// 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";
test(function isAbsolute() {

View file

@ -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";
const backslashRE = /\\/g;

View file

@ -1,7 +1,7 @@
// Copyright the Browserify authors. MIT License.
// 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";
const winPaths = [

View file

@ -1,7 +1,7 @@
// Copyright the Browserify authors. MIT License.
// 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";
const relativeTests = {

View file

@ -1,7 +1,7 @@
// Copyright the Browserify authors. MIT License.
// 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 { cwd } from "deno";

View file

@ -1,7 +1,7 @@
// Copyright the Browserify authors. MIT License.
// 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 { cwd } from "deno";

28
test.ts
View file

@ -1,27 +1,14 @@
#!/usr/bin/env deno --allow-run --allow-net --allow-write
import { run } from "deno";
// colors tests
import "colors/main_test.ts";
// flags tests
import "examples/test.ts";
import "flags/test.ts";
// net tests
import "logging/test.ts";
import "mkdirp/test.ts";
import "net/bufio_test.ts";
import "net/http_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/dirname_test.ts";
import "path/extname_test.ts";
@ -31,11 +18,14 @@ import "path/parse_format_test.ts";
import "path/relative_test.ts";
import "path/resolve_test.ts";
import "path/zero_length_strings_test.ts";
import "testing/test.ts";
// mkdirp tests
import "mkdirp/test.ts";
import { runTests, completePromise } from "net/file_server_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)));
(async () => {
await completePromise;

162
testing/mod.ts Normal file
View 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
View 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);
});