-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add missing members to parser exception type declarations (#1322)
- Loading branch information
Showing
7 changed files
with
196 additions
and
90 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
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 |
---|---|---|
@@ -1,45 +1,120 @@ | ||
import * as ErrorStackParser from "error-stack-parser" | ||
import { createTokenInstance, EOF } from "../../src/scan/tokens_public" | ||
import { | ||
EarlyExitException, | ||
NoViableAltException, | ||
NotAllInputParsedException, | ||
MismatchedTokenException | ||
} from "../../src/parse/exceptions_public" | ||
import { functionName } from "../../src/lang/lang_extensions" | ||
|
||
describe("Chevrotain's Parsing Exceptions", () => { | ||
describe("the mappings between a an exception instance and its matching an exception's name for: ", () => { | ||
let isRunningInNodeJS = module && module.exports | ||
let it_node = isRunningInNodeJS ? it : it.skip | ||
describe("the exception instance subclasses Error with the right properties for: ", () => { | ||
let currentToken = createTokenInstance(EOF, "cur", -1, -1, -1, -1, -1, -1) | ||
let previousToken = createTokenInstance(EOF, "prv", -1, -1, -1, -1, -1, -1) | ||
|
||
let dummyToken = createTokenInstance(EOF, "", -1, -1, -1, -1, -1, -1) | ||
|
||
it_node("EarlyExitException", () => { | ||
let exceptionInstance = new EarlyExitException("", dummyToken, dummyToken) | ||
expect(exceptionInstance.name).to.equal(functionName(exceptionInstance)) | ||
it("EarlyExitException", () => { | ||
let exceptionInstance = new EarlyExitException( | ||
"error message", | ||
currentToken, | ||
previousToken | ||
) | ||
expect(exceptionInstance).to.be.an.instanceOf(EarlyExitException) | ||
expect(exceptionInstance).to.be.an.instanceOf(Error) | ||
expect(exceptionInstance.name).to.equal("EarlyExitException") | ||
expect(exceptionInstance.message).to.equal("error message") | ||
expect(exceptionInstance.token).to.equal(currentToken) | ||
expect(exceptionInstance.previousToken).to.equal(previousToken) | ||
expect(exceptionInstance.resyncedTokens).to.be.empty | ||
}) | ||
|
||
it_node("NoViableAltException", () => { | ||
it("NoViableAltException", () => { | ||
let exceptionInstance = new NoViableAltException( | ||
"", | ||
dummyToken, | ||
dummyToken | ||
"error message", | ||
currentToken, | ||
previousToken | ||
) | ||
expect(exceptionInstance.name).to.equal(functionName(exceptionInstance)) | ||
expect(exceptionInstance).to.be.an.instanceOf(NoViableAltException) | ||
expect(exceptionInstance).to.be.an.instanceOf(Error) | ||
expect(exceptionInstance.name).to.equal("NoViableAltException") | ||
expect(exceptionInstance.message).to.equal("error message") | ||
expect(exceptionInstance.token).to.equal(currentToken) | ||
expect(exceptionInstance.previousToken).to.equal(previousToken) | ||
expect(exceptionInstance.resyncedTokens).to.be.empty | ||
}) | ||
|
||
it_node("NotAllInputParsedException", () => { | ||
let exceptionInstance = new NotAllInputParsedException("", dummyToken) | ||
expect(exceptionInstance.name).to.equal(functionName(exceptionInstance)) | ||
it("NotAllInputParsedException", () => { | ||
let exceptionInstance = new NotAllInputParsedException( | ||
"error message", | ||
currentToken | ||
) | ||
expect(exceptionInstance).to.be.an.instanceOf(NotAllInputParsedException) | ||
expect(exceptionInstance).to.be.an.instanceOf(Error) | ||
expect(exceptionInstance.name).to.equal("NotAllInputParsedException") | ||
expect(exceptionInstance.message).to.equal("error message") | ||
expect(exceptionInstance.token).to.equal(currentToken) | ||
expect(exceptionInstance.resyncedTokens).to.be.empty | ||
}) | ||
|
||
it_node("MismatchedTokenException", () => { | ||
it("MismatchedTokenException", () => { | ||
let exceptionInstance = new MismatchedTokenException( | ||
"", | ||
dummyToken, | ||
dummyToken | ||
"error message", | ||
currentToken, | ||
previousToken | ||
) | ||
expect(exceptionInstance).to.be.an.instanceOf(MismatchedTokenException) | ||
expect(exceptionInstance).to.be.an.instanceOf(Error) | ||
expect(exceptionInstance.name).to.equal("MismatchedTokenException") | ||
expect(exceptionInstance.message).to.equal("error message") | ||
expect(exceptionInstance.token).to.equal(currentToken) | ||
expect(exceptionInstance.resyncedTokens).to.be.empty | ||
}) | ||
}) | ||
|
||
describe("the exception instance stacktrace is valid for: ", () => { | ||
let dummyToken = createTokenInstance(EOF, "cur", -1, -1, -1, -1, -1, -1) | ||
|
||
function throwAndCatchException(errorFactory: () => Error) { | ||
try { | ||
throw errorFactory() | ||
} catch (e) { | ||
return e | ||
} | ||
} | ||
|
||
it("EarlyExitException", () => { | ||
let exceptionInstance = throwAndCatchException( | ||
() => new EarlyExitException("", dummyToken, dummyToken) | ||
) | ||
let stacktrace = ErrorStackParser.parse(exceptionInstance) | ||
expect(stacktrace[0].functionName).to.be.undefined // lambda function | ||
expect(stacktrace[1].functionName).to.equal("throwAndCatchException") | ||
}) | ||
|
||
it("NoViableAltException", () => { | ||
let exceptionInstance = throwAndCatchException( | ||
() => new NoViableAltException("", dummyToken, dummyToken) | ||
) | ||
let stacktrace = ErrorStackParser.parse(exceptionInstance) | ||
expect(stacktrace[0].functionName).to.be.undefined // lambda function | ||
expect(stacktrace[1].functionName).to.equal("throwAndCatchException") | ||
}) | ||
|
||
it("NotAllInputParsedException", () => { | ||
let exceptionInstance = throwAndCatchException( | ||
() => new NotAllInputParsedException("", dummyToken) | ||
) | ||
let stacktrace = ErrorStackParser.parse(exceptionInstance) | ||
expect(stacktrace[0].functionName).to.be.undefined // lambda function | ||
expect(stacktrace[1].functionName).to.equal("throwAndCatchException") | ||
}) | ||
|
||
it("MismatchedTokenException", () => { | ||
let exceptionInstance = throwAndCatchException( | ||
() => new MismatchedTokenException("", dummyToken, dummyToken) | ||
) | ||
expect(exceptionInstance.name).to.equal(functionName(exceptionInstance)) | ||
let stacktrace = ErrorStackParser.parse(exceptionInstance) | ||
expect(stacktrace[0].functionName).to.be.undefined // lambda function | ||
expect(stacktrace[1].functionName).to.equal("throwAndCatchException") | ||
}) | ||
}) | ||
}) |
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