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!"]);
|
|
|
|
});
|
|
|
|
|
|
|
|
const xevalPath = "examples/xeval.ts";
|
|
|
|
|
2020-03-18 19:25:55 -04:00
|
|
|
Deno.test({
|
|
|
|
name: "xevalCliReplvar",
|
|
|
|
fn: async function(): Promise<void> {
|
|
|
|
const p = run({
|
|
|
|
args: [execPath(), xevalPath, "--replvar=abc", "console.log(abc)"],
|
|
|
|
stdin: "piped",
|
|
|
|
stdout: "piped",
|
|
|
|
stderr: "null"
|
|
|
|
});
|
|
|
|
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-01-30 21:02:14 -05:00
|
|
|
});
|
|
|
|
|
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");
|
2020-03-18 19:25:55 -04:00
|
|
|
p.close();
|
2020-01-30 21:02:14 -05:00
|
|
|
});
|