forked from peggyjs/peggy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Errors: Fix prototype chain and add ability to append diagnostic note…
…s to the `GrammarError`
- Loading branch information
Showing
3 changed files
with
255 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
"use strict"; | ||
|
||
const chai = require("chai"); | ||
const GrammarError = require("../../lib/grammar-error"); | ||
|
||
const expect = chai.expect; | ||
|
||
const location = { | ||
source: undefined, | ||
start: { offset: 0, line: 1, column: 1 }, | ||
end: { offset: 4, line: 1, column: 5 } | ||
}; | ||
|
||
describe("Grammar Errors", () => { | ||
it("might not have a location", () => { | ||
const e = new GrammarError("message"); | ||
expect(e.location).to.equal(undefined); | ||
expect(e.toString()).to.equal("GrammarError: message"); | ||
}); | ||
it("might have locations", () => { | ||
location.source = undefined; | ||
let e = new GrammarError("message", location); | ||
expect(e.location).to.eql(location); | ||
expect(e.toString()).to.equal(`\ | ||
GrammarError: message | ||
at 1:1`); | ||
|
||
e = new GrammarError("message", null, [{ message: "Subinfo", location }]); | ||
expect(e.location).to.equal(null); | ||
expect(e.toString()).to.equal(`\ | ||
GrammarError: message | ||
from 1:1: Subinfo`); | ||
|
||
location.source = "foo.peggy"; | ||
e = new GrammarError("message", location, [{ message: "Subinfo", location }]); | ||
expect(e.toString()).to.equal(`\ | ||
GrammarError: message | ||
at foo.peggy:1:1 | ||
from foo.peggy:1:1: Subinfo`); | ||
}); | ||
|
||
it("formats", () => { | ||
location.source = "foo.peggy"; | ||
const source = { | ||
source: "foo.peggy", | ||
text: "some error\nthat" | ||
}; | ||
let e = new GrammarError("message", location, [{ | ||
message: "Subinfo", | ||
location: { | ||
source: "foo.peggy", | ||
start: { offset: 5, line: 1, column: 6 }, | ||
end: { offset: 11, line: 2, column: 1 } | ||
} | ||
}]); | ||
expect(e.format([source])).to.equal(`\ | ||
Error: message | ||
--> foo.peggy:1:1 | ||
| | ||
1 | some error | ||
| ^^^^ | ||
note: Subinfo | ||
--> foo.peggy:1:6 | ||
| | ||
1 | some error | ||
| ^^^^^`); | ||
expect (e.format([])).to.equal(`\ | ||
Error: message | ||
at foo.peggy:1:1 | ||
at foo.peggy:1:6: Subinfo`); | ||
|
||
e = new GrammarError("message", null, [{ | ||
message: "Subinfo", | ||
location: { | ||
source: "foo.peggy", | ||
start: { offset: 5, line: 1, column: 6 }, | ||
end: { offset: 11, line: 2, column: 1 } | ||
} | ||
}]); | ||
expect(e.format([source])).to.equal(`\ | ||
Error: message | ||
note: Subinfo | ||
--> foo.peggy:1:6 | ||
| | ||
1 | some error | ||
| ^^^^^`); | ||
}); | ||
}); |