114 lines
2.1 KiB
C++
114 lines
2.1 KiB
C++
#include <catch2/catch.hpp>
|
|
#include "../lib/Lexer.hpp"
|
|
|
|
class LexerTest
|
|
{
|
|
public:
|
|
explicit LexerTest() {}
|
|
virtual ~LexerTest() {}
|
|
|
|
void test_next(wg::Lexer& lexer, std::string const& oracle)
|
|
{
|
|
auto n = lexer.next();
|
|
REQUIRE(nullptr != n);
|
|
|
|
REQUIRE(oracle == n->string());
|
|
}
|
|
|
|
void test_end(wg::Lexer& lexer)
|
|
{
|
|
auto n = lexer.next();
|
|
REQUIRE(nullptr == n);
|
|
}
|
|
|
|
protected:
|
|
};
|
|
|
|
TEST_CASE_METHOD(LexerTest, "Lexer_")
|
|
{
|
|
wg::Lexer lex;
|
|
lex.scan(" # canard #canard");
|
|
test_next(lex, "HASH");
|
|
test_next(lex, "IDENT[canard]");
|
|
test_next(lex, "HASH");
|
|
test_next(lex, "IDENT[canard]");
|
|
test_end(lex);
|
|
}
|
|
|
|
TEST_CASE_METHOD(LexerTest, "Lexer_int_literal")
|
|
{
|
|
wg::Lexer lex;
|
|
lex.scan(" 3 -2 78 ");
|
|
test_next(lex, "INT[3]");
|
|
test_next(lex, "INT[-2]");
|
|
test_next(lex, "INT[78]");
|
|
test_end(lex);
|
|
}
|
|
|
|
TEST_CASE_METHOD(LexerTest, "Lexer_int_arith")
|
|
{
|
|
wg::Lexer lex;
|
|
lex.scan(" +-*/% ()");
|
|
test_next(lex, "ADD");
|
|
test_next(lex, "SUB");
|
|
test_next(lex, "MUL");
|
|
test_next(lex, "DIV");
|
|
test_next(lex, "MOD");
|
|
test_next(lex, "OPAR");
|
|
test_next(lex, "CPAR");
|
|
test_end(lex);
|
|
}
|
|
|
|
TEST_CASE_METHOD(LexerTest, "Lexer_fun_call")
|
|
{
|
|
wg::Lexer lex;
|
|
lex.scan(" , int extern ");
|
|
test_next(lex, "COMMA");
|
|
test_next(lex, "TYPE[int]");
|
|
test_next(lex, "EXTERN");
|
|
test_end(lex);
|
|
}
|
|
|
|
TEST_CASE_METHOD(LexerTest, "Lexer_pkg_namespace")
|
|
{
|
|
wg::Lexer lex;
|
|
lex.scan(" . ");
|
|
test_next(lex, "DOT");
|
|
test_end(lex);
|
|
}
|
|
|
|
TEST_CASE_METHOD(LexerTest, "Lexer_as")
|
|
{
|
|
wg::Lexer lex;
|
|
lex.scan(" as ");
|
|
test_next(lex, "AS");
|
|
test_end(lex);
|
|
}
|
|
|
|
TEST_CASE_METHOD(LexerTest, "Lexer_bool")
|
|
{
|
|
wg::Lexer lex;
|
|
lex.scan(" true and false or not afalse truea ");
|
|
test_next(lex, "BOOL[true]");
|
|
test_next(lex, "AND");
|
|
test_next(lex, "BOOL[false]");
|
|
test_next(lex, "OR");
|
|
test_next(lex, "NOT");
|
|
test_next(lex, "IDENT[afalse]");
|
|
test_next(lex, "IDENT[truea]");
|
|
test_end(lex);
|
|
}
|
|
|
|
TEST_CASE_METHOD(LexerTest, "Lexer_cmp")
|
|
{
|
|
wg::Lexer lex;
|
|
lex.scan(" == <> < <= > >= ");
|
|
test_next(lex, "EQ");
|
|
test_next(lex, "NE");
|
|
test_next(lex, "LT");
|
|
test_next(lex, "LE");
|
|
test_next(lex, "GT");
|
|
test_next(lex, "GE");
|
|
test_end(lex);
|
|
}
|