Skip to content

Commit 3ecf5b6

Browse files
committed
refactor(test): prefer assert_diagnostics over EXPECT_THAT
1 parent f8550e6 commit 3ecf5b6

File tree

5 files changed

+93
-89
lines changed

5 files changed

+93
-89
lines changed

test/test-parse-expression.cpp

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,10 +1461,10 @@ TEST_F(Test_Parse_Expression, parse_invalid_assignment) {
14611461
Test_Parser p(code, capture_diags);
14621462
p.parse_expression();
14631463

1464-
EXPECT_THAT(p.errors,
1465-
ElementsAreArray({
1466-
DIAG_TYPE(Diag_Invalid_Expression_Left_Of_Assignment),
1467-
}));
1464+
assert_diagnostics(p.code, p.errors,
1465+
{
1466+
u8"Diag_Invalid_Expression_Left_Of_Assignment"_diag,
1467+
});
14681468
}
14691469

14701470
for (String8_View code : {
@@ -1474,10 +1474,10 @@ TEST_F(Test_Parse_Expression, parse_invalid_assignment) {
14741474
Test_Parser p(code, typescript_options, capture_diags);
14751475
p.parse_expression();
14761476

1477-
EXPECT_THAT(p.errors,
1478-
ElementsAreArray({
1479-
DIAG_TYPE(Diag_Invalid_Expression_Left_Of_Assignment),
1480-
}));
1477+
assert_diagnostics(p.code, p.errors,
1478+
{
1479+
u8"Diag_Invalid_Expression_Left_Of_Assignment"_diag,
1480+
});
14811481
}
14821482
}
14831483

@@ -1716,40 +1716,40 @@ TEST_F(Test_Parse_Expression, untagged_template_with_invalid_escape) {
17161716
Test_Parser p(u8R"(`invalid\uescape`)"_sv, capture_diags);
17171717
Expression* ast = p.parse_expression();
17181718
EXPECT_EQ(summarize(ast), "literal");
1719-
EXPECT_THAT(p.errors,
1720-
ElementsAreArray({
1721-
DIAG_TYPE(Diag_Expected_Hex_Digits_In_Unicode_Escape),
1722-
}));
1719+
assert_diagnostics(p.code, p.errors,
1720+
{
1721+
u8"Diag_Expected_Hex_Digits_In_Unicode_Escape"_diag,
1722+
});
17231723
}
17241724

17251725
{
17261726
Test_Parser p(u8R"(`invalid\u${expr}escape`)"_sv, capture_diags);
17271727
Expression* ast = p.parse_expression();
17281728
EXPECT_EQ(summarize(ast), "template(var expr)");
1729-
EXPECT_THAT(p.errors,
1730-
ElementsAreArray({
1731-
DIAG_TYPE(Diag_Expected_Hex_Digits_In_Unicode_Escape),
1732-
}));
1729+
assert_diagnostics(p.code, p.errors,
1730+
{
1731+
u8"Diag_Expected_Hex_Digits_In_Unicode_Escape"_diag,
1732+
});
17331733
}
17341734

17351735
{
17361736
Test_Parser p(u8R"(`invalid${expr}\uescape`)"_sv, capture_diags);
17371737
Expression* ast = p.parse_expression();
17381738
EXPECT_EQ(summarize(ast), "template(var expr)");
1739-
EXPECT_THAT(p.errors,
1740-
ElementsAreArray({
1741-
DIAG_TYPE(Diag_Expected_Hex_Digits_In_Unicode_Escape),
1742-
}));
1739+
assert_diagnostics(p.code, p.errors,
1740+
{
1741+
u8"Diag_Expected_Hex_Digits_In_Unicode_Escape"_diag,
1742+
});
17431743
}
17441744

17451745
{
17461746
Test_Parser p(u8R"(`invalid${expr1}\u${expr2}escape`)"_sv, capture_diags);
17471747
Expression* ast = p.parse_expression();
17481748
EXPECT_EQ(summarize(ast), "template(var expr1, var expr2)");
1749-
EXPECT_THAT(p.errors,
1750-
ElementsAreArray({
1751-
DIAG_TYPE(Diag_Expected_Hex_Digits_In_Unicode_Escape),
1752-
}));
1749+
assert_diagnostics(p.code, p.errors,
1750+
{
1751+
u8"Diag_Expected_Hex_Digits_In_Unicode_Escape"_diag,
1752+
});
17531753
}
17541754
}
17551755

@@ -3519,10 +3519,11 @@ TEST_F(Test_Parse_Expression,
35193519
capture_diags);
35203520
Expression* ast = p.parse_expression();
35213521
EXPECT_EQ(summarize(ast), "object(literal: function)");
3522-
EXPECT_THAT(p.errors,
3523-
ElementsAreArray({
3524-
DIAG_TYPE(Diag_Methods_Should_Not_Use_Function_Keyword),
3525-
}));
3522+
assert_diagnostics(
3523+
p.code, p.errors,
3524+
{
3525+
u8"Diag_Methods_Should_Not_Use_Function_Keyword"_diag,
3526+
});
35263527
}
35273528
}
35283529
}
@@ -3587,8 +3588,10 @@ TEST_F(Test_Parse_Expression, whitespace_between_bang_and_equal) {
35873588
TEST_F(Test_Parse_Expression, Diag_Spread_Must_Precede_Expression) {
35883589
Test_Parser p(u8"a = ...;"_sv, capture_diags);
35893590
p.parse_and_visit_expression();
3590-
EXPECT_THAT(p.errors, ElementsAreArray(
3591-
{DIAG_TYPE(Diag_Spread_Must_Precede_Expression)}));
3591+
assert_diagnostics(p.code, p.errors,
3592+
{
3593+
u8"Diag_Spread_Must_Precede_Expression"_diag,
3594+
});
35923595
}
35933596

35943597
TEST_F(Test_Parse_Expression, precedence) {

test/test-parse-function.cpp

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,9 +1026,10 @@ TEST_F(Test_Parse_Function, arrow_function_with_invalid_parameters) {
10261026
SCOPED_TRACE(p.code);
10271027
auto guard = p.enter_function(Function_Attributes::async_generator);
10281028
p.parse_and_visit_statement();
1029-
EXPECT_THAT(p.errors, ElementsAreArray({
1030-
DIAG_TYPE(Diag_Invalid_Parameter),
1031-
}));
1029+
assert_diagnostics(p.code, p.errors,
1030+
{
1031+
u8"Diag_Invalid_Parameter"_diag,
1032+
});
10321033
}
10331034

10341035
{
@@ -1041,16 +1042,12 @@ TEST_F(Test_Parse_Function, arrow_function_with_invalid_parameters) {
10411042
Test_Parser p(u8"([(x,)] => {});"_sv, capture_diags);
10421043
auto guard = p.enter_function(Function_Attributes::generator);
10431044
p.parse_and_visit_statement();
1044-
EXPECT_THAT(
1045-
p.errors,
1046-
ElementsAreArray({
1047-
DIAG_TYPE_OFFSETS( //
1048-
p.code, Diag_Unexpected_Function_Parameter_Is_Parenthesized, //
1049-
left_paren_to_right_paren, u8"(["_sv.size(), u8"(x,)"_sv), //
1050-
DIAG_TYPE_OFFSETS( //
1051-
p.code, Diag_Stray_Comma_In_Parameter, //
1052-
comma, u8"([(x"_sv.size(), u8","_sv), //
1053-
}));
1045+
assert_diagnostics(
1046+
p.code, p.errors,
1047+
{
1048+
u8" ^ Diag_Stray_Comma_In_Parameter"_diag, //
1049+
u8" ^^^^ Diag_Unexpected_Function_Parameter_Is_Parenthesized"_diag,
1050+
});
10541051
EXPECT_THAT(p.visits, ElementsAreArray({
10551052
"visit_enter_function_scope", //
10561053
"visit_variable_declaration", // x
@@ -1063,10 +1060,11 @@ TEST_F(Test_Parse_Function, arrow_function_with_invalid_parameters) {
10631060
Test_Parser p(u8"((yield) => {});"_sv, capture_diags);
10641061
auto guard = p.enter_function(Function_Attributes::generator);
10651062
p.parse_and_visit_statement();
1066-
EXPECT_THAT(p.errors,
1067-
ElementsAreArray({
1068-
DIAG_TYPE(Diag_Cannot_Declare_Yield_In_Generator_Function),
1069-
}));
1063+
assert_diagnostics(
1064+
p.code, p.errors,
1065+
{
1066+
u8"Diag_Cannot_Declare_Yield_In_Generator_Function"_diag,
1067+
});
10701068
}
10711069

10721070
{

test/test-parse-typescript-function.cpp

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,14 +1306,12 @@ TEST_F(Test_Parse_TypeScript_Function,
13061306
EXPECT_THAT(
13071307
p.variable_declarations,
13081308
ElementsAreArray({function_decl(u8"f"_sv), function_decl(u8"g"_sv)}));
1309-
EXPECT_THAT(
1310-
p.errors,
1311-
ElementsAreArray({
1312-
DIAG_TYPE_OFFSETS(p.code, Diag_Missing_Function_Body, expected_body,
1313-
u8"function f()"_sv.size(), u8""_sv),
1314-
}))
1315-
<< "missing function body is more likely, so don't report "
1316-
"Diag_TypeScript_Function_Overload_Signature_Must_Have_Same_Name";
1309+
// Missing function body is more likely, so don't report
1310+
// Diag_TypeScript_Function_Overload_Signature_Must_Have_Same_Name.
1311+
assert_diagnostics(p.code, p.errors,
1312+
{
1313+
u8" ` Diag_Missing_Function_Body"_diag,
1314+
});
13171315
}
13181316

13191317
{
@@ -1342,14 +1340,12 @@ TEST_F(Test_Parse_TypeScript_Function,
13421340
EXPECT_THAT(p.variable_uses,
13431341
ElementsAreArray({u8"async", u8"await", u8"myPromise"}))
13441342
<< "'async' should be a variable reference, not a keyword";
1345-
EXPECT_THAT(
1346-
p.errors,
1347-
ElementsAreArray({
1348-
DIAG_TYPE_OFFSETS(p.code, Diag_Missing_Function_Body, expected_body,
1349-
u8"function f()"_sv.size(), u8""_sv),
1350-
}))
1351-
<< "missing function body is more likely, so don't report "
1352-
"Diag_TypeScript_Function_Overload_Signature_Must_Have_Same_Name";
1343+
// Missing function body is more likely, so don't report
1344+
// Diag_TypeScript_Function_Overload_Signature_Must_Have_Same_Name.
1345+
assert_diagnostics(p.code, p.errors,
1346+
{
1347+
u8" ` Diag_Missing_Function_Body"_diag,
1348+
});
13531349
}
13541350
}
13551351

test/test-parse-typescript-generic.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -574,9 +574,10 @@ TEST_F(Test_Parse_TypeScript_Generic,
574574
Expression* ast = p.parse_expression();
575575
// FIXME(#557): Precedence is incorrect.
576576
EXPECT_EQ(summarize(ast), "new(binary(var Foo, var T, missing))");
577-
EXPECT_THAT(p.errors, ElementsAreArray({
578-
DIAG_TYPE(Diag_Missing_Operand_For_Operator),
579-
}));
577+
assert_diagnostics(p.code, p.errors,
578+
{
579+
u8"Diag_Missing_Operand_For_Operator"_diag,
580+
});
580581
}
581582

582583
{

test/test-parse.cpp

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -640,9 +640,10 @@ TEST_F(Test_Parse, unimplemented_token_returns_to_innermost_handler) {
640640
});
641641
EXPECT_TRUE(outer_ok);
642642
EXPECT_TRUE(inner_catch_returned);
643-
EXPECT_THAT(v.errors, ElementsAreArray({
644-
DIAG_TYPE(Diag_Unexpected_Token),
645-
}));
643+
assert_diagnostics(&code, v.errors,
644+
{
645+
u8"Diag_Unexpected_Token"_diag,
646+
});
646647
}
647648
}
648649

@@ -663,9 +664,10 @@ TEST_F(Test_Parse,
663664
});
664665
EXPECT_FALSE(outer_ok);
665666
EXPECT_TRUE(inner_catch_returned);
666-
EXPECT_THAT(v.errors, ElementsAreArray({
667-
DIAG_TYPE(Diag_Unexpected_Token),
668-
}));
667+
assert_diagnostics(&code, v.errors,
668+
{
669+
u8"Diag_Unexpected_Token"_diag,
670+
});
669671
}
670672
}
671673

@@ -706,11 +708,11 @@ TEST_F(Test_Parse, unimplemented_token_is_reported_on_outer_diag_reporter) {
706708
EXPECT_THAT(v.errors, IsEmpty())
707709
<< "Diag_Unexpected_Token should be buffered in the transaction";
708710
p.commit_transaction(std::move(transaction));
709-
EXPECT_THAT(v.errors, ElementsAreArray({
710-
DIAG_TYPE(Diag_Unexpected_Token),
711-
}))
712-
<< "Diag_Unexpected_Token should be reported when committing the "
713-
"transaction";
711+
// Diag_Unexpected_Token should be reported when committing the transaction.
712+
assert_diagnostics(&code, v.errors,
713+
{
714+
u8"Diag_Unexpected_Token"_diag,
715+
});
714716
}
715717
}
716718

@@ -880,9 +882,10 @@ TEST_F(Test_Overflow, parser_depth_limit_exceeded) {
880882
Parser p(&code, &v, javascript_options);
881883
bool ok = p.parse_and_visit_module_catching_fatal_parse_errors(v);
882884
EXPECT_FALSE(ok);
883-
EXPECT_THAT(v.errors, ElementsAreArray({
884-
DIAG_TYPE(Diag_Depth_Limit_Exceeded),
885-
}));
885+
assert_diagnostics(&code, v.errors,
886+
{
887+
u8"Diag_Depth_Limit_Exceeded"_diag,
888+
});
886889
}
887890

888891
{
@@ -903,9 +906,10 @@ TEST_F(Test_Overflow, parser_depth_limit_exceeded) {
903906
capture_diags);
904907
bool ok = p.parse_and_visit_module_catching_fatal_parse_errors();
905908
EXPECT_FALSE(ok);
906-
EXPECT_THAT(p.errors, ElementsAreArray({
907-
DIAG_TYPE(Diag_Depth_Limit_Exceeded),
908-
}));
909+
assert_diagnostics(p.code, p.errors,
910+
{
911+
u8"Diag_Depth_Limit_Exceeded"_diag,
912+
});
909913
}
910914

911915
for (const String8& jsx : {
@@ -926,9 +930,10 @@ TEST_F(Test_Overflow, parser_depth_limit_exceeded) {
926930
Parser p(&code, &v, jsx_options);
927931
bool ok = p.parse_and_visit_module_catching_fatal_parse_errors(v);
928932
EXPECT_FALSE(ok);
929-
EXPECT_THAT(v.errors, ElementsAreArray({
930-
DIAG_TYPE(Diag_Depth_Limit_Exceeded),
931-
}));
933+
assert_diagnostics(&code, v.errors,
934+
{
935+
u8"Diag_Depth_Limit_Exceeded"_diag,
936+
});
932937
}
933938

934939
for (const String8& type : {
@@ -940,9 +945,10 @@ TEST_F(Test_Overflow, parser_depth_limit_exceeded) {
940945
Parser p(&code, &v, typescript_options);
941946
bool ok = p.parse_and_visit_module_catching_fatal_parse_errors(v);
942947
EXPECT_FALSE(ok);
943-
EXPECT_THAT(v.errors, ElementsAreArray({
944-
DIAG_TYPE(Diag_Depth_Limit_Exceeded),
945-
}));
948+
assert_diagnostics(&code, v.errors,
949+
{
950+
u8"Diag_Depth_Limit_Exceeded"_diag,
951+
});
946952
}
947953
}
948954
}

0 commit comments

Comments
 (0)