fakir/tests/Parser.cpp

50 lines
1.1 KiB
C++
Raw Normal View History

#include <catch2/catch.hpp>
#include "../src/Parser.hpp"
class ParserTest
{
public:
explicit ParserTest() {}
virtual ~ParserTest() {}
void test_parse(std::string const& oracle, std::string const& source)
{
auto node = m_parser.parse(source);
REQUIRE(oracle == node->string());
}
protected:
fk::Loc m_loc {"tests/parser"};
fk::Lexer m_lexer { m_loc };
fk::Parser m_parser { m_lexer };
};
TEST_CASE_METHOD(ParserTest, "Parser_empty")
{
test_parse("MODULE", "");
}
TEST_CASE_METHOD(ParserTest, "Parser_builtins")
{
test_parse("MODULE(INT[4],FLOAT[3.0],BOOL[false],STRING['salut'])",
"4 3. false 'salut'");
}
2023-09-20 15:17:13 +00:00
TEST_CASE_METHOD(ParserTest, "Parser_call")
{
test_parse("MODULE(CALL(IDENT[bim],INT[2],STRING['3'],BOOL[false]))",
" (bim 2 '3' false) ");
}
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(INT[7])))",
" (-> (x y) 7) ");
}