Skip to content

Commit

Permalink
Add tests for local function exceptions in cppia
Browse files Browse the repository at this point in the history
  • Loading branch information
tobil4sk committed Feb 3, 2025
1 parent 540e2f1 commit 18af688
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
20 changes: 20 additions & 0 deletions test/cppia/Client.hx
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,26 @@ class Client
return;
}

switch LocalFunctionExceptions.testLocalCallingStatic() {
case Error(message):
Common.status = 'Failed test for throw in static called by local: ' + message;
return;
default:
}

switch LocalFunctionExceptions.testCatchWithinLocal() {
case Error(message):
Common.status = 'Failed test for catch in local function: ' + message;
return;
default:
}

switch LocalFunctionExceptions.testCatchFromLocal() {
case Error(message):
Common.status = 'Failed test for catching exception from local function: ' + message;
return;
default:
}

final extending = new ClientExtendedExtendedRoot();

Expand Down
68 changes: 68 additions & 0 deletions test/cppia/LocalFunctionExceptions.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
enum Status {
Ok;
Error(message:String);
}

class LocalFunctionExceptions {
static function staticFunction() {
throw 'Thrown from static';
}

public static function testLocalCallingStatic():Status {
function localFunction() {
staticFunction();
throw 'Thrown from local';
}

try {
localFunction();
} catch (e:String) {
if (e == 'Thrown from static') {
return Ok;
} else {
return Error("Incorrect exception caught from local function call");
}
}

return Error("No exception caught");
}

public static function testCatchWithinLocal():Status {
function localFunction() {
try {
staticFunction();
} catch (e:String) {
if (e == 'Thrown from static') {
return Ok;
} else {
return Error("Incorrect exception caught from local function call");
}
}
return Error("Exception from static function not caught");
}

return try {
localFunction();
} catch (e) {
Error('Exception leaked from local function: $e');
};
}

public static function testCatchFromLocal():Status {
function localFunction() {
throw 'Thrown from local';
}

try {
localFunction();
} catch (e:String) {
if (e == 'Thrown from local') {
return Ok;
} else {
return Error("Incorrect exception caught from local function call");
}
}

return Error("No exception caught");
}
}

0 comments on commit 18af688

Please sign in to comment.