2023-09-20 11:31:35 +00:00
|
|
|
#include <catch2/catch.hpp>
|
|
|
|
#include "../src/Lexer.hpp"
|
|
|
|
|
|
|
|
class LexerTest
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit LexerTest() {}
|
|
|
|
virtual ~LexerTest() {}
|
|
|
|
|
|
|
|
void test_next(std::string const& oracle)
|
|
|
|
{
|
|
|
|
auto node = m_lexer.next();
|
|
|
|
REQUIRE(node);
|
|
|
|
REQUIRE(oracle == node->string());
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_end()
|
|
|
|
{
|
|
|
|
auto node = m_lexer.next();
|
|
|
|
REQUIRE(nullptr == node);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
fk::Loc m_loc {"tests/lexer"};
|
|
|
|
fk::Lexer m_lexer { m_loc };
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_CASE_METHOD(LexerTest, "Lexer_int")
|
|
|
|
{
|
|
|
|
m_lexer.scan(" 34 7 -296");
|
|
|
|
|
|
|
|
test_next("INT[34]");
|
|
|
|
test_next("INT[7]");
|
|
|
|
test_next("INT[-296]");
|
|
|
|
test_end();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_METHOD(LexerTest, "Lexer_float")
|
|
|
|
{
|
|
|
|
m_lexer.scan(" .34 7.3 -296. ");
|
|
|
|
|
|
|
|
test_next("FLOAT[0.34]");
|
|
|
|
test_next("FLOAT[7.3]");
|
|
|
|
test_next("FLOAT[-296.0]");
|
|
|
|
test_end();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST_CASE_METHOD(LexerTest, "Lexer_bool")
|
|
|
|
{
|
|
|
|
m_lexer.scan(" true false ");
|
|
|
|
|
|
|
|
test_next("BOOL[true]");
|
|
|
|
test_next("BOOL[false]");
|
|
|
|
test_end();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_METHOD(LexerTest, "Lexer_string")
|
|
|
|
{
|
|
|
|
m_lexer.scan(" 'true ' 'false' ");
|
|
|
|
|
|
|
|
test_next("STRING['true ']");
|
|
|
|
test_next("STRING['false']");
|
|
|
|
test_end();
|
|
|
|
}
|
2023-09-20 15:17:13 +00:00
|
|
|
|
|
|
|
TEST_CASE_METHOD(LexerTest, "Lexer_ident")
|
|
|
|
{
|
|
|
|
m_lexer.scan(" odd? hello-world! Aze06 _gdb ");
|
|
|
|
|
|
|
|
test_next("IDENT[odd?]");
|
|
|
|
test_next("IDENT[hello-world!]");
|
|
|
|
test_next("IDENT[Aze06]");
|
|
|
|
test_next("IDENT[_gdb]");
|
|
|
|
|
|
|
|
test_end();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE_METHOD(LexerTest, "Lexer_parenthesis")
|
|
|
|
{
|
|
|
|
m_lexer.scan(" () (aze) ");
|
|
|
|
|
|
|
|
test_next("OPAR");
|
|
|
|
test_next("CPAR");
|
|
|
|
|
|
|
|
test_next("OPAR");
|
|
|
|
test_next("IDENT[aze]");
|
|
|
|
test_next("CPAR");
|
|
|
|
test_end();
|
|
|
|
}
|
2023-09-21 19:17:39 +00:00
|
|
|
|
|
|
|
TEST_CASE_METHOD(LexerTest, "Lexer_lambda")
|
|
|
|
{
|
|
|
|
m_lexer.scan(" (->) ");
|
|
|
|
test_next("OPAR");
|
|
|
|
test_next("RARROW");
|
|
|
|
test_next("CPAR");
|
|
|
|
test_end();
|
|
|
|
}
|