Skip to content

Commit 504148f

Browse files
committed
refactor(test): avoid Diag_Collector in Variable_Analyzer test
#1154
1 parent 17003d3 commit 504148f

File tree

2 files changed

+84
-54
lines changed

2 files changed

+84
-54
lines changed

test/quick-lint-js/gtest.h

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,35 @@ std::string get_matcher_message(::testing::Matcher<const Value &> matcher,
5454
::testing::internal::MakePredicateFormatterFromMatcher(matcher), value, \
5555
ADD_FAILURE_AT_CALLER)
5656

57+
// Like ASSERT_EQ, but using the 'caller' variable for source locations.
58+
#define ASSERT_NE_AT_CALLER(lhs, rhs) \
59+
GTEST_PRED_FORMAT2_(::testing::internal::CmpHelperNE, lhs, rhs, \
60+
GTEST_FAIL_AT_CALLER)
61+
5762
// Like EXPECT_EQ, but using the 'caller' variable for source locations.
5863
#define EXPECT_EQ_AT_CALLER(lhs, rhs) \
5964
GTEST_PRED_FORMAT2_(::testing::internal::EqHelper::Compare, lhs, rhs, \
6065
ADD_FAILURE_AT_CALLER)
6166

62-
#define ADD_FAILURE_AT_CALLER(message) \
67+
// Like EXPECT_TRUE, but using the 'caller' variable for source locations.
68+
#define EXPECT_TRUE_AT_CALLER(value) EXPECT_EQ_AT_CALLER(value, true)
69+
70+
// Like ADD_FAILURE, but using the 'caller' variable for source locations.
71+
#define ADD_FAILURE_AT_CALLER(message) \
72+
ADD_FAILURE_OR_FAIL_AT_CALLER(message, \
73+
::testing::TestPartResult::kNonFatalFailure)
74+
75+
// Like GTEST_FAIL, but using the 'caller' variable for source locations.
76+
#define GTEST_FAIL_AT_CALLER(message) \
77+
ADD_FAILURE_OR_FAIL_AT_CALLER(message, \
78+
::testing::TestPartResult::kFatalFailure)
79+
80+
#define ADD_FAILURE_OR_FAIL_AT_CALLER(message, failure_kind) \
6381
GTEST_MESSAGE_AT_( \
6482
(caller.valid() ? caller.file_name() : __FILE__), \
6583
(caller.valid() ? ::quick_lint_js::narrow_cast<int>(caller.line()) \
6684
: __LINE__), \
67-
message, ::testing::TestPartResult::kNonFatalFailure)
85+
message, failure_kind)
6886
}
6987

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

test/test-variable-analyzer-type.cpp

Lines changed: 64 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,19 @@ 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+
286299
TEST(Test_Variable_Analyzer_Type,
287300
interfaces_are_ignored_in_runtime_expressions) {
288301
static const Char8 outer_declaration[] = u8"I";
@@ -299,6 +312,8 @@ TEST(Test_Variable_Analyzer_Type,
299312
delete_expression.data() + 7, delete_expression.data() + 8);
300313
ASSERT_EQ(deleted_variable_span.string_view(), u8"I"_sv);
301314

315+
Monotonic_Allocator memory("test");
316+
302317
struct Variable_Visit_Kind {
303318
const char* description;
304319
void (*visit)(Variable_Analyzer&);
@@ -310,11 +325,11 @@ TEST(Test_Variable_Analyzer_Type,
310325
// If no run-time variable exists with the same name as the interface,
311326
// 'runtime_var_kind' is nullopt.
312327
void (*check_diagnostics_impl)(
313-
Diag_Collector& diags, std::optional<Variable_Kind> runtime_var_kind,
328+
const Diag_List& diags, std::optional<Variable_Kind> runtime_var_kind,
314329
Source_Location caller);
315330

316331
void check_diagnostics(
317-
Diag_Collector& diags, std::optional<Variable_Kind> runtime_var_kind,
332+
const Diag_List& diags, std::optional<Variable_Kind> runtime_var_kind,
318333
Source_Location caller = Source_Location::current()) {
319334
return this->check_diagnostics_impl(diags, runtime_var_kind, caller);
320335
}
@@ -329,30 +344,30 @@ TEST(Test_Variable_Analyzer_Type,
329344
Variable_Assignment_Flags::none);
330345
},
331346
.check_diagnostics_impl =
332-
[](Diag_Collector& diags,
347+
[](const Diag_List& diags,
333348
std::optional<Variable_Kind> runtime_var_kind,
334349
Source_Location caller) -> void {
335350
if (runtime_var_kind.has_value()) {
336351
if (*runtime_var_kind == Variable_Kind::_const) {
337-
EXPECT_THAT_AT_CALLER(
338-
diags.errors,
339-
ElementsAreArray({
340-
DIAG_TYPE_2_SPANS(Diag_Assignment_To_Const_Variable, //
341-
assignment, span_of(assignment), //
342-
declaration,
343-
span_of(outer_declaration)),
344-
}));
352+
auto* diag =
353+
get_only_diagnostic<Diag_Assignment_To_Const_Variable>(
354+
diags, Diag_Type::Diag_Assignment_To_Const_Variable);
355+
ASSERT_NE_AT_CALLER(diag, nullptr);
356+
EXPECT_TRUE_AT_CALLER(
357+
same_pointers(diag->assignment, span_of(assignment)));
358+
EXPECT_TRUE_AT_CALLER(same_pointers(
359+
diag->declaration, span_of(outer_declaration)));
345360
} else {
346-
EXPECT_THAT_AT_CALLER(diags.errors, IsEmpty());
361+
EXPECT_TRUE_AT_CALLER(diags.empty());
347362
}
348363
} else {
349364
// TODO(strager): Report a more helpful diagnostic.
350-
EXPECT_THAT_AT_CALLER(
351-
diags.errors,
352-
ElementsAreArray({
353-
DIAG_TYPE_SPAN(Diag_Assignment_To_Undeclared_Variable,
354-
assignment, span_of(assignment)),
355-
}));
365+
auto* diag =
366+
get_only_diagnostic<Diag_Assignment_To_Undeclared_Variable>(
367+
diags, Diag_Type::Diag_Assignment_To_Undeclared_Variable);
368+
ASSERT_NE_AT_CALLER(diag, nullptr);
369+
EXPECT_TRUE_AT_CALLER(
370+
same_pointers(diag->assignment, span_of(assignment)));
356371
}
357372
},
358373
},
@@ -365,20 +380,19 @@ TEST(Test_Variable_Analyzer_Type,
365380
delete_keyword_span);
366381
},
367382
.check_diagnostics_impl =
368-
[](Diag_Collector& diags,
383+
[](const Diag_List& diags,
369384
std::optional<Variable_Kind> runtime_var_kind,
370385
Source_Location caller) -> void {
371386
if (runtime_var_kind.has_value()) {
372-
EXPECT_THAT_AT_CALLER(
373-
diags.errors,
374-
ElementsAreArray({
375-
DIAG_TYPE_OFFSETS(
376-
&delete_expression,
377-
Diag_Redundant_Delete_Statement_On_Variable, //
378-
delete_expression, 0, u8"delete I"_sv),
379-
}));
387+
auto* diag = get_only_diagnostic<
388+
Diag_Redundant_Delete_Statement_On_Variable>(
389+
diags,
390+
Diag_Type::Diag_Redundant_Delete_Statement_On_Variable);
391+
ASSERT_NE_AT_CALLER(diag, nullptr);
392+
EXPECT_TRUE_AT_CALLER(same_pointers(diag->delete_expression,
393+
span_of(delete_expression)));
380394
} else {
381-
EXPECT_THAT_AT_CALLER(diags.errors, IsEmpty());
395+
EXPECT_TRUE_AT_CALLER(diags.empty());
382396
}
383397
},
384398
},
@@ -390,19 +404,17 @@ TEST(Test_Variable_Analyzer_Type,
390404
l.visit_variable_use(identifier_of(use));
391405
},
392406
.check_diagnostics_impl =
393-
[](Diag_Collector& diags,
407+
[](const Diag_List& diags,
394408
std::optional<Variable_Kind> runtime_var_kind,
395409
Source_Location caller) -> void {
396410
if (runtime_var_kind.has_value()) {
397-
EXPECT_THAT_AT_CALLER(diags.errors, IsEmpty());
411+
EXPECT_TRUE_AT_CALLER(diags.empty());
398412
} else {
399413
// TODO(strager): Report a more helpful diagnostic.
400-
EXPECT_THAT_AT_CALLER(
401-
diags.errors,
402-
ElementsAreArray({
403-
DIAG_TYPE_SPAN(Diag_Use_Of_Undeclared_Variable, name,
404-
span_of(use)),
405-
}));
414+
auto* diag = get_only_diagnostic<Diag_Use_Of_Undeclared_Variable>(
415+
diags, Diag_Type::Diag_Use_Of_Undeclared_Variable);
416+
ASSERT_NE_AT_CALLER(diag, nullptr);
417+
EXPECT_TRUE_AT_CALLER(same_pointers(diag->name, span_of(use)));
406418
}
407419
},
408420
},
@@ -414,23 +426,23 @@ TEST(Test_Variable_Analyzer_Type,
414426
{
415427
// interface I {}
416428
// I; // ERROR
417-
Diag_Collector v;
429+
Diag_List_Diag_Reporter v(&memory);
418430
Variable_Analyzer l(&v, &default_globals, javascript_var_options);
419431
l.visit_variable_declaration(identifier_of(declaration),
420432
Variable_Kind::_interface,
421433
Variable_Declaration_Flags::none);
422434
visit_kind.visit(l);
423435
l.visit_end_of_module();
424436

425-
visit_kind.check_diagnostics(v, std::nullopt);
437+
visit_kind.check_diagnostics(v.diags(), std::nullopt);
426438
}
427439

428440
{
429441
// interface I {}
430442
// {
431443
// I; // ERROR
432444
// }
433-
Diag_Collector v;
445+
Diag_List_Diag_Reporter v(&memory);
434446
Variable_Analyzer l(&v, &default_globals, javascript_var_options);
435447
l.visit_variable_declaration(identifier_of(declaration),
436448
Variable_Kind::_interface,
@@ -440,7 +452,7 @@ TEST(Test_Variable_Analyzer_Type,
440452
l.visit_exit_block_scope();
441453
l.visit_end_of_module();
442454

443-
visit_kind.check_diagnostics(v, std::nullopt);
455+
visit_kind.check_diagnostics(v.diags(), std::nullopt);
444456
}
445457

446458
{
@@ -450,7 +462,7 @@ TEST(Test_Variable_Analyzer_Type,
450462
// I; // ERROR
451463
// });
452464
// });
453-
Diag_Collector v;
465+
Diag_List_Diag_Reporter v(&memory);
454466
Variable_Analyzer l(&v, &default_globals, javascript_var_options);
455467
l.visit_variable_declaration(identifier_of(declaration),
456468
Variable_Kind::_interface,
@@ -464,7 +476,7 @@ TEST(Test_Variable_Analyzer_Type,
464476
l.visit_exit_function_scope();
465477
l.visit_end_of_module();
466478

467-
visit_kind.check_diagnostics(v, std::nullopt);
479+
visit_kind.check_diagnostics(v.diags(), std::nullopt);
468480
}
469481

470482
for (Variable_Kind outer_kind : {
@@ -485,7 +497,7 @@ TEST(Test_Variable_Analyzer_Type,
485497
// interface I {}
486498
// I;
487499
// }
488-
Diag_Collector v;
500+
Diag_List_Diag_Reporter v(&memory);
489501
Variable_Analyzer l(&v, &default_globals, javascript_var_options);
490502
l.visit_variable_declaration(identifier_of(outer_declaration),
491503
outer_kind,
@@ -498,7 +510,7 @@ TEST(Test_Variable_Analyzer_Type,
498510
l.visit_exit_block_scope();
499511
l.visit_end_of_module();
500512

501-
visit_kind.check_diagnostics(v, outer_kind);
513+
visit_kind.check_diagnostics(v.diags(), outer_kind);
502514
}
503515

504516
{
@@ -507,7 +519,7 @@ TEST(Test_Variable_Analyzer_Type,
507519
// {
508520
// I;
509521
// }
510-
Diag_Collector v;
522+
Diag_List_Diag_Reporter v(&memory);
511523
Variable_Analyzer l(&v, &default_globals, javascript_var_options);
512524
l.visit_variable_declaration(identifier_of(outer_declaration),
513525
outer_kind,
@@ -520,14 +532,14 @@ TEST(Test_Variable_Analyzer_Type,
520532
l.visit_exit_block_scope();
521533
l.visit_end_of_module();
522534

523-
visit_kind.check_diagnostics(v, outer_kind);
535+
visit_kind.check_diagnostics(v.diags(), outer_kind);
524536
}
525537

526538
{
527539
// let I;
528540
// interface I {}
529541
// I;
530-
Diag_Collector v;
542+
Diag_List_Diag_Reporter v(&memory);
531543
Variable_Analyzer l(&v, &default_globals, javascript_var_options);
532544
l.visit_variable_declaration(identifier_of(outer_declaration),
533545
outer_kind,
@@ -538,14 +550,14 @@ TEST(Test_Variable_Analyzer_Type,
538550
visit_kind.visit(l);
539551
l.visit_end_of_module();
540552

541-
visit_kind.check_diagnostics(v, outer_kind);
553+
visit_kind.check_diagnostics(v.diags(), outer_kind);
542554
}
543555

544556
{
545557
// interface I {}
546558
// let I;
547559
// I;
548-
Diag_Collector v;
560+
Diag_List_Diag_Reporter v(&memory);
549561
Variable_Analyzer l(&v, &default_globals, javascript_var_options);
550562
l.visit_variable_declaration(identifier_of(declaration),
551563
Variable_Kind::_interface,
@@ -556,7 +568,7 @@ TEST(Test_Variable_Analyzer_Type,
556568
visit_kind.visit(l);
557569
l.visit_end_of_module();
558570

559-
visit_kind.check_diagnostics(v, outer_kind);
571+
visit_kind.check_diagnostics(v.diags(), outer_kind);
560572
}
561573

562574
{
@@ -565,7 +577,7 @@ TEST(Test_Variable_Analyzer_Type,
565577
// });
566578
// interface I {}
567579
// let I;
568-
Diag_Collector v;
580+
Diag_List_Diag_Reporter v(&memory);
569581
Variable_Analyzer l(&v, &default_globals, javascript_var_options);
570582
l.visit_enter_function_scope();
571583
l.visit_enter_function_scope_body();
@@ -579,7 +591,7 @@ TEST(Test_Variable_Analyzer_Type,
579591
Variable_Declaration_Flags::none);
580592
l.visit_end_of_module();
581593

582-
visit_kind.check_diagnostics(v, outer_kind);
594+
visit_kind.check_diagnostics(v.diags(), outer_kind);
583595
}
584596
}
585597
}

0 commit comments

Comments
 (0)