snake/tests/Parser.cpp

77 lines
1.8 KiB
C++
Raw Normal View History

2023-10-14 01:56:31 +00:00
#include <catch2/catch.hpp>
#include "../src/Lexer.hpp"
#include "../src/Parser.hpp"
using namespace sn;
class ParserTest
{
public:
explicit ParserTest() {}
virtual ~ParserTest() {}
void test_parse(std::string const& oracle,
std::string const& source)
{
Lexer lexer;
lexer.scan(source);
Parser parser;
auto node = parser.parse(lexer.all());
REQUIRE(oracle == node->string());
}
protected:
};
TEST_CASE_METHOD(ParserTest, "Parser_rule")
{
std::stringstream ss;
ss << " hello.elf -> hello.cpp {" << std::endl;
ss << " g++ hello.cpp -o hello.elf, " << std::endl;
ss << " ls " << std::endl;
ss << " }" << std::endl;
test_parse("DOC(RULE(TARGET("
"IDENT[hello.elf]"
"),DEPS("
"IDENT[hello.cpp]"
"),BLOCK("
"CMD("
"IDENT[g++],IDENT[hello.cpp],IDENT[-o],IDENT[hello.elf]"
"),CMD("
"IDENT[ls]"
")"
")))",
ss.str());
}
TEST_CASE_METHOD(ParserTest, "Parser_var_decl_and_use")
{
std::stringstream ss;
ss << "$BIM = hello.cpp" << std::endl;
ss << "$BAM = world.hpp" << std::endl;
ss << " $BIM -> hello.cpp {" << std::endl;
ss << " g++ $BIM -o hello.elf, " << std::endl;
ss << " ls " << std::endl;
ss << " }" << std::endl;
test_parse("DOC("
"VAR_DECL(VAR[BIM],IDENT[hello.cpp]),"
"VAR_DECL(VAR[BAM],IDENT[world.hpp]),"
"RULE("
"TARGET("
"VAR[BIM]"
"),DEPS("
"IDENT[hello.cpp]"
"),BLOCK("
"CMD("
"IDENT[g++],VAR[BIM],IDENT[-o],IDENT[hello.elf]"
"),CMD("
"IDENT[ls]"
")"
")))",
ss.str());
}