@@ -438,6 +438,55 @@ pub mod testing {
438
438
use super :: * ;
439
439
use core:: sync:: atomic:: { AtomicU32 , Ordering } ;
440
440
use std:: sync:: Mutex ;
441
+ pub mod endpoint {
442
+ use super :: * ;
443
+ pub struct Subscriber {
444
+ location : Option < Location > ,
445
+ output : Mutex < Vec < String > > ,
446
+ }
447
+ impl Drop for Subscriber {
448
+ fn drop ( & mut self ) {
449
+ if std:: thread:: panicking ( ) {
450
+ return ;
451
+ }
452
+ if let Some ( location) = self . location . as_ref ( ) {
453
+ location. snapshot ( & self . output . lock ( ) . unwrap ( ) ) ;
454
+ }
455
+ }
456
+ }
457
+ impl Subscriber {
458
+ #[ doc = r" Creates a subscriber with snapshot assertions enabled" ]
459
+ #[ track_caller]
460
+ pub fn snapshot ( ) -> Self {
461
+ let mut sub = Self :: no_snapshot ( ) ;
462
+ sub. location = Location :: try_new ( ) ;
463
+ sub
464
+ }
465
+ #[ doc = r" Creates a subscriber with snapshot assertions enabled" ]
466
+ #[ track_caller]
467
+ pub fn named_snapshot < Name : core:: fmt:: Display > ( name : Name ) -> Self {
468
+ let mut sub = Self :: no_snapshot ( ) ;
469
+ sub. location = Some ( Location :: from_name ( name) ) ;
470
+ sub
471
+ }
472
+ #[ doc = r" Creates a subscriber with snapshot assertions disabled" ]
473
+ pub fn no_snapshot ( ) -> Self {
474
+ Self {
475
+ location : None ,
476
+ output : Default :: default ( ) ,
477
+ }
478
+ }
479
+ }
480
+ impl super :: super :: Subscriber for Subscriber {
481
+ type ConnectionContext = ( ) ;
482
+ fn create_connection_context (
483
+ & self ,
484
+ _meta : & api:: ConnectionMeta ,
485
+ _info : & api:: ConnectionInfo ,
486
+ ) -> Self :: ConnectionContext {
487
+ }
488
+ }
489
+ }
441
490
#[ derive( Clone , Debug ) ]
442
491
pub struct Subscriber {
443
492
location : Option < Location > ,
@@ -462,6 +511,13 @@ pub mod testing {
462
511
sub. location = Location :: try_new ( ) ;
463
512
sub
464
513
}
514
+ #[ doc = r" Creates a subscriber with snapshot assertions enabled" ]
515
+ #[ track_caller]
516
+ pub fn named_snapshot < Name : core:: fmt:: Display > ( name : Name ) -> Self {
517
+ let mut sub = Self :: no_snapshot ( ) ;
518
+ sub. location = Some ( Location :: from_name ( name) ) ;
519
+ sub
520
+ }
465
521
#[ doc = r" Creates a subscriber with snapshot assertions disabled" ]
466
522
pub fn no_snapshot ( ) -> Self {
467
523
Self {
@@ -508,6 +564,13 @@ pub mod testing {
508
564
sub. location = Location :: try_new ( ) ;
509
565
sub
510
566
}
567
+ #[ doc = r" Creates a publisher with snapshot assertions enabled" ]
568
+ #[ track_caller]
569
+ pub fn named_snapshot < Name : core:: fmt:: Display > ( name : Name ) -> Self {
570
+ let mut sub = Self :: no_snapshot ( ) ;
571
+ sub. location = Some ( Location :: from_name ( name) ) ;
572
+ sub
573
+ }
511
574
#[ doc = r" Creates a publisher with snapshot assertions disabled" ]
512
575
pub fn no_snapshot ( ) -> Self {
513
576
Self {
@@ -548,26 +611,35 @@ pub mod testing {
548
611
}
549
612
}
550
613
#[ derive( Clone , Debug ) ]
551
- struct Location ( & ' static core:: panic:: Location < ' static > ) ;
614
+ struct Location {
615
+ location : & ' static core:: panic:: Location < ' static > ,
616
+ name : String ,
617
+ }
552
618
impl Location {
553
619
#[ track_caller]
620
+ #[ allow( clippy:: manual_map) ]
554
621
fn try_new ( ) -> Option < Self > {
555
622
let thread = std:: thread:: current ( ) ;
556
- if thread. name ( ) . map_or ( false , |name| name != "main" ) {
557
- Some ( Self ( core :: panic :: Location :: caller ( ) ) )
623
+ if let Some ( name ) = thread. name ( ) . filter ( |name| * name != "main" ) {
624
+ Some ( Self :: from_name ( name ) )
558
625
} else {
559
626
None
560
627
}
561
628
}
629
+ #[ track_caller]
630
+ fn from_name < Name : core:: fmt:: Display > ( name : Name ) -> Self {
631
+ let location = core:: panic:: Location :: caller ( ) ;
632
+ let name = name. to_string ( ) ;
633
+ Self { location, name }
634
+ }
562
635
fn snapshot ( & self , output : & [ String ] ) {
563
636
if cfg ! ( miri) {
564
637
return ;
565
638
}
566
639
use std:: path:: { Component , Path } ;
567
640
let value = output. join ( "\n " ) ;
568
- let thread = std:: thread:: current ( ) ;
569
- let function_name = thread. name ( ) . unwrap ( ) ;
570
- let test_path = Path :: new ( self . 0 . file ( ) . trim_end_matches ( ".rs" ) ) ;
641
+ let name = & self . name ;
642
+ let test_path = Path :: new ( self . location . file ( ) . trim_end_matches ( ".rs" ) ) ;
571
643
let module_path = test_path
572
644
. components ( )
573
645
. filter_map ( |comp| match comp {
@@ -582,10 +654,10 @@ pub mod testing {
582
654
insta:: _macro_support:: AutoName . into ( ) ,
583
655
& value,
584
656
current_dir. to_str ( ) . unwrap ( ) ,
585
- function_name ,
657
+ name ,
586
658
& module_path,
587
- self . 0 . file ( ) ,
588
- self . 0 . line ( ) ,
659
+ self . location . file ( ) ,
660
+ self . location . line ( ) ,
589
661
"" ,
590
662
)
591
663
. unwrap ( )
0 commit comments