2020-02-24 08:31:40 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-01-30 21:02:14 -05:00
|
|
|
import { xeval } from "../xeval.ts";
|
2020-06-06 10:37:52 -04:00
|
|
|
import { StringReader } from "../../io/readers.ts";
|
2020-04-01 15:23:39 -04:00
|
|
|
import { decode, encode } from "../../encoding/utf8.ts";
|
2020-02-07 02:23:38 -05:00
|
|
|
import {
|
|
|
|
assertEquals,
|
2020-06-05 23:43:00 -04:00
|
|
|
assertStringContains,
|
2020-03-28 13:03:49 -04:00
|
|
|
assert,
|
2020-02-07 02:23:38 -05:00
|
|
|
} from "../../testing/asserts.ts";
|
2020-01-30 21:02:14 -05:00
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("xevalSuccess", async function (): Promise<void> {
|
2020-01-30 21:02:14 -05:00
|
|
|
const chunks: string[] = [];
|
2020-06-06 10:37:52 -04:00
|
|
|
await xeval(new StringReader("a\nb\nc"), ($): number => chunks.push($));
|
2020-01-30 21:02:14 -05:00
|
|
|
assertEquals(chunks, ["a", "b", "c"]);
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("xevalDelimiter", async function (): Promise<void> {
|
2020-01-30 21:02:14 -05:00
|
|
|
const chunks: string[] = [];
|
2020-06-06 10:37:52 -04:00
|
|
|
await xeval(
|
|
|
|
new StringReader("!MADMADAMADAM!"),
|
|
|
|
($): number => chunks.push($),
|
|
|
|
{
|
|
|
|
delimiter: "MADAM",
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-06-06 10:37:52 -04:00
|
|
|
);
|
2020-01-30 21:02:14 -05:00
|
|
|
assertEquals(chunks, ["!MAD", "ADAM!"]);
|
|
|
|
});
|
|
|
|
|
|
|
|
const xevalPath = "examples/xeval.ts";
|
|
|
|
|
2020-03-18 19:25:55 -04:00
|
|
|
Deno.test({
|
|
|
|
name: "xevalCliReplvar",
|
2020-03-28 13:03:49 -04:00
|
|
|
fn: async function (): Promise<void> {
|
2020-06-12 15:23:38 -04:00
|
|
|
const p = Deno.run({
|
|
|
|
cmd: [
|
|
|
|
Deno.execPath(),
|
|
|
|
"run",
|
|
|
|
xevalPath,
|
|
|
|
"--replvar=abc",
|
|
|
|
"console.log(abc)",
|
|
|
|
],
|
2020-03-18 19:25:55 -04:00
|
|
|
stdin: "piped",
|
|
|
|
stdout: "piped",
|
2020-03-28 13:03:49 -04:00
|
|
|
stderr: "null",
|
2020-03-18 19:25:55 -04:00
|
|
|
});
|
|
|
|
assert(p.stdin != null);
|
|
|
|
await p.stdin.write(encode("hello"));
|
|
|
|
p.stdin.close();
|
|
|
|
assertEquals(await p.status(), { code: 0, success: true });
|
|
|
|
assertEquals(decode(await p.output()).trimEnd(), "hello");
|
|
|
|
p.close();
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2020-01-30 21:02:14 -05:00
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:09 -04:00
|
|
|
Deno.test("xevalCliSyntaxError", async function (): Promise<void> {
|
2020-06-12 15:23:38 -04:00
|
|
|
const p = Deno.run({
|
|
|
|
cmd: [Deno.execPath(), "run", xevalPath, "("],
|
2020-01-30 21:02:14 -05:00
|
|
|
stdin: "null",
|
|
|
|
stdout: "piped",
|
2020-03-28 13:03:49 -04:00
|
|
|
stderr: "piped",
|
2020-01-30 21:02:14 -05:00
|
|
|
});
|
|
|
|
assertEquals(await p.status(), { code: 1, success: false });
|
|
|
|
assertEquals(decode(await p.output()), "");
|
2020-06-05 23:43:00 -04:00
|
|
|
assertStringContains(decode(await p.stderrOutput()), "Uncaught SyntaxError");
|
2020-03-18 19:25:55 -04:00
|
|
|
p.close();
|
2020-01-30 21:02:14 -05:00
|
|
|
});
|