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";
|
|
|
|
import { stringsReader } from "../../io/util.ts";
|
|
|
|
import { decode, encode } from "../../strings/mod.ts";
|
2020-02-07 02:23:38 -05:00
|
|
|
import {
|
|
|
|
assertEquals,
|
|
|
|
assertStrContains,
|
|
|
|
assert
|
|
|
|
} from "../../testing/asserts.ts";
|
2020-01-30 21:02:14 -05:00
|
|
|
const { execPath, run } = Deno;
|
|
|
|
|
2020-02-11 11:24:27 -05:00
|
|
|
Deno.test(async function xevalSuccess(): Promise<void> {
|
2020-01-30 21:02:14 -05:00
|
|
|
const chunks: string[] = [];
|
|
|
|
await xeval(stringsReader("a\nb\nc"), ($): number => chunks.push($));
|
|
|
|
assertEquals(chunks, ["a", "b", "c"]);
|
|
|
|
});
|
|
|
|
|
2020-02-11 11:24:27 -05:00
|
|
|
Deno.test(async function xevalDelimiter(): Promise<void> {
|
2020-01-30 21:02:14 -05:00
|
|
|
const chunks: string[] = [];
|
|
|
|
await xeval(stringsReader("!MADMADAMADAM!"), ($): number => chunks.push($), {
|
|
|
|
delimiter: "MADAM"
|
|
|
|
});
|
|
|
|
assertEquals(chunks, ["!MAD", "ADAM!"]);
|
|
|
|
});
|
|
|
|
|
|
|
|
// https://github.com/denoland/deno/issues/2861
|
|
|
|
const xevalPath = "examples/xeval.ts";
|
|
|
|
|
2020-02-11 11:24:27 -05:00
|
|
|
Deno.test(async function xevalCliReplvar(): Promise<void> {
|
2020-01-30 21:02:14 -05:00
|
|
|
const p = run({
|
2020-02-17 19:51:13 -05:00
|
|
|
args: [execPath(), xevalPath, "--replvar=abc", "console.log(abc)"],
|
2020-01-30 21:02:14 -05:00
|
|
|
stdin: "piped",
|
|
|
|
stdout: "piped",
|
|
|
|
stderr: "null"
|
|
|
|
});
|
2020-02-07 02:23:38 -05:00
|
|
|
assert(p.stdin != null);
|
|
|
|
await p.stdin.write(encode("hello"));
|
|
|
|
await p.stdin.close();
|
2020-01-30 21:02:14 -05:00
|
|
|
assertEquals(await p.status(), { code: 0, success: true });
|
|
|
|
assertEquals(decode(await p.output()).trimEnd(), "hello");
|
|
|
|
});
|
|
|
|
|
2020-02-11 11:24:27 -05:00
|
|
|
Deno.test(async function xevalCliSyntaxError(): Promise<void> {
|
2020-01-30 21:02:14 -05:00
|
|
|
const p = run({
|
2020-02-17 19:51:13 -05:00
|
|
|
args: [execPath(), xevalPath, "("],
|
2020-01-30 21:02:14 -05:00
|
|
|
stdin: "null",
|
|
|
|
stdout: "piped",
|
|
|
|
stderr: "piped"
|
|
|
|
});
|
|
|
|
assertEquals(await p.status(), { code: 1, success: false });
|
|
|
|
assertEquals(decode(await p.output()), "");
|
|
|
|
assertStrContains(decode(await p.stderrOutput()), "Uncaught SyntaxError");
|
|
|
|
});
|