mirror of
https://github.com/denoland/deno.git
synced 2025-09-26 12:19:12 +00:00
fix(std/examples): add tests for examples (#4094)
This commit is contained in:
parent
162d66d23f
commit
fe9ac35a65
16 changed files with 258 additions and 5 deletions
|
@ -12,7 +12,8 @@ import * as colors from "../fmt/colors.ts";
|
||||||
|
|
||||||
const decoder = new TextDecoder();
|
const decoder = new TextDecoder();
|
||||||
|
|
||||||
function isObject(arg): arg is object {
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
|
function isObject(arg: any): arg is object {
|
||||||
return !!arg && arg.constructor === Object;
|
return !!arg && arg.constructor === Object;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,7 +36,8 @@ function printValue(value: unknown, path: string): void {
|
||||||
console.log(path + " = " + value);
|
console.log(path + " = " + value);
|
||||||
}
|
}
|
||||||
|
|
||||||
function printObject(obj: object, path: string): void {
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
|
function printObject(obj: { [key: string]: any }, path: string): void {
|
||||||
for (const key of Object.keys(obj)) {
|
for (const key of Object.keys(obj)) {
|
||||||
const value = obj[key];
|
const value = obj[key];
|
||||||
let nodePath = path + colors.cyan(".") + key;
|
let nodePath = path + colors.cyan(".") + key;
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||||
import { assert, assertEquals } from "../../testing/asserts.ts";
|
import { assert, assertEquals } from "../../testing/asserts.ts";
|
||||||
import { TextProtoReader } from "../../textproto/mod.ts";
|
import { TextProtoReader } from "../../textproto/mod.ts";
|
||||||
import { BufReader } from "../../io/bufio.ts";
|
import { BufReader } from "../../io/bufio.ts";
|
||||||
|
@ -22,10 +23,12 @@ async function startServer(): Promise<void> {
|
||||||
|
|
||||||
const { test } = Deno;
|
const { test } = Deno;
|
||||||
|
|
||||||
|
test("beforeAll", async () => {
|
||||||
await startServer();
|
await startServer();
|
||||||
|
});
|
||||||
|
|
||||||
test("GET / should serve html", async () => {
|
test("GET / should serve html", async () => {
|
||||||
const resp = await fetch("http://0.0.0.0:8080/");
|
const resp = await fetch("http://127.0.0.1:8080/");
|
||||||
assertEquals(resp.status, 200);
|
assertEquals(resp.status, 200);
|
||||||
assertEquals(resp.headers.get("content-type"), "text/html");
|
assertEquals(resp.headers.get("content-type"), "text/html");
|
||||||
const html = await resp.body.text();
|
const html = await resp.body.text();
|
||||||
|
@ -34,7 +37,7 @@ test("GET / should serve html", async () => {
|
||||||
|
|
||||||
let ws: WebSocket | undefined;
|
let ws: WebSocket | undefined;
|
||||||
test("GET /ws should upgrade conn to ws", async () => {
|
test("GET /ws should upgrade conn to ws", async () => {
|
||||||
ws = await connectWebSocket("http://0.0.0.0:8080/ws");
|
ws = await connectWebSocket("http://127.0.0.1:8080/ws");
|
||||||
const it = ws.receive();
|
const it = ws.receive();
|
||||||
assertEquals((await it.next()).value, "Connected: [1]");
|
assertEquals((await it.next()).value, "Connected: [1]");
|
||||||
ws.send("Hello");
|
ws.send("Hello");
|
||||||
|
|
1
std/examples/testdata/cat/hello.txt
vendored
Normal file
1
std/examples/testdata/cat/hello.txt
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
Hello
|
1
std/examples/testdata/cat/world.txt
vendored
Normal file
1
std/examples/testdata/cat/world.txt
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
World
|
5
std/examples/testdata/catj/array.json
vendored
Normal file
5
std/examples/testdata/catj/array.json
vendored
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
[
|
||||||
|
"string",
|
||||||
|
100,
|
||||||
|
{ "key": "value", "array": ["foo", "bar"] }
|
||||||
|
]
|
6
std/examples/testdata/catj/object.json
vendored
Normal file
6
std/examples/testdata/catj/object.json
vendored
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
{
|
||||||
|
"string": "foobar",
|
||||||
|
"number": 123,
|
||||||
|
"array": [{ "message": "hello" }]
|
||||||
|
}
|
||||||
|
|
1
std/examples/testdata/catj/simple-array.json
vendored
Normal file
1
std/examples/testdata/catj/simple-array.json
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
[1, 2]
|
1
std/examples/testdata/catj/simple-object.json
vendored
Normal file
1
std/examples/testdata/catj/simple-object.json
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
{ "message": "hello" }
|
25
std/examples/tests/cat_test.ts
Normal file
25
std/examples/tests/cat_test.ts
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||||
|
import { assertStrictEq } from "../../testing/asserts.ts";
|
||||||
|
|
||||||
|
Deno.test("[examples/cat] print multiple files", async () => {
|
||||||
|
const decoder = new TextDecoder();
|
||||||
|
const process = Deno.run({
|
||||||
|
args: [
|
||||||
|
Deno.execPath(),
|
||||||
|
"--allow-read",
|
||||||
|
"cat.ts",
|
||||||
|
"testdata/cat/hello.txt",
|
||||||
|
"testdata/cat/world.txt"
|
||||||
|
],
|
||||||
|
cwd: "examples",
|
||||||
|
stdout: "piped"
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
const output = await Deno.readAll(process.stdout!);
|
||||||
|
const actual = decoder.decode(output).trim();
|
||||||
|
assertStrictEq(actual, "Hello\nWorld");
|
||||||
|
} finally {
|
||||||
|
process.close();
|
||||||
|
}
|
||||||
|
});
|
83
std/examples/tests/catj_test.ts
Normal file
83
std/examples/tests/catj_test.ts
Normal file
|
@ -0,0 +1,83 @@
|
||||||
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||||
|
import { assertStrictEq } from "../../testing/asserts.ts";
|
||||||
|
|
||||||
|
Deno.test("[examples/catj] print an array", async () => {
|
||||||
|
const decoder = new TextDecoder();
|
||||||
|
const process = catj("testdata/catj/array.json");
|
||||||
|
try {
|
||||||
|
const output = await Deno.readAll(process.stdout!);
|
||||||
|
const actual = decoder.decode(output).trim();
|
||||||
|
const expected = [
|
||||||
|
'.[0] = "string"',
|
||||||
|
".[1] = 100",
|
||||||
|
'.[2].key = "value"',
|
||||||
|
'.[2].array[0] = "foo"',
|
||||||
|
'.[2].array[1] = "bar"'
|
||||||
|
].join("\n");
|
||||||
|
|
||||||
|
assertStrictEq(actual, expected);
|
||||||
|
} finally {
|
||||||
|
process.close();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Deno.test("[examples/catj] print an object", async () => {
|
||||||
|
const decoder = new TextDecoder();
|
||||||
|
const process = catj("testdata/catj/object.json");
|
||||||
|
try {
|
||||||
|
const output = await Deno.readAll(process.stdout!);
|
||||||
|
const actual = decoder.decode(output).trim();
|
||||||
|
const expected = [
|
||||||
|
'.string = "foobar"',
|
||||||
|
".number = 123",
|
||||||
|
'.array[0].message = "hello"'
|
||||||
|
].join("\n");
|
||||||
|
|
||||||
|
assertStrictEq(actual, expected);
|
||||||
|
} finally {
|
||||||
|
process.close();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Deno.test("[examples/catj] print multiple files", async () => {
|
||||||
|
const decoder = new TextDecoder();
|
||||||
|
const process = catj(
|
||||||
|
"testdata/catj/simple-object.json",
|
||||||
|
"testdata/catj/simple-array.json"
|
||||||
|
);
|
||||||
|
try {
|
||||||
|
const output = await Deno.readAll(process.stdout!);
|
||||||
|
const actual = decoder.decode(output).trim();
|
||||||
|
const expected = ['.message = "hello"', ".[0] = 1", ".[1] = 2"].join("\n");
|
||||||
|
|
||||||
|
assertStrictEq(actual, expected);
|
||||||
|
} finally {
|
||||||
|
process.close();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Deno.test("[examples/catj] read from stdin", async () => {
|
||||||
|
const decoder = new TextDecoder();
|
||||||
|
const process = catj("-");
|
||||||
|
const input = `{ "foo": "bar" }`;
|
||||||
|
try {
|
||||||
|
await process.stdin!.write(new TextEncoder().encode(input));
|
||||||
|
process.stdin!.close();
|
||||||
|
const output = await Deno.readAll(process.stdout!);
|
||||||
|
const actual = decoder.decode(output).trim();
|
||||||
|
|
||||||
|
assertStrictEq(actual, '.foo = "bar"');
|
||||||
|
} finally {
|
||||||
|
process.close();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
function catj(...files: string[]): Deno.Process {
|
||||||
|
return Deno.run({
|
||||||
|
args: [Deno.execPath(), "--allow-read", "catj.ts", ...files],
|
||||||
|
cwd: "examples",
|
||||||
|
stdin: "piped",
|
||||||
|
stdout: "piped",
|
||||||
|
env: { NO_COLOR: "true" }
|
||||||
|
});
|
||||||
|
}
|
19
std/examples/tests/colors_test.ts
Normal file
19
std/examples/tests/colors_test.ts
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||||
|
import { assertStrictEq } from "../../testing/asserts.ts";
|
||||||
|
|
||||||
|
Deno.test("[examples/colors] print a colored text", async () => {
|
||||||
|
const decoder = new TextDecoder();
|
||||||
|
const process = Deno.run({
|
||||||
|
args: [Deno.execPath(), "colors.ts"],
|
||||||
|
cwd: "examples",
|
||||||
|
stdout: "piped"
|
||||||
|
});
|
||||||
|
try {
|
||||||
|
const output = await Deno.readAll(process.stdout!);
|
||||||
|
const actual = decoder.decode(output).trim();
|
||||||
|
const expected = "[44m[3m[31m[1mHello world![22m[39m[23m[49m";
|
||||||
|
assertStrictEq(actual, expected);
|
||||||
|
} finally {
|
||||||
|
process.close();
|
||||||
|
}
|
||||||
|
});
|
41
std/examples/tests/curl_test.ts
Normal file
41
std/examples/tests/curl_test.ts
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||||
|
import { Server, serve } from "../../http/server.ts";
|
||||||
|
import { assertStrictEq } from "../../testing/asserts.ts";
|
||||||
|
|
||||||
|
let server: Server | undefined;
|
||||||
|
|
||||||
|
async function startTestServer(): Promise<void> {
|
||||||
|
server = await serve({ port: 8080 });
|
||||||
|
(async (): Promise<void> => {
|
||||||
|
for await (const req of server) {
|
||||||
|
req.respond({ body: "Hello world" });
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
}
|
||||||
|
|
||||||
|
Deno.test("[examples/curl] beforeAll", async () => {
|
||||||
|
await startTestServer();
|
||||||
|
});
|
||||||
|
|
||||||
|
Deno.test("[examples/curl] send a request to a specified url", async () => {
|
||||||
|
const decoder = new TextDecoder();
|
||||||
|
const process = Deno.run({
|
||||||
|
args: [Deno.execPath(), "--allow-net", "curl.ts", "http://localhost:8080"],
|
||||||
|
cwd: "examples",
|
||||||
|
stdout: "piped"
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
const output = await Deno.readAll(process.stdout!);
|
||||||
|
const actual = decoder.decode(output).trim();
|
||||||
|
const expected = "Hello world";
|
||||||
|
|
||||||
|
assertStrictEq(actual, expected);
|
||||||
|
} finally {
|
||||||
|
process.close();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Deno.test("[examples/curl] afterAll", () => {
|
||||||
|
server?.close();
|
||||||
|
});
|
43
std/examples/tests/echo_server_test.ts
Normal file
43
std/examples/tests/echo_server_test.ts
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||||
|
import { assertStrictEq, assertNotEquals } from "../../testing/asserts.ts";
|
||||||
|
import { BufReader, ReadLineResult } from "../../io/bufio.ts";
|
||||||
|
|
||||||
|
Deno.test("[examples/echo_server]", async () => {
|
||||||
|
const encoder = new TextEncoder();
|
||||||
|
const decoder = new TextDecoder();
|
||||||
|
const process = Deno.run({
|
||||||
|
args: [Deno.execPath(), "--allow-net", "echo_server.ts"],
|
||||||
|
cwd: "examples",
|
||||||
|
stdout: "piped"
|
||||||
|
});
|
||||||
|
|
||||||
|
let conn: Deno.Conn | undefined;
|
||||||
|
try {
|
||||||
|
const processReader = new BufReader(process.stdout!);
|
||||||
|
const message = await processReader.readLine();
|
||||||
|
|
||||||
|
assertNotEquals(message, Deno.EOF);
|
||||||
|
assertStrictEq(
|
||||||
|
decoder.decode((message as ReadLineResult).line).trim(),
|
||||||
|
"Listening on 0.0.0.0:8080"
|
||||||
|
);
|
||||||
|
|
||||||
|
conn = await Deno.connect({ hostname: "127.0.0.1", port: 8080 });
|
||||||
|
const connReader = new BufReader(conn);
|
||||||
|
|
||||||
|
await conn.write(encoder.encode("Hello echo_server\n"));
|
||||||
|
const result = await connReader.readLine();
|
||||||
|
|
||||||
|
assertNotEquals(result, Deno.EOF);
|
||||||
|
|
||||||
|
const actualResponse = decoder
|
||||||
|
.decode((result as ReadLineResult).line)
|
||||||
|
.trim();
|
||||||
|
const expectedResponse = "Hello echo_server";
|
||||||
|
|
||||||
|
assertStrictEq(actualResponse, expectedResponse);
|
||||||
|
} finally {
|
||||||
|
conn?.close();
|
||||||
|
process.close();
|
||||||
|
}
|
||||||
|
});
|
2
std/examples/tests/test_test.ts
Normal file
2
std/examples/tests/test_test.ts
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||||
|
import "../test.ts";
|
19
std/examples/tests/welcome_test.ts
Normal file
19
std/examples/tests/welcome_test.ts
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||||
|
import { assertStrictEq } from "../../testing/asserts.ts";
|
||||||
|
|
||||||
|
Deno.test("[examples/welcome] print a welcome message", async () => {
|
||||||
|
const decoder = new TextDecoder();
|
||||||
|
const process = Deno.run({
|
||||||
|
args: [Deno.execPath(), "welcome.ts"],
|
||||||
|
cwd: "examples",
|
||||||
|
stdout: "piped"
|
||||||
|
});
|
||||||
|
try {
|
||||||
|
const output = await Deno.readAll(process.stdout!);
|
||||||
|
const actual = decoder.decode(output).trim();
|
||||||
|
const expected = "Welcome to Deno 🦕";
|
||||||
|
assertStrictEq(actual, expected);
|
||||||
|
} finally {
|
||||||
|
process.close();
|
||||||
|
}
|
||||||
|
});
|
|
@ -1,3 +1,4 @@
|
||||||
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||||
import { xeval } from "../xeval.ts";
|
import { xeval } from "../xeval.ts";
|
||||||
import { stringsReader } from "../../io/util.ts";
|
import { stringsReader } from "../../io/util.ts";
|
||||||
import { decode, encode } from "../../strings/mod.ts";
|
import { decode, encode } from "../../strings/mod.ts";
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue