roza/tests/Parser.cpp

73 lines
1.8 KiB
C++
Raw Normal View History

2023-08-30 18:06:26 +00:00
#include <catch2/catch.hpp>
#include "../lib/Lexer.hpp"
#include "../lib/Parser.hpp"
#include "lib/SrcLoc.hpp"
#include "lib/StatusLog.hpp"
class ParserTest
{
public:
explicit ParserTest() {}
virtual ~ParserTest() {}
void test_node(std::string const& oracle, std::string const& source)
{
roza::SrcLoc loc {"parser_tests"};
roza::StatusLog log;
roza::Lexer lexer {log, loc};
roza::Parser parser {lexer, log};
lexer.scan(source);
auto node = parser.parse();
REQUIRE(oracle == node->string());
}
void test_node_err(std::string const& oracle)
{
roza::SrcLoc loc {"parser_tests"};
roza::StatusLog log;
roza::Lexer lexer {log, loc};
roza::Parser parser {lexer, log};
lexer.scan(oracle);
REQUIRE_THROWS(parser.parse());
}
protected:
};
TEST_CASE_METHOD(ParserTest, "Parser_integers")
{
test_node("PROG", "");
2023-08-30 22:31:19 +00:00
test_node("PROG(INT[27])", " 27 ");
test_node("PROG(INT[27])", " 27 ");
2023-08-30 18:06:26 +00:00
2023-08-30 22:31:19 +00:00
test_node("PROG(INT[27],"
"INT[9],"
"USUB(INT[99]))", "27 \n 9 \n -99");
2023-08-30 18:06:26 +00:00
test_node_err("32 14 -12");
}
2023-08-30 22:31:19 +00:00
TEST_CASE_METHOD(ParserTest, "Parser_int_arith")
{
test_node("PROG(ADD(ADD(INT[1],INT[2]),INT[3]))", "1 + 2 + 3");
test_node("PROG(SUB(SUB(INT[1],INT[2]),INT[3]))", "1 - 2 - 3");
test_node("PROG(ADD(INT[1],MUL(INT[2],INT[3])))", "1 + 2 * 3");
test_node("PROG(MUL(ADD(INT[1],INT[2]),INT[3]))", "(1 + 2) * 3");
test_node("PROG(SUB(INT[1],DIV(INT[2],INT[3])))", "1 - 2 / 3");
test_node("PROG(DIV(SUB(INT[1],INT[2]),INT[3]))", "(1 - 2) / 3");
test_node("PROG(ADD(INT[1],MOD(INT[2],INT[3])))", "1 + 2 % 3");
test_node("PROG(ADD(USUB(INT[2]),INT[3]))", "-2 + 3");
test_node("PROG(ADD(UADD(INT[2]),INT[3]))", "+ 2 + 3");
test_node("PROG(USUB(ADD(INT[2],INT[3])))", " -(2 + 3)");
test_node("PROG(USUB(POW(INT[2],INT[7])))", " -2^7 ");
}