muzgen/tests/Parser.cpp

58 lines
1.2 KiB
C++
Raw Permalink Normal View History

#include <catch2/catch.hpp>
#include "../lib/Parser.hpp"
#include "../lib/Lexer.hpp"
class ParserTest
{
public:
explicit ParserTest() {}
virtual ~ParserTest() {}
protected:
};
static void test_parser(std::string const& oracle,
std::string const& source)
{
muz::Lexer lexer;
lexer.scan(source);
muz::Parser parser;
auto node = parser.parse(lexer);
REQUIRE(oracle == node->string());
}
static void test_parser_err(std::string const& source)
{
muz::Lexer lexer;
lexer.scan(source);
muz::Parser parser;
REQUIRE_THROWS_AS(parser.parse(lexer), muz::syntax_error);
}
TEST_CASE_METHOD(ParserTest, "Parser_commands")
{
test_parser_err("[hello");
test_parser_err("hello]");
test_parser_err("12");
test_parser("PROG(CMD(IDENT[hello]),CMD(IDENT[world]))",
"[hello] [world]");
test_parser("PROG(CMD(IDENT[hello_world]))",
"[hello_world]");
test_parser("PROG(CMD(IDENT[sine],NUM[440]))",
"[sine 440]");
test_parser("PROG(CMD(IDENT[sine],NUM[440],NUM[217]))",
"[sine 440 217]");
}
TEST_CASE_METHOD(ParserTest, "Parser_directives")
{
test_parser("PROG(DIR(DIR_IDENT[@bim],CMD(IDENT[hello_world])))",
"@bim [hello_world]");
}