108 lines
3.2 KiB
C
Raw Normal View History

2025-12-04 22:46:38 +01:00
#include "test_utils.h"
2025-12-04 16:55:18 +01:00
2025-12-04 22:46:38 +01:00
UnittestTestResult_t* test_super_method_basic(void) {
UNITTEST_BEGIN_TEST("TestSuper", "test_super_method_basic");
2025-12-04 16:55:18 +01:00
2025-12-04 22:46:38 +01:00
RAVA_TEST_RUN(_unittest_result,
2025-12-04 16:55:18 +01:00
"class Parent {\n"
" int getValue() {\n"
" return 10;\n"
" }\n"
"}\n"
"class Child extends Parent {\n"
" int getValue() {\n"
" return super.getValue() + 5;\n"
" }\n"
"}\n"
"public class Test {\n"
" public static int main() {\n"
" Child c = new Child();\n"
" return c.getValue();\n"
" }\n"
2025-12-04 22:46:38 +01:00
"}\n",
"Test", "main", 15, "super.method() should call parent method");
UNITTEST_END_TEST();
}
2025-12-04 16:55:18 +01:00
2025-12-04 22:46:38 +01:00
UnittestTestResult_t* test_super_method_with_args(void) {
UNITTEST_BEGIN_TEST("TestSuper", "test_super_method_with_args");
RAVA_TEST_RUN(_unittest_result,
2025-12-04 16:55:18 +01:00
"class Parent {\n"
" int add(int a, int b) {\n"
" return a + b;\n"
" }\n"
"}\n"
"class Child extends Parent {\n"
" int add(int a, int b) {\n"
" return super.add(a, b) * 2;\n"
" }\n"
"}\n"
"public class Test {\n"
" public static int main() {\n"
" Child c = new Child();\n"
" return c.add(3, 4);\n"
" }\n"
2025-12-04 22:46:38 +01:00
"}\n",
"Test", "main", 14, "super.method(args) should pass arguments to parent");
UNITTEST_END_TEST();
}
2025-12-04 16:55:18 +01:00
2025-12-04 22:46:38 +01:00
UnittestTestResult_t* test_super_method_chain(void) {
UNITTEST_BEGIN_TEST("TestSuper", "test_super_method_chain");
RAVA_TEST_RUN(_unittest_result,
2025-12-04 16:55:18 +01:00
"class GrandParent {\n"
" int getValue() {\n"
" return 100;\n"
" }\n"
"}\n"
"class Parent extends GrandParent {\n"
" int getValue() {\n"
" return super.getValue() + 10;\n"
" }\n"
"}\n"
"class Child extends Parent {\n"
" int getValue() {\n"
" return super.getValue() + 1;\n"
" }\n"
"}\n"
"public class Test {\n"
" public static int main() {\n"
" Child c = new Child();\n"
" return c.getValue();\n"
" }\n"
2025-12-04 22:46:38 +01:00
"}\n",
"Test", "main", 111, "super chain should accumulate values");
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("Super Keyword Tests");
UnittestTestCase_t *tc = unittest_test_case_create("TestSuper");
unittest_test_case_add_result(tc, test_super_method_basic());
unittest_test_case_add_result(tc, test_super_method_with_args());
unittest_test_case_add_result(tc, test_super_method_chain());
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:46:38 +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:46:38 +01:00
return failures > 0 ? 1 : 0;
2025-12-04 16:55:18 +01:00
}