Skip to content

Commit

Permalink
refactor(test): prefer assert_diagnostics over EXPECT_THAT
Browse files Browse the repository at this point in the history
  • Loading branch information
strager committed Jul 25, 2023
1 parent f8550e6 commit 3ecf5b6
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 89 deletions.
63 changes: 33 additions & 30 deletions test/test-parse-expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1461,10 +1461,10 @@ TEST_F(Test_Parse_Expression, parse_invalid_assignment) {
Test_Parser p(code, capture_diags);
p.parse_expression();

EXPECT_THAT(p.errors,
ElementsAreArray({
DIAG_TYPE(Diag_Invalid_Expression_Left_Of_Assignment),
}));
assert_diagnostics(p.code, p.errors,
{
u8"Diag_Invalid_Expression_Left_Of_Assignment"_diag,
});
}

for (String8_View code : {
Expand All @@ -1474,10 +1474,10 @@ TEST_F(Test_Parse_Expression, parse_invalid_assignment) {
Test_Parser p(code, typescript_options, capture_diags);
p.parse_expression();

EXPECT_THAT(p.errors,
ElementsAreArray({
DIAG_TYPE(Diag_Invalid_Expression_Left_Of_Assignment),
}));
assert_diagnostics(p.code, p.errors,
{
u8"Diag_Invalid_Expression_Left_Of_Assignment"_diag,
});
}
}

Expand Down Expand Up @@ -1716,40 +1716,40 @@ TEST_F(Test_Parse_Expression, untagged_template_with_invalid_escape) {
Test_Parser p(u8R"(`invalid\uescape`)"_sv, capture_diags);
Expression* ast = p.parse_expression();
EXPECT_EQ(summarize(ast), "literal");
EXPECT_THAT(p.errors,
ElementsAreArray({
DIAG_TYPE(Diag_Expected_Hex_Digits_In_Unicode_Escape),
}));
assert_diagnostics(p.code, p.errors,
{
u8"Diag_Expected_Hex_Digits_In_Unicode_Escape"_diag,
});
}

{
Test_Parser p(u8R"(`invalid\u${expr}escape`)"_sv, capture_diags);
Expression* ast = p.parse_expression();
EXPECT_EQ(summarize(ast), "template(var expr)");
EXPECT_THAT(p.errors,
ElementsAreArray({
DIAG_TYPE(Diag_Expected_Hex_Digits_In_Unicode_Escape),
}));
assert_diagnostics(p.code, p.errors,
{
u8"Diag_Expected_Hex_Digits_In_Unicode_Escape"_diag,
});
}

{
Test_Parser p(u8R"(`invalid${expr}\uescape`)"_sv, capture_diags);
Expression* ast = p.parse_expression();
EXPECT_EQ(summarize(ast), "template(var expr)");
EXPECT_THAT(p.errors,
ElementsAreArray({
DIAG_TYPE(Diag_Expected_Hex_Digits_In_Unicode_Escape),
}));
assert_diagnostics(p.code, p.errors,
{
u8"Diag_Expected_Hex_Digits_In_Unicode_Escape"_diag,
});
}

{
Test_Parser p(u8R"(`invalid${expr1}\u${expr2}escape`)"_sv, capture_diags);
Expression* ast = p.parse_expression();
EXPECT_EQ(summarize(ast), "template(var expr1, var expr2)");
EXPECT_THAT(p.errors,
ElementsAreArray({
DIAG_TYPE(Diag_Expected_Hex_Digits_In_Unicode_Escape),
}));
assert_diagnostics(p.code, p.errors,
{
u8"Diag_Expected_Hex_Digits_In_Unicode_Escape"_diag,
});
}
}

Expand Down Expand Up @@ -3519,10 +3519,11 @@ TEST_F(Test_Parse_Expression,
capture_diags);
Expression* ast = p.parse_expression();
EXPECT_EQ(summarize(ast), "object(literal: function)");
EXPECT_THAT(p.errors,
ElementsAreArray({
DIAG_TYPE(Diag_Methods_Should_Not_Use_Function_Keyword),
}));
assert_diagnostics(
p.code, p.errors,
{
u8"Diag_Methods_Should_Not_Use_Function_Keyword"_diag,
});
}
}
}
Expand Down Expand Up @@ -3587,8 +3588,10 @@ TEST_F(Test_Parse_Expression, whitespace_between_bang_and_equal) {
TEST_F(Test_Parse_Expression, Diag_Spread_Must_Precede_Expression) {
Test_Parser p(u8"a = ...;"_sv, capture_diags);
p.parse_and_visit_expression();
EXPECT_THAT(p.errors, ElementsAreArray(
{DIAG_TYPE(Diag_Spread_Must_Precede_Expression)}));
assert_diagnostics(p.code, p.errors,
{
u8"Diag_Spread_Must_Precede_Expression"_diag,
});
}

TEST_F(Test_Parse_Expression, precedence) {
Expand Down
32 changes: 15 additions & 17 deletions test/test-parse-function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1026,9 +1026,10 @@ TEST_F(Test_Parse_Function, arrow_function_with_invalid_parameters) {
SCOPED_TRACE(p.code);
auto guard = p.enter_function(Function_Attributes::async_generator);
p.parse_and_visit_statement();
EXPECT_THAT(p.errors, ElementsAreArray({
DIAG_TYPE(Diag_Invalid_Parameter),
}));
assert_diagnostics(p.code, p.errors,
{
u8"Diag_Invalid_Parameter"_diag,
});
}

{
Expand All @@ -1041,16 +1042,12 @@ TEST_F(Test_Parse_Function, arrow_function_with_invalid_parameters) {
Test_Parser p(u8"([(x,)] => {});"_sv, capture_diags);
auto guard = p.enter_function(Function_Attributes::generator);
p.parse_and_visit_statement();
EXPECT_THAT(
p.errors,
ElementsAreArray({
DIAG_TYPE_OFFSETS( //
p.code, Diag_Unexpected_Function_Parameter_Is_Parenthesized, //
left_paren_to_right_paren, u8"(["_sv.size(), u8"(x,)"_sv), //
DIAG_TYPE_OFFSETS( //
p.code, Diag_Stray_Comma_In_Parameter, //
comma, u8"([(x"_sv.size(), u8","_sv), //
}));
assert_diagnostics(
p.code, p.errors,
{
u8" ^ Diag_Stray_Comma_In_Parameter"_diag, //
u8" ^^^^ Diag_Unexpected_Function_Parameter_Is_Parenthesized"_diag,
});
EXPECT_THAT(p.visits, ElementsAreArray({
"visit_enter_function_scope", //
"visit_variable_declaration", // x
Expand All @@ -1063,10 +1060,11 @@ TEST_F(Test_Parse_Function, arrow_function_with_invalid_parameters) {
Test_Parser p(u8"((yield) => {});"_sv, capture_diags);
auto guard = p.enter_function(Function_Attributes::generator);
p.parse_and_visit_statement();
EXPECT_THAT(p.errors,
ElementsAreArray({
DIAG_TYPE(Diag_Cannot_Declare_Yield_In_Generator_Function),
}));
assert_diagnostics(
p.code, p.errors,
{
u8"Diag_Cannot_Declare_Yield_In_Generator_Function"_diag,
});
}

{
Expand Down
28 changes: 12 additions & 16 deletions test/test-parse-typescript-function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1306,14 +1306,12 @@ TEST_F(Test_Parse_TypeScript_Function,
EXPECT_THAT(
p.variable_declarations,
ElementsAreArray({function_decl(u8"f"_sv), function_decl(u8"g"_sv)}));
EXPECT_THAT(
p.errors,
ElementsAreArray({
DIAG_TYPE_OFFSETS(p.code, Diag_Missing_Function_Body, expected_body,
u8"function f()"_sv.size(), u8""_sv),
}))
<< "missing function body is more likely, so don't report "
"Diag_TypeScript_Function_Overload_Signature_Must_Have_Same_Name";
// Missing function body is more likely, so don't report
// Diag_TypeScript_Function_Overload_Signature_Must_Have_Same_Name.
assert_diagnostics(p.code, p.errors,
{
u8" ` Diag_Missing_Function_Body"_diag,
});
}

{
Expand Down Expand Up @@ -1342,14 +1340,12 @@ TEST_F(Test_Parse_TypeScript_Function,
EXPECT_THAT(p.variable_uses,
ElementsAreArray({u8"async", u8"await", u8"myPromise"}))
<< "'async' should be a variable reference, not a keyword";
EXPECT_THAT(
p.errors,
ElementsAreArray({
DIAG_TYPE_OFFSETS(p.code, Diag_Missing_Function_Body, expected_body,
u8"function f()"_sv.size(), u8""_sv),
}))
<< "missing function body is more likely, so don't report "
"Diag_TypeScript_Function_Overload_Signature_Must_Have_Same_Name";
// Missing function body is more likely, so don't report
// Diag_TypeScript_Function_Overload_Signature_Must_Have_Same_Name.
assert_diagnostics(p.code, p.errors,
{
u8" ` Diag_Missing_Function_Body"_diag,
});
}
}

Expand Down
7 changes: 4 additions & 3 deletions test/test-parse-typescript-generic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -574,9 +574,10 @@ TEST_F(Test_Parse_TypeScript_Generic,
Expression* ast = p.parse_expression();
// FIXME(#557): Precedence is incorrect.
EXPECT_EQ(summarize(ast), "new(binary(var Foo, var T, missing))");
EXPECT_THAT(p.errors, ElementsAreArray({
DIAG_TYPE(Diag_Missing_Operand_For_Operator),
}));
assert_diagnostics(p.code, p.errors,
{
u8"Diag_Missing_Operand_For_Operator"_diag,
});
}

{
Expand Down
52 changes: 29 additions & 23 deletions test/test-parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -640,9 +640,10 @@ TEST_F(Test_Parse, unimplemented_token_returns_to_innermost_handler) {
});
EXPECT_TRUE(outer_ok);
EXPECT_TRUE(inner_catch_returned);
EXPECT_THAT(v.errors, ElementsAreArray({
DIAG_TYPE(Diag_Unexpected_Token),
}));
assert_diagnostics(&code, v.errors,
{
u8"Diag_Unexpected_Token"_diag,
});
}
}

Expand All @@ -663,9 +664,10 @@ TEST_F(Test_Parse,
});
EXPECT_FALSE(outer_ok);
EXPECT_TRUE(inner_catch_returned);
EXPECT_THAT(v.errors, ElementsAreArray({
DIAG_TYPE(Diag_Unexpected_Token),
}));
assert_diagnostics(&code, v.errors,
{
u8"Diag_Unexpected_Token"_diag,
});
}
}

Expand Down Expand Up @@ -706,11 +708,11 @@ TEST_F(Test_Parse, unimplemented_token_is_reported_on_outer_diag_reporter) {
EXPECT_THAT(v.errors, IsEmpty())
<< "Diag_Unexpected_Token should be buffered in the transaction";
p.commit_transaction(std::move(transaction));
EXPECT_THAT(v.errors, ElementsAreArray({
DIAG_TYPE(Diag_Unexpected_Token),
}))
<< "Diag_Unexpected_Token should be reported when committing the "
"transaction";
// Diag_Unexpected_Token should be reported when committing the transaction.
assert_diagnostics(&code, v.errors,
{
u8"Diag_Unexpected_Token"_diag,
});
}
}

Expand Down Expand Up @@ -880,9 +882,10 @@ TEST_F(Test_Overflow, parser_depth_limit_exceeded) {
Parser p(&code, &v, javascript_options);
bool ok = p.parse_and_visit_module_catching_fatal_parse_errors(v);
EXPECT_FALSE(ok);
EXPECT_THAT(v.errors, ElementsAreArray({
DIAG_TYPE(Diag_Depth_Limit_Exceeded),
}));
assert_diagnostics(&code, v.errors,
{
u8"Diag_Depth_Limit_Exceeded"_diag,
});
}

{
Expand All @@ -903,9 +906,10 @@ TEST_F(Test_Overflow, parser_depth_limit_exceeded) {
capture_diags);
bool ok = p.parse_and_visit_module_catching_fatal_parse_errors();
EXPECT_FALSE(ok);
EXPECT_THAT(p.errors, ElementsAreArray({
DIAG_TYPE(Diag_Depth_Limit_Exceeded),
}));
assert_diagnostics(p.code, p.errors,
{
u8"Diag_Depth_Limit_Exceeded"_diag,
});
}

for (const String8& jsx : {
Expand All @@ -926,9 +930,10 @@ TEST_F(Test_Overflow, parser_depth_limit_exceeded) {
Parser p(&code, &v, jsx_options);
bool ok = p.parse_and_visit_module_catching_fatal_parse_errors(v);
EXPECT_FALSE(ok);
EXPECT_THAT(v.errors, ElementsAreArray({
DIAG_TYPE(Diag_Depth_Limit_Exceeded),
}));
assert_diagnostics(&code, v.errors,
{
u8"Diag_Depth_Limit_Exceeded"_diag,
});
}

for (const String8& type : {
Expand All @@ -940,9 +945,10 @@ TEST_F(Test_Overflow, parser_depth_limit_exceeded) {
Parser p(&code, &v, typescript_options);
bool ok = p.parse_and_visit_module_catching_fatal_parse_errors(v);
EXPECT_FALSE(ok);
EXPECT_THAT(v.errors, ElementsAreArray({
DIAG_TYPE(Diag_Depth_Limit_Exceeded),
}));
assert_diagnostics(&code, v.errors,
{
u8"Diag_Depth_Limit_Exceeded"_diag,
});
}
}
}
Expand Down

0 comments on commit 3ecf5b6

Please sign in to comment.