forked from denoland/std
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(examples): add tests for examples (denoland/deno#4094)
- Loading branch information
1 parent
55b3dcf
commit f7fe6d1
Showing
16 changed files
with
258 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Hello |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
World |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[ | ||
"string", | ||
100, | ||
{ "key": "value", "array": ["foo", "bar"] } | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"string": "foobar", | ||
"number": 123, | ||
"array": [{ "message": "hello" }] | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[1, 2] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{ "message": "hello" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" } | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. | ||
import "../test.ts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters