Skip to content

Commit 8cc289f

Browse files
author
ramdoys
authored
Replace parser test getParseError function for matchParseError (#1532)
Removes the getParseError function in Parser.test.cpp to use matchParseError instead.
1 parent 8f2ab4c commit 8cc289f

File tree

1 file changed

+49
-100
lines changed

1 file changed

+49
-100
lines changed

tests/Parser.test.cpp

+49-100
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,6 @@ struct Counter
3939

4040
int Counter::instanceCount = 0;
4141

42-
// TODO: delete this and replace all other use of this function with matchParseError
43-
std::string getParseError(const std::string& code)
44-
{
45-
Fixture f;
46-
47-
try
48-
{
49-
f.parse(code);
50-
}
51-
catch (const Luau::ParseErrors& e)
52-
{
53-
// in general, tests check only the first error
54-
return e.getErrors().front().getMessage();
55-
}
56-
57-
throw std::runtime_error("Expected a parse error in '" + code + "'");
58-
}
59-
6042
} // namespace
6143

6244
TEST_SUITE_BEGIN("AllocatorTests");
@@ -465,62 +447,38 @@ TEST_CASE_FIXTURE(Fixture, "type_alias_span_is_correct")
465447

466448
TEST_CASE_FIXTURE(Fixture, "parse_error_messages")
467449
{
468-
CHECK_EQ(
469-
getParseError(R"(
470-
local a: (number, number) -> (string
471-
)"),
472-
"Expected ')' (to close '(' at line 2), got <eof>"
473-
);
450+
matchParseError(R"(
451+
local a: (number, number) -> (string
452+
)", "Expected ')' (to close '(' at line 2), got <eof>");
474453

475-
CHECK_EQ(
476-
getParseError(R"(
477-
local a: (number, number) -> (
478-
string
479-
)"),
480-
"Expected ')' (to close '(' at line 2), got <eof>"
481-
);
454+
matchParseError(R"(
455+
local a: (number, number) -> (
456+
string
457+
)", "Expected ')' (to close '(' at line 2), got <eof>");
482458

483-
CHECK_EQ(
484-
getParseError(R"(
485-
local a: (number, number)
486-
)"),
487-
"Expected '->' when parsing function type, got <eof>"
488-
);
459+
matchParseError(R"(
460+
local a: (number, number)
461+
)", "Expected '->' when parsing function type, got <eof>");
489462

490-
CHECK_EQ(
491-
getParseError(R"(
492-
local a: (number, number
493-
)"),
494-
"Expected ')' (to close '(' at line 2), got <eof>"
495-
);
463+
matchParseError(R"(
464+
local a: (number, number
465+
)", "Expected ')' (to close '(' at line 2), got <eof>");
496466

497-
CHECK_EQ(
498-
getParseError(R"(
499-
local a: {foo: string,
500-
)"),
501-
"Expected identifier when parsing table field, got <eof>"
502-
);
467+
matchParseError(R"(
468+
local a: {foo: string,
469+
)", "Expected identifier when parsing table field, got <eof>");
503470

504-
CHECK_EQ(
505-
getParseError(R"(
506-
local a: {foo: string
507-
)"),
508-
"Expected '}' (to close '{' at line 2), got <eof>"
509-
);
471+
matchParseError(R"(
472+
local a: {foo: string
473+
)", "Expected '}' (to close '{' at line 2), got <eof>");
510474

511-
CHECK_EQ(
512-
getParseError(R"(
513-
local a: { [string]: number, [number]: string }
514-
)"),
515-
"Cannot have more than one table indexer"
516-
);
475+
matchParseError(R"(
476+
local a: { [string]: number, [number]: string }
477+
)", "Cannot have more than one table indexer");
517478

518-
CHECK_EQ(
519-
getParseError(R"(
520-
type T = <a>foo
521-
)"),
522-
"Expected '(' when parsing function parameters, got 'foo'"
523-
);
479+
matchParseError(R"(
480+
type T = <a>foo
481+
)", "Expected '(' when parsing function parameters, got 'foo'");
524482
}
525483

526484
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")
548506

549507
TEST_CASE_FIXTURE(Fixture, "type_alias_error_messages")
550508
{
551-
CHECK_EQ(getParseError("type 5 = number"), "Expected identifier when parsing type name, got '5'");
552-
CHECK_EQ(getParseError("type A"), "Expected '=' when parsing type alias, got <eof>");
553-
CHECK_EQ(getParseError("type A<"), "Expected identifier, got <eof>");
554-
CHECK_EQ(getParseError("type A<B"), "Expected '>' (to close '<' at column 7), got <eof>");
509+
matchParseError("type 5 = number", "Expected identifier when parsing type name, got '5'");
510+
matchParseError("type A", "Expected '=' when parsing type alias, got <eof>");
511+
matchParseError("type A<", "Expected identifier, got <eof>");
512+
matchParseError("type A<B", "Expected '>' (to close '<' at column 7), got <eof>");
555513
}
556514

557515
TEST_CASE_FIXTURE(Fixture, "type_assertion_expression")
@@ -655,12 +613,9 @@ TEST_CASE_FIXTURE(Fixture, "vertical_space")
655613

656614
TEST_CASE_FIXTURE(Fixture, "parse_error_type_name")
657615
{
658-
CHECK_EQ(
659-
getParseError(R"(
660-
local a: Foo.=
661-
)"),
662-
"Expected identifier when parsing field name, got '='"
663-
);
616+
matchParseError(R"(
617+
local a: Foo.=
618+
)", "Expected identifier when parsing field name, got '='");
664619
}
665620

666621
TEST_CASE_FIXTURE(Fixture, "parse_numbers_decimal")
@@ -706,28 +661,25 @@ TEST_CASE_FIXTURE(Fixture, "parse_numbers_binary")
706661

707662
TEST_CASE_FIXTURE(Fixture, "parse_numbers_error")
708663
{
709-
CHECK_EQ(getParseError("return 0b123"), "Malformed number");
710-
CHECK_EQ(getParseError("return 123x"), "Malformed number");
711-
CHECK_EQ(getParseError("return 0xg"), "Malformed number");
712-
CHECK_EQ(getParseError("return 0x0x123"), "Malformed number");
713-
CHECK_EQ(getParseError("return 0xffffffffffffffffffffllllllg"), "Malformed number");
714-
CHECK_EQ(getParseError("return 0x0xffffffffffffffffffffffffffff"), "Malformed number");
664+
matchParseError("return 0b123", "Malformed number");
665+
matchParseError("return 123x", "Malformed number");
666+
matchParseError("return 0xg", "Malformed number");
667+
matchParseError("return 0x0x123", "Malformed number");
668+
matchParseError("return 0xffffffffffffffffffffllllllg", "Malformed number");
669+
matchParseError("return 0x0xffffffffffffffffffffffffffff", "Malformed number");
715670
}
716671

717672
TEST_CASE_FIXTURE(Fixture, "break_return_not_last_error")
718673
{
719-
CHECK_EQ(getParseError("return 0 print(5)"), "Expected <eof>, got 'print'");
720-
CHECK_EQ(getParseError("while true do break print(5) end"), "Expected 'end' (to close 'do' at column 12), got 'print'");
674+
matchParseError("return 0 print(5)", "Expected <eof>, got 'print'");
675+
matchParseError("while true do break print(5) end", "Expected 'end' (to close 'do' at column 12), got 'print'");
721676
}
722677

723678
TEST_CASE_FIXTURE(Fixture, "error_on_unicode")
724679
{
725-
CHECK_EQ(
726-
getParseError(R"(
680+
matchParseError(R"(
727681
local ☃ = 10
728-
)"),
729-
"Expected identifier when parsing variable name, got Unicode character U+2603"
730-
);
682+
)", "Expected identifier when parsing variable name, got Unicode character U+2603");
731683
}
732684

733685
TEST_CASE_FIXTURE(Fixture, "allow_unicode_in_string")
@@ -738,20 +690,17 @@ TEST_CASE_FIXTURE(Fixture, "allow_unicode_in_string")
738690

739691
TEST_CASE_FIXTURE(Fixture, "error_on_confusable")
740692
{
741-
CHECK_EQ(
742-
getParseError(R"(
743-
local pi = 3․13
744-
)"),
745-
"Expected identifier when parsing expression, got Unicode character U+2024 (did you mean '.'?)"
746-
);
693+
matchParseError(R"(
694+
local pi = 3․13
695+
)", "Expected identifier when parsing expression, got Unicode character U+2024 (did you mean '.'?)");
747696
}
748697

749698
TEST_CASE_FIXTURE(Fixture, "error_on_non_utf8_sequence")
750699
{
751700
const char* expected = "Expected identifier when parsing expression, got invalid UTF-8 sequence";
752701

753-
CHECK_EQ(getParseError("local pi = \xFF!"), expected);
754-
CHECK_EQ(getParseError("local pi = \xE2!"), expected);
702+
matchParseError("local pi = \xFF!", expected);
703+
matchParseError("local pi = \xE2!", expected);
755704
}
756705

757706
TEST_CASE_FIXTURE(Fixture, "lex_broken_unicode")
@@ -819,7 +768,7 @@ TEST_CASE_FIXTURE(Fixture, "parse_continue")
819768

820769
TEST_CASE_FIXTURE(Fixture, "continue_not_last_error")
821770
{
822-
CHECK_EQ(getParseError("while true do continue print(5) end"), "Expected 'end' (to close 'do' at column 12), got 'print'");
771+
matchParseError("while true do continue print(5) end", "Expected 'end' (to close 'do' at column 12), got 'print'");
823772
}
824773

825774
TEST_CASE_FIXTURE(Fixture, "parse_export_type")
@@ -862,7 +811,7 @@ TEST_CASE_FIXTURE(Fixture, "export_is_an_identifier_only_when_followed_by_type")
862811

863812
TEST_CASE_FIXTURE(Fixture, "incomplete_statement_error")
864813
{
865-
CHECK_EQ(getParseError("fiddlesticks"), "Incomplete statement: expected assignment or a function call");
814+
matchParseError("fiddlesticks", "Incomplete statement: expected assignment or a function call");
866815
}
867816

868817
TEST_CASE_FIXTURE(Fixture, "parse_compound_assignment")

0 commit comments

Comments
 (0)