forked from sjbarag/brs
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
121 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,42 @@ | ||
sub main() | ||
a = 5 | ||
|
||
print "[pre_try] a = " a | ||
print "[pre_try] a =" a | ||
try | ||
a = a * 2 | ||
print "[in_try] a = " a | ||
print "[in_try] a =" a | ||
subFunc(a) | ||
catch e | ||
print "[in_catch] e = " e.message | ||
print "[in_catch] message = " e.message | ||
print "[in_catch] customField = " e.customField | ||
for each bt in e.backtrace | ||
print "[backtrace] = " bt.line_number | ||
print "[backtrace] =" bt.line_number | ||
end for | ||
end try | ||
|
||
print "[post_try] a = " a | ||
print "[post_try] a =" a | ||
subFunc(a + 1) | ||
end sub | ||
|
||
function subFunc(a) | ||
try | ||
print "[subFunc] a = " a | ||
thirdLevel() | ||
print "[subFunc] a =" a | ||
if a = 10 | ||
thirdLevel() | ||
end if | ||
a = a * "" ' force a type error | ||
catch e | ||
print e.number | ||
e.customField = false | ||
throw e | ||
print "Error # =" e.number | ||
if a = 10 | ||
e.customField = true | ||
throw e | ||
else | ||
print "Error message = " e.message | ||
end if | ||
endtry | ||
end function | ||
|
||
sub thirdLevel() | ||
print "[thirdLevel]" | ||
throw {message: "subFunc custom error message!", number: 6502, customField: true} | ||
throw {message: "subFunc custom error message!", number: 6502, customField: false} | ||
end sub |
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,83 @@ | ||
const Expr = require("../../lib/parser/Expression"); | ||
const Stmt = require("../../lib/parser/Statement"); | ||
const { Interpreter } = require("../../lib/interpreter"); | ||
const brs = require("../../lib"); | ||
const { Lexeme } = brs.lexer; | ||
const { Int32, BrsString } = brs.types; | ||
const { createMockStreams, allArgs } = require("../e2e/E2ETests"); | ||
const { token, identifier } = require("../parser/ParserTests"); | ||
|
||
let interpreter, stdout; | ||
|
||
describe("interpreter try-catch blocks", () => { | ||
const printError = new Stmt.Print({ print: token(Lexeme.Print, "print") }, [ | ||
new Expr.Variable(identifier("e")), | ||
]); | ||
|
||
beforeEach(() => { | ||
const outputStreams = createMockStreams(); | ||
interpreter = new Interpreter(outputStreams); | ||
stdout = outputStreams.stdout; | ||
}); | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
it("executes the try block when no errors are thrown", () => { | ||
const varAssignment = new Stmt.Assignment( | ||
{ equals: token(Lexeme.Equals, "=") }, | ||
identifier("a"), | ||
new Expr.Literal(new Int32(0)) | ||
); | ||
const trySpy = jest.spyOn(varAssignment, "accept"); | ||
const catchSpy = jest.spyOn(printError, "accept"); | ||
|
||
const statements = [ | ||
new Stmt.TryCatch( | ||
/* try block */ new Stmt.Block([varAssignment]), | ||
/* catch block */ new Stmt.Block([printError]), | ||
/* error binding */ new Expr.Variable(identifier("e")), | ||
{ | ||
try: token(Lexeme.Try, "try"), | ||
catch: token(Lexeme.Catch, "catch"), | ||
endtry: token(Lexeme.EndTry, "end try"), | ||
} | ||
), | ||
]; | ||
|
||
interpreter.exec(statements); | ||
expect(trySpy).toHaveBeenCalledTimes(1); | ||
expect(catchSpy).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("executes the catch block when an error is thrown", () => { | ||
const badComparison = new Expr.Binary( | ||
new Expr.Literal(new Int32(2)), | ||
token(Lexeme.Less), | ||
new Expr.Literal(new BrsString("1")) | ||
); | ||
const trySpy = jest.spyOn(badComparison, "accept"); | ||
const catchSpy = jest.spyOn(printError, "accept"); | ||
|
||
const statements = [ | ||
new Stmt.TryCatch( | ||
/* try block */ new Stmt.Block([badComparison]), | ||
/* catch block */ new Stmt.Block([printError]), | ||
/* error binding */ new Expr.Variable(identifier("e")), | ||
{ | ||
try: token(Lexeme.Try, "try"), | ||
catch: token(Lexeme.Catch, "catch"), | ||
endtry: token(Lexeme.EndTry, "end try"), | ||
} | ||
), | ||
]; | ||
|
||
interpreter.exec(statements); | ||
expect(trySpy).toHaveBeenCalledTimes(1); | ||
expect(catchSpy).toHaveBeenCalledTimes(1); | ||
const output = allArgs(stdout.write).filter((arg) => arg !== "\n"); | ||
expect(output[0]).toMatch(/Type Mismatch./); | ||
}); | ||
}); |