diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/parser_test.c | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/test/parser_test.c b/test/parser_test.c index a1614b9..12b2cac 100644 --- a/test/parser_test.c +++ b/test/parser_test.c @@ -131,6 +131,58 @@ test_parse_variable_definition(const MunitParameter params[], void *user_data_or } static MunitResult +test_parse_boolean(const MunitParameter params[], void *user_data_or_fixture) +{ + parser_t parser; + lexer_t lexer; + scope_t *scope = scope_new(); + + make_lexer_from_static_src(&lexer, "fn my_bool_fn(): bool { \nlet variable: bool = true; \nreturn variable;\n }"); + parser_init(&parser, &lexer, scope); + ast_node_t *ast_function = parser_parse_function_declaration(&parser); + + assert_true(ast_function != NULL); + + assert_string_view_equal("my_bool_fn", ast_function->data.function.identifier.name); + assert_int(AST_FUNCTION_DECLARATION, ==, ast_function->kind); + + ast_node_t *ast_variable = vector_at(ast_function->data.function.body, 0); + + assert_int(AST_VARIABLE_DECLARATION, ==, ast_variable->kind); + + assert_string_view_equal("variable", ast_variable->data.variable_declaration.identifier.name); + assert_int(TYPE_BOOL, ==, ast_variable->data.variable_declaration.type); + assert_int(AST_LITERAL, ==, ast_variable->data.variable_declaration.value->kind); + assert_int(AST_LITERAL_BOOL, ==, ast_variable->data.variable_declaration.value->data.literal.kind); + assert_true(ast_variable->data.variable_declaration.value->data.literal.value.boolean); + + ast_node_t *ast_return = vector_at(ast_function->data.function.body, 1); + + assert_int(AST_RETURN_STMT, ==, ast_return->kind); + + ast_node_t *ast_literal = ast_return->data.return_stmt.argument; + + assert_int(AST_VARIABLE, ==, ast_literal->kind); + assert_string_view_equal("variable", ast_literal->data.variable.identifier->name); + + ast_node_destroy(ast_function); + scope_destroy(scope); + + scope = scope_new(); + + make_lexer_from_static_src(&lexer, "fn my_bool_fn(): bool { \nlet variable: bool = false; \nreturn variable;\n }"); + parser_init(&parser, &lexer, scope); + ast_function = parser_parse_function_declaration(&parser); + + assert_true(ast_function != NULL); + ast_node_destroy(ast_function); + + scope_destroy(scope); + + return MUNIT_OK; +} + +static MunitResult test_parse_arithmetic_expression(const MunitParameter params[], void *user_data_or_fixture) { parser_t parser; @@ -225,6 +277,7 @@ test_parse_basic_syntax_errors(const MunitParameter params[], void *user_data_or static MunitTest tests[] = { { "/test_parse_function", test_parse_function, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }, + { "/test_parse_boolean", test_parse_boolean, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }, { "/test_parse_basic_syntax_errors", test_parse_basic_syntax_errors, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }, { "/test_parse_arithmetic_expression", test_parse_arithmetic_expression, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }, { "/test_parse_variable_definition", test_parse_variable_definition, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }, |