38 lines
2.1 KiB
C++
38 lines
2.1 KiB
C++
#include "../../extern/doctest.hpp"
|
|
#include "../../src/Parser/Lexer/Lexer.hpp"
|
|
|
|
using namespace ElohimScript::Parser;
|
|
|
|
#define STRING_TEST(str, constraint) \
|
|
TEST_CASE("Lex string " constraint str constraint) { \
|
|
ElohimScript::Diagnostics::Diagnostics diag; \
|
|
auto lexer = Lexer(constraint str constraint, &diag); \
|
|
const auto* token = lexer.Lex(); \
|
|
CHECK(diag.GetMessages().empty()); \
|
|
REQUIRE(token->GetKind() == LexTokenKind::StringLiteral); \
|
|
auto value = ((const StringLiteral*)token)->GetValue(); \
|
|
CHECK(value == std::u8string(reinterpret_cast<const char8_t*>(str))); \
|
|
CHECK(token->GetNext()->GetKind() == LexTokenKind::EndOfFile); \
|
|
delete token; \
|
|
}
|
|
|
|
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") {
|
|
ElohimScript::Diagnostics::Diagnostics diag;
|
|
auto lexer = Lexer(R"("""foo
|
|
bar""")",
|
|
&diag);
|
|
const auto* token = lexer.Lex();
|
|
CHECK(diag.GetMessages().empty());
|
|
REQUIRE(token->GetKind() == LexTokenKind::StringLiteral);
|
|
auto value = (dynamic_cast<const StringLiteral*>(token))->GetValue();
|
|
CHECK(value == std::u8string(reinterpret_cast<const char8_t*>(R"(foo
|
|
bar)")));
|
|
CHECK(token->GetNext()->GetKind() == LexTokenKind::EndOfFile);
|
|
delete token;
|
|
} |