Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Issue pegjs#38 (import feature): Add testcases for import clause.
  • Loading branch information
Mingun committed Nov 26, 2016
1 parent c9bdb3d commit e0b1313
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 6 deletions.
4 changes: 2 additions & 2 deletions spec/unit/compiler/passes/helpers.js
Expand Up @@ -55,7 +55,7 @@ beforeEach(function() {
+ "to match " + jasmine.pp(details) + ", "
+ "but it " + (this.isNot ? "did" : "didn't") + ".";

return matchDetails(ast, details);
return this.isNot ^ matchDetails(ast, details);
},

toReportError(grammar, details) {
Expand All @@ -68,7 +68,7 @@ beforeEach(function() {
this.message = () =>
"Expected the pass not to report an error "
+ "for grammar " + jasmine.pp(grammar) + ", "
+ "but it did.";
+ "but it did: " + jasmine.pp(e) + ".";
} else {
if (details) {
let keys = Object.keys(details);
Expand Down
47 changes: 47 additions & 0 deletions spec/unit/compiler/passes/remove-proxy-rules.spec.js
Expand Up @@ -52,4 +52,51 @@ describe("compiler pass |removeProxyRules|", function() {
);
});
});

describe("when a proxy rule is reffered to imported rule", function() {
it("do not update references and doesn't remove it", function() {
expect(pass).not.toChangeAST(
[
'start = notProxy',
'notProxy = #notProxied',
'notProxied = "a"'
].join("\n"),
{
rules: [
{
name: "start",
expression: { type: "rule_ref", name: "notProxy" }
},
{
name: "notProxy",
expression: { type: "rule_ref", namespace: "notProxied", name: null }
},
{ name: "notProxied" }
]
},
{ allowedStartRules: ["start"] }
);
expect(pass).not.toChangeAST(
[
'start = notProxy',
'notProxy = #imported:notProxied',
'notProxied = "a"'
].join("\n"),
{
rules: [
{
name: "start",
expression: { type: "rule_ref", name: "notProxy" }
},
{
name: "notProxy",
expression: { type: "rule_ref", namespace: "imported", name: "notProxied" }
},
{ name: "notProxied" }
]
},
{ allowedStartRules: ["start", "notProxy"] }
);
});
});
});
15 changes: 15 additions & 0 deletions spec/unit/compiler/passes/report-infinite-recursion.spec.js
Expand Up @@ -112,4 +112,19 @@ describe("compiler pass |reportInfiniteRecursion|", function() {
expect(pass).not.toReportError("start = . start");
});
});

it("not check imported rules", function() {
expect(pass).not.toReportError('start = #start');
expect(pass).not.toReportError('start = #start:start');

expect(pass).not.toReportError([
'start = stop',
'stop = #start'
].join('\n'));
expect(pass).not.toReportError([
'start = stop',
'stop = #start:start'
].join('\n'));
});

});
5 changes: 5 additions & 0 deletions spec/unit/compiler/passes/report-undefined-rules.spec.js
Expand Up @@ -14,4 +14,9 @@ describe("compiler pass |reportUndefinedRules|", function() {
}
});
});

it("not report imported rules as undefined", function() {
expect(pass).not.toReportError('start = #imported');
expect(pass).not.toReportError('start = #imported:rule');
});
});
50 changes: 46 additions & 4 deletions spec/unit/parser.spec.js
Expand Up @@ -256,6 +256,37 @@ describe("PEG.js grammar parser", function() {
);
});

// Canonical Import is "#alias = 'path'".
it("parses Import", function() {
expect('#alias = "path/to/grammar";start = "abcd"').toParseAs({
type: "grammar",
imports: [{ type: "import", alias: "alias", path: "path/to/grammar" }],
initializer: null,
rules: [ruleStart]
});
expect("#alias = 'path/to/grammar';start = 'abcd'").toParseAs({
type: "grammar",
imports: [{ type: "import", alias: "alias", path: "path/to/grammar" }],
initializer: null,
rules: [ruleStart]
});
expect("#alias1 = 'path/to/grammar1'\n#alias2='path/to/grammar2'\nstart = 'abcd'").toParseAs({
type: "grammar",
imports: [
{ type: "import", alias: "alias1", path: "path/to/grammar1" },
{ type: "import", alias: "alias2", path: "path/to/grammar2" }
],
initializer: null,
rules: [ruleStart]
});
expect("#alias = 'path/to/grammar';{ code };start = 'abcd'").toParseAs({
type: "grammar",
imports: [{ type: "import", alias: "alias", path: "path/to/grammar" }],
initializer: initializer,
rules: [ruleStart]
});
});

// Canonical Initializer is "{ code }".
it("parses Initializer", function() {
expect("{ code };start = 'abcd'").toParseAs(
Expand Down Expand Up @@ -364,11 +395,22 @@ describe("PEG.js grammar parser", function() {
});

// Canonical RuleReferenceExpression is "a".
it("parses RuleReferenceExpression", function() {
expect("start = a").toParseAs(ruleRefGrammar("a"));
describe("parses RuleReferenceExpression", function() {
it("refer to rule defined in this grammar", function() {
expect("start = a").toParseAs(ruleRefGrammar("a"));

expect("start = a\n=").toFailToParse();
expect("start = a\n'abcd'\n=").toFailToParse();
});

expect("start = a\n=").toFailToParse();
expect("start = a\n'abcd'\n=").toFailToParse();
it("refer to rule imported from other grammar", function() {
expect("start = #alias").toParseAs(oneRuleGrammar({ type: "rule_ref", namespace: "alias", name: null }));
expect("start = #\nalias").toFailToParse();
expect("start = #alias:").toFailToParse();
expect("start = #alias:a").toParseAs(oneRuleGrammar({ type: "rule_ref", namespace: "alias", name: "a" }));
expect("start = #alias\n:a").toFailToParse();
expect("start = #alias:\na").toFailToParse();
});
});

// Canonical SemanticPredicateExpression is "!{ code }".
Expand Down

0 comments on commit e0b1313

Please sign in to comment.