@@ -283,6 +283,19 @@ TEST(Test_Variable_Analyzer_Type, type_use_does_not_see_non_type_variables) {
283
283
typescript_analyze_options, default_globals);
284
284
}
285
285
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
+
286
299
TEST (Test_Variable_Analyzer_Type,
287
300
interfaces_are_ignored_in_runtime_expressions) {
288
301
static const Char8 outer_declaration[] = u8" I" ;
@@ -299,6 +312,8 @@ TEST(Test_Variable_Analyzer_Type,
299
312
delete_expression.data () + 7 , delete_expression.data () + 8 );
300
313
ASSERT_EQ (deleted_variable_span.string_view (), u8" I" _sv);
301
314
315
+ Monotonic_Allocator memory (" test" );
316
+
302
317
struct Variable_Visit_Kind {
303
318
const char * description;
304
319
void (*visit)(Variable_Analyzer&);
@@ -310,11 +325,11 @@ TEST(Test_Variable_Analyzer_Type,
310
325
// If no run-time variable exists with the same name as the interface,
311
326
// 'runtime_var_kind' is nullopt.
312
327
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,
314
329
Source_Location caller);
315
330
316
331
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,
318
333
Source_Location caller = Source_Location::current()) {
319
334
return this ->check_diagnostics_impl (diags, runtime_var_kind, caller);
320
335
}
@@ -329,30 +344,30 @@ TEST(Test_Variable_Analyzer_Type,
329
344
Variable_Assignment_Flags::none);
330
345
},
331
346
.check_diagnostics_impl =
332
- [](Diag_Collector & diags,
347
+ [](const Diag_List & diags,
333
348
std::optional<Variable_Kind> runtime_var_kind,
334
349
Source_Location caller) -> void {
335
350
if (runtime_var_kind.has_value ()) {
336
351
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) ));
345
360
} else {
346
- EXPECT_THAT_AT_CALLER (diags.errors , IsEmpty ());
361
+ EXPECT_TRUE_AT_CALLER (diags.empty ());
347
362
}
348
363
} else {
349
364
// 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) ));
356
371
}
357
372
},
358
373
},
@@ -365,20 +380,19 @@ TEST(Test_Variable_Analyzer_Type,
365
380
delete_keyword_span);
366
381
},
367
382
.check_diagnostics_impl =
368
- [](Diag_Collector & diags,
383
+ [](const Diag_List & diags,
369
384
std::optional<Variable_Kind> runtime_var_kind,
370
385
Source_Location caller) -> void {
371
386
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)));
380
394
} else {
381
- EXPECT_THAT_AT_CALLER (diags.errors , IsEmpty ());
395
+ EXPECT_TRUE_AT_CALLER (diags.empty ());
382
396
}
383
397
},
384
398
},
@@ -390,19 +404,17 @@ TEST(Test_Variable_Analyzer_Type,
390
404
l.visit_variable_use (identifier_of (use));
391
405
},
392
406
.check_diagnostics_impl =
393
- [](Diag_Collector & diags,
407
+ [](const Diag_List & diags,
394
408
std::optional<Variable_Kind> runtime_var_kind,
395
409
Source_Location caller) -> void {
396
410
if (runtime_var_kind.has_value ()) {
397
- EXPECT_THAT_AT_CALLER (diags.errors , IsEmpty ());
411
+ EXPECT_TRUE_AT_CALLER (diags.empty ());
398
412
} else {
399
413
// 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)));
406
418
}
407
419
},
408
420
},
@@ -414,23 +426,23 @@ TEST(Test_Variable_Analyzer_Type,
414
426
{
415
427
// interface I {}
416
428
// I; // ERROR
417
- Diag_Collector v ;
429
+ Diag_List_Diag_Reporter v (&memory) ;
418
430
Variable_Analyzer l (&v, &default_globals, javascript_var_options);
419
431
l.visit_variable_declaration (identifier_of (declaration),
420
432
Variable_Kind::_interface,
421
433
Variable_Declaration_Flags::none);
422
434
visit_kind.visit (l);
423
435
l.visit_end_of_module ();
424
436
425
- visit_kind.check_diagnostics (v, std::nullopt);
437
+ visit_kind.check_diagnostics (v. diags () , std::nullopt);
426
438
}
427
439
428
440
{
429
441
// interface I {}
430
442
// {
431
443
// I; // ERROR
432
444
// }
433
- Diag_Collector v ;
445
+ Diag_List_Diag_Reporter v (&memory) ;
434
446
Variable_Analyzer l (&v, &default_globals, javascript_var_options);
435
447
l.visit_variable_declaration (identifier_of (declaration),
436
448
Variable_Kind::_interface,
@@ -440,7 +452,7 @@ TEST(Test_Variable_Analyzer_Type,
440
452
l.visit_exit_block_scope ();
441
453
l.visit_end_of_module ();
442
454
443
- visit_kind.check_diagnostics (v, std::nullopt);
455
+ visit_kind.check_diagnostics (v. diags () , std::nullopt);
444
456
}
445
457
446
458
{
@@ -450,7 +462,7 @@ TEST(Test_Variable_Analyzer_Type,
450
462
// I; // ERROR
451
463
// });
452
464
// });
453
- Diag_Collector v ;
465
+ Diag_List_Diag_Reporter v (&memory) ;
454
466
Variable_Analyzer l (&v, &default_globals, javascript_var_options);
455
467
l.visit_variable_declaration (identifier_of (declaration),
456
468
Variable_Kind::_interface,
@@ -464,7 +476,7 @@ TEST(Test_Variable_Analyzer_Type,
464
476
l.visit_exit_function_scope ();
465
477
l.visit_end_of_module ();
466
478
467
- visit_kind.check_diagnostics (v, std::nullopt);
479
+ visit_kind.check_diagnostics (v. diags () , std::nullopt);
468
480
}
469
481
470
482
for (Variable_Kind outer_kind : {
@@ -485,7 +497,7 @@ TEST(Test_Variable_Analyzer_Type,
485
497
// interface I {}
486
498
// I;
487
499
// }
488
- Diag_Collector v ;
500
+ Diag_List_Diag_Reporter v (&memory) ;
489
501
Variable_Analyzer l (&v, &default_globals, javascript_var_options);
490
502
l.visit_variable_declaration (identifier_of (outer_declaration),
491
503
outer_kind,
@@ -498,7 +510,7 @@ TEST(Test_Variable_Analyzer_Type,
498
510
l.visit_exit_block_scope ();
499
511
l.visit_end_of_module ();
500
512
501
- visit_kind.check_diagnostics (v, outer_kind);
513
+ visit_kind.check_diagnostics (v. diags () , outer_kind);
502
514
}
503
515
504
516
{
@@ -507,7 +519,7 @@ TEST(Test_Variable_Analyzer_Type,
507
519
// {
508
520
// I;
509
521
// }
510
- Diag_Collector v ;
522
+ Diag_List_Diag_Reporter v (&memory) ;
511
523
Variable_Analyzer l (&v, &default_globals, javascript_var_options);
512
524
l.visit_variable_declaration (identifier_of (outer_declaration),
513
525
outer_kind,
@@ -520,14 +532,14 @@ TEST(Test_Variable_Analyzer_Type,
520
532
l.visit_exit_block_scope ();
521
533
l.visit_end_of_module ();
522
534
523
- visit_kind.check_diagnostics (v, outer_kind);
535
+ visit_kind.check_diagnostics (v. diags () , outer_kind);
524
536
}
525
537
526
538
{
527
539
// let I;
528
540
// interface I {}
529
541
// I;
530
- Diag_Collector v ;
542
+ Diag_List_Diag_Reporter v (&memory) ;
531
543
Variable_Analyzer l (&v, &default_globals, javascript_var_options);
532
544
l.visit_variable_declaration (identifier_of (outer_declaration),
533
545
outer_kind,
@@ -538,14 +550,14 @@ TEST(Test_Variable_Analyzer_Type,
538
550
visit_kind.visit (l);
539
551
l.visit_end_of_module ();
540
552
541
- visit_kind.check_diagnostics (v, outer_kind);
553
+ visit_kind.check_diagnostics (v. diags () , outer_kind);
542
554
}
543
555
544
556
{
545
557
// interface I {}
546
558
// let I;
547
559
// I;
548
- Diag_Collector v ;
560
+ Diag_List_Diag_Reporter v (&memory) ;
549
561
Variable_Analyzer l (&v, &default_globals, javascript_var_options);
550
562
l.visit_variable_declaration (identifier_of (declaration),
551
563
Variable_Kind::_interface,
@@ -556,7 +568,7 @@ TEST(Test_Variable_Analyzer_Type,
556
568
visit_kind.visit (l);
557
569
l.visit_end_of_module ();
558
570
559
- visit_kind.check_diagnostics (v, outer_kind);
571
+ visit_kind.check_diagnostics (v. diags () , outer_kind);
560
572
}
561
573
562
574
{
@@ -565,7 +577,7 @@ TEST(Test_Variable_Analyzer_Type,
565
577
// });
566
578
// interface I {}
567
579
// let I;
568
- Diag_Collector v ;
580
+ Diag_List_Diag_Reporter v (&memory) ;
569
581
Variable_Analyzer l (&v, &default_globals, javascript_var_options);
570
582
l.visit_enter_function_scope ();
571
583
l.visit_enter_function_scope_body ();
@@ -579,7 +591,7 @@ TEST(Test_Variable_Analyzer_Type,
579
591
Variable_Declaration_Flags::none);
580
592
l.visit_end_of_module ();
581
593
582
- visit_kind.check_diagnostics (v, outer_kind);
594
+ visit_kind.check_diagnostics (v. diags () , outer_kind);
583
595
}
584
596
}
585
597
}
0 commit comments