58 lines
1.2 KiB
C++
58 lines
1.2 KiB
C++
#include <catch2/catch.hpp>
|
|
#include "../lib/Lexer.hpp"
|
|
#include "../lib/Parser.hpp"
|
|
|
|
class ParserTest
|
|
{
|
|
public:
|
|
explicit ParserTest() {}
|
|
virtual ~ParserTest() {}
|
|
|
|
void test_parse(std::string const& oracle,
|
|
std::string const& source)
|
|
{
|
|
wg::Lexer lex;
|
|
lex.scan(source);
|
|
auto tokens = lex.all();
|
|
|
|
wg::Parser parser;
|
|
|
|
auto node = parser.parse(tokens);
|
|
|
|
REQUIRE(oracle == node->string());
|
|
}
|
|
|
|
protected:
|
|
};
|
|
|
|
TEST_CASE_METHOD(ParserTest, "Parser_dir")
|
|
{
|
|
test_parse("PROG(DIR(IDENT[hello],IDENT[world]))",
|
|
"#hello world");
|
|
}
|
|
|
|
|
|
TEST_CASE_METHOD(ParserTest, "Parser_int")
|
|
{
|
|
test_parse("PROG(INT[45])",
|
|
" 45; ");
|
|
|
|
test_parse("PROG(ADD(INT[1],MUL(INT[2],INT[3])))",
|
|
" 1 + 2 * 3; ");
|
|
|
|
test_parse("PROG(MUL(ADD(INT[1],INT[2]),INT[3]))",
|
|
" (1 + 2) * 3; ");
|
|
|
|
test_parse("PROG(SUB(INT[1],DIV(INT[2],INT[3])))",
|
|
" 1 - 2 / 3; ");
|
|
|
|
test_parse("PROG(DIV(SUB(INT[1],INT[2]),INT[3]))",
|
|
" (1 - 2) / 3; ");
|
|
|
|
test_parse("PROG(ADD(INT[1],MOD(INT[2],INT[3])))",
|
|
" 1 + 2 % 3; ");
|
|
|
|
test_parse("PROG(MOD(ADD(INT[1],INT[2]),INT[3]))",
|
|
" (1 + 2) % 3; ");
|
|
}
|