2023-08-06 06:38:52 +00:00
|
|
|
import { parse } from "./notcl";
|
|
|
|
|
|
|
|
describe("Parsing Notcl", () => {
|
|
|
|
describe("Commands", () => {
|
2023-08-06 23:52:00 +00:00
|
|
|
it("can parse an empty script", () =>
|
2023-08-06 06:38:52 +00:00
|
|
|
expect(parse("")).toEqual([true, []]));
|
2023-08-06 23:52:00 +00:00
|
|
|
|
|
|
|
it("can parse a one-word command", () =>
|
2023-08-07 03:18:13 +00:00
|
|
|
expect(parse("a")).toEqual([true, [[{ enchanted: "a" }]]]));
|
2023-08-06 23:52:00 +00:00
|
|
|
|
|
|
|
it("can parse a multi-word command", () =>
|
|
|
|
expect(parse("a b c")).toEqual([
|
|
|
|
true,
|
2023-08-07 03:18:13 +00:00
|
|
|
[[{ enchanted: "a" }, { enchanted: "b" }, { enchanted: "c" }]],
|
2023-08-06 23:52:00 +00:00
|
|
|
]));
|
|
|
|
|
|
|
|
it("accepts newlines as command separators", () =>
|
|
|
|
expect(parse("a\nb")).toEqual([
|
|
|
|
true,
|
2023-08-07 03:18:13 +00:00
|
|
|
[[{ enchanted: "a" }], [{ enchanted: "b" }]],
|
2023-08-06 23:52:00 +00:00
|
|
|
]));
|
|
|
|
it("does not split commands on folded newlines", () =>
|
|
|
|
expect(
|
|
|
|
parse(String.raw`a\
|
|
|
|
b`)
|
2023-08-07 03:18:13 +00:00
|
|
|
).toEqual([true, [[{ enchanted: "a" }, { enchanted: "b" }]]]));
|
2023-08-07 00:18:38 +00:00
|
|
|
it("does split words on folded newlines", () =>
|
|
|
|
expect(
|
|
|
|
parse(String.raw`a\
|
|
|
|
b`)
|
2023-08-07 03:18:13 +00:00
|
|
|
).toEqual([true, [[{ enchanted: "a" }, { enchanted: "b" }]]]));
|
2023-08-07 00:18:38 +00:00
|
|
|
it("does split commands on newlines with escaped backslashes", () =>
|
2023-08-06 23:52:00 +00:00
|
|
|
expect(
|
|
|
|
parse(String.raw`a\\
|
|
|
|
b`)
|
2023-08-07 03:18:13 +00:00
|
|
|
).toEqual([true, [[{ text: "a\\\\" }], [{ enchanted: "b" }]]]));
|
2023-08-06 23:52:00 +00:00
|
|
|
it("does not split commands on folded newlines with escaped backslashes", () =>
|
|
|
|
expect(
|
|
|
|
parse(String.raw`a\\\
|
|
|
|
b`)
|
2023-08-07 03:18:13 +00:00
|
|
|
).toEqual([true, [[{ text: "a\\\\" }, { enchanted: "b" }]]]));
|
2023-08-06 23:52:00 +00:00
|
|
|
|
|
|
|
it("accepts semicolons as command separators", () =>
|
2023-08-07 03:18:13 +00:00
|
|
|
expect(parse("a;b")).toEqual([
|
|
|
|
true,
|
|
|
|
[[{ enchanted: "a" }], [{ enchanted: "b" }]],
|
|
|
|
]));
|
2023-08-06 23:52:00 +00:00
|
|
|
|
|
|
|
it("tolerates, and ignores, empty commands", () =>
|
|
|
|
expect(parse("a;;b\n\nc")).toEqual([
|
|
|
|
true,
|
2023-08-07 03:18:13 +00:00
|
|
|
[[{ enchanted: "a" }], [{ enchanted: "b" }], [{ enchanted: "c" }]],
|
2023-08-06 23:52:00 +00:00
|
|
|
]));
|
|
|
|
|
|
|
|
test.each([[" a"], ["a "], ["a ;"], ["; a"]])(
|
|
|
|
"tolerates whitespace before and after commands {%s}",
|
2023-08-07 03:18:13 +00:00
|
|
|
(text) => expect(parse(text)).toEqual([true, [[{ enchanted: "a" }]]])
|
2023-08-06 23:52:00 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("Comments", () => {
|
|
|
|
it("ignores comments", () => expect(parse("#comment")).toEqual([true, []]));
|
|
|
|
it("does not treat # in argument position as a comment", () =>
|
2023-08-07 03:18:13 +00:00
|
|
|
expect(parse("a #1")).toEqual([
|
|
|
|
true,
|
|
|
|
[[{ enchanted: "a" }, { enchanted: "#1" }]],
|
|
|
|
]));
|
2023-08-06 23:52:00 +00:00
|
|
|
it("can have commands before a comment", () =>
|
2023-08-07 03:18:13 +00:00
|
|
|
expect(parse("a ;#comment")).toEqual([true, [[{ enchanted: "a" }]]]));
|
2023-08-06 23:52:00 +00:00
|
|
|
|
|
|
|
it("ignores the whole line after a comment", () =>
|
|
|
|
expect(parse("# comment ; not a command")).toEqual([true, []]));
|
|
|
|
|
|
|
|
it("continues the comment through a folded newline", () =>
|
|
|
|
expect(
|
|
|
|
parse(String.raw`#a\
|
|
|
|
b`)
|
|
|
|
).toEqual([true, []]));
|
|
|
|
it("does not continue the comment through a newline with escaped backslashes", () =>
|
|
|
|
expect(
|
|
|
|
parse(String.raw`#a\\
|
|
|
|
b`)
|
2023-08-07 03:18:13 +00:00
|
|
|
).toEqual([true, [[{ enchanted: "b" }]]]));
|
2023-08-06 23:52:00 +00:00
|
|
|
it("continues the comment through a folded newline with escaped backslashes", () =>
|
|
|
|
expect(
|
|
|
|
parse(String.raw`#a\\\
|
|
|
|
b`)
|
|
|
|
).toEqual([true, []]));
|
2023-08-06 06:38:52 +00:00
|
|
|
});
|
2023-08-06 23:52:00 +00:00
|
|
|
|
|
|
|
// Enchanted Words
|
|
|
|
|
|
|
|
// Braces
|
|
|
|
// nesting
|
|
|
|
// escapes
|
|
|
|
// {, }, correct newline folding
|
2023-08-07 03:10:12 +00:00
|
|
|
// no trailing chars
|
2023-08-06 23:52:00 +00:00
|
|
|
|
|
|
|
// "Quotes"
|
2023-08-07 03:10:12 +00:00
|
|
|
// no trailing chars
|
2023-08-06 23:52:00 +00:00
|
|
|
|
|
|
|
// Interpolated words
|
|
|
|
// bare
|
|
|
|
// quotes
|
|
|
|
// variables- bare, brace
|
|
|
|
// command subst
|
2023-08-07 03:10:12 +00:00
|
|
|
|
|
|
|
describe("Misc", () => {
|
|
|
|
test("Big mess of markup", () => {
|
|
|
|
expect(
|
|
|
|
parse(String.raw`
|
|
|
|
h1 "Hello, World!"
|
|
|
|
para [2 + 2]
|
|
|
|
block {
|
|
|
|
This is a paragraph of text, with one [b bold] word. Yes, this means there has to be some magic in text processing... <b>this</b> won't work.
|
|
|
|
}
|
|
|
|
block -red "Beware!"
|
|
|
|
para "All text should be quoted, it's clearer that way. & blockquotes already should contain paragraphs. (maybe normalize nested paragraphs)"
|
|
|
|
block {
|
|
|
|
First block
|
|
|
|
} {
|
|
|
|
Second block
|
|
|
|
|
|
|
|
Is this markdown-parsed?
|
|
|
|
|
|
|
|
[button "No we want to render UI" \\{noop}]
|
|
|
|
} {
|
|
|
|
Since we want escapes to work, these blocks [i will] be subject to substitutions.
|
|
|
|
}
|
|
|
|
# A comment
|
|
|
|
para {
|
|
|
|
line endings escaped\
|
|
|
|
one slash
|
|
|
|
|
|
|
|
not escaped if \\
|
|
|
|
two slashes
|
|
|
|
|
|
|
|
escaped with a slash if \\\
|
|
|
|
three slashes
|
|
|
|
|
|
|
|
not escaped with two slashes if \\\\
|
|
|
|
four slashes
|
|
|
|
|
|
|
|
escaped with two slashes if \\\\\
|
|
|
|
five slashes
|
|
|
|
|
|
|
|
not escaped with three slashes if \\\\\\
|
|
|
|
six slashes
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
});
|
2023-08-06 06:38:52 +00:00
|
|
|
});
|