diff options
Diffstat (limited to 'test/parser_test.c')
-rw-r--r-- | test/parser_test.c | 89 |
1 files changed, 43 insertions, 46 deletions
diff --git a/test/parser_test.c b/test/parser_test.c index daa2fe7..d8aee07 100644 --- a/test/parser_test.c +++ b/test/parser_test.c @@ -1,19 +1,19 @@ /* -* Copyright (C) 2023 Carlos Maniero -* -* This program is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program. If not, see <https://www.gnu.org/licenses/>. -*/ + * Copyright (C) 2023 Carlos Maniero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ #define MUNIT_ENABLE_ASSERT_ALIASES #include "ast.h" #include "lexer.h" @@ -22,22 +22,24 @@ #include "string.h" #include "vector.h" -void assert_string_view_equal(char *expected, string_view_t actual); +void +assert_string_view_equal(char *expected, string_view_t actual); void make_lexer_from_static_src(lexer_t *lexer, char *src) { lexer->filepath = "test.pipa"; - lexer->srclen = 0; - lexer->cur = 0; - lexer->row = 0; - lexer->bol = 0; - lexer->src = src; - lexer->srclen = strlen(src); + lexer->srclen = 0; + lexer->cur = 0; + lexer->row = 0; + lexer->bol = 0; + lexer->src = src; + lexer->srclen = strlen(src); } void -assert_parser_error(char* src, char* error_msg) { +assert_parser_error(char *src, char *error_msg) +{ parser_t parser; lexer_t lexer; @@ -55,8 +57,7 @@ assert_parser_error(char* src, char* error_msg) { } static MunitResult -test_parse_function(const MunitParameter params[], - void *user_data_or_fixture) +test_parse_function(const MunitParameter params[], void *user_data_or_fixture) { parser_t parser; lexer_t lexer; @@ -89,8 +90,7 @@ test_parse_function(const MunitParameter params[], } static MunitResult -test_parse_variable_definition(const MunitParameter params[], - void *user_data_or_fixture) +test_parse_variable_definition(const MunitParameter params[], void *user_data_or_fixture) { parser_t parser; lexer_t lexer; @@ -129,8 +129,7 @@ test_parse_variable_definition(const MunitParameter params[], } static MunitResult -test_parse_arithmetic_expression(const MunitParameter params[], - void *user_data_or_fixture) +test_parse_arithmetic_expression(const MunitParameter params[], void *user_data_or_fixture) { parser_t parser; lexer_t lexer; @@ -189,7 +188,8 @@ test_parse_arithmetic_expression(const MunitParameter params[], return MUNIT_OK; } -void assert_string_view_equal(char *expected, string_view_t actual) +void +assert_string_view_equal(char *expected, string_view_t actual) { size_t expected_len = strlen(expected); assert_int(expected_len, ==, actual.size); @@ -197,22 +197,21 @@ void assert_string_view_equal(char *expected, string_view_t actual) } static MunitResult -test_parse_basic_syntax_errors(const MunitParameter params[], - void *user_data_or_fixture) +test_parse_basic_syntax_errors(const MunitParameter params[], void *user_data_or_fixture) { - assert_parser_error("(): i32 { return 42; }" , "expected 'TOKEN_NAME' but got '('"); - assert_parser_error("main): i32 { return 42; }" , "expected '(' but got ')'"); - assert_parser_error("main(: i32 { return 42; }" , "expected ')' but got ':'"); - assert_parser_error("main() i32 { return 42; }" , "expected ':' but got 'TOKEN_NAME'"); - assert_parser_error("main(): { return 42; }" , "expected 'TOKEN_NAME' but got '{'"); - assert_parser_error("main(): i32 return 42; }" , "expected '{' but got 'TOKEN_NAME'"); - assert_parser_error("main(): i32 { 42; }" , "expected 'TOKEN_NAME' but got 'TOKEN_NUMBER'"); - assert_parser_error("main(): i32 { return; }" , "unexpected '; (;)' token"); - assert_parser_error("main(): i32 { return 42;" , "expected '}' but got end of file"); - assert_parser_error("main(): beff { return 42; }" , "type 'beff' is not defined"); + assert_parser_error("(): i32 { return 42; }", "expected 'TOKEN_NAME' but got '('"); + assert_parser_error("main): i32 { return 42; }", "expected '(' but got ')'"); + assert_parser_error("main(: i32 { return 42; }", "expected ')' but got ':'"); + assert_parser_error("main() i32 { return 42; }", "expected ':' but got 'TOKEN_NAME'"); + assert_parser_error("main(): { return 42; }", "expected 'TOKEN_NAME' but got '{'"); + assert_parser_error("main(): i32 return 42; }", "expected '{' but got 'TOKEN_NAME'"); + assert_parser_error("main(): i32 { 42; }", "expected 'TOKEN_NAME' but got 'TOKEN_NUMBER'"); + assert_parser_error("main(): i32 { return; }", "unexpected '; (;)' token"); + assert_parser_error("main(): i32 { return 42;", "expected '}' but got end of file"); + assert_parser_error("main(): beff { return 42; }", "type 'beff' is not defined"); // FIXME: once function calls are implemented, this error should inform that // neither a variable or function call was found. - assert_parser_error("main(): i32 { oxi 42; }" , "expected ':' but got 'TOKEN_NUMBER'"); + assert_parser_error("main(): i32 { oxi 42; }", "expected ':' but got 'TOKEN_NUMBER'"); return MUNIT_OK; } @@ -227,9 +226,7 @@ static MunitTest tests[] = { { NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL } }; -static const MunitSuite suite = { - "/parser", tests, NULL, 1, MUNIT_SUITE_OPTION_NONE -}; +static const MunitSuite suite = { "/parser", tests, NULL, 1, MUNIT_SUITE_OPTION_NONE }; int main(int argc, char *argv[]) |