From cbe84bd5ea47cdfa989ee596f0b2750bf030af9e Mon Sep 17 00:00:00 2001 From: Kampfkarren Date: Mon, 22 Aug 2022 14:41:27 -0700 Subject: [PATCH] More tests --- Ast/src/Lexer.cpp | 2 +- tests/Lexer.test.cpp | 20 +++++++++++++++++++- tests/Parser.test.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 2 deletions(-) diff --git a/Ast/src/Lexer.cpp b/Ast/src/Lexer.cpp index 6367c1a1..e3c0938e 100644 --- a/Ast/src/Lexer.cpp +++ b/Ast/src/Lexer.cpp @@ -621,7 +621,6 @@ Lexeme Lexer::readInterpolatedStringSection(Position start, Lexeme::Type formatT case 0: case '\r': case '\n': - // INTERP TODO: Clear anything we've added to the brace stack, and write a test to see what happens if we don't return Lexeme(Location(start, position()), Lexeme::BrokenString); case '\\': @@ -647,6 +646,7 @@ Lexeme Lexer::readInterpolatedStringSection(Position start, Lexeme::Type formatT } consume(); + return Lexeme(Location(start, position()), endType, &buffer[startOffset], offset - startOffset - 1); } diff --git a/tests/Lexer.test.cpp b/tests/Lexer.test.cpp index 54a55400..7330aba6 100644 --- a/tests/Lexer.test.cpp +++ b/tests/Lexer.test.cpp @@ -138,7 +138,7 @@ TEST_CASE("lookahead") CHECK_EQ(lexer.lookahead().type, Lexeme::Eof); } -TEST_CASE("stringInterpolation") +TEST_CASE("string_interpolation_basic") { ScopedFastFlag sff{"LuauInterpolatedStringBaseSupport", true}; @@ -157,4 +157,22 @@ TEST_CASE("stringInterpolation") CHECK_EQ(interpEnd.type, Lexeme::InterpStringEnd); } +TEST_CASE("string_interpolation_unmatched_brace") +{ + ScopedFastFlag sff{"LuauInterpolatedStringBaseSupport", true}; + + const std::string testInput = R"({ + `hello {"world"} + } -- this might be incorrectly parsed as a string)"; + Luau::Allocator alloc; + AstNameTable table(alloc); + Lexer lexer(testInput.c_str(), testInput.size(), table); + + CHECK_EQ(lexer.next().type, '{'); + CHECK_EQ(lexer.next().type, Lexeme::InterpStringBegin); + CHECK_EQ(lexer.next().type, Lexeme::QuotedString); + CHECK_EQ(lexer.next().type, Lexeme::BrokenString); + CHECK_EQ(lexer.next().type, '}'); +} + TEST_SUITE_END(); diff --git a/tests/Parser.test.cpp b/tests/Parser.test.cpp index 5d85fe34..95fccb06 100644 --- a/tests/Parser.test.cpp +++ b/tests/Parser.test.cpp @@ -966,6 +966,46 @@ TEST_CASE_FIXTURE(Fixture, "parse_interpolated_string_without_end_brace") CHECK_NE(columnOfEndBraceError("_ = `{a`"), columnOfEndBraceError("_ = `{a`")); } +TEST_CASE_FIXTURE(Fixture, "parse_interpolated_string_without_end_brace_in_table") +{ + ScopedFastFlag sff{"LuauInterpolatedStringBaseSupport", true}; + + try + { + parse(R"( + _ = { `{a` } + )"); + FAIL("Expected ParseErrors to be thrown"); + } + catch (const ParseErrors& e) + { + CHECK_EQ(e.getErrors().size(), 2); + + CHECK_EQ("Malformed interpolated string, did you forget to add a '}'?", e.getErrors().front().getMessage()); + CHECK_EQ("Expected '}' (to close '{' at line 2), got ", e.getErrors().back().getMessage()); + } +} + +TEST_CASE_FIXTURE(Fixture, "parse_interpolated_string_mid_without_end_brace_in_table") +{ + ScopedFastFlag sff{"LuauInterpolatedStringBaseSupport", true}; + + try + { + parse(R"( + _ = { `x {"y"} {z` } + )"); + FAIL("Expected ParseErrors to be thrown"); + } + catch (const ParseErrors& e) + { + CHECK_EQ(e.getErrors().size(), 2); + + CHECK_EQ("Malformed interpolated string, did you forget to add a '}'?", e.getErrors().front().getMessage()); + CHECK_EQ("Expected '}' (to close '{' at line 2), got ", e.getErrors().back().getMessage()); + } +} + TEST_CASE_FIXTURE(Fixture, "parse_interpolated_string_as_type_fail") { ScopedFastFlag sff{"LuauInterpolatedStringBaseSupport", true};