2025-12-04 22:38:05 +01:00
|
|
|
#include "test_utils.h"
|
2025-12-04 16:55:18 +01:00
|
|
|
|
2025-12-04 22:38:05 +01:00
|
|
|
UnittestTestResult_t* test_basic_try_catch(void) {
|
|
|
|
|
UNITTEST_BEGIN_TEST("TestExceptions", "test_basic_try_catch");
|
2025-12-04 16:55:18 +01:00
|
|
|
|
2025-12-04 22:38:05 +01:00
|
|
|
RAVA_TEST_RUN(_unittest_result,
|
2025-12-04 16:55:18 +01:00
|
|
|
"public class Test {\n"
|
|
|
|
|
" public static int main() {\n"
|
|
|
|
|
" int result = 0;\n"
|
|
|
|
|
" try {\n"
|
|
|
|
|
" throw 42;\n"
|
|
|
|
|
" } catch (Exception e) {\n"
|
|
|
|
|
" result = e;\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
" return result;\n"
|
|
|
|
|
" }\n"
|
2025-12-04 22:38:05 +01:00
|
|
|
"}\n",
|
|
|
|
|
"Test", "main", 42, "basic try-catch should return 42");
|
|
|
|
|
|
|
|
|
|
UNITTEST_END_TEST();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UnittestTestResult_t* test_try_without_exception(void) {
|
|
|
|
|
UNITTEST_BEGIN_TEST("TestExceptions", "test_try_without_exception");
|
2025-12-04 16:55:18 +01:00
|
|
|
|
2025-12-04 22:38:05 +01:00
|
|
|
RAVA_TEST_RUN(_unittest_result,
|
2025-12-04 16:55:18 +01:00
|
|
|
"public class Test {\n"
|
|
|
|
|
" public static int main() {\n"
|
|
|
|
|
" int result = 10;\n"
|
|
|
|
|
" try {\n"
|
|
|
|
|
" result = 20;\n"
|
|
|
|
|
" } catch (Exception e) {\n"
|
|
|
|
|
" result = 99;\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
" return result;\n"
|
|
|
|
|
" }\n"
|
2025-12-04 22:38:05 +01:00
|
|
|
"}\n",
|
|
|
|
|
"Test", "main", 20, "try without exception should return 20");
|
2025-12-04 16:55:18 +01:00
|
|
|
|
2025-12-04 22:38:05 +01:00
|
|
|
UNITTEST_END_TEST();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UnittestTestResult_t* test_catch_modifies_value(void) {
|
|
|
|
|
UNITTEST_BEGIN_TEST("TestExceptions", "test_catch_modifies_value");
|
|
|
|
|
|
|
|
|
|
RAVA_TEST_RUN(_unittest_result,
|
2025-12-04 16:55:18 +01:00
|
|
|
"public class Test {\n"
|
|
|
|
|
" public static int main() {\n"
|
|
|
|
|
" int x = 5;\n"
|
|
|
|
|
" try {\n"
|
|
|
|
|
" throw 10;\n"
|
|
|
|
|
" } catch (Exception e) {\n"
|
|
|
|
|
" x = x + e;\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
" return x;\n"
|
|
|
|
|
" }\n"
|
2025-12-04 22:38:05 +01:00
|
|
|
"}\n",
|
|
|
|
|
"Test", "main", 15, "catch modifies value should return 15");
|
2025-12-04 16:55:18 +01:00
|
|
|
|
2025-12-04 22:38:05 +01:00
|
|
|
UNITTEST_END_TEST();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UnittestTestResult_t* test_try_finally(void) {
|
|
|
|
|
UNITTEST_BEGIN_TEST("TestExceptions", "test_try_finally");
|
|
|
|
|
|
|
|
|
|
RAVA_TEST_RUN(_unittest_result,
|
2025-12-04 16:55:18 +01:00
|
|
|
"public class Test {\n"
|
|
|
|
|
" public static int main() {\n"
|
|
|
|
|
" int result = 0;\n"
|
|
|
|
|
" try {\n"
|
|
|
|
|
" result = 10;\n"
|
|
|
|
|
" } catch (Exception e) {\n"
|
|
|
|
|
" result = 99;\n"
|
|
|
|
|
" } finally {\n"
|
|
|
|
|
" result = result + 5;\n"
|
|
|
|
|
" }\n"
|
|
|
|
|
" return result;\n"
|
|
|
|
|
" }\n"
|
2025-12-04 22:38:05 +01:00
|
|
|
"}\n",
|
|
|
|
|
"Test", "main", 15, "try-finally without exception should return 15");
|
|
|
|
|
|
|
|
|
|
UNITTEST_END_TEST();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
|
UnittestConfig_t *config = unittest_config_create();
|
|
|
|
|
config->verbosity = 2;
|
|
|
|
|
|
|
|
|
|
if (argc > 1 && strcmp(argv[1], "--json") == 0) {
|
|
|
|
|
config->output_format = UNITTEST_FORMAT_JSON;
|
|
|
|
|
config->use_colors = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UnittestTestSuite_t *suite = unittest_test_suite_create("Exception Handling Tests");
|
|
|
|
|
|
|
|
|
|
UnittestTestCase_t *tc = unittest_test_case_create("TestExceptions");
|
|
|
|
|
unittest_test_case_add_result(tc, test_basic_try_catch());
|
|
|
|
|
unittest_test_case_add_result(tc, test_try_without_exception());
|
|
|
|
|
unittest_test_case_add_result(tc, test_catch_modifies_value());
|
|
|
|
|
unittest_test_case_add_result(tc, test_try_finally());
|
|
|
|
|
unittest_test_suite_add_test_case(suite, tc);
|
|
|
|
|
|
|
|
|
|
unittest_generate_report(suite, config);
|
2025-12-04 16:55:18 +01:00
|
|
|
|
2025-12-04 22:38:05 +01:00
|
|
|
int failures = suite->total_failed + suite->total_errors;
|
|
|
|
|
unittest_test_suite_destroy(suite);
|
|
|
|
|
unittest_config_destroy(config);
|
2025-12-04 16:55:18 +01:00
|
|
|
|
2025-12-04 22:38:05 +01:00
|
|
|
return failures > 0 ? 1 : 0;
|
2025-12-04 16:55:18 +01:00
|
|
|
}
|