From d7f865abaa41497d7abc00dd6c5742896376f874 Mon Sep 17 00:00:00 2001 From: "Matthew \"strager\" Glazar" Date: Sun, 7 Jan 2024 12:35:15 -0500 Subject: [PATCH] refactor(test): avoid Diag_Collector in more tests https://github.com/quick-lint/quick-lint-js/issues/1154 --- test/quick-lint-js/diagnostic-assertion.h | 14 ++++ test/test-variable-analyzer-globals.cpp | 80 ++++++++++++----------- test/test-variable-analyzer-type.cpp | 13 ---- 3 files changed, 55 insertions(+), 52 deletions(-) diff --git a/test/quick-lint-js/diagnostic-assertion.h b/test/quick-lint-js/diagnostic-assertion.h index d66b19536e..74fffdca71 100644 --- a/test/quick-lint-js/diagnostic-assertion.h +++ b/test/quick-lint-js/diagnostic-assertion.h @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -209,6 +210,19 @@ ::testing::Matcher diagnostics_matcher_2( ::testing::Matcher diagnostics_matcher_2( Padded_String_View code, std::initializer_list assertions); + +template +Diag* get_only_diagnostic(const Diag_List& diags, Diag_Type type) { + Diag* diag = nullptr; + int found_count = 0; + diags.for_each([&](Diag_Type current_type, void* raw_diag) -> void { + if (current_type == type) { + ++found_count; + diag = static_cast(raw_diag); + } + }); + return found_count == 1 ? diag : nullptr; +} } // quick-lint-js finds bugs in JavaScript programs. diff --git a/test/test-variable-analyzer-globals.cpp b/test/test-variable-analyzer-globals.cpp index 18eef8a722..da9e7928fa 100644 --- a/test/test-variable-analyzer-globals.cpp +++ b/test/test-variable-analyzer-globals.cpp @@ -190,7 +190,12 @@ constexpr const Char8 *nodejs_global_variables[] = { u8"unescape", }; -TEST(Test_Variable_Analyzer_Globals, global_variables_are_usable) { +class Test_Variable_Analyzer_Globals : public ::testing::Test { + protected: + Monotonic_Allocator memory_{"test"}; +}; + +TEST_F(Test_Variable_Analyzer_Globals, global_variables_are_usable) { // Array = null; // Array; for (const Char8 *global_variable : writable_global_variables) { @@ -215,23 +220,22 @@ TEST(Test_Variable_Analyzer_Globals, global_variables_are_usable) { } } -TEST(Test_Variable_Analyzer_Globals, - immutable_global_variables_are_not_assignable) { +TEST_F(Test_Variable_Analyzer_Globals, + immutable_global_variables_are_not_assignable) { for (const Char8 *global_variable : non_writable_global_variables) { SCOPED_TRACE(out_string8(global_variable)); // NaN = null; // ERROR - Diag_Collector v; - Variable_Analyzer l(&v, &default_globals, javascript_var_options); + Diag_List_Diag_Reporter diags(&this->memory_); + Variable_Analyzer l(&diags, &default_globals, javascript_var_options); l.visit_variable_assignment(identifier_of(global_variable), Variable_Assignment_Flags::none); l.visit_end_of_module(); - EXPECT_THAT(v.errors, - ElementsAreArray({ - DIAG_TYPE_SPAN(Diag_Assignment_To_Const_Global_Variable, - assignment, span_of(global_variable)), - })); + auto *diag = get_only_diagnostic( + diags.diags(), Diag_Type::Diag_Assignment_To_Const_Global_Variable); + ASSERT_NE(diag, nullptr); + EXPECT_TRUE(same_pointers(diag->assignment, span_of(global_variable))); } for (const Char8 *global_variable : non_writable_global_variables) { @@ -240,8 +244,8 @@ TEST(Test_Variable_Analyzer_Globals, // (() => { // NaN = null; // ERROR // }); - Diag_Collector v; - Variable_Analyzer l(&v, &default_globals, javascript_var_options); + Diag_List_Diag_Reporter diags(&this->memory_); + Variable_Analyzer l(&diags, &default_globals, javascript_var_options); l.visit_enter_function_scope(); l.visit_enter_function_scope_body(); l.visit_variable_assignment(identifier_of(global_variable), @@ -249,15 +253,14 @@ TEST(Test_Variable_Analyzer_Globals, l.visit_exit_function_scope(); l.visit_end_of_module(); - EXPECT_THAT(v.errors, - ElementsAreArray({ - DIAG_TYPE_SPAN(Diag_Assignment_To_Const_Global_Variable, - assignment, span_of(global_variable)), - })); + auto *diag = get_only_diagnostic( + diags.diags(), Diag_Type::Diag_Assignment_To_Const_Global_Variable); + ASSERT_NE(diag, nullptr); + EXPECT_TRUE(same_pointers(diag->assignment, span_of(global_variable))); } } -TEST(Test_Variable_Analyzer_Globals, nodejs_global_variables_are_usable) { +TEST_F(Test_Variable_Analyzer_Globals, nodejs_global_variables_are_usable) { for (const Char8 *global_variable : nodejs_global_variables) { SCOPED_TRACE(out_string8(global_variable)); Diag_Collector v; @@ -268,8 +271,8 @@ TEST(Test_Variable_Analyzer_Globals, nodejs_global_variables_are_usable) { } } -TEST(Test_Variable_Analyzer_Globals, - non_module_nodejs_global_variables_are_shadowable) { +TEST_F(Test_Variable_Analyzer_Globals, + non_module_nodejs_global_variables_are_shadowable) { for (Variable_Declaration_Flags flags : {Variable_Declaration_Flags::none, Variable_Declaration_Flags::initialized_with_equals}) { @@ -286,41 +289,40 @@ TEST(Test_Variable_Analyzer_Globals, } } -TEST(Test_Variable_Analyzer_Globals, - type_only_global_variables_are_not_usable_in_expressions) { +TEST_F(Test_Variable_Analyzer_Globals, + type_only_global_variables_are_not_usable_in_expressions) { for (const Char8 *global_variable : type_only_global_variables) { SCOPED_TRACE(out_string8(global_variable)); // Awaited; { - Diag_Collector v; - Variable_Analyzer l(&v, &default_globals, typescript_var_options); + Diag_List_Diag_Reporter diags(&this->memory_); + Variable_Analyzer l(&diags, &default_globals, typescript_var_options); l.visit_variable_use(identifier_of(global_variable)); l.visit_end_of_module(); - EXPECT_THAT(v.errors, ElementsAreArray({ - DIAG_TYPE_SPAN(Diag_Use_Of_Undeclared_Variable, - name, span_of(global_variable)), - })); + auto *diag = get_only_diagnostic( + diags.diags(), Diag_Type::Diag_Use_Of_Undeclared_Variable); + ASSERT_NE(diag, nullptr); + EXPECT_TRUE(same_pointers(diag->name, span_of(global_variable))); } // Awaited = null; { - Diag_Collector v; - Variable_Analyzer l(&v, &default_globals, typescript_var_options); + Diag_List_Diag_Reporter diags(&this->memory_); + Variable_Analyzer l(&diags, &default_globals, typescript_var_options); l.visit_variable_assignment(identifier_of(global_variable), Variable_Assignment_Flags::none); l.visit_end_of_module(); - EXPECT_THAT(v.errors, - ElementsAreArray({ - DIAG_TYPE_SPAN(Diag_Assignment_To_Undeclared_Variable, - assignment, span_of(global_variable)), - })); + auto *diag = get_only_diagnostic( + diags.diags(), Diag_Type::Diag_Assignment_To_Undeclared_Variable); + ASSERT_NE(diag, nullptr); + EXPECT_TRUE(same_pointers(diag->assignment, span_of(global_variable))); } } } -TEST(Test_Variable_Analyzer_Globals, - type_only_global_variables_are_usable_in_types) { +TEST_F(Test_Variable_Analyzer_Globals, + type_only_global_variables_are_usable_in_types) { // null as Awaited; for (const Char8 *global_variable : type_only_global_variables) { SCOPED_TRACE(out_string8(global_variable)); @@ -332,8 +334,8 @@ TEST(Test_Variable_Analyzer_Globals, } } -TEST(Test_Variable_Analyzer_Globals, - any_variable_is_declarable_and_usable_if_opted_into) { +TEST_F(Test_Variable_Analyzer_Globals, + any_variable_is_declarable_and_usable_if_opted_into) { // This tests the "literally-anything" global group. Global_Declared_Variable_Set globals; diff --git a/test/test-variable-analyzer-type.cpp b/test/test-variable-analyzer-type.cpp index bab3dac628..945a12d7f4 100644 --- a/test/test-variable-analyzer-type.cpp +++ b/test/test-variable-analyzer-type.cpp @@ -283,19 +283,6 @@ TEST(Test_Variable_Analyzer_Type, type_use_does_not_see_non_type_variables) { typescript_analyze_options, default_globals); } -template -Diag* get_only_diagnostic(const Diag_List& diags, Diag_Type type) { - Diag* diag = nullptr; - int found_count = 0; - diags.for_each([&](Diag_Type current_type, void* raw_diag) -> void { - if (current_type == type) { - ++found_count; - diag = static_cast(raw_diag); - } - }); - return found_count == 1 ? diag : nullptr; -} - TEST(Test_Variable_Analyzer_Type, interfaces_are_ignored_in_runtime_expressions) { static const Char8 outer_declaration[] = u8"I";