@@ -407,13 +407,33 @@ pub struct Collector {
407
407
pub tests : Vec < testing:: TestDescAndFn > ,
408
408
// to be removed when hoedown will be definitely gone
409
409
pub old_tests : HashMap < String , Vec < String > > ,
410
+
411
+ // The name of the test displayed to the user, separated by `::`.
412
+ //
413
+ // In tests from Rust source, this is the path to the item
414
+ // e.g. `["std", "vec", "Vec", "push"]`.
415
+ //
416
+ // In tests from a markdown file, this is the titles of all headers (h1~h6)
417
+ // of the sections that contain the code block, e.g. if the markdown file is
418
+ // written as:
419
+ //
420
+ // ``````markdown
421
+ // # Title
422
+ //
423
+ // ## Subtitle
424
+ //
425
+ // ```rust
426
+ // assert!(true);
427
+ // ```
428
+ // ``````
429
+ //
430
+ // the `names` vector of that test will be `["Title", "Subtitle"]`.
410
431
names : Vec < String > ,
432
+
411
433
cfgs : Vec < String > ,
412
434
libs : SearchPaths ,
413
435
externs : Externs ,
414
- cnt : usize ,
415
436
use_headers : bool ,
416
- current_header : Option < String > ,
417
437
cratename : String ,
418
438
opts : TestOptions ,
419
439
maybe_sysroot : Option < PathBuf > ,
@@ -436,9 +456,7 @@ impl Collector {
436
456
cfgs,
437
457
libs,
438
458
externs,
439
- cnt : 0 ,
440
459
use_headers,
441
- current_header : None ,
442
460
cratename,
443
461
opts,
444
462
maybe_sysroot,
@@ -450,28 +468,12 @@ impl Collector {
450
468
}
451
469
452
470
fn generate_name ( & self , line : usize , filename : & str ) -> String {
453
- if self . use_headers {
454
- if let Some ( ref header) = self . current_header {
455
- format ! ( "{} - {} (line {})" , filename, header, line)
456
- } else {
457
- format ! ( "{} - (line {})" , filename, line)
458
- }
459
- } else {
460
- format ! ( "{} - {} (line {})" , filename, self . names. join( "::" ) , line)
461
- }
471
+ format ! ( "{} - {} (line {})" , filename, self . names. join( "::" ) , line)
462
472
}
463
473
464
474
// to be removed once hoedown is gone
465
475
fn generate_name_beginning ( & self , filename : & str ) -> String {
466
- if self . use_headers {
467
- if let Some ( ref header) = self . current_header {
468
- format ! ( "{} - {} (line" , filename, header)
469
- } else {
470
- format ! ( "{} - (line" , filename)
471
- }
472
- } else {
473
- format ! ( "{} - {} (line" , filename, self . names. join( "::" ) )
474
- }
476
+ format ! ( "{} - {} (line" , filename, self . names. join( "::" ) )
475
477
}
476
478
477
479
pub fn add_old_test ( & mut self , test : String , filename : String ) {
@@ -580,7 +582,7 @@ impl Collector {
580
582
}
581
583
582
584
pub fn register_header ( & mut self , name : & str , level : u32 ) {
583
- if self . use_headers && level == 1 {
585
+ if self . use_headers {
584
586
// we use these headings as test names, so it's good if
585
587
// they're valid identifiers.
586
588
let name = name. chars ( ) . enumerate ( ) . map ( |( i, c) | {
@@ -592,9 +594,28 @@ impl Collector {
592
594
}
593
595
} ) . collect :: < String > ( ) ;
594
596
595
- // new header => reset count.
596
- self . cnt = 0 ;
597
- self . current_header = Some ( name) ;
597
+ // Here we try to efficiently assemble the header titles into the
598
+ // test name in the form of `h1::h2::h3::h4::h5::h6`.
599
+ //
600
+ // Suppose originally `self.names` contains `[h1, h2, h3]`...
601
+ let level = level as usize ;
602
+ if level <= self . names . len ( ) {
603
+ // ... Consider `level == 2`. All headers in the lower levels
604
+ // are irrelevant in this new level. So we should reset
605
+ // `self.names` to contain headers until <h2>, and replace that
606
+ // slot with the new name: `[h1, name]`.
607
+ self . names . truncate ( level) ;
608
+ self . names [ level - 1 ] = name;
609
+ } else {
610
+ // ... On the other hand, consider `level == 5`. This means we
611
+ // need to extend `self.names` to contain five headers. We fill
612
+ // in the missing level (<h4>) with `_`. Thus `self.names` will
613
+ // become `[h1, h2, h3, "_", name]`.
614
+ if level - 1 > self . names . len ( ) {
615
+ self . names . resize ( level - 1 , "_" . to_owned ( ) ) ;
616
+ }
617
+ self . names . push ( name) ;
618
+ }
598
619
}
599
620
}
600
621
}
@@ -625,7 +646,6 @@ impl<'a, 'hir> HirCollector<'a, 'hir> {
625
646
attrs. collapse_doc_comments ( ) ;
626
647
attrs. unindent_doc_comments ( ) ;
627
648
if let Some ( doc) = attrs. doc_value ( ) {
628
- self . collector . cnt = 0 ;
629
649
if self . collector . render_type == RenderType :: Pulldown {
630
650
markdown:: old_find_testable_code ( doc, self . collector ,
631
651
attrs. span . unwrap_or ( DUMMY_SP ) ) ;
0 commit comments