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 170cb30
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 66 deletions.
57 changes: 29 additions & 28 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
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
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
42 changes: 24 additions & 18 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 @@ -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 170cb30

Please sign in to comment.