snake/tests/Lexer.cpp

44 lines
804 B
C++
Raw Normal View History

2023-10-14 01:56:31 +00:00
#include <catch2/catch.hpp>
#include "../src/Lexer.hpp"
using namespace sn;
class LexerTest
{
public:
explicit LexerTest() {}
virtual ~LexerTest() {}
void test_next(std::string const& oracle)
{
auto tok = m_lexer.next();
INFO("tok for '" << oracle << "' is nullopt");
REQUIRE(tok);
INFO("expected '" << oracle << "', got '" << (*tok)->string() << "'");
REQUIRE(oracle == (*tok)->string());
}
void test_end()
{
auto tok = m_lexer.next();
INFO("end not found");
REQUIRE(std::nullopt == tok);
}
protected:
Lexer m_lexer;
};
TEST_CASE_METHOD(LexerTest, "Lexer_rule")
{
m_lexer.scan("hello.world, -> { }");
test_next("IDENT[hello.world]");
test_next("COMMA");
test_next("RARROW");
test_next("OBRACE");
test_next("CBRACE");
test_end();
}