grino/tests/Lexer.cpp

142 lines
2.9 KiB
C++

#include <catch2/catch.hpp>
#include "../src/Lexer.hpp"
class LexerTest
{
public:
explicit LexerTest() {}
virtual ~LexerTest() {}
void test_next(grino::Lexer& lexer, std::string const& oracle)
{
auto node = lexer.next();
REQUIRE(node != nullptr);
REQUIRE(oracle == node->string());
}
void test_end(grino::Lexer& lexer)
{
auto node = lexer.next();
REQUIRE(node == nullptr);
}
protected:
grino::Logger m_logger;
};
TEST_CASE_METHOD(LexerTest, "Lexer_booleans")
{
grino::Lexer lexer {m_logger, "tests/lexer"};
SECTION("space around")
{
lexer.scan(" true false ");
test_next(lexer, "BOOL[true]");
test_next(lexer, "BOOL[false]");
test_end(lexer);
}
SECTION("no space around")
{
lexer.scan("true false");
test_next(lexer, "BOOL[true]");
test_next(lexer, "BOOL[false]");
test_end(lexer);
}
}
TEST_CASE_METHOD(LexerTest, "Lexer_comments")
{
grino::Lexer lexer {m_logger, "tests/lexer"};
lexer.scan(" true ; false \n\n true ");
test_next(lexer, "BOOL[true]");
test_next(lexer, "BOOL[true]");
test_end(lexer);
}
TEST_CASE_METHOD(LexerTest, "Lexer_vardecl")
{
grino::Lexer lexer {m_logger, "tests/lexer"};
lexer.scan(" $ () coucou) ");
test_next(lexer, "DECL");
test_next(lexer, "OPAR");
test_next(lexer, "CPAR");
test_next(lexer, "IDENT[coucou]");
test_next(lexer, "CPAR");
test_end(lexer);
}
TEST_CASE_METHOD(LexerTest, "Lexer_no_end_space")
{
grino::Lexer lexer {m_logger, "tests/lexer"};
lexer.scan(")");
test_next(lexer, "CPAR");
test_end(lexer);
}
TEST_CASE_METHOD(LexerTest, "Lexer_int")
{
grino::Lexer lexer {m_logger, "tests/lexer"};
lexer.scan("4 39 256 2 -7");
test_next(lexer, "INT[4]");
test_next(lexer, "INT[39]");
test_next(lexer, "INT[256]");
test_next(lexer, "INT[2]");
test_next(lexer, "INT[-7]");
test_end(lexer);
}
TEST_CASE_METHOD(LexerTest, "Lexer_lambda")
{
grino::Lexer lexer {m_logger, "tests/lexer"};
lexer.scan(" -> ");
test_next(lexer, "LAMBDA");
test_end(lexer);
}
TEST_CASE_METHOD(LexerTest, "Lexer_block")
{
grino::Lexer lexer {m_logger, "tests/lexer"};
lexer.scan(" : ");
test_next(lexer, "COLON");
test_end(lexer);
}
TEST_CASE_METHOD(LexerTest, "Lexer_float")
{
grino::Lexer lexer {m_logger, "tests/lexer"};
lexer.scan(" 3.12 -0.7 1.0");
test_next(lexer, "FLOAT[3.12]");
test_next(lexer, "FLOAT[-0.7]");
test_next(lexer, "FLOAT[1.0]");
test_end(lexer);
}
TEST_CASE_METHOD(LexerTest, "Lexer_strings")
{
grino::Lexer lexer {m_logger, "tests/lexer"};
lexer.scan(" ' hello ' '\\'bim\\'' ");
test_next(lexer, "STRING[' hello ']");
test_next(lexer, "STRING[''bim'']");
test_end(lexer);
}
TEST_CASE_METHOD(LexerTest, "Lexer_import")
{
grino::Lexer lexer {m_logger, "tests/lexer"};
lexer.scan(" @ :: ");
test_next(lexer, "IMPORT");
test_next(lexer, "NS");
test_end(lexer);
}