snake/tests/Lexer.cpp

76 lines
1.3 KiB
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();
}
TEST_CASE_METHOD(LexerTest, "Lexer_vars")
{
m_lexer.scan("$hello $WORLD = ");
test_next("VAR[hello]");
test_next("VAR[WORLD]");
test_next("ASSIGN");
test_end();
}
2023-10-14 15:55:51 +00:00
TEST_CASE_METHOD(LexerTest, "Lexer_arrays")
{
2023-10-14 19:20:39 +00:00
m_lexer.scan(" ()[] ");
2023-10-14 15:55:51 +00:00
test_next("OPAR");
test_next("CPAR");
2023-10-14 19:20:39 +00:00
test_next("OSQUARE");
test_next("CSQUARE");
2023-10-14 15:55:51 +00:00
test_end();
}
TEST_CASE_METHOD(LexerTest, "Lexer_generic")
{
m_lexer.scan(" ^.o ^.cpp ^.tar.gz ");
test_next("GENERIC[.o]");
test_next("GENERIC[.cpp]");
test_next("GENERIC[.tar.gz]");
test_end();
}