From 170cb30cdc94f2b6e15a4435f1794168f5585b9a Mon Sep 17 00:00:00 2001 From: "Matthew \"strager\" Glazar" Date: Tue, 25 Jul 2023 16:39:29 -0700 Subject: [PATCH] refactor(test): prefer assert_diagnostics over EXPECT_THAT --- test/test-parse-expression.cpp | 57 +++++++++++++------------- test/test-parse-function.cpp | 32 +++++++-------- test/test-parse-typescript-generic.cpp | 7 ++-- test/test-parse.cpp | 42 +++++++++++-------- 4 files changed, 72 insertions(+), 66 deletions(-) diff --git a/test/test-parse-expression.cpp b/test/test-parse-expression.cpp index 60e1a68817..93cc878837 100644 --- a/test/test-parse-expression.cpp +++ b/test/test-parse-expression.cpp @@ -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 : { @@ -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, + }); } } @@ -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, + }); } } @@ -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, + }); } } } diff --git a/test/test-parse-function.cpp b/test/test-parse-function.cpp index ded85a908f..14e7cf05a1 100644 --- a/test/test-parse-function.cpp +++ b/test/test-parse-function.cpp @@ -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, + }); } { @@ -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 @@ -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, + }); } { diff --git a/test/test-parse-typescript-generic.cpp b/test/test-parse-typescript-generic.cpp index a3ba9697c2..1ba9067c59 100644 --- a/test/test-parse-typescript-generic.cpp +++ b/test/test-parse-typescript-generic.cpp @@ -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, + }); } { diff --git a/test/test-parse.cpp b/test/test-parse.cpp index ad95901777..4e0fcf66c7 100644 --- a/test/test-parse.cpp +++ b/test/test-parse.cpp @@ -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, + }); } } @@ -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, + }); } } @@ -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, + }); } { @@ -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 : { @@ -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 : { @@ -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, + }); } } }