grino/tests/Parser.cpp

108 lines
2.5 KiB
C++
Raw Normal View History

2023-09-10 23:05:29 +00:00
#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)
{
2023-09-11 06:00:50 +00:00
grino::Logger logger;
grino::Lexer lexer {logger, "tests/parser"};
grino::Parser parser {logger, lexer};
auto root = parser.parse(source);
2023-09-10 23:05:29 +00:00
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");
}
2023-09-11 06:00:50 +00:00
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)");
}
2023-09-11 10:14:01 +00:00
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)");
}
2023-09-11 13:59:37 +00:00
TEST_CASE_METHOD(ParserTest, "Parser_integer")
{
test_parse("MODULE(FUNCALL(IDENT[hello],INT[2],INT[34]))",
"(hello 2 34)");
}
2023-09-11 18:59:54 +00:00
TEST_CASE_METHOD(ParserTest, "Parser_lambda")
{
test_parse("MODULE(LAMBDA(PARAMS,BODY))",
"(-> ())");
test_parse("MODULE(LAMBDA(PARAMS(IDENT[x]),BODY))",
"(-> (x))");
test_parse("MODULE(LAMBDA(PARAMS(IDENT[x],IDENT[y]),BODY(IDENT[y])))",
"(-> (x y) y)");
test_parse("MODULE(FUNCALL(LAMBDA(PARAMS("
"IDENT[x]"
"),BODY("
"IDENT[x]"
")),INT[4]))",
"( (-> (x) x ) 4 )");
}
TEST_CASE_METHOD(ParserTest, "Parser_fundecl")
{
test_parse("MODULE(VARDECL(IDENT[f],LAMBDA(PARAMS(IDENT[x]),"
"BODY(FUNCALL(IDENT[+],IDENT[x],INT[1])))))",
"($ (f x) (+ x 1))");
}
2023-09-12 03:53:16 +00:00
TEST_CASE_METHOD(ParserTest, "Parser_block")
{
test_parse("MODULE(BLOCK(INT[1],BOOL[true],IDENT[salut]))",
"(: 1 true salut )");
}
2023-09-13 05:46:33 +00:00
TEST_CASE_METHOD(ParserTest, "Parser_array")
{
test_parse("MODULE(BLOCK(ARRAY(INT[1],BOOL[true],IDENT[salut])))",
"(: [1 true salut] )");
}
2023-09-13 10:15:12 +00:00
TEST_CASE_METHOD(ParserTest, "Parser_float")
{
test_parse("MODULE(BLOCK(ARRAY(INT[1],FLOAT[28.5],IDENT[salut])))",
"(: [1 28.5 salut] )");
}
2023-09-13 11:45:09 +00:00
TEST_CASE_METHOD(ParserTest, "Parser_string")
{
test_parse("MODULE(BLOCK(ARRAY(STRING['bim !'],FLOAT[28.5],IDENT[salut])))",
"(: ['bim !' 28.5 salut] )");
}