106 lines
2.5 KiB
C++
106 lines
2.5 KiB
C++
#include <string>
|
|
#include <catch2/catch.hpp>
|
|
#include "../lib/Lexer.hpp"
|
|
#include "lib/SrcLoc.hpp"
|
|
#include "lib/StatusLog.hpp"
|
|
|
|
class LexerTest
|
|
{
|
|
public:
|
|
explicit LexerTest() {}
|
|
virtual ~LexerTest() {}
|
|
|
|
std::string get_str(size_t index)
|
|
{
|
|
auto node = m_lexer.get_or_nullptr(index);
|
|
|
|
if (!node) { return ""; }
|
|
else { return node->string(); }
|
|
}
|
|
|
|
protected:
|
|
roza::StatusLog m_log;
|
|
roza::Lexer m_lexer {m_log, roza::SrcLoc("lexer_tests")};
|
|
};
|
|
|
|
TEST_CASE_METHOD(LexerTest, "Lexer_integers")
|
|
{
|
|
m_lexer.scan("45 12 -3");
|
|
REQUIRE("INT[45]" == get_str(0));
|
|
REQUIRE("INT[12]" == get_str(1));
|
|
REQUIRE("SUB" == get_str(2));
|
|
REQUIRE("INT[3]" == get_str(3));
|
|
REQUIRE("" == get_str(4));
|
|
}
|
|
|
|
TEST_CASE_METHOD(LexerTest, "Lexer_comments")
|
|
{
|
|
m_lexer.scan("45 # 12 \n -3");
|
|
REQUIRE("INT[45]" == get_str(0));
|
|
REQUIRE("SUB" == get_str(1));
|
|
REQUIRE("INT[3]" == get_str(2));
|
|
REQUIRE("" == get_str(3));
|
|
}
|
|
|
|
TEST_CASE_METHOD(LexerTest, "Lexer_int_arith")
|
|
{
|
|
m_lexer.scan("+-*/%^()");
|
|
REQUIRE("ADD" == get_str(0));
|
|
REQUIRE("SUB" == get_str(1));
|
|
REQUIRE("MUL" == get_str(2));
|
|
REQUIRE("DIV" == get_str(3));
|
|
REQUIRE("MOD" == get_str(4));
|
|
REQUIRE("POW" == get_str(5));
|
|
REQUIRE("OPAR" == get_str(6));
|
|
REQUIRE("CPAR" == get_str(7));
|
|
REQUIRE("" == get_str(8));
|
|
}
|
|
|
|
TEST_CASE_METHOD(LexerTest, "Lexer_keywords")
|
|
{
|
|
REQUIRE_NOTHROW(m_lexer.scan(" and+ "));
|
|
REQUIRE_NOTHROW(m_lexer.scan(" (and) "));
|
|
}
|
|
|
|
TEST_CASE_METHOD(LexerTest, "Lexer_bool")
|
|
{
|
|
m_lexer.scan("and or not true false =>");
|
|
REQUIRE("AND" == get_str(0));
|
|
REQUIRE("OR" == get_str(1));
|
|
REQUIRE("NOT" == get_str(2));
|
|
REQUIRE("BOOL[true]" == get_str(3));
|
|
REQUIRE("BOOL[false]" == get_str(4));
|
|
REQUIRE("IMP" == get_str(5));
|
|
REQUIRE("" == get_str(6));
|
|
}
|
|
|
|
TEST_CASE_METHOD(LexerTest, "Lexer_comparisons")
|
|
{
|
|
m_lexer.scan("== != < > <= >=");
|
|
REQUIRE("EQ" == get_str(0));
|
|
REQUIRE("NE" == get_str(1));
|
|
REQUIRE("LT" == get_str(2));
|
|
REQUIRE("GT" == get_str(3));
|
|
REQUIRE("LE" == get_str(4));
|
|
REQUIRE("GE" == get_str(5));
|
|
REQUIRE("" == get_str(6));
|
|
}
|
|
|
|
TEST_CASE_METHOD(LexerTest, "Lexer_asserts")
|
|
{
|
|
m_lexer.scan(" assert assert_static_fail ");
|
|
REQUIRE("ASSERT" == get_str(0));
|
|
REQUIRE("ASSERT_STATIC_FAIL" == get_str(1));
|
|
REQUIRE("" == get_str(2));
|
|
}
|
|
|
|
TEST_CASE_METHOD(LexerTest, "Lexer_declarations")
|
|
{
|
|
m_lexer.scan(" let let! hello = ");
|
|
REQUIRE("LET" == get_str(0));
|
|
REQUIRE("LET_MUT" == get_str(1));
|
|
REQUIRE("IDENT[hello]" == get_str(2));
|
|
REQUIRE("ASSIGN" == get_str(3));
|
|
REQUIRE("" == get_str(4));
|
|
}
|