Skip to content

Commit f1d7b45

Browse files
committed
revise test gen macro for str
1 parent 030aa9b commit f1d7b45

File tree

1 file changed

+81
-144
lines changed

1 file changed

+81
-144
lines changed

src/liballoc/tests/str.rs

+81-144
Original file line numberDiff line numberDiff line change
@@ -291,11 +291,14 @@ fn test_replace_pattern() {
291291
assert_eq!(data.replace(|c| c == 'γ', "😺😺😺"), "abcdαβ😺😺😺δabcdαβ😺😺😺δ");
292292
}
293293

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.
294297
mod slice_index {
295298
// Test a slicing operation **that should succeed,**
296299
// testing it on all of the indexing methods.
297300
//
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.
299302
macro_rules! assert_range_eq {
300303
($s:expr, $range:expr, $expected:expr)
301304
=> {
@@ -340,7 +343,7 @@ mod slice_index {
340343
// because if it can't, then what are we even doing here?
341344
//
342345
// (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
344347
// to be used in `should_panic`)
345348
#[test]
346349
#[should_panic(expected = "out of bounds")]
@@ -363,8 +366,8 @@ mod slice_index {
363366
// and `None` test cases for get/get_mut.
364367
macro_rules! panic_cases {
365368
($(
366-
mod $case_name:ident {
367-
let DATA = $data:expr;
369+
in mod $case_name:ident {
370+
data: $data:expr;
368371

369372
// optional:
370373
//
@@ -373,14 +376,11 @@ mod slice_index {
373376
// straddles the boundary between valid and invalid.
374377
// (such as the input `len..len`, which is just barely valid)
375378
$(
376-
let GOOD_INPUT = $good:expr;
377-
let GOOD_OUTPUT = $output:expr;
379+
good: data[$good:expr] == $output:expr;
378380
)*
379381

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
384384
}
385385
)*) => {$(
386386
mod $case_name {
@@ -509,114 +509,72 @@ mod slice_index {
509509
}
510510

511511
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";
522517
}
523518

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";
534524
}
535525

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";
546531
}
547532

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";
558538
}
559539

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";
570545
}
571546
}
572547

573548
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)";
584554
}
585555

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)";
596561
}
597562
}
598563

599564
mod overflow {
600565
panic_cases! {
601-
602-
mod rangeinclusive {
603-
let DATA = "hello";
604-
566+
in mod rangeinclusive {
567+
data: "hello";
605568
// note: using 0 specifically ensures that the result of overflowing is 0..0,
606569
// 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";
611572
}
612573

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";
620578
}
621579
}
622580
}
@@ -635,74 +593,53 @@ mod slice_index {
635593
// because some of the logic may be duplicated as part of micro-optimizations
636594
// to dodge unicode boundary checks on half-ranges.
637595
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:
643600
"byte index 4 is not a char boundary; it is inside 'α' (bytes 3..5) of";
644-
645-
!!generate_tests!!
646601
}
647602

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:
653607
"byte index 6 is not a char boundary; it is inside 'β' (bytes 5..7) of";
654-
655-
!!generate_tests!!
656608
}
657609

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:
663614
"byte index 4 is not a char boundary; it is inside 'α' (bytes 3..5) of";
664-
665-
!!generate_tests!!
666615
}
667616

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:
673621
"byte index 6 is not a char boundary; it is inside 'β' (bytes 5..7) of";
674-
675-
!!generate_tests!!
676622
}
677623

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:
683628
"byte index 4 is not a char boundary; it is inside 'α' (bytes 3..5) of";
684-
685-
!!generate_tests!!
686629
}
687630

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:
693635
"byte index 6 is not a char boundary; it is inside 'β' (bytes 5..7) of";
694-
695-
!!generate_tests!!
696636
}
697637

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:
703642
"byte index 6 is not a char boundary; it is inside 'β' (bytes 5..7) of";
704-
705-
!!generate_tests!!
706643
}
707644
}
708645
}

0 commit comments

Comments
 (0)