117 lines
3.6 KiB
C
Raw Normal View History

2025-12-04 22:38:05 +01:00
#include "test_utils.h"
UnittestTestResult_t* test_semantic_variable_declaration(void) {
UNITTEST_BEGIN_TEST("TestSemantic", "test_semantic_variable_declaration");
RAVA_TEST_SEMANTIC_OK(_unittest_result,
"public class Test {\n"
" public static int main() {\n"
" int x = 10;\n"
" int y = x + 5;\n"
" return y;\n"
" }\n"
"}\n",
"variable declaration should pass semantic analysis");
UNITTEST_END_TEST();
}
UnittestTestResult_t* test_semantic_type_checking(void) {
UNITTEST_BEGIN_TEST("TestSemantic", "test_semantic_type_checking");
RAVA_TEST_SEMANTIC_OK(_unittest_result,
2025-12-04 16:55:18 +01:00
"public class Test {\n"
" public static int calculate(int x, int y) {\n"
" int sum = x + y;\n"
" int product = x * y;\n"
" boolean isPositive = sum > 0;\n"
" if (isPositive) {\n"
" return sum;\n"
" }\n"
" return product;\n"
" }\n"
2025-12-04 22:38:05 +01:00
"}\n",
"type checking should pass");
UNITTEST_END_TEST();
}
UnittestTestResult_t* test_semantic_method_resolution(void) {
UNITTEST_BEGIN_TEST("TestSemantic", "test_semantic_method_resolution");
RAVA_TEST_SEMANTIC_OK(_unittest_result,
"public class Test {\n"
" public static int add(int a, int b) {\n"
" return a + b;\n"
" }\n"
" public static int main() {\n"
" int result = add(10, 20);\n"
" return result;\n"
" }\n"
"}\n",
"method resolution should pass");
UNITTEST_END_TEST();
}
UnittestTestResult_t* test_semantic_array_types(void) {
UNITTEST_BEGIN_TEST("TestSemantic", "test_semantic_array_types");
RAVA_TEST_SEMANTIC_OK(_unittest_result,
"public class Test {\n"
" public static int main() {\n"
" int[] arr = new int[10];\n"
" arr[0] = 42;\n"
" int x = arr[0];\n"
" return x;\n"
2025-12-04 16:55:18 +01:00
" }\n"
2025-12-04 22:38:05 +01:00
"}\n",
"array type checking should pass");
2025-12-04 16:55:18 +01:00
2025-12-04 22:38:05 +01:00
UNITTEST_END_TEST();
}
2025-12-04 16:55:18 +01:00
2025-12-04 22:38:05 +01:00
UnittestTestResult_t* test_semantic_class_fields(void) {
UNITTEST_BEGIN_TEST("TestSemantic", "test_semantic_class_fields");
2025-12-04 16:55:18 +01:00
2025-12-04 22:38:05 +01:00
RAVA_TEST_SEMANTIC_OK(_unittest_result,
"public class Test {\n"
" public static int counter = 0;\n"
" public static int main() {\n"
" counter = counter + 1;\n"
" return counter;\n"
" }\n"
"}\n",
"class fields should pass semantic analysis");
UNITTEST_END_TEST();
}
int main(int argc, char **argv) {
UnittestConfig_t *config = unittest_config_create();
config->verbosity = 2;
2025-12-04 16:55:18 +01:00
2025-12-04 22:38:05 +01:00
if (argc > 1 && strcmp(argv[1], "--json") == 0) {
config->output_format = UNITTEST_FORMAT_JSON;
config->use_colors = false;
2025-12-04 16:55:18 +01:00
}
2025-12-04 22:38:05 +01:00
UnittestTestSuite_t *suite = unittest_test_suite_create("Semantic Analysis Tests");
UnittestTestCase_t *tc = unittest_test_case_create("TestSemantic");
unittest_test_case_add_result(tc, test_semantic_variable_declaration());
unittest_test_case_add_result(tc, test_semantic_type_checking());
unittest_test_case_add_result(tc, test_semantic_method_resolution());
unittest_test_case_add_result(tc, test_semantic_array_types());
unittest_test_case_add_result(tc, test_semantic_class_fields());
unittest_test_suite_add_test_case(suite, tc);
unittest_generate_report(suite, config);
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
}