#include #include #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)); } TEST_CASE_METHOD(LexerTest, "Lexer_if") { m_lexer.scan(" if else end "); REQUIRE("IF" == get_str(0)); REQUIRE("ELSE" == get_str(1)); REQUIRE("END" == get_str(2)); REQUIRE("" == get_str(3)); } TEST_CASE_METHOD(LexerTest, "Lexer_types") { m_lexer.scan(" int bool "); REQUIRE("TYPE[int]" == get_str(0)); REQUIRE("TYPE[bool]" == get_str(1)); REQUIRE("" == get_str(2)); } TEST_CASE_METHOD(LexerTest, "Lexer_fun") { m_lexer.scan(" fun as -> "); REQUIRE("FUN" == get_str(0)); REQUIRE("AS" == get_str(1)); REQUIRE("ARROW" == get_str(2)); REQUIRE("" == get_str(3)); } TEST_CASE_METHOD(LexerTest, "Lexer_call") { m_lexer.scan(" {} "); REQUIRE("OBRACE" == get_str(0)); REQUIRE("CBRACE" == get_str(1)); REQUIRE("" == get_str(2)); }