Skip to content

Commit

Permalink
Added unit tests for Try Catch
Browse files Browse the repository at this point in the history
  • Loading branch information
lvcabral committed May 1, 2024
1 parent 9aab204 commit cfaffc8
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 21 deletions.
29 changes: 19 additions & 10 deletions test/e2e/Syntax.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,24 +280,33 @@ describe("end to end syntax", () => {
test("try-catch.brs", async () => {
await execute([resourceFile("try-catch.brs")], outputStreams);
expect(allArgs(outputStreams.stdout.write).filter((arg) => arg !== "\n")).toEqual([
"[pre_try] a = ",
"[pre_try] a =",
" 5",
"[in_try] a = ",
"[in_try] a =",
" 10",
"[subFunc] a = ",
"[subFunc] a =",
" 10",
"[thirdLevel]",
"Error # =",
" 6502",
"[in_catch] e = ",
"[in_catch] message = ",
"subFunc custom error message!",
"[backtrace] = ",
"[in_catch] customField = ",
"true",
"[backtrace] =",
" 8",
"[backtrace] = ",
" 22",
"[backtrace] = ",
" 33",
"[post_try] a = ",
"[backtrace] =",
" 25",
"[backtrace] =",
" 41",
"[post_try] a =",
" 10",
"[subFunc] a =",
" 11",
"Error # =",
" 24",
"Error message = ",
`Type Mismatch. Operator "*" can't be applied to "Integer" and "String".`,
]);
});
});
30 changes: 19 additions & 11 deletions test/e2e/resources/try-catch.brs
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
83 changes: 83 additions & 0 deletions test/interpreter/TryCatch.test.js
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./);
});
});

0 comments on commit cfaffc8

Please sign in to comment.