This repository has been archived on 2024-03-07. You can view files and clone it, but cannot push or open issues/pull-requests.
wongola/tests/Lexer.cpp

87 lines
1.5 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);
}