fakir/tests/Parser.cpp

96 lines
2.2 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) ");
}
2023-09-23 17:21:55 +00:00
TEST_CASE_METHOD(ParserTest, "Parser_vardecl")
{
test_parse("MODULE(VARDECL(IDENT[hello],INT[3]))",
" ($ hello 3) ");
}
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) ");
}
TEST_CASE_METHOD(ParserTest, "Parser_fundecl")
{
2023-09-23 17:21:55 +00:00
test_parse("MODULE(VARDECL(IDENT[f],"
"LAMBDA(PARAMS(IDENT[x]),BODY(INT[7]))))",
" ($ (f x) 7) ");
}
2023-09-23 17:21:55 +00:00
TEST_CASE_METHOD(ParserTest, "Parser_namespace")
{
test_parse("MODULE(CALL(NS(IDENT[x],IDENT[y]),IDENT[z]))",
" (x::y z) ");
}
TEST_CASE_METHOD(ParserTest, "Parser_import")
{
test_parse("MODULE(VARDECL(IDENT[x],IMPORT(STRING['y'])))",
" ($ x (@ 'y') )");
test_parse("MODULE(VARDECL(IDENT[x],IMPORT(STRING['y'])))",
" ($ @x 'y' )");
test_parse("MODULE(VARDECL(IDENT[bim],IMPORT(STRING['bam'])))",
" ($ @bim 'bam' )");
test_parse("MODULE(VARDECL(IDENT[example],IMPORT(STRING['example'])))",
" ($ @example )");
}
2023-09-23 22:40:08 +00:00
TEST_CASE_METHOD(ParserTest, "Parser_array")
{
test_parse("MODULE(ARRAY)",
" [] ");
test_parse("MODULE(ARRAY(BOOL[true]))",
" [true] ");
test_parse("MODULE(ARRAY(INT[2],STRING['bim']))",
" [ 2 'bim'] ");
}