Chih-Hung Hsieh | e42c505 | 2020-04-16 10:44:21 -0700 | [diff] [blame^] | 1 | macro_rules! replace( |
| 2 | ($name:ident, $which:ident, $re:expr, |
| 3 | $search:expr, $replace:expr, $result:expr) => ( |
| 4 | #[test] |
| 5 | fn $name() { |
| 6 | let re = regex!($re); |
| 7 | assert_eq!(re.$which(text!($search), $replace), text!($result)); |
| 8 | } |
| 9 | ); |
| 10 | ); |
| 11 | |
| 12 | replace!(first, replace, r"[0-9]", "age: 26", t!("Z"), "age: Z6"); |
| 13 | replace!(plus, replace, r"[0-9]+", "age: 26", t!("Z"), "age: Z"); |
| 14 | replace!(all, replace_all, r"[0-9]", "age: 26", t!("Z"), "age: ZZ"); |
| 15 | replace!( |
| 16 | groups, |
| 17 | replace, |
| 18 | r"(?-u)(\S+)\s+(\S+)", |
| 19 | "w1 w2", |
| 20 | t!("$2 $1"), |
| 21 | "w2 w1" |
| 22 | ); |
| 23 | replace!( |
| 24 | double_dollar, |
| 25 | replace, |
| 26 | r"(?-u)(\S+)\s+(\S+)", |
| 27 | "w1 w2", |
| 28 | t!("$2 $$1"), |
| 29 | "w2 $1" |
| 30 | ); |
| 31 | // replace!(adjacent_index, replace, |
| 32 | // r"([^aeiouy])ies$", "skies", t!("$1y"), "sky"); |
| 33 | replace!( |
| 34 | named, |
| 35 | replace_all, |
| 36 | r"(?-u)(?P<first>\S+)\s+(?P<last>\S+)(?P<space>\s*)", |
| 37 | "w1 w2 w3 w4", |
| 38 | t!("$last $first$space"), |
| 39 | "w2 w1 w4 w3" |
| 40 | ); |
| 41 | replace!( |
| 42 | trim, |
| 43 | replace_all, |
| 44 | "^[ \t]+|[ \t]+$", |
| 45 | " \t trim me\t \t", |
| 46 | t!(""), |
| 47 | "trim me" |
| 48 | ); |
| 49 | replace!(number_hypen, replace, r"(.)(.)", "ab", t!("$1-$2"), "a-b"); |
| 50 | // replace!(number_underscore, replace, r"(.)(.)", "ab", t!("$1_$2"), "a_b"); |
| 51 | replace!( |
| 52 | simple_expand, |
| 53 | replace_all, |
| 54 | r"(?-u)(\w) (\w)", |
| 55 | "a b", |
| 56 | t!("$2 $1"), |
| 57 | "b a" |
| 58 | ); |
| 59 | replace!( |
| 60 | literal_dollar1, |
| 61 | replace_all, |
| 62 | r"(?-u)(\w+) (\w+)", |
| 63 | "a b", |
| 64 | t!("$$1"), |
| 65 | "$1" |
| 66 | ); |
| 67 | replace!( |
| 68 | literal_dollar2, |
| 69 | replace_all, |
| 70 | r"(?-u)(\w+) (\w+)", |
| 71 | "a b", |
| 72 | t!("$2 $$c $1"), |
| 73 | "b $c a" |
| 74 | ); |
| 75 | replace!( |
| 76 | no_expand1, |
| 77 | replace, |
| 78 | r"(?-u)(\S+)\s+(\S+)", |
| 79 | "w1 w2", |
| 80 | no_expand!("$2 $1"), |
| 81 | "$2 $1" |
| 82 | ); |
| 83 | replace!( |
| 84 | no_expand2, |
| 85 | replace, |
| 86 | r"(?-u)(\S+)\s+(\S+)", |
| 87 | "w1 w2", |
| 88 | no_expand!("$$1"), |
| 89 | "$$1" |
| 90 | ); |
| 91 | use_!(Captures); |
| 92 | replace!( |
| 93 | closure_returning_reference, |
| 94 | replace, |
| 95 | r"([0-9]+)", |
| 96 | "age: 26", |
| 97 | |captures: &Captures| { |
| 98 | match_text!(captures.get(1).unwrap())[0..1].to_owned() |
| 99 | }, |
| 100 | "age: 2" |
| 101 | ); |
| 102 | replace!( |
| 103 | closure_returning_value, |
| 104 | replace, |
| 105 | r"[0-9]+", |
| 106 | "age: 26", |
| 107 | |_captures: &Captures| t!("Z").to_owned(), |
| 108 | "age: Z" |
| 109 | ); |
| 110 | |
| 111 | // See https://github.com/rust-lang/regex/issues/314 |
| 112 | replace!( |
| 113 | match_at_start_replace_with_empty, |
| 114 | replace_all, |
| 115 | r"foo", |
| 116 | "foobar", |
| 117 | t!(""), |
| 118 | "bar" |
| 119 | ); |
| 120 | |
| 121 | // See https://github.com/rust-lang/regex/issues/393 |
| 122 | replace!(single_empty_match, replace, r"^", "bar", t!("foo"), "foobar"); |
| 123 | |
| 124 | // See https://github.com/rust-lang/regex/issues/399 |
| 125 | replace!( |
| 126 | capture_longest_possible_name, |
| 127 | replace_all, |
| 128 | r"(.)", |
| 129 | "b", |
| 130 | t!("${1}a $1a"), |
| 131 | "ba " |
| 132 | ); |