Skip to content

Commit faf1cb7

Browse files
committed
other: Added tests for InvalidErrorBindingTypeError and catch-all catch blocks
1 parent bb9e867 commit faf1cb7

2 files changed

Lines changed: 106 additions & 0 deletions

File tree

test/module/core/core-functionality/try-catch.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,42 @@ import { ScriptTarget } from "typescript";
66
import { testPrintOutput } from "../index";
77

88
describe("Try-Catch Statements", () => {
9+
it("should be able to catch all errors using default catch", async () => {
10+
const fileContent = `var x: num = 4; try { x = 5; } catch (_) { x = 6; } print(x);`;
11+
const instance: KipperCompileResult = await compiler.compile(fileContent, { target: defaultTarget });
12+
13+
assert.isDefined(instance.programCtx);
14+
assert.deepEqual(instance.programCtx!!.errors, [], "Expected no compilation errors");
15+
16+
const written = instance.write();
17+
assert.include(
18+
written,
19+
`try\n{\n x = 5;\n}\ncatch (__kipper_e)\n{\n if (true) {\n const _ = __kipper_e;\n x = 6;\n }\n}`,
20+
"Invalid TypeScript code (Expected different output)",
21+
);
22+
23+
const jsCode = ts.transpile(instance.write(), { target: ScriptTarget.ES2015 });
24+
testPrintOutput((message: any) => assert.equal(message, "5", "Expected different output"), jsCode);
25+
});
26+
27+
it("should be able to catch all errors using any catch", async () => {
28+
const fileContent = `var x: num = 4; try { x = 5; } catch (e: any) { x = 6; } print(x);`;
29+
const instance: KipperCompileResult = await compiler.compile(fileContent, { target: defaultTarget });
30+
31+
assert.isDefined(instance.programCtx);
32+
assert.deepEqual(instance.programCtx!!.errors, [], "Expected no compilation errors");
33+
34+
const written = instance.write();
35+
assert.include(
36+
written,
37+
`try\n{\n x = 5;\n}\ncatch (__kipper_e)\n{\n if (true) {\n const e = __kipper_e;\n x = 6;\n }\n}`,
38+
"Invalid TypeScript code (Expected different output)",
39+
);
40+
41+
const jsCode = ts.transpile(instance.write(), { target: ScriptTarget.ES2015 });
42+
testPrintOutput((message: any) => assert.equal(message, "5", "Expected different output"), jsCode);
43+
});
44+
945
it("should be able to catch errors using try-catch", async () => {
1046
const fileContent = `class CustomError {} var x: num = 4; try { x = 5; } catch (e: CustomError) { x = 6; } print(x);`;
1147
const instance: KipperCompileResult = await compiler.compile(fileContent, { target: defaultTarget });
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import type { KipperCompileResult, KipperError } from "@kipper/core";
2+
import { KipperCompiler } from "@kipper/core";
3+
import { defaultConfig, ensureTracebackDataExists } from "../index";
4+
import { assert } from "chai";
5+
6+
describe("InvalidErrorBindingTypeError", () => {
7+
describe("Error", () => {
8+
it("num error binding", async () => {
9+
try {
10+
await new KipperCompiler().compile("try {} catch (e: num) {}", defaultConfig);
11+
} catch (e) {
12+
assert.equal((<KipperError>e).constructor.name, "InvalidErrorBindingTypeError", "Expected different error");
13+
assert.equal((<KipperError>e).name, "TypeError", "Expected different error");
14+
ensureTracebackDataExists(<KipperError>e);
15+
return;
16+
}
17+
assert.fail("Expected 'InvalidErrorBindingTypeError'");
18+
});
19+
20+
it("interface error binding", async () => {
21+
try {
22+
await new KipperCompiler().compile("interface A {}; try {} catch (e: A) {}", defaultConfig);
23+
} catch (e) {
24+
assert.equal((<KipperError>e).constructor.name, "InvalidErrorBindingTypeError", "Expected different error");
25+
assert.equal((<KipperError>e).name, "TypeError", "Expected different error");
26+
ensureTracebackDataExists(<KipperError>e);
27+
return;
28+
}
29+
assert.fail("Expected 'InvalidErrorBindingTypeError'");
30+
});
31+
});
32+
33+
describe("No Error", () => {
34+
it("no error type", async () => {
35+
let result: KipperCompileResult | undefined = undefined;
36+
try {
37+
result = await new KipperCompiler().compile("try {} catch (e) {}", defaultConfig);
38+
} catch (e) {
39+
assert.fail(`Expected no '${(<KipperError>e).name}'`);
40+
}
41+
assert.isDefined(result, "Expected defined compilation result");
42+
assert.isDefined(result?.programCtx, "Expected programCtx to be defined");
43+
assert.isFalse(result?.programCtx?.hasFailed, "Expected no errors");
44+
});
45+
46+
it("any error binding", async () => {
47+
let result: KipperCompileResult | undefined = undefined;
48+
try {
49+
result = await new KipperCompiler().compile("try {} catch (e: any) {}", defaultConfig);
50+
} catch (e) {
51+
assert.fail(`Expected no '${(<KipperError>e).name}'`);
52+
}
53+
assert.isDefined(result, "Expected defined compilation result");
54+
assert.isDefined(result?.programCtx, "Expected programCtx to be defined");
55+
assert.isFalse(result?.programCtx?.hasFailed, "Expected no errors");
56+
});
57+
58+
it("class error binding", async () => {
59+
let result: KipperCompileResult | undefined = undefined;
60+
try {
61+
result = await new KipperCompiler().compile("class A {}; try {} catch (e: A) {}", defaultConfig);
62+
} catch (e) {
63+
assert.fail(`Expected no '${(<KipperError>e).name}'`);
64+
}
65+
assert.isDefined(result, "Expected defined compilation result");
66+
assert.isDefined(result?.programCtx, "Expected programCtx to be defined");
67+
assert.isFalse(result?.programCtx?.hasFailed, "Expected no errors");
68+
});
69+
});
70+
});

0 commit comments

Comments
 (0)