Replace parser test getParseError function for matchParseError (#1532)

Removes the getParseError function in Parser.test.cpp to use
matchParseError instead.
This commit is contained in:
ramdoys 2024-11-27 10:34:14 -05:00 committed by GitHub
parent 8f2ab4cbad
commit 8cc289fae4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -39,24 +39,6 @@ struct Counter
int Counter::instanceCount = 0; int Counter::instanceCount = 0;
// TODO: delete this and replace all other use of this function with matchParseError
std::string getParseError(const std::string& code)
{
Fixture f;
try
{
f.parse(code);
}
catch (const Luau::ParseErrors& e)
{
// in general, tests check only the first error
return e.getErrors().front().getMessage();
}
throw std::runtime_error("Expected a parse error in '" + code + "'");
}
} // namespace } // namespace
TEST_SUITE_BEGIN("AllocatorTests"); TEST_SUITE_BEGIN("AllocatorTests");
@ -465,62 +447,38 @@ TEST_CASE_FIXTURE(Fixture, "type_alias_span_is_correct")
TEST_CASE_FIXTURE(Fixture, "parse_error_messages") TEST_CASE_FIXTURE(Fixture, "parse_error_messages")
{ {
CHECK_EQ( matchParseError(R"(
getParseError(R"( local a: (number, number) -> (string
local a: (number, number) -> (string )", "Expected ')' (to close '(' at line 2), got <eof>");
)"),
"Expected ')' (to close '(' at line 2), got <eof>"
);
CHECK_EQ( matchParseError(R"(
getParseError(R"( local a: (number, number) -> (
local a: (number, number) -> ( string
string )", "Expected ')' (to close '(' at line 2), got <eof>");
)"),
"Expected ')' (to close '(' at line 2), got <eof>"
);
CHECK_EQ( matchParseError(R"(
getParseError(R"( local a: (number, number)
local a: (number, number) )", "Expected '->' when parsing function type, got <eof>");
)"),
"Expected '->' when parsing function type, got <eof>"
);
CHECK_EQ( matchParseError(R"(
getParseError(R"( local a: (number, number
local a: (number, number )", "Expected ')' (to close '(' at line 2), got <eof>");
)"),
"Expected ')' (to close '(' at line 2), got <eof>"
);
CHECK_EQ( matchParseError(R"(
getParseError(R"( local a: {foo: string,
local a: {foo: string, )", "Expected identifier when parsing table field, got <eof>");
)"),
"Expected identifier when parsing table field, got <eof>"
);
CHECK_EQ( matchParseError(R"(
getParseError(R"( local a: {foo: string
local a: {foo: string )", "Expected '}' (to close '{' at line 2), got <eof>");
)"),
"Expected '}' (to close '{' at line 2), got <eof>"
);
CHECK_EQ( matchParseError(R"(
getParseError(R"( local a: { [string]: number, [number]: string }
local a: { [string]: number, [number]: string } )", "Cannot have more than one table indexer");
)"),
"Cannot have more than one table indexer"
);
CHECK_EQ( matchParseError(R"(
getParseError(R"( type T = <a>foo
type T = <a>foo )", "Expected '(' when parsing function parameters, got 'foo'");
)"),
"Expected '(' when parsing function parameters, got 'foo'"
);
} }
TEST_CASE_FIXTURE(Fixture, "mixed_intersection_and_union_not_allowed") TEST_CASE_FIXTURE(Fixture, "mixed_intersection_and_union_not_allowed")
@ -548,10 +506,10 @@ TEST_CASE_FIXTURE(Fixture, "cannot_write_multiple_values_in_type_groups")
TEST_CASE_FIXTURE(Fixture, "type_alias_error_messages") TEST_CASE_FIXTURE(Fixture, "type_alias_error_messages")
{ {
CHECK_EQ(getParseError("type 5 = number"), "Expected identifier when parsing type name, got '5'"); matchParseError("type 5 = number", "Expected identifier when parsing type name, got '5'");
CHECK_EQ(getParseError("type A"), "Expected '=' when parsing type alias, got <eof>"); matchParseError("type A", "Expected '=' when parsing type alias, got <eof>");
CHECK_EQ(getParseError("type A<"), "Expected identifier, got <eof>"); matchParseError("type A<", "Expected identifier, got <eof>");
CHECK_EQ(getParseError("type A<B"), "Expected '>' (to close '<' at column 7), got <eof>"); matchParseError("type A<B", "Expected '>' (to close '<' at column 7), got <eof>");
} }
TEST_CASE_FIXTURE(Fixture, "type_assertion_expression") TEST_CASE_FIXTURE(Fixture, "type_assertion_expression")
@ -655,12 +613,9 @@ TEST_CASE_FIXTURE(Fixture, "vertical_space")
TEST_CASE_FIXTURE(Fixture, "parse_error_type_name") TEST_CASE_FIXTURE(Fixture, "parse_error_type_name")
{ {
CHECK_EQ( matchParseError(R"(
getParseError(R"( local a: Foo.=
local a: Foo.= )", "Expected identifier when parsing field name, got '='");
)"),
"Expected identifier when parsing field name, got '='"
);
} }
TEST_CASE_FIXTURE(Fixture, "parse_numbers_decimal") TEST_CASE_FIXTURE(Fixture, "parse_numbers_decimal")
@ -706,28 +661,25 @@ TEST_CASE_FIXTURE(Fixture, "parse_numbers_binary")
TEST_CASE_FIXTURE(Fixture, "parse_numbers_error") TEST_CASE_FIXTURE(Fixture, "parse_numbers_error")
{ {
CHECK_EQ(getParseError("return 0b123"), "Malformed number"); matchParseError("return 0b123", "Malformed number");
CHECK_EQ(getParseError("return 123x"), "Malformed number"); matchParseError("return 123x", "Malformed number");
CHECK_EQ(getParseError("return 0xg"), "Malformed number"); matchParseError("return 0xg", "Malformed number");
CHECK_EQ(getParseError("return 0x0x123"), "Malformed number"); matchParseError("return 0x0x123", "Malformed number");
CHECK_EQ(getParseError("return 0xffffffffffffffffffffllllllg"), "Malformed number"); matchParseError("return 0xffffffffffffffffffffllllllg", "Malformed number");
CHECK_EQ(getParseError("return 0x0xffffffffffffffffffffffffffff"), "Malformed number"); matchParseError("return 0x0xffffffffffffffffffffffffffff", "Malformed number");
} }
TEST_CASE_FIXTURE(Fixture, "break_return_not_last_error") TEST_CASE_FIXTURE(Fixture, "break_return_not_last_error")
{ {
CHECK_EQ(getParseError("return 0 print(5)"), "Expected <eof>, got 'print'"); matchParseError("return 0 print(5)", "Expected <eof>, got 'print'");
CHECK_EQ(getParseError("while true do break print(5) end"), "Expected 'end' (to close 'do' at column 12), got 'print'"); matchParseError("while true do break print(5) end", "Expected 'end' (to close 'do' at column 12), got 'print'");
} }
TEST_CASE_FIXTURE(Fixture, "error_on_unicode") TEST_CASE_FIXTURE(Fixture, "error_on_unicode")
{ {
CHECK_EQ( matchParseError(R"(
getParseError(R"(
local = 10 local = 10
)"), )", "Expected identifier when parsing variable name, got Unicode character U+2603");
"Expected identifier when parsing variable name, got Unicode character U+2603"
);
} }
TEST_CASE_FIXTURE(Fixture, "allow_unicode_in_string") TEST_CASE_FIXTURE(Fixture, "allow_unicode_in_string")
@ -738,20 +690,17 @@ TEST_CASE_FIXTURE(Fixture, "allow_unicode_in_string")
TEST_CASE_FIXTURE(Fixture, "error_on_confusable") TEST_CASE_FIXTURE(Fixture, "error_on_confusable")
{ {
CHECK_EQ( matchParseError(R"(
getParseError(R"( local pi = 313
local pi = 313 )", "Expected identifier when parsing expression, got Unicode character U+2024 (did you mean '.'?)");
)"),
"Expected identifier when parsing expression, got Unicode character U+2024 (did you mean '.'?)"
);
} }
TEST_CASE_FIXTURE(Fixture, "error_on_non_utf8_sequence") TEST_CASE_FIXTURE(Fixture, "error_on_non_utf8_sequence")
{ {
const char* expected = "Expected identifier when parsing expression, got invalid UTF-8 sequence"; const char* expected = "Expected identifier when parsing expression, got invalid UTF-8 sequence";
CHECK_EQ(getParseError("local pi = \xFF!"), expected); matchParseError("local pi = \xFF!", expected);
CHECK_EQ(getParseError("local pi = \xE2!"), expected); matchParseError("local pi = \xE2!", expected);
} }
TEST_CASE_FIXTURE(Fixture, "lex_broken_unicode") TEST_CASE_FIXTURE(Fixture, "lex_broken_unicode")
@ -819,7 +768,7 @@ TEST_CASE_FIXTURE(Fixture, "parse_continue")
TEST_CASE_FIXTURE(Fixture, "continue_not_last_error") TEST_CASE_FIXTURE(Fixture, "continue_not_last_error")
{ {
CHECK_EQ(getParseError("while true do continue print(5) end"), "Expected 'end' (to close 'do' at column 12), got 'print'"); matchParseError("while true do continue print(5) end", "Expected 'end' (to close 'do' at column 12), got 'print'");
} }
TEST_CASE_FIXTURE(Fixture, "parse_export_type") TEST_CASE_FIXTURE(Fixture, "parse_export_type")
@ -862,7 +811,7 @@ TEST_CASE_FIXTURE(Fixture, "export_is_an_identifier_only_when_followed_by_type")
TEST_CASE_FIXTURE(Fixture, "incomplete_statement_error") TEST_CASE_FIXTURE(Fixture, "incomplete_statement_error")
{ {
CHECK_EQ(getParseError("fiddlesticks"), "Incomplete statement: expected assignment or a function call"); matchParseError("fiddlesticks", "Incomplete statement: expected assignment or a function call");
} }
TEST_CASE_FIXTURE(Fixture, "parse_compound_assignment") TEST_CASE_FIXTURE(Fixture, "parse_compound_assignment")