MalachScript/tests/LexerTests/StringLexTests.cpp

36 lines
2.0 KiB
C++
Raw Normal View History

2020-10-04 15:15:28 +00:00
#include "../../extern/doctest.hpp"
#include "../../src/Parser/Lexer/Lexer.hpp"
2020-10-05 15:45:00 +00:00
using namespace MalachScript::Parser;
2020-10-04 15:15:28 +00:00
#define STRING_TEST(str, constraint) \
TEST_CASE("Lex string " constraint str constraint) { \
2021-01-01 22:31:30 +00:00
MalachScript::Diagnostics::Logger diag; \
auto lexer = Lexer(str, constraint str constraint, &diag); \
2020-10-04 15:15:28 +00:00
const auto* token = lexer.Lex(); \
2020-10-04 17:38:13 +00:00
CHECK(diag.GetMessages().empty()); \
2020-10-04 15:15:28 +00:00
REQUIRE(token->GetKind() == LexTokenKind::StringLiteral); \
auto value = ((const StringLiteral*)token)->GetValue(); \
CHECK(value == std::string(reinterpret_cast<const char*>(str))); \
2020-10-04 15:15:28 +00:00
CHECK(token->GetNext()->GetKind() == LexTokenKind::EndOfFile); \
}
STRING_TEST("foo bar", "'");
STRING_TEST("foo bar", "\"");
STRING_TEST("foo bar", "\"\"\"");
STRING_TEST("\"foo bar\"", "\"\"\"");
STRING_TEST("\"\"foo bar\"\"", "\"\"\"");
TEST_CASE("Lex multiline string") {
2021-01-01 22:31:30 +00:00
MalachScript::Diagnostics::Logger diag;
auto lexer = Lexer("multiline", R"("""foo
2020-10-04 17:38:13 +00:00
bar""")",
&diag);
2020-10-04 15:15:28 +00:00
const auto* token = lexer.Lex();
2020-10-04 17:38:13 +00:00
CHECK(diag.GetMessages().empty());
2020-10-04 15:15:28 +00:00
REQUIRE(token->GetKind() == LexTokenKind::StringLiteral);
auto value = (dynamic_cast<const StringLiteral*>(token))->GetValue();
CHECK(value == std::string(reinterpret_cast<const char*>(R"(foo
2020-10-04 15:15:28 +00:00
bar)")));
CHECK(token->GetNext()->GetKind() == LexTokenKind::EndOfFile);
}