@@ -291,11 +291,14 @@ fn test_replace_pattern() {
291
291
assert_eq ! ( data. replace( |c| c == 'γ' , "😺😺😺" ) , "abcdαβ😺😺😺δabcdαβ😺😺😺δ" ) ;
292
292
}
293
293
294
+ // The current implementation of SliceIndex fails to handle methods
295
+ // orthogonally from range types; therefore, it is worth testing
296
+ // all of the indexing operations on each input.
294
297
mod slice_index {
295
298
// Test a slicing operation **that should succeed,**
296
299
// testing it on all of the indexing methods.
297
300
//
298
- // DO NOT use this in `should_panic` tests, unless you are testing the macro itself .
301
+ // This is not suitable for testing failure on invalid inputs .
299
302
macro_rules! assert_range_eq {
300
303
( $s: expr, $range: expr, $expected: expr)
301
304
=> {
@@ -340,7 +343,7 @@ mod slice_index {
340
343
// because if it can't, then what are we even doing here?
341
344
//
342
345
// (Be aware this only demonstrates the ability to detect bugs
343
- // in the FIRST method it calls , as the macro is not designed
346
+ // in the FIRST method that panics , as the macro is not designed
344
347
// to be used in `should_panic`)
345
348
#[ test]
346
349
#[ should_panic( expected = "out of bounds" ) ]
@@ -363,8 +366,8 @@ mod slice_index {
363
366
// and `None` test cases for get/get_mut.
364
367
macro_rules! panic_cases {
365
368
( $(
366
- mod $case_name: ident {
367
- let DATA = $data: expr;
369
+ in mod $case_name: ident {
370
+ data : $data: expr;
368
371
369
372
// optional:
370
373
//
@@ -373,14 +376,11 @@ mod slice_index {
373
376
// straddles the boundary between valid and invalid.
374
377
// (such as the input `len..len`, which is just barely valid)
375
378
$(
376
- let GOOD_INPUT = $good: expr;
377
- let GOOD_OUTPUT = $output: expr;
379
+ good: data[ $good: expr] == $output: expr;
378
380
) *
379
381
380
- let BAD_INPUT = $bad: expr;
381
- const EXPECT_MSG = $expect_msg: expr; // must be a literal
382
-
383
- !!generate_tests!!
382
+ bad: data[ $bad: expr] ;
383
+ message: $expect_msg: expr; // must be a literal
384
384
}
385
385
) * ) => { $(
386
386
mod $case_name {
@@ -509,114 +509,72 @@ mod slice_index {
509
509
}
510
510
511
511
panic_cases ! {
512
- mod rangefrom_len {
513
- let DATA = "abcdef" ;
514
-
515
- let GOOD_INPUT = 6 ..;
516
- let GOOD_OUTPUT = "" ;
517
-
518
- let BAD_INPUT = 7 ..;
519
- const EXPECT_MSG = "out of bounds" ;
520
-
521
- !!generate_tests!!
512
+ in mod rangefrom_len {
513
+ data: "abcdef" ;
514
+ good: data[ 6 ..] == "" ;
515
+ bad: data[ 7 ..] ;
516
+ message: "out of bounds" ;
522
517
}
523
518
524
- mod rangeto_len {
525
- let DATA = "abcdef" ;
526
-
527
- let GOOD_INPUT = ..6 ;
528
- let GOOD_OUTPUT = "abcdef" ;
529
-
530
- let BAD_INPUT = ..7 ;
531
- const EXPECT_MSG = "out of bounds" ;
532
-
533
- !!generate_tests!!
519
+ in mod rangeto_len {
520
+ data: "abcdef" ;
521
+ good: data[ ..6 ] == "abcdef" ;
522
+ bad: data[ ..7 ] ;
523
+ message: "out of bounds" ;
534
524
}
535
525
536
- mod rangetoinclusive_len {
537
- let DATA = "abcdef" ;
538
-
539
- let GOOD_INPUT = ..=5 ;
540
- let GOOD_OUTPUT = "abcdef" ;
541
-
542
- let BAD_INPUT = ..=6 ;
543
- const EXPECT_MSG = "out of bounds" ;
544
-
545
- !!generate_tests!!
526
+ in mod rangetoinclusive_len {
527
+ data: "abcdef" ;
528
+ good: data[ ..=5 ] == "abcdef" ;
529
+ bad: data[ ..=6 ] ;
530
+ message: "out of bounds" ;
546
531
}
547
532
548
- mod range_len_len {
549
- let DATA = "abcdef" ;
550
-
551
- let GOOD_INPUT = 6 ..6 ;
552
- let GOOD_OUTPUT = "" ;
553
-
554
- let BAD_INPUT = 7 ..7 ;
555
- const EXPECT_MSG = "out of bounds" ;
556
-
557
- !!generate_tests!!
533
+ in mod range_len_len {
534
+ data: "abcdef" ;
535
+ good: data[ 6 ..6 ] == "" ;
536
+ bad: data[ 7 ..7 ] ;
537
+ message: "out of bounds" ;
558
538
}
559
539
560
- mod rangeinclusive_len_len {
561
- let DATA = "abcdef" ;
562
-
563
- let GOOD_INPUT = 6 ..=5 ;
564
- let GOOD_OUTPUT = "" ;
565
-
566
- let BAD_INPUT = 7 ..=6 ;
567
- const EXPECT_MSG = "out of bounds" ;
568
-
569
- !!generate_tests!!
540
+ in mod rangeinclusive_len_len {
541
+ data: "abcdef" ;
542
+ good: data[ 6 ..=5 ] == "" ;
543
+ bad: data[ 7 ..=6 ] ;
544
+ message: "out of bounds" ;
570
545
}
571
546
}
572
547
573
548
panic_cases ! {
574
- mod range_neg_width {
575
- let DATA = "abcdef" ;
576
-
577
- let GOOD_INPUT = 4 ..4 ;
578
- let GOOD_OUTPUT = "" ;
579
-
580
- let BAD_INPUT = 4 ..3 ;
581
- const EXPECT_MSG = "begin <= end (4 <= 3)" ;
582
-
583
- !!generate_tests!!
549
+ in mod range_neg_width {
550
+ data: "abcdef" ;
551
+ good: data[ 4 ..4 ] == "" ;
552
+ bad: data[ 4 ..3 ] ;
553
+ message: "begin <= end (4 <= 3)" ;
584
554
}
585
555
586
- mod rangeinclusive_neg_width {
587
- let DATA = "abcdef" ;
588
-
589
- let GOOD_INPUT = 4 ..=3 ;
590
- let GOOD_OUTPUT = "" ;
591
-
592
- let BAD_INPUT = 4 ..=2 ;
593
- const EXPECT_MSG = "begin <= end (4 <= 3)" ;
594
-
595
- !!generate_tests!!
556
+ in mod rangeinclusive_neg_width {
557
+ data: "abcdef" ;
558
+ good: data[ 4 ..=3 ] == "" ;
559
+ bad: data[ 4 ..=2 ] ;
560
+ message: "begin <= end (4 <= 3)" ;
596
561
}
597
562
}
598
563
599
564
mod overflow {
600
565
panic_cases ! {
601
-
602
- mod rangeinclusive {
603
- let DATA = "hello" ;
604
-
566
+ in mod rangeinclusive {
567
+ data: "hello" ;
605
568
// note: using 0 specifically ensures that the result of overflowing is 0..0,
606
569
// so that `get` doesn't simply return None for the wrong reason.
607
- let BAD_INPUT = 0 ..=usize :: max_value( ) ;
608
- const EXPECT_MSG = "maximum usize" ;
609
-
610
- !!generate_tests!!
570
+ bad: data[ 0 ..=usize :: max_value( ) ] ;
571
+ message: "maximum usize" ;
611
572
}
612
573
613
- mod rangetoinclusive {
614
- let DATA = "hello" ;
615
-
616
- let BAD_INPUT = ..=usize :: max_value( ) ;
617
- const EXPECT_MSG = "maximum usize" ;
618
-
619
- !!generate_tests!!
574
+ in mod rangetoinclusive {
575
+ data: "hello" ;
576
+ bad: data[ ..=usize :: max_value( ) ] ;
577
+ message: "maximum usize" ;
620
578
}
621
579
}
622
580
}
@@ -635,74 +593,53 @@ mod slice_index {
635
593
// because some of the logic may be duplicated as part of micro-optimizations
636
594
// to dodge unicode boundary checks on half-ranges.
637
595
panic_cases ! {
638
- mod range_1 {
639
- let DATA = super :: DATA ;
640
-
641
- let BAD_INPUT = super :: BAD_START ..super :: GOOD_END ;
642
- const EXPECT_MSG =
596
+ in mod range_1 {
597
+ data: super :: DATA ;
598
+ bad: data[ super :: BAD_START ..super :: GOOD_END ] ;
599
+ message:
643
600
"byte index 4 is not a char boundary; it is inside 'α' (bytes 3..5) of" ;
644
-
645
- !!generate_tests!!
646
601
}
647
602
648
- mod range_2 {
649
- let DATA = super :: DATA ;
650
-
651
- let BAD_INPUT = super :: GOOD_START ..super :: BAD_END ;
652
- const EXPECT_MSG =
603
+ in mod range_2 {
604
+ data: super :: DATA ;
605
+ bad: data[ super :: GOOD_START ..super :: BAD_END ] ;
606
+ message:
653
607
"byte index 6 is not a char boundary; it is inside 'β' (bytes 5..7) of" ;
654
-
655
- !!generate_tests!!
656
608
}
657
609
658
- mod rangefrom {
659
- let DATA = super :: DATA ;
660
-
661
- let BAD_INPUT = super :: BAD_START ..;
662
- const EXPECT_MSG =
610
+ in mod rangefrom {
611
+ data: super :: DATA ;
612
+ bad: data[ super :: BAD_START ..] ;
613
+ message:
663
614
"byte index 4 is not a char boundary; it is inside 'α' (bytes 3..5) of" ;
664
-
665
- !!generate_tests!!
666
615
}
667
616
668
- mod rangeto {
669
- let DATA = super :: DATA ;
670
-
671
- let BAD_INPUT = ..super :: BAD_END ;
672
- const EXPECT_MSG =
617
+ in mod rangeto {
618
+ data: super :: DATA ;
619
+ bad: data[ ..super :: BAD_END ] ;
620
+ message:
673
621
"byte index 6 is not a char boundary; it is inside 'β' (bytes 5..7) of" ;
674
-
675
- !!generate_tests!!
676
622
}
677
623
678
- mod rangeinclusive_1 {
679
- let DATA = super :: DATA ;
680
-
681
- let BAD_INPUT = super :: BAD_START ..=super :: GOOD_END_INCL ;
682
- const EXPECT_MSG =
624
+ in mod rangeinclusive_1 {
625
+ data: super :: DATA ;
626
+ bad: data[ super :: BAD_START ..=super :: GOOD_END_INCL ] ;
627
+ message:
683
628
"byte index 4 is not a char boundary; it is inside 'α' (bytes 3..5) of" ;
684
-
685
- !!generate_tests!!
686
629
}
687
630
688
- mod rangeinclusive_2 {
689
- let DATA = super :: DATA ;
690
-
691
- let BAD_INPUT = super :: GOOD_START ..=super :: BAD_END_INCL ;
692
- const EXPECT_MSG =
631
+ in mod rangeinclusive_2 {
632
+ data: super :: DATA ;
633
+ bad: data[ super :: GOOD_START ..=super :: BAD_END_INCL ] ;
634
+ message:
693
635
"byte index 6 is not a char boundary; it is inside 'β' (bytes 5..7) of" ;
694
-
695
- !!generate_tests!!
696
636
}
697
637
698
- mod rangetoinclusive {
699
- let DATA = super :: DATA ;
700
-
701
- let BAD_INPUT = ..=super :: BAD_END_INCL ;
702
- const EXPECT_MSG =
638
+ in mod rangetoinclusive {
639
+ data: super :: DATA ;
640
+ bad: data[ ..=super :: BAD_END_INCL ] ;
641
+ message:
703
642
"byte index 6 is not a char boundary; it is inside 'β' (bytes 5..7) of" ;
704
-
705
- !!generate_tests!!
706
643
}
707
644
}
708
645
}
0 commit comments