summaryrefslogtreecommitdiff
path: root/vendor/matchit/tests/insert.rs
blob: 5513c1c6655bd71bdf10a0dc73da9174e221e236 (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
use matchit::{InsertError, Router};

struct InsertTest(Vec<(&'static str, Result<(), InsertError>)>);

impl InsertTest {
    fn run(self) {
        let mut router = Router::new();
        for (route, expected) in self.0 {
            let got = router.insert(route, route.to_owned());
            assert_eq!(got, expected, "{route}");
        }
    }
}

fn conflict(with: &'static str) -> InsertError {
    InsertError::Conflict { with: with.into() }
}

#[test]
fn wildcard_conflict() {
    InsertTest(vec![
        ("/cmd/{tool}/{sub}", Ok(())),
        ("/cmd/vet", Ok(())),
        ("/foo/bar", Ok(())),
        ("/foo/{name}", Ok(())),
        ("/foo/{names}", Err(conflict("/foo/{name}"))),
        ("/cmd/{*path}", Err(conflict("/cmd/{tool}/{sub}"))),
        ("/cmd/{xxx}/names", Ok(())),
        ("/cmd/{tool}/{xxx}/foo", Ok(())),
        ("/src/{*filepath}", Ok(())),
        ("/src/{file}", Err(conflict("/src/{*filepath}"))),
        ("/src/static.json", Ok(())),
        ("/src/$filepathx", Ok(())),
        ("/src/", Ok(())),
        ("/src/foo/bar", Ok(())),
        ("/src1/", Ok(())),
        ("/src1/{*filepath}", Ok(())),
        ("/src2{*filepath}", Ok(())),
        ("/src2/{*filepath}", Ok(())),
        ("/src2/", Ok(())),
        ("/src2", Ok(())),
        ("/src3", Ok(())),
        ("/src3/{*filepath}", Ok(())),
        ("/search/{query}", Ok(())),
        ("/search/valid", Ok(())),
        ("/user_{name}", Ok(())),
        ("/user_x", Ok(())),
        ("/user_{bar}", Err(conflict("/user_{name}"))),
        ("/id{id}", Ok(())),
        ("/id/{id}", Ok(())),
    ])
    .run()
}

#[test]
fn invalid_catchall() {
    InsertTest(vec![
        ("/non-leading-{*catchall}", Ok(())),
        ("/foo/bar{*catchall}", Ok(())),
        ("/src/{*filepath}/x", Err(InsertError::InvalidCatchAll)),
        ("/src2/", Ok(())),
        ("/src2/{*filepath}/x", Err(InsertError::InvalidCatchAll)),
    ])
    .run()
}

#[test]
fn catchall_root_conflict() {
    InsertTest(vec![("/", Ok(())), ("/{*filepath}", Ok(()))]).run()
}

#[test]
fn child_conflict() {
    InsertTest(vec![
        ("/cmd/vet", Ok(())),
        ("/cmd/{tool}", Ok(())),
        ("/cmd/{tool}/{sub}", Ok(())),
        ("/cmd/{tool}/misc", Ok(())),
        ("/cmd/{tool}/{bad}", Err(conflict("/cmd/{tool}/{sub}"))),
        ("/src/AUTHORS", Ok(())),
        ("/src/{*filepath}", Ok(())),
        ("/user_x", Ok(())),
        ("/user_{name}", Ok(())),
        ("/id/{id}", Ok(())),
        ("/id{id}", Ok(())),
        ("/{id}", Ok(())),
        ("/{*filepath}", Err(conflict("/{id}"))),
    ])
    .run()
}

#[test]
fn duplicates() {
    InsertTest(vec![
        ("/", Ok(())),
        ("/", Err(conflict("/"))),
        ("/doc/", Ok(())),
        ("/doc/", Err(conflict("/doc/"))),
        ("/src/{*filepath}", Ok(())),
        ("/src/{*filepath}", Err(conflict("/src/{*filepath}"))),
        ("/search/{query}", Ok(())),
        ("/search/{query}", Err(conflict("/search/{query}"))),
        ("/user_{name}", Ok(())),
        ("/user_{name}", Err(conflict("/user_{name}"))),
    ])
    .run()
}

#[test]
fn unnamed_param() {
    InsertTest(vec![
        ("/{}", Err(InsertError::InvalidParam)),
        ("/user{}/", Err(InsertError::InvalidParam)),
        ("/cmd/{}/", Err(InsertError::InvalidParam)),
        ("/src/{*}", Err(InsertError::InvalidParam)),
    ])
    .run()
}

#[test]
fn double_params() {
    InsertTest(vec![
        ("/{foo}{bar}", Err(InsertError::InvalidParamSegment)),
        ("/{foo}{bar}/", Err(InsertError::InvalidParamSegment)),
        ("/{foo}{{*bar}/", Err(InsertError::InvalidParamSegment)),
    ])
    .run()
}

#[test]
fn normalized_conflict() {
    InsertTest(vec![
        ("/x/{foo}/bar", Ok(())),
        ("/x/{bar}/bar", Err(conflict("/x/{foo}/bar"))),
        ("/{y}/bar/baz", Ok(())),
        ("/{y}/baz/baz", Ok(())),
        ("/{z}/bar/bat", Ok(())),
        ("/{z}/bar/baz", Err(conflict("/{y}/bar/baz"))),
    ])
    .run()
}

#[test]
fn more_conflicts() {
    InsertTest(vec![
        ("/con{tact}", Ok(())),
        ("/who/are/{*you}", Ok(())),
        ("/who/foo/hello", Ok(())),
        ("/whose/{users}/{name}", Ok(())),
        ("/who/are/foo", Ok(())),
        ("/who/are/foo/bar", Ok(())),
        ("/con{nection}", Err(conflict("/con{tact}"))),
        (
            "/whose/{users}/{user}",
            Err(conflict("/whose/{users}/{name}")),
        ),
    ])
    .run()
}

#[test]
fn catchall_static_overlap() {
    InsertTest(vec![
        ("/bar", Ok(())),
        ("/bar/", Ok(())),
        ("/bar/{*foo}", Ok(())),
    ])
    .run();

    InsertTest(vec![
        ("/foo", Ok(())),
        ("/{*bar}", Ok(())),
        ("/bar", Ok(())),
        ("/baz", Ok(())),
        ("/baz/{split}", Ok(())),
        ("/", Ok(())),
        ("/{*bar}", Err(conflict("/{*bar}"))),
        ("/{*zzz}", Err(conflict("/{*bar}"))),
        ("/{xxx}", Err(conflict("/{*bar}"))),
    ])
    .run();

    InsertTest(vec![
        ("/{*bar}", Ok(())),
        ("/bar", Ok(())),
        ("/bar/x", Ok(())),
        ("/bar_{x}", Ok(())),
        ("/bar_{x}", Err(conflict("/bar_{x}"))),
        ("/bar_{x}/y", Ok(())),
        ("/bar/{x}", Ok(())),
    ])
    .run();
}

#[test]
fn duplicate_conflict() {
    InsertTest(vec![
        ("/hey", Ok(())),
        ("/hey/users", Ok(())),
        ("/hey/user", Ok(())),
        ("/hey/user", Err(conflict("/hey/user"))),
    ])
    .run()
}

#[test]
fn invalid_param() {
    InsertTest(vec![
        ("{", Err(InsertError::InvalidParam)),
        ("}", Err(InsertError::InvalidParam)),
        ("x{y", Err(InsertError::InvalidParam)),
        ("x}", Err(InsertError::InvalidParam)),
        ("/{foo}s", Err(InsertError::InvalidParamSegment)),
    ])
    .run();
}

#[test]
fn escaped_param() {
    InsertTest(vec![
        ("{{", Ok(())),
        ("}}", Ok(())),
        ("xx}}", Ok(())),
        ("}}yy", Ok(())),
        ("}}yy{{}}", Ok(())),
        ("}}yy{{}}{{}}y{{", Ok(())),
        ("}}yy{{}}{{}}y{{", Err(conflict("}yy{}{}y{"))),
        ("/{{yy", Ok(())),
        ("/{yy}", Ok(())),
        ("/foo", Ok(())),
        ("/foo/{{", Ok(())),
        ("/foo/{{/{x}", Ok(())),
        ("/foo/{ba{{r}", Ok(())),
        ("/bar/{ba}}r}", Ok(())),
        ("/xxx/{x{{}}y}", Ok(())),
    ])
    .run()
}

#[test]
fn bare_catchall() {
    InsertTest(vec![("{*foo}", Ok(())), ("foo/{*bar}", Ok(()))]).run()
}