|
#include "test_utils.h"
|
|
|
|
UnittestTestResult_t* test_parser_class_declaration(void) {
|
|
UNITTEST_BEGIN_TEST("TestParser", "test_parser_class_declaration");
|
|
|
|
RAVA_TEST_PARSER_OK(_unittest_result,
|
|
"public class Calculator {\n"
|
|
" public static int add(int a, int b) {\n"
|
|
" int result = a + b;\n"
|
|
" return result;\n"
|
|
" }\n"
|
|
"}\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"
|
|
" public static void main(String[] args) {\n"
|
|
" int x = 10;\n"
|
|
" int y = 20;\n"
|
|
" }\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"
|
|
" }\n"
|
|
" }\n"
|
|
"}\n",
|
|
"if-else should parse successfully");
|
|
|
|
UNITTEST_END_TEST();
|
|
}
|
|
|
|
UnittestTestResult_t* test_parser_for_loop(void) {
|
|
UNITTEST_BEGIN_TEST("TestParser", "test_parser_for_loop");
|
|
|
|
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");
|
|
|
|
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;
|
|
}
|
|
|
|
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);
|
|
|
|
int failures = suite->total_failed + suite->total_errors;
|
|
unittest_test_suite_destroy(suite);
|
|
unittest_config_destroy(config);
|
|
|
|
return failures > 0 ? 1 : 0;
|
|
}
|