2020-02-24 08:31:40 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-09-26 11:54:26 -04:00
|
|
|
import { xeval } from "./xeval.ts";
|
|
|
|
import { StringReader } from "../io/readers.ts";
|
|
|
|
import { decode, encode } from "../encoding/utf8.ts";
|
2020-02-07 02:23:38 -05:00
|
|
|
import {
|
2020-09-27 06:22:32 -04:00
|
|
|
assert,
|
2020-02-07 02:23:38 -05:00
|
|
|
assertEquals,
|
2020-10-26 11:03:30 -04:00
|
|
|
assertStringIncludes,
|
2020-09-26 11:54:26 -04:00
|
|
|
} from "../testing/asserts.ts";
|
|
|
|
import { dirname, fromFileUrl } from "../path/mod.ts";
|
2020-09-09 14:57:49 -04:00
|
|
|
|
2020-09-26 11:54:26 -04:00
|
|
|
const moduleDir = dirname(fromFileUrl(import.meta.url));
|
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!"]);
|
|
|
|
});
|
|
|
|
|
2020-09-09 14:57:49 -04:00
|
|
|
const xevalPath = "xeval.ts";
|
2020-01-30 21:02:14 -05:00
|
|
|
|
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",
|
2020-11-20 12:01:58 -05:00
|
|
|
"--quiet",
|
2020-06-12 15:23:38 -04:00
|
|
|
xevalPath,
|
|
|
|
"--replvar=abc",
|
|
|
|
"console.log(abc)",
|
|
|
|
],
|
2020-09-09 14:57:49 -04:00
|
|
|
cwd: moduleDir,
|
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({
|
2020-11-20 12:01:58 -05:00
|
|
|
cmd: [Deno.execPath(), "run", "--quiet", xevalPath, "("],
|
2020-09-09 14:57:49 -04:00
|
|
|
cwd: moduleDir,
|
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-10-26 11:03:30 -04:00
|
|
|
assertStringIncludes(decode(await p.stderrOutput()), "SyntaxError");
|
2020-03-18 19:25:55 -04:00
|
|
|
p.close();
|
2020-01-30 21:02:14 -05:00
|
|
|
});
|