Skip to content

Commit ece8f2f

Browse files
committed
refactor(test): avoid Diag_Collector in more tests
#1154
1 parent 504148f commit ece8f2f

File tree

3 files changed

+55
-52
lines changed

3 files changed

+55
-52
lines changed

test/quick-lint-js/diagnostic-assertion.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <quick-lint-js/container/padded-string.h>
1111
#include <quick-lint-js/container/result.h>
1212
#include <quick-lint-js/diag-collector.h>
13+
#include <quick-lint-js/diag/diag-list.h>
1314
#include <quick-lint-js/diag/diagnostic-types.h>
1415
#include <quick-lint-js/port/char8.h>
1516
#include <quick-lint-js/port/span.h>
@@ -216,6 +217,19 @@ ::testing::Matcher<const Diag_List&> diagnostics_matcher_2(
216217
::testing::Matcher<const Diag_List&> diagnostics_matcher_2(
217218
Padded_String_View code,
218219
std::initializer_list<Diagnostic_Assertion> assertions);
220+
221+
template <class Diag>
222+
Diag* get_only_diagnostic(const Diag_List& diags, Diag_Type type) {
223+
Diag* diag = nullptr;
224+
int found_count = 0;
225+
diags.for_each([&](Diag_Type current_type, void* raw_diag) -> void {
226+
if (current_type == type) {
227+
++found_count;
228+
diag = static_cast<Diag*>(raw_diag);
229+
}
230+
});
231+
return found_count == 1 ? diag : nullptr;
232+
}
219233
}
220234

221235
// quick-lint-js finds bugs in JavaScript programs.

test/test-variable-analyzer-globals.cpp

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,12 @@ constexpr const Char8 *nodejs_global_variables[] = {
190190
u8"unescape",
191191
};
192192

193-
TEST(Test_Variable_Analyzer_Globals, global_variables_are_usable) {
193+
class Test_Variable_Analyzer_Globals : public ::testing::Test {
194+
protected:
195+
Monotonic_Allocator memory_{"test"};
196+
};
197+
198+
TEST_F(Test_Variable_Analyzer_Globals, global_variables_are_usable) {
194199
// Array = null;
195200
// Array;
196201
for (const Char8 *global_variable : writable_global_variables) {
@@ -215,23 +220,22 @@ TEST(Test_Variable_Analyzer_Globals, global_variables_are_usable) {
215220
}
216221
}
217222

218-
TEST(Test_Variable_Analyzer_Globals,
219-
immutable_global_variables_are_not_assignable) {
223+
TEST_F(Test_Variable_Analyzer_Globals,
224+
immutable_global_variables_are_not_assignable) {
220225
for (const Char8 *global_variable : non_writable_global_variables) {
221226
SCOPED_TRACE(out_string8(global_variable));
222227

223228
// NaN = null; // ERROR
224-
Diag_Collector v;
225-
Variable_Analyzer l(&v, &default_globals, javascript_var_options);
229+
Diag_List_Diag_Reporter diags(&this->memory_);
230+
Variable_Analyzer l(&diags, &default_globals, javascript_var_options);
226231
l.visit_variable_assignment(identifier_of(global_variable),
227232
Variable_Assignment_Flags::none);
228233
l.visit_end_of_module();
229234

230-
EXPECT_THAT(v.errors,
231-
ElementsAreArray({
232-
DIAG_TYPE_SPAN(Diag_Assignment_To_Const_Global_Variable,
233-
assignment, span_of(global_variable)),
234-
}));
235+
auto *diag = get_only_diagnostic<Diag_Assignment_To_Const_Global_Variable>(
236+
diags.diags(), Diag_Type::Diag_Assignment_To_Const_Global_Variable);
237+
ASSERT_NE(diag, nullptr);
238+
EXPECT_TRUE(same_pointers(diag->assignment, span_of(global_variable)));
235239
}
236240

237241
for (const Char8 *global_variable : non_writable_global_variables) {
@@ -240,24 +244,23 @@ TEST(Test_Variable_Analyzer_Globals,
240244
// (() => {
241245
// NaN = null; // ERROR
242246
// });
243-
Diag_Collector v;
244-
Variable_Analyzer l(&v, &default_globals, javascript_var_options);
247+
Diag_List_Diag_Reporter diags(&this->memory_);
248+
Variable_Analyzer l(&diags, &default_globals, javascript_var_options);
245249
l.visit_enter_function_scope();
246250
l.visit_enter_function_scope_body();
247251
l.visit_variable_assignment(identifier_of(global_variable),
248252
Variable_Assignment_Flags::none);
249253
l.visit_exit_function_scope();
250254
l.visit_end_of_module();
251255

252-
EXPECT_THAT(v.errors,
253-
ElementsAreArray({
254-
DIAG_TYPE_SPAN(Diag_Assignment_To_Const_Global_Variable,
255-
assignment, span_of(global_variable)),
256-
}));
256+
auto *diag = get_only_diagnostic<Diag_Assignment_To_Const_Global_Variable>(
257+
diags.diags(), Diag_Type::Diag_Assignment_To_Const_Global_Variable);
258+
ASSERT_NE(diag, nullptr);
259+
EXPECT_TRUE(same_pointers(diag->assignment, span_of(global_variable)));
257260
}
258261
}
259262

260-
TEST(Test_Variable_Analyzer_Globals, nodejs_global_variables_are_usable) {
263+
TEST_F(Test_Variable_Analyzer_Globals, nodejs_global_variables_are_usable) {
261264
for (const Char8 *global_variable : nodejs_global_variables) {
262265
SCOPED_TRACE(out_string8(global_variable));
263266
Diag_Collector v;
@@ -268,8 +271,8 @@ TEST(Test_Variable_Analyzer_Globals, nodejs_global_variables_are_usable) {
268271
}
269272
}
270273

271-
TEST(Test_Variable_Analyzer_Globals,
272-
non_module_nodejs_global_variables_are_shadowable) {
274+
TEST_F(Test_Variable_Analyzer_Globals,
275+
non_module_nodejs_global_variables_are_shadowable) {
273276
for (Variable_Declaration_Flags flags :
274277
{Variable_Declaration_Flags::none,
275278
Variable_Declaration_Flags::initialized_with_equals}) {
@@ -286,41 +289,40 @@ TEST(Test_Variable_Analyzer_Globals,
286289
}
287290
}
288291

289-
TEST(Test_Variable_Analyzer_Globals,
290-
type_only_global_variables_are_not_usable_in_expressions) {
292+
TEST_F(Test_Variable_Analyzer_Globals,
293+
type_only_global_variables_are_not_usable_in_expressions) {
291294
for (const Char8 *global_variable : type_only_global_variables) {
292295
SCOPED_TRACE(out_string8(global_variable));
293296

294297
// Awaited;
295298
{
296-
Diag_Collector v;
297-
Variable_Analyzer l(&v, &default_globals, typescript_var_options);
299+
Diag_List_Diag_Reporter diags(&this->memory_);
300+
Variable_Analyzer l(&diags, &default_globals, typescript_var_options);
298301
l.visit_variable_use(identifier_of(global_variable));
299302
l.visit_end_of_module();
300-
EXPECT_THAT(v.errors, ElementsAreArray({
301-
DIAG_TYPE_SPAN(Diag_Use_Of_Undeclared_Variable,
302-
name, span_of(global_variable)),
303-
}));
303+
auto *diag = get_only_diagnostic<Diag_Use_Of_Undeclared_Variable>(
304+
diags.diags(), Diag_Type::Diag_Use_Of_Undeclared_Variable);
305+
ASSERT_NE(diag, nullptr);
306+
EXPECT_TRUE(same_pointers(diag->name, span_of(global_variable)));
304307
}
305308

306309
// Awaited = null;
307310
{
308-
Diag_Collector v;
309-
Variable_Analyzer l(&v, &default_globals, typescript_var_options);
311+
Diag_List_Diag_Reporter diags(&this->memory_);
312+
Variable_Analyzer l(&diags, &default_globals, typescript_var_options);
310313
l.visit_variable_assignment(identifier_of(global_variable),
311314
Variable_Assignment_Flags::none);
312315
l.visit_end_of_module();
313-
EXPECT_THAT(v.errors,
314-
ElementsAreArray({
315-
DIAG_TYPE_SPAN(Diag_Assignment_To_Undeclared_Variable,
316-
assignment, span_of(global_variable)),
317-
}));
316+
auto *diag = get_only_diagnostic<Diag_Assignment_To_Undeclared_Variable>(
317+
diags.diags(), Diag_Type::Diag_Assignment_To_Undeclared_Variable);
318+
ASSERT_NE(diag, nullptr);
319+
EXPECT_TRUE(same_pointers(diag->assignment, span_of(global_variable)));
318320
}
319321
}
320322
}
321323

322-
TEST(Test_Variable_Analyzer_Globals,
323-
type_only_global_variables_are_usable_in_types) {
324+
TEST_F(Test_Variable_Analyzer_Globals,
325+
type_only_global_variables_are_usable_in_types) {
324326
// null as Awaited;
325327
for (const Char8 *global_variable : type_only_global_variables) {
326328
SCOPED_TRACE(out_string8(global_variable));
@@ -332,8 +334,8 @@ TEST(Test_Variable_Analyzer_Globals,
332334
}
333335
}
334336

335-
TEST(Test_Variable_Analyzer_Globals,
336-
any_variable_is_declarable_and_usable_if_opted_into) {
337+
TEST_F(Test_Variable_Analyzer_Globals,
338+
any_variable_is_declarable_and_usable_if_opted_into) {
337339
// This tests the "literally-anything" global group.
338340

339341
Global_Declared_Variable_Set globals;

test/test-variable-analyzer-type.cpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -283,19 +283,6 @@ TEST(Test_Variable_Analyzer_Type, type_use_does_not_see_non_type_variables) {
283283
typescript_analyze_options, default_globals);
284284
}
285285

286-
template <class Diag>
287-
Diag* get_only_diagnostic(const Diag_List& diags, Diag_Type type) {
288-
Diag* diag = nullptr;
289-
int found_count = 0;
290-
diags.for_each([&](Diag_Type current_type, void* raw_diag) -> void {
291-
if (current_type == type) {
292-
++found_count;
293-
diag = static_cast<Diag*>(raw_diag);
294-
}
295-
});
296-
return found_count == 1 ? diag : nullptr;
297-
}
298-
299286
TEST(Test_Variable_Analyzer_Type,
300287
interfaces_are_ignored_in_runtime_expressions) {
301288
static const Char8 outer_declaration[] = u8"I";

0 commit comments

Comments
 (0)