You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
2.4 KiB
83 lines
2.4 KiB
import { parse, SyntaxError } from "./NomScript.peggy";
|
|
|
|
describe("parse", () => {
|
|
describe("ScriptFile", () => {
|
|
test("works with an empty input", () => {
|
|
expect(() => parse("", {grammarSource: "testData", start: "ScriptFile"})).not.toThrow()
|
|
});
|
|
test("works with empty lines", () => {
|
|
expect(() => parse("\n\n\n\n\n", {grammarSource: "testData", start: "ScriptFile"})).not.toThrow()
|
|
});
|
|
test("works with lines filled only with whitespace", () => {
|
|
expect(() => parse("\n\n \n\t\t \t\n\n", {grammarSource: "testData", start: "ScriptFile"})).not.toThrow()
|
|
});
|
|
test("works with lines filled only with comments", () => {
|
|
expect(() => parse("\n\n /* */ \n// test \n /* */\n", {grammarSource: "testData", start: "ScriptFile"})).not.toThrow()
|
|
});
|
|
test("throws SyntaxError for invalid text", () => {
|
|
expect(() => parse("!! invalid !!", {grammarSource: "testData", start: "ScriptFile"})).toThrow(SyntaxError)
|
|
});
|
|
})
|
|
});
|
|
|
|
describe("SyntaxError", () => {
|
|
describe("constructor", () => {
|
|
test("populates the members accordingly", () => {
|
|
const location = {
|
|
source: "source",
|
|
start: {
|
|
offset: 0,
|
|
line: 1,
|
|
column: 1,
|
|
},
|
|
end: {
|
|
offset: 7,
|
|
line: 1,
|
|
column: 8,
|
|
},
|
|
};
|
|
const err = new SyntaxError("message", ["expected"], "found", location);
|
|
|
|
expect(err.name).toEqual("SyntaxError");
|
|
expect(err.message).toEqual("message");
|
|
expect(err.expected).toEqual(["expected"]);
|
|
expect(err.found).toEqual("found");
|
|
expect(err.location).toEqual(location);
|
|
});
|
|
});
|
|
describe("format", () => {
|
|
test("displays a pretty error message for the failure", () => {
|
|
const location = {
|
|
source: "source",
|
|
start: {
|
|
offset: 0,
|
|
line: 1,
|
|
column: 1,
|
|
},
|
|
end: {
|
|
offset: 7,
|
|
line: 1,
|
|
column: 8,
|
|
},
|
|
};
|
|
const err = new SyntaxError(
|
|
"message",
|
|
["valid", "cool"],
|
|
"errored",
|
|
location
|
|
);
|
|
const result = err.format([
|
|
{ source: "source", text: "errored text" },
|
|
{ source: "source2", text: "irrelevant" },
|
|
]);
|
|
|
|
expect(result).toMatchInlineSnapshot(`
|
|
"Error: message
|
|
--> source:1:1
|
|
|
|
|
1 | errored text
|
|
| ^^^^^^^"
|
|
`);
|
|
});
|
|
});
|
|
});
|
|
|