moved some stuff around in an attempt to make

things cleaner (not sure it is, but we tryin)
This commit is contained in:
Karma Riuk
2025-07-19 13:41:25 +02:00
parent c943380d58
commit 7b01840f4d
3 changed files with 58 additions and 56 deletions

33
test/utils/fixtures.cpp Normal file
View File

@@ -0,0 +1,33 @@
#include "utils.hpp"
#include <doctest.h>
namespace test::utils {
void check_parser_errors(const std::vector<ast::error::error*>& 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<lexer::lexer>(input);
parser = std::make_unique<parser::parser>(*lexer);
program = parser->parse_program();
check_parser_errors(parser->errors);
REQUIRE_MESSAGE(
program != nullptr,
"parse_program() returned a null pointer"
);
}
} // namespace test::utils

View File

@@ -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 <doctest.h>
namespace test::utils {
void check_parser_errors(const std::vector<ast::error::error*>& 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<lexer::lexer>(input);
parser = std::make_unique<parser::parser>(*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<ast::identifier>(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

View File

@@ -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<ast::error::error*>&);
struct ParserFixture {
std::stringstream input;
std::unique_ptr<lexer::lexer> lexer;
std::unique_ptr<parser::parser> parser;
std::unique_ptr<ast::program> 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<token::type>, int = 0);
namespace {
std::string demangle(const char* name) {
@@ -52,22 +73,4 @@ namespace test::utils {
return cast_impl<T, ast::error::error>(err);
}
struct ParserFixture {
std::stringstream input;
std::unique_ptr<lexer::lexer> lexer;
std::unique_ptr<parser::parser> parser;
std::unique_ptr<ast::program> 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<token::type>, int = 0);
} // namespace test::utils