Skip to content

Commit

Permalink
Add parser RTT test
Browse files Browse the repository at this point in the history
  • Loading branch information
cd1m0 committed Mar 13, 2024
1 parent 41b91f3 commit e751f83
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/ast/declarations/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class Rule extends Declaration {
.map(([num, nums]) => `${num}: (${nums.map((x) => `${x}`).join(", ")})`)
.join(", ");
}
return `${indent}${this.head.pp()} :- ${this.body.pp()}.${queryPlanStr}`;
return `${indent}${this.head.pp()} :- ${this.body.pp().slice(1, -1)}.${queryPlanStr}`;
}

children(): Iterable<Node> {
Expand Down
2 changes: 1 addition & 1 deletion src/ast/declarations/subsumption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class Subsumption extends Declaration {
.join(", ");
}

return `${indent}${this.dominatedHead.pp()} <= ${this.dominatingHead.pp()} :- ${this.body.pp()}.${queryPlanStr}`;
return `${indent}${this.dominatedHead.pp()} <= ${this.dominatingHead.pp()} :- ${this.body.pp().slice(1, -1)}.${queryPlanStr}`;
}

children(): Iterable<Node> {
Expand Down
2 changes: 1 addition & 1 deletion src/ast/expressions/binary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class BinaryOperator extends Expression {
}

pp(): string {
return `(${this.left.pp()} ${this.op} ${this.right.pp()})`;
return `${this.left.pp()} ${this.op} ${this.right.pp()}`;
}

children(): Iterable<Node> {
Expand Down
2 changes: 1 addition & 1 deletion src/ast/expressions/unary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class UnaryOperator extends Expression {
}

pp(): string {
return `(${this.op}${this.subExpr.pp()})`;
return `${this.op}${this.subExpr.pp()}`;
}

children(): Iterable<Node> {
Expand Down
2 changes: 1 addition & 1 deletion src/ast/rules/constraints/comparison.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class Comparison extends Constraint {
}

pp(): string {
return `(${this.lhs.pp()} ${this.op} ${this.rhs.pp()})`;
return `${this.lhs.pp()} ${this.op} ${this.rhs.pp()}`;
}

children(): Iterable<Node> {
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./instance";
export * from "./types";
export * from "./relation";
export * from "./utils";
41 changes: 41 additions & 0 deletions test/parser_rtt.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import expect from "expect";
import fse from "fs-extra";
import { searchRecursive } from "../src";
import { Program } from "../src/ast";
import { parseProgram } from "../src/parser";

function ppProg(p: Program): string {
return p.map((n) => n.pp()).join("\n");
}

describe(`Parse Round-trip tests`, () => {
for (const dl of searchRecursive("test/samples/", (name) => name.endsWith(".dl"))) {
describe(`Sample ${dl}`, () => {
let contents: string;
let prog: Program;
let output: string;
let reParsedProg: Program;

beforeAll(() => {
contents = fse.readFileSync(dl, { encoding: "utf-8" });
});

it("Parses correctly", () => {
expect(() => {
prog = parseProgram(contents);
}).not.toThrow();
});

it("Re-parses after outputting correctly", () => {
expect(() => {
output = ppProg(prog);
reParsedProg = parseProgram(output);
}).not.toThrow();
});

it("Produces an equivalent AST to the original", () => {
expect(output).toEqual(ppProg(reParsedProg));
});
});
}
});

0 comments on commit e751f83

Please sign in to comment.