aboutsummaryrefslogtreecommitdiff
path: root/tests/replace.rs
blob: c156a399ff0729ed9a3065524464bf9d2c779fd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
macro_rules! replace(
    ($name:ident, $which:ident, $re:expr,
     $search:expr, $replace:expr, $result:expr) => (
        #[test]
        fn $name() {
            let re = regex!($re);
            assert_eq!(re.$which(text!($search), $replace), text!($result));
        }
    );
);

replace!(first, replace, r"[0-9]", "age: 26", t!("Z"), "age: Z6");
replace!(plus, replace, r"[0-9]+", "age: 26", t!("Z"), "age: Z");
replace!(all, replace_all, r"[0-9]", "age: 26", t!("Z"), "age: ZZ");
replace!(
    groups,
    replace,
    r"(?-u)(\S+)\s+(\S+)",
    "w1 w2",
    t!("$2 $1"),
    "w2 w1"
);
replace!(
    double_dollar,
    replace,
    r"(?-u)(\S+)\s+(\S+)",
    "w1 w2",
    t!("$2 $$1"),
    "w2 $1"
);
// replace!(adjacent_index, replace,
// r"([^aeiouy])ies$", "skies", t!("$1y"), "sky");
replace!(
    named,
    replace_all,
    r"(?-u)(?P<first>\S+)\s+(?P<last>\S+)(?P<space>\s*)",
    "w1 w2 w3 w4",
    t!("$last $first$space"),
    "w2 w1 w4 w3"
);
replace!(
    trim,
    replace_all,
    "^[ \t]+|[ \t]+$",
    " \t  trim me\t   \t",
    t!(""),
    "trim me"
);
replace!(number_hypen, replace, r"(.)(.)", "ab", t!("$1-$2"), "a-b");
// replace!(number_underscore, replace, r"(.)(.)", "ab", t!("$1_$2"), "a_b");
replace!(
    simple_expand,
    replace_all,
    r"(?-u)(\w) (\w)",
    "a b",
    t!("$2 $1"),
    "b a"
);
replace!(
    literal_dollar1,
    replace_all,
    r"(?-u)(\w+) (\w+)",
    "a b",
    t!("$$1"),
    "$1"
);
replace!(
    literal_dollar2,
    replace_all,
    r"(?-u)(\w+) (\w+)",
    "a b",
    t!("$2 $$c $1"),
    "b $c a"
);
replace!(
    no_expand1,
    replace,
    r"(?-u)(\S+)\s+(\S+)",
    "w1 w2",
    no_expand!("$2 $1"),
    "$2 $1"
);
replace!(
    no_expand2,
    replace,
    r"(?-u)(\S+)\s+(\S+)",
    "w1 w2",
    no_expand!("$$1"),
    "$$1"
);
use_!(Captures);
replace!(
    closure_returning_reference,
    replace,
    r"([0-9]+)",
    "age: 26",
    |captures: &Captures| {
        match_text!(captures.get(1).unwrap())[0..1].to_owned()
    },
    "age: 2"
);
replace!(
    closure_returning_value,
    replace,
    r"[0-9]+",
    "age: 26",
    |_captures: &Captures| t!("Z").to_owned(),
    "age: Z"
);

// See https://github.com/rust-lang/regex/issues/314
replace!(
    match_at_start_replace_with_empty,
    replace_all,
    r"foo",
    "foobar",
    t!(""),
    "bar"
);

// See https://github.com/rust-lang/regex/issues/393
replace!(single_empty_match, replace, r"^", "bar", t!("foo"), "foobar");

// See https://github.com/rust-lang/regex/issues/399
replace!(
    capture_longest_possible_name,
    replace_all,
    r"(.)",
    "b",
    t!("${1}a $1a"),
    "ba "
);