Skip to content

Commit 18af688

Browse files
committed
Add tests for local function exceptions in cppia
1 parent 540e2f1 commit 18af688

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

test/cppia/Client.hx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,26 @@ class Client
159159
return;
160160
}
161161

162+
switch LocalFunctionExceptions.testLocalCallingStatic() {
163+
case Error(message):
164+
Common.status = 'Failed test for throw in static called by local: ' + message;
165+
return;
166+
default:
167+
}
168+
169+
switch LocalFunctionExceptions.testCatchWithinLocal() {
170+
case Error(message):
171+
Common.status = 'Failed test for catch in local function: ' + message;
172+
return;
173+
default:
174+
}
175+
176+
switch LocalFunctionExceptions.testCatchFromLocal() {
177+
case Error(message):
178+
Common.status = 'Failed test for catching exception from local function: ' + message;
179+
return;
180+
default:
181+
}
162182

163183
final extending = new ClientExtendedExtendedRoot();
164184

test/cppia/LocalFunctionExceptions.hx

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
enum Status {
2+
Ok;
3+
Error(message:String);
4+
}
5+
6+
class LocalFunctionExceptions {
7+
static function staticFunction() {
8+
throw 'Thrown from static';
9+
}
10+
11+
public static function testLocalCallingStatic():Status {
12+
function localFunction() {
13+
staticFunction();
14+
throw 'Thrown from local';
15+
}
16+
17+
try {
18+
localFunction();
19+
} catch (e:String) {
20+
if (e == 'Thrown from static') {
21+
return Ok;
22+
} else {
23+
return Error("Incorrect exception caught from local function call");
24+
}
25+
}
26+
27+
return Error("No exception caught");
28+
}
29+
30+
public static function testCatchWithinLocal():Status {
31+
function localFunction() {
32+
try {
33+
staticFunction();
34+
} catch (e:String) {
35+
if (e == 'Thrown from static') {
36+
return Ok;
37+
} else {
38+
return Error("Incorrect exception caught from local function call");
39+
}
40+
}
41+
return Error("Exception from static function not caught");
42+
}
43+
44+
return try {
45+
localFunction();
46+
} catch (e) {
47+
Error('Exception leaked from local function: $e');
48+
};
49+
}
50+
51+
public static function testCatchFromLocal():Status {
52+
function localFunction() {
53+
throw 'Thrown from local';
54+
}
55+
56+
try {
57+
localFunction();
58+
} catch (e:String) {
59+
if (e == 'Thrown from local') {
60+
return Ok;
61+
} else {
62+
return Error("Incorrect exception caught from local function call");
63+
}
64+
}
65+
66+
return Error("No exception caught");
67+
}
68+
}

0 commit comments

Comments
 (0)