grino/tests/Parser.cpp

51 lines
1.1 KiB
C++

#include <catch2/catch.hpp>
#include "../src/Parser.hpp"
#include "../src/Lexer.hpp"
class ParserTest
{
public:
explicit ParserTest() {}
virtual ~ParserTest() {}
void test_parse(std::string const& oracle, std::string const& source)
{
grino::Logger logger;
grino::Lexer lexer {logger, "tests/parser"};
grino::Parser parser {logger, lexer};
auto root = parser.parse(source);
REQUIRE(oracle == root->string());
}
protected:
};
TEST_CASE_METHOD(ParserTest, "Parser_empty")
{
test_parse("MODULE", "");
}
TEST_CASE_METHOD(ParserTest, "Parser_booleans")
{
test_parse("MODULE(BOOL[true],BOOL[false])", "true false");
}
TEST_CASE_METHOD(ParserTest, "Parser_vardecl")
{
test_parse("MODULE(VARDECL(IDENT[hello],BOOL[false]))",
"($ hello false)");
test_parse("MODULE(VARDECL(IDENT[hello],IDENT[world]))",
"($ hello world)");
}
TEST_CASE_METHOD(ParserTest, "Parser_funcall")
{
test_parse("MODULE(FUNCALL(IDENT[hello]))",
"(hello)");
test_parse("MODULE(FUNCALL(IDENT[f],BOOL[false],BOOL[true]))",
"(f false true)");
}