Skip to content

Commit

Permalink
Add some source map tests to generate-bytecode.spec.js
Browse files Browse the repository at this point in the history
  • Loading branch information
markw65 committed Mar 30, 2022
1 parent 4a0a2b6 commit 911b4ee
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 7 deletions.
69 changes: 63 additions & 6 deletions test/unit/compiler/passes/generate-bytecode.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ describe("compiler pass |generateBytecode|", () => {
};
}

function bytecodeLocationDetails(bytecode, locations) {
return {
rules: [{ bytecode }],
locations,
};
}

function constsDetails(literals, classes, expectations, functions) {
return { literals, classes, expectations, functions };
}
Expand Down Expand Up @@ -51,6 +58,24 @@ describe("compiler pass |generateBytecode|", () => {
));
});

it("generates correct source mapping", () => {
expect(pass).to.changeAST([
"a = 'a'",
].join("\n"), bytecodeLocationDetails(
[37, 0, 18, 0, 2, 2, 22, 0, 23, 0, 38],
[
{
source: "-",
start: { offset: 4, line: 1, column: 5 },
end: { offset: 7, line: 1, column: 8 },
},
]
), {
grammarSource: "-",
output: "source-and-map",
});
});

it("generates correct plucking bytecode", () => {
expect(pass).to.changeAST("start = 'a' @'b' 'c'", bytecodeDetails([
5, // PUSH_CURR_POS
Expand Down Expand Up @@ -129,16 +154,48 @@ describe("compiler pass |generateBytecode|", () => {
});

describe("for choice", () => {
it("generates correct bytecode", () => {
expect(pass).to.changeAST("start = 'a' / 'b' / 'c'", bytecodeDetails([
it("generates correct bytecode and source mapping", () => {
expect(pass).to.changeAST("start = 'a' / 'b' / 'c'", bytecodeLocationDetails([
37, 3, // SOURCE_MAP_PUSH 3
37, 0, // SOURCE_MAP_PUSH 0
18, 0, 2, 2, 22, 0, 23, 0, // <alternatives[0]>
14, 21, 0, // IF_ERROR
38, // SOURCE_MAP_POP
14, 27, 0, // IF_ERROR
6, // * POP
37, 1, // SOURCE_MAP_PUSH 1
18, 1, 2, 2, 22, 1, 23, 1, // <alternatives[1]>
14, 9, 0, // IF_ERROR
38, // SOURCE_MAP_POP
14, 12, 0, // IF_ERROR
6, // * POP
18, 2, 2, 2, 22, 2, 23, 2, // <alternatives[2]>
]));
37, 2, // SOURCE_MAP_PUSH 2
18, 2, 2, 2, 22, 2, 23, 2, // <alternatives[2]>
38, // SOURCE_MAP_POP
38, // SOURCE_MAP_POP
], [
{
source: "-",
start: { offset: 8, line: 1, column: 9 },
end: { offset: 11, line: 1, column: 12 },
},
{
source: "-",
start: { offset: 14, line: 1, column: 15 },
end: { offset: 17, line: 1, column: 18 },
},
{
source: "-",
start: { offset: 20, line: 1, column: 21 },
end: { offset: 23, line: 1, column: 24 },
},
{
source: "-",
start: { offset: 8, line: 1, column: 9 },
end: { offset: 23, line: 1, column: 24 },
},
]), {
grammarSource: "-",
output: "source-and-map",
});
});
});

Expand Down
2 changes: 1 addition & 1 deletion test/unit/compiler/passes/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ module.exports = function(chai, utils) {
}
}

const ast = parser.parse(grammar);
const ast = parser.parse(grammar, options);

utils.flag(this, "object")(ast, options, new Session({
error(stage, ...args) { throw new GrammarError(...args); },
Expand Down

0 comments on commit 911b4ee

Please sign in to comment.