From daf386385d7aa133eacd071b4f4d5730eef3d3ba Mon Sep 17 00:00:00 2001 From: "Matthew \"strager\" Glazar" Date: Mon, 8 Jan 2024 02:02:20 -0500 Subject: [PATCH] refactor(test): replace DIAG_TYPE_2_OFFSETS -> DIAGNOSTIC_ASSERTION_2_SPANS https://github.com/quick-lint/quick-lint-js/issues/1154 --- test/quick-lint-js/diagnostic-assertion.h | 44 ++++++++++++++++++++ test/test-parse-expression-typescript.cpp | 16 +++---- test/test-parse-expression.cpp | 22 +++++----- test/test-parse-typescript-class.cpp | 38 ++++++++--------- test/test-parse-typescript-declare-class.cpp | 16 ++++--- test/test-parse-var.cpp | 24 +++++------ 6 files changed, 100 insertions(+), 60 deletions(-) diff --git a/test/quick-lint-js/diagnostic-assertion.h b/test/quick-lint-js/diagnostic-assertion.h index 74fffdca71..ddb7d61192 100644 --- a/test/quick-lint-js/diagnostic-assertion.h +++ b/test/quick-lint-js/diagnostic-assertion.h @@ -157,6 +157,9 @@ struct Diagnostic_Assertion { // Create a Diagnostic_Assertion which matches 'type_'. It asserts that // 'type_::member_' is a Source_Code_Span beginning at 'begin_offset_' and // ending at 'begin_offset_ + span_string_.size()'. +// +// If you need to match two fields of the diagnostic type, see +// DIAGNOSTIC_ASSERTION_2_SPANS. #define DIAGNOSTIC_ASSERTION_SPAN(type_, member_, begin_offset_, span_string_) \ (::quick_lint_js::Diagnostic_Assertion::make_raw( \ Diag_Type::type_, \ @@ -176,6 +179,47 @@ struct Diagnostic_Assertion { }, \ })) +// Create a Diagnostic_Assertion which matches 'type_'. +// +// It asserts that 'type_::member_0_' is a Source_Code_Span beginning at +// 'begin_offset_0_' and ending at 'begin_offset_0_ + span_string_0_.size()'. +// +// It asserts that 'type_::member_1_' is a Source_Code_Span beginning at +// 'begin_offset_1_' and ending at 'begin_offset_1_ + span_string_1_.size()'. +#define DIAGNOSTIC_ASSERTION_2_SPANS(type_, member_0_, begin_offset_0_, \ + span_string_0_, member_1_, \ + begin_offset_1_, span_string_1_) \ + (::quick_lint_js::Diagnostic_Assertion::make_raw( \ + Diag_Type::type_, \ + { \ + ::quick_lint_js::Diagnostic_Assertion::Member{ \ + .name = QLJS_CPP_QUOTE_U8_SV(member_0_), \ + .offset = offsetof(type_, member_0_), \ + .type = Diagnostic_Arg_Type::source_code_span, \ + .span_begin_offset = \ + ::quick_lint_js::narrow_cast( \ + (begin_offset_0_)), \ + .span_end_offset = \ + ::quick_lint_js::narrow_cast( \ + (begin_offset_0_)) + \ + ::quick_lint_js::narrow_cast( \ + (span_string_0_).size()), \ + }, \ + ::quick_lint_js::Diagnostic_Assertion::Member{ \ + .name = QLJS_CPP_QUOTE_U8_SV(member_1_), \ + .offset = offsetof(type_, member_1_), \ + .type = Diagnostic_Arg_Type::source_code_span, \ + .span_begin_offset = \ + ::quick_lint_js::narrow_cast( \ + (begin_offset_1_)), \ + .span_end_offset = \ + ::quick_lint_js::narrow_cast( \ + (begin_offset_1_)) + \ + ::quick_lint_js::narrow_cast( \ + (span_string_1_).size()), \ + }, \ + })) + // See [_diag-syntax]. // // Exits the program at run-time if the specification is malformed. diff --git a/test/test-parse-expression-typescript.cpp b/test/test-parse-expression-typescript.cpp index 94897bfa77..10eba684e3 100644 --- a/test/test-parse-expression-typescript.cpp +++ b/test/test-parse-expression-typescript.cpp @@ -417,14 +417,14 @@ TEST_F(Test_Parse_Expression_TypeScript, SCOPED_TRACE(code); Test_Parser p(code.string_view(), typescript_options, capture_diags); p.parse_and_visit_expression(); - EXPECT_THAT(p.legacy_errors(), - ElementsAreArray({ - DIAG_TYPE_2_OFFSETS( - p.code, - Diag_TypeScript_As_Const_With_Non_Literal_Typeable, // - expression, 0, expression, // - as_const, expression.size() + 1, u8"as const"_sv), - })); + assert_diagnostics( + p.code, p.errors, + { + DIAGNOSTIC_ASSERTION_2_SPANS( + Diag_TypeScript_As_Const_With_Non_Literal_Typeable, // + expression, 0, expression, // + as_const, expression.size() + 1, u8"as const"_sv), + }); } test_parse_and_visit_expression( diff --git a/test/test-parse-expression.cpp b/test/test-parse-expression.cpp index 8517a47128..132b03a20f 100644 --- a/test/test-parse-expression.cpp +++ b/test/test-parse-expression.cpp @@ -3548,15 +3548,14 @@ TEST_F(Test_Parse_Expression, unary_cannot_mix_with_star_star) { SCOPED_TRACE(p.code); Expression* ast = p.parse_expression(); EXPECT_EQ(summarize(ast), "binary(unary(var a), var b)"); - EXPECT_THAT( - p.legacy_errors(), - ElementsAreArray({ - DIAG_TYPE_2_OFFSETS( - p.code, + assert_diagnostics( + p.code, p.errors, + { + DIAGNOSTIC_ASSERTION_2_SPANS( Diag_Missing_Parentheses_Around_Unary_Lhs_Of_Exponent, // unary_expression, 0, op + u8"a"s, // exponent_operator, (op + u8"a "s).size(), u8"**"_sv), - })); + }); } for (String8_View op : {u8"delete"s, u8"typeof"s, u8"void"s}) { @@ -3567,15 +3566,14 @@ TEST_F(Test_Parse_Expression, unary_cannot_mix_with_star_star) { // TODO(strager): Rewrite the AST into something like the following: EXPECT_EQ(summarize(ast), "typeof(binary(var a, var b))"); } - EXPECT_THAT( - p.legacy_errors(), - ElementsAreArray({ - DIAG_TYPE_2_OFFSETS( - p.code, + assert_diagnostics( + p.code, p.errors, + { + DIAGNOSTIC_ASSERTION_2_SPANS( Diag_Missing_Parentheses_Around_Exponent_With_Unary_Lhs, // exponent_expression, concat(op, u8" "s).size(), u8"a ** b"_sv, unary_operator, 0, op), - })); + }); } } diff --git a/test/test-parse-typescript-class.cpp b/test/test-parse-typescript-class.cpp index 63f232254a..355374193e 100644 --- a/test/test-parse-typescript-class.cpp +++ b/test/test-parse-typescript-class.cpp @@ -984,16 +984,16 @@ TEST_F(Test_Parse_TypeScript_Class, Test_Parser p(code.string_view(), typescript_options, capture_diags); p.parse_and_visit_statement(); - EXPECT_THAT( - p.legacy_errors(), - ElementsAreArray({ - DIAG_TYPE_2_OFFSETS( - p.code, Diag_Access_Specifier_Must_Precede_Other_Modifiers, + assert_diagnostics( + p.code, p.errors, + { + DIAGNOSTIC_ASSERTION_2_SPANS( + Diag_Access_Specifier_Must_Precede_Other_Modifiers, second_modifier, concat(u8"class C { "_sv, other_modifier, u8" "_sv).size(), access_specifier, // first_modifier, u8"class C { "_sv.size(), other_modifier), - })); + }); } } @@ -1005,16 +1005,16 @@ TEST_F(Test_Parse_TypeScript_Class, Test_Parser p(code.string_view(), typescript_options, capture_diags); p.parse_and_visit_statement(); - EXPECT_THAT( - p.legacy_errors(), - ElementsAreArray({ - DIAG_TYPE_2_OFFSETS( - p.code, Diag_Access_Specifier_Must_Precede_Other_Modifiers, + assert_diagnostics( + p.code, p.errors, + { + DIAGNOSTIC_ASSERTION_2_SPANS( + Diag_Access_Specifier_Must_Precede_Other_Modifiers, // second_modifier, concat(u8"class C { "_sv, other_modifier, u8" "_sv).size(), access_specifier, // first_modifier, u8"class C { "_sv.size(), other_modifier), - })); + }); } } } @@ -1860,17 +1860,17 @@ TEST_F(Test_Parse_TypeScript_Class, parameter_property_in_constructor) { SCOPED_TRACE(p.code); p.parse_and_visit_module(); - EXPECT_THAT( - p.legacy_errors(), - ElementsAreArray({ - DIAG_TYPE_2_OFFSETS( - p.code, Diag_Access_Specifier_Must_Precede_Other_Modifiers, + assert_diagnostics( + p.code, p.errors, + { + DIAGNOSTIC_ASSERTION_2_SPANS( + Diag_Access_Specifier_Must_Precede_Other_Modifiers, // second_modifier, u8"class C {\n constructor(readonly "_sv.size(), access_specifier, // first_modifier, u8"class C {\n constructor("_sv.size(), - u8"readonly"), - })); + u8"readonly"_sv), + }); } } diff --git a/test/test-parse-typescript-declare-class.cpp b/test/test-parse-typescript-declare-class.cpp index b2e043b874..7c90ebb2e3 100644 --- a/test/test-parse-typescript-declare-class.cpp +++ b/test/test-parse-typescript-declare-class.cpp @@ -549,19 +549,17 @@ TEST_F(Test_Parse_TypeScript_Declare_Class, EXPECT_THAT(p.variable_declarations, ElementsAreArray( {func_param_decl(u8"field"_sv), class_decl(u8"C"_sv)})); - EXPECT_THAT( - p.legacy_errors(), - ElementsAreArray({ - DIAG_TYPE_2_OFFSETS( - p.code, + // only keyword should report a diagnostic; 'readonly' should not have its + // own diagnostic. + assert_diagnostics( + p.code, p.errors, + { + DIAGNOSTIC_ASSERTION_2_SPANS( Diag_TypeScript_Parameter_Property_Not_Allowed_In_Declare_Class, // property_keyword, u8"declare class C {\n constructor("_sv.size(), keyword, // declare_keyword, 0, u8"declare"_sv), - })) - << "only '" << out_string8(keyword) - << "' should report a diagnostic; 'readonly' should not have its own " - "diagnostic"; + }); } } } diff --git a/test/test-parse-var.cpp b/test/test-parse-var.cpp index b5ebadf04f..c697c74a21 100644 --- a/test/test-parse-var.cpp +++ b/test/test-parse-var.cpp @@ -518,15 +518,15 @@ TEST_F(Test_Parse_Var, parse_invalid_let) { EXPECT_THAT(p.variable_declarations, ElementsAreArray( {let_init_decl(u8"x"_sv), let_noinit_decl(u8"z"_sv)})); - EXPECT_THAT( - p.legacy_errors(), - ElementsAreArray({ - DIAG_TYPE_2_OFFSETS( - p.code, Diag_Cannot_Update_Variable_During_Declaration, // + assert_diagnostics( + p.code, p.errors, + { + DIAGNOSTIC_ASSERTION_2_SPANS( + Diag_Cannot_Update_Variable_During_Declaration, // updating_operator, u8"let x "_sv.size(), compound_assignment_operator, // declaring_token, 0, u8"let"_sv), - })); + }); } { @@ -544,15 +544,15 @@ TEST_F(Test_Parse_Var, parse_invalid_let) { EXPECT_THAT(p.variable_declarations, ElementsAreArray( {const_init_decl(u8"x"_sv), const_init_decl(u8"y"_sv)})); - EXPECT_THAT( - p.legacy_errors(), - ElementsAreArray({ - DIAG_TYPE_2_OFFSETS( - p.code, Diag_Cannot_Update_Variable_During_Declaration, // + assert_diagnostics( + p.code, p.errors, + { + DIAGNOSTIC_ASSERTION_2_SPANS( + Diag_Cannot_Update_Variable_During_Declaration, // updating_operator, u8"const [x, y] "_sv.size(), compound_assignment_operator, // declaring_token, 0, u8"const"_sv), - })); + }); } }