#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(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(token))->GetValue(); CHECK(value == std::u8string(reinterpret_cast(R"(foo bar)"))); CHECK(token->GetNext()->GetKind() == LexTokenKind::EndOfFile); delete token; }