From 7b01840f4d2de4250dfa5a673c8e7a7c0d0a433b Mon Sep 17 00:00:00 2001 From: Karma Riuk Date: Sat, 19 Jul 2025 13:41:25 +0200 Subject: [PATCH] moved some stuff around in an attempt to make things cleaner (not sure it is, but we tryin) --- test/utils/fixtures.cpp | 33 +++++++++++++++++++++++ test/utils/{utils.cpp => test.cpp} | 42 +++--------------------------- test/utils/utils.hpp | 39 ++++++++++++++------------- 3 files changed, 58 insertions(+), 56 deletions(-) create mode 100644 test/utils/fixtures.cpp rename test/utils/{utils.cpp => test.cpp} (71%) diff --git a/test/utils/fixtures.cpp b/test/utils/fixtures.cpp new file mode 100644 index 0000000..af11622 --- /dev/null +++ b/test/utils/fixtures.cpp @@ -0,0 +1,33 @@ +#include "utils.hpp" + +#include + +namespace test::utils { + void check_parser_errors(const std::vector& errors) { + if (errors.empty()) + return; + + INFO("parser has " << errors.size() << " errors:"); + std::ostringstream combined; + for (auto& err : errors) + combined << " > " << err->what() << '\n'; + + INFO(combined.str()); + + FAIL("Parser had errors."); + } + + void ParserFixture::setup(std::string source) { + input.clear(); + input << source; + lexer = std::make_unique(input); + parser = std::make_unique(*lexer); + program = parser->parse_program(); + check_parser_errors(parser->errors); + + REQUIRE_MESSAGE( + program != nullptr, + "parse_program() returned a null pointer" + ); + } +} // namespace test::utils diff --git a/test/utils/utils.cpp b/test/utils/test.cpp similarity index 71% rename from test/utils/utils.cpp rename to test/utils/test.cpp index a8eb7a9..af01c2f 100644 --- a/test/utils/utils.cpp +++ b/test/utils/test.cpp @@ -1,40 +1,6 @@ #include "utils.hpp" -#include "ast/expressions/boolean.hpp" -#include "ast/expressions/identifier.hpp" -#include "ast/expressions/infix.hpp" -#include "ast/expressions/integer.hpp" - -#include - namespace test::utils { - void check_parser_errors(const std::vector& errors) { - if (errors.empty()) - return; - - INFO("parser has " << errors.size() << " errors:"); - std::ostringstream combined; - for (auto& err : errors) - combined << " > " << err->what() << '\n'; - - INFO(combined.str()); - - FAIL("Parser had errors."); - } - - void ParserFixture::setup(std::string source) { - input.clear(); - input << source; - lexer = std::make_unique(input); - parser = std::make_unique(*lexer); - program = parser->parse_program(); - check_parser_errors(parser->errors); - - REQUIRE_MESSAGE( - program != nullptr, - "parse_program() returned a null pointer" - ); - } void test_identifier(ast::expression* expr, std::string value) { ast::identifier* ident = cast(expr); @@ -120,9 +86,9 @@ namespace test::utils { "parse_program() returned a null pointer" ); REQUIRE(program->statements.size() >= n_good_statements); - // ^^ because even though you were thinking - // about a specific number of statements to be there, it failing for - // `expect_next` might trigger a sub-expression to be triggered - // correctly and be parsed as the expression_stmt + // ^^ because even though you were + // thinking about a specific number of statements to be there, it + // failing for `expect_next` might trigger a sub-expression to be + // triggered correctly and be parsed as the expression_stmt } } // namespace test::utils diff --git a/test/utils/utils.hpp b/test/utils/utils.hpp index 85a5ae2..d0c54af 100644 --- a/test/utils/utils.hpp +++ b/test/utils/utils.hpp @@ -1,3 +1,5 @@ +#pragma once + #include "ast/ast.hpp" #include "lexer/lexer.hpp" #include "parser/parser.hpp" @@ -11,6 +13,25 @@ namespace test::utils { void check_parser_errors(const std::vector&); + struct ParserFixture { + std::stringstream input; + std::unique_ptr lexer; + std::unique_ptr parser; + std::unique_ptr program; + + ParserFixture() = default; + + void setup(std::string); + }; + + void test_identifier(ast::expression*, std::string); + void test_integer_literal(ast::expression*, int); + void test_boolean_literal(ast::expression*, bool); + void test_literal_expression(ast::expression*, std::any&); + void + test_infix_expression(ast::expression*, std::any, std::string, std::any); + void test_failing_parsing(std::string, std::vector, int = 0); + namespace { std::string demangle(const char* name) { @@ -52,22 +73,4 @@ namespace test::utils { return cast_impl(err); } - struct ParserFixture { - std::stringstream input; - std::unique_ptr lexer; - std::unique_ptr parser; - std::unique_ptr program; - - ParserFixture() = default; - - void setup(std::string); - }; - - void test_identifier(ast::expression*, std::string); - void test_integer_literal(ast::expression*, int); - void test_boolean_literal(ast::expression*, bool); - void test_literal_expression(ast::expression*, std::any&); - void - test_infix_expression(ast::expression*, std::any, std::string, std::any); - void test_failing_parsing(std::string, std::vector, int = 0); } // namespace test::utils