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; \
|
2020-10-08 16:15:48 +00:00
|
|
|
auto lexer = Lexer(u8##str, u8##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::u8string(reinterpret_cast<const char8_t*>(str))); \
|
|
|
|
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;
|
2020-10-08 16:15:48 +00:00
|
|
|
auto lexer = Lexer(u8"multiline", u8R"("""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::u8string(reinterpret_cast<const char8_t*>(R"(foo
|
|
|
|
bar)")));
|
|
|
|
CHECK(token->GetNext()->GetKind() == LexTokenKind::EndOfFile);
|
|
|
|
}
|