151 lines
4.5 KiB
C
Raw Normal View History

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_parser_class_declaration(void) {
UNITTEST_BEGIN_TEST("TestParser", "test_parser_class_declaration");
2025-12-04 16:55:18 +01:00
2025-12-04 22:38:05 +01:00
RAVA_TEST_PARSER_OK(_unittest_result,
2025-12-04 16:55:18 +01:00
"public class Calculator {\n"
" public static int add(int a, int b) {\n"
" int result = a + b;\n"
" return result;\n"
" }\n"
2025-12-04 22:38:05 +01:00
"}\n",
"class declaration should parse successfully");
UNITTEST_END_TEST();
}
UnittestTestResult_t* test_parser_method_with_params(void) {
UNITTEST_BEGIN_TEST("TestParser", "test_parser_method_with_params");
RAVA_TEST_PARSER_OK(_unittest_result,
"public class Test {\n"
2025-12-04 16:55:18 +01:00
" public static void main(String[] args) {\n"
" int x = 10;\n"
" int y = 20;\n"
2025-12-04 22:38:05 +01:00
" }\n"
"}\n",
"method with parameters should parse successfully");
UNITTEST_END_TEST();
}
UnittestTestResult_t* test_parser_if_else(void) {
UNITTEST_BEGIN_TEST("TestParser", "test_parser_if_else");
RAVA_TEST_PARSER_OK(_unittest_result,
"public class Test {\n"
" public static int main() {\n"
" int x = 10;\n"
" if (x > 0) {\n"
" return 1;\n"
" } else {\n"
" return 0;\n"
2025-12-04 16:55:18 +01:00
" }\n"
" }\n"
2025-12-04 22:38:05 +01:00
"}\n",
"if-else should parse successfully");
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_parser_for_loop(void) {
UNITTEST_BEGIN_TEST("TestParser", "test_parser_for_loop");
2025-12-04 16:55:18 +01:00
2025-12-04 22:38:05 +01:00
RAVA_TEST_PARSER_OK(_unittest_result,
"public class Test {\n"
" public static int main() {\n"
" int sum = 0;\n"
" for (int i = 0; i < 10; i++) {\n"
" sum = sum + i;\n"
" }\n"
" return sum;\n"
" }\n"
"}\n",
"for loop should parse successfully");
UNITTEST_END_TEST();
}
UnittestTestResult_t* test_parser_while_loop(void) {
UNITTEST_BEGIN_TEST("TestParser", "test_parser_while_loop");
RAVA_TEST_PARSER_OK(_unittest_result,
"public class Test {\n"
" public static int main() {\n"
" int i = 0;\n"
" while (i < 10) {\n"
" i = i + 1;\n"
" }\n"
" return i;\n"
" }\n"
"}\n",
"while loop should parse successfully");
2025-12-04 16:55:18 +01:00
2025-12-04 22:38:05 +01:00
UNITTEST_END_TEST();
}
UnittestTestResult_t* test_parser_method_call(void) {
UNITTEST_BEGIN_TEST("TestParser", "test_parser_method_call");
RAVA_TEST_PARSER_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 call should parse successfully");
UNITTEST_END_TEST();
}
UnittestTestResult_t* test_parser_array_access(void) {
UNITTEST_BEGIN_TEST("TestParser", "test_parser_array_access");
RAVA_TEST_PARSER_OK(_unittest_result,
"public class Test {\n"
" public static int main() {\n"
" int[] arr = new int[10];\n"
" arr[0] = 42;\n"
" return arr[0];\n"
" }\n"
"}\n",
"array access should parse successfully");
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;
2025-12-04 16:55:18 +01:00
}
2025-12-04 22:38:05 +01:00
UnittestTestSuite_t *suite = unittest_test_suite_create("Parser Tests");
UnittestTestCase_t *tc = unittest_test_case_create("TestParser");
unittest_test_case_add_result(tc, test_parser_class_declaration());
unittest_test_case_add_result(tc, test_parser_method_with_params());
unittest_test_case_add_result(tc, test_parser_if_else());
unittest_test_case_add_result(tc, test_parser_for_loop());
unittest_test_case_add_result(tc, test_parser_while_loop());
unittest_test_case_add_result(tc, test_parser_method_call());
unittest_test_case_add_result(tc, test_parser_array_access());
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
}