Skip to content

Commit

Permalink
tests(parser): add token-ASTData-mapper.test, #38
Browse files Browse the repository at this point in the history
  • Loading branch information
DiscreteTom committed Jan 7, 2024
1 parent bc048c1 commit 271c8eb
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions tests/parser/token-ASTData-mapper.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { ELR, Lexer } from "../../src";

describe("ensure TNode's data is generated", () => {
test("ParserBuilder", () => {
const { parser } = new ELR.ParserBuilder({
lexer: new Lexer.Builder().define({ a: /a/ }).build(),
})
.data("")
.mapper({
a: (token) => token.content,
})
.define({ entry: "a a a" })
.build({ entry: "entry" });

const res = parser.parseAll("aaa");
expect(res.accept).toBe(true);

if (res.accept) {
expect(res.buffer[0].asNT().children[0].asT().data).toBe("a");
}
});
test("AdvancedBuilder", () => {
const { parser } = new ELR.AdvancedBuilder({
lexer: new Lexer.Builder().define({ a: /a/ }).build(),
})
.data("")
.mapper({
a: (token) => token.content,
})
.define({ entry: "a a a" })
.build({ entry: "entry" });

const res = parser.parseAll("aaa");
expect(res.accept).toBe(true);

if (res.accept) {
expect(res.buffer[0].asNT().children[0].asT().data).toBe("a");
}
});
});

0 comments on commit 271c8eb

Please sign in to comment.