File: test.rs

package info (click to toggle)
rust-try-match 0.4.2-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 236 kB
  • sloc: makefile: 2
file content (346 lines) | stat: -rw-r--r-- 9,636 bytes parent folder | download
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
extern crate core as abcdefgh;
extern crate std as ijklmn;
use try_match::{match_ok, match_or_default, try_match, unwrap_match};

#[test]
fn input_evaled_only_once() {
    #[derive(Debug)]
    struct A;
    let input = A;

    // `A: !Copy`, so the following line would fail to compile if
    // `input` were evaluated twice
    #[allow(clippy::let_unit_value)]
    let _ = try_match!(input, A => ()).unwrap();
}

#[cfg(feature = "implicit_map")]
#[test]
#[allow(unused_parens)]
fn pat_paren_implicit_map() {
    assert_eq!(try_match!((), (())), Ok(()));
}

#[cfg(feature = "implicit_map")]
#[test]
#[allow(warnings)]
fn ident_inside_types() {
    trait Tr {
        const C: () = ();
    }

    impl<T> Tr for T {
        const C: () = ();
    }

    assert_eq!(
        try_match!(
            (),
            <[(); {
                match () {
                    // `_4` is a `Pat::Ident`, but `try_match!` should not
                    // consider it as a part of the input pattern
                    _4 => 42,
                }
            }] as Tr>::C
        ),
        Ok(())
    );
}

// requires `inline_const_pat` <https://github.com/rust-lang/rust/issues/76001>
// #[cfg(feature = "implicit_map")]
// #[test]
// fn pat_const_implicit_map() {
//     assert_eq!(try_match!(42, const { 42 }), Ok(()));
// }

#[cfg(feature = "implicit_map")]
#[test]
#[allow(clippy::redundant_pattern)]
fn pat_ident_subpat_implicit_map() {
    // Tuple pattern
    assert_eq!(try_match!((), _0 @ _), Ok(()));

    // Single
    assert_eq!(try_match!((), a @ _), Ok(()));

    // Multiple
    let m = try_match!((1, 2), (a @ _, b @ _)).unwrap();
    assert_eq!((m.a, m.b), (1, 2));
}

#[cfg(feature = "implicit_map")]
#[test]
#[allow(non_snake_case)]
fn pat_ident_guessed_binding_implicit_map() {
    assert_eq!(try_match!(42, f), Ok(42));
    assert_eq!(try_match!(42, fA), Ok(42));
    assert_eq!(try_match!(42, fは), Ok(42));
    assert_eq!(try_match!(42, _f), Ok(42));
    assert_eq!(try_match!(42, _fA), Ok(42));
    assert_eq!(try_match!(42, _fは), Ok(42));
    assert_eq!(try_match!(42, _0), Ok(42));
    assert_eq!(try_match!(42, r#f), Ok(42));
    assert_eq!(try_match!(42, r#fA), Ok(42));
    assert_eq!(try_match!(42, r#fは), Ok(42));
    assert_eq!(try_match!(42, r#_f), Ok(42));
    assert_eq!(try_match!(42, r#_fA), Ok(42));
    assert_eq!(try_match!(42, r#_fは), Ok(42));
    assert_eq!(try_match!(42, r#_0), Ok(42));
}

#[cfg(feature = "implicit_map")]
#[test]
fn pat_ident_guessed_constant_implicit_map() {
    const F: i32 = 0;
    const FA: i32 = 0;
    const Fは: i32 = 0;
    const _F: i32 = 0;
    const _FA: i32 = 0;
    const _Fは: i32 = 0;

    assert_eq!(try_match!(42, F), Err(42));
    assert_eq!(try_match!(42, FA), Err(42));
    assert_eq!(try_match!(42, Fは), Err(42));
    assert_eq!(try_match!(42, _F), Err(42));
    assert_eq!(try_match!(42, _FA), Err(42));
    assert_eq!(try_match!(42, _Fは), Err(42));
    assert_eq!(try_match!(42, r#F), Err(42));
    assert_eq!(try_match!(42, r#FA), Err(42));
    assert_eq!(try_match!(42, r#Fは), Err(42));
    assert_eq!(try_match!(42, r#_F), Err(42));
    assert_eq!(try_match!(42, r#_FA), Err(42));
    assert_eq!(try_match!(42, r#_Fは), Err(42));
}

#[test]
fn trailing_comma() {
    assert_eq!(try_match!(Some(12), Some(a) => a,), Ok(12));
    assert_eq!(try_match!(Some(12), Some(a) if a < 20 => a,), Ok(12));
    assert_eq!(match_ok!(Some(12), Some(a) => a,), Some(12));
    assert_eq!(match_ok!(Some(12), Some(a) if a < 20 => a,), Some(12));
    assert_eq!(unwrap_match!(Some(12), Some(a) => a,), 12);
    assert_eq!(unwrap_match!(Some(12), Some(a) if a < 20 => a,), 12);
}

#[cfg(feature = "implicit_map")]
#[test]
fn trailing_comma_implicit_map() {
    assert_eq!(try_match!(Some(12), Some(a),), Ok(12));
    assert_eq!(try_match!(Some(12), Some(a) if a < 20,), Ok(12));
    assert_eq!(match_ok!(Some(12), Some(a),), Some(12));
    assert_eq!(match_ok!(Some(12), Some(a) if a < 20,), Some(12));
    assert_eq!(unwrap_match!(Some(12), Some(a),), 12);
    assert_eq!(unwrap_match!(Some(12), Some(a) if a < 20,), 12);
}

#[cfg(feature = "implicit_map")]
#[test]
#[deny(clippy::just_underscores_and_digits)]
fn clippy_just_underscores_and_digits_implicit_map() {
    // A variable binding like `_0` usually triggers
    // `clippy::just_underscores_and_digits`, but we specifically need this
    // family of names for tuple implicit mapping
    unwrap_match!(Some(12), Some(_0 @ _));

    // TODO: Use `#[expect(...)]` to check that the lint is still effective in
    // future toolchains
}

#[test]
fn guards() {
    assert_eq!(try_match!(Some(12), Some(a) if a < 20 => a), Ok(12));
    assert_eq!(try_match!(Some(42), Some(a) if a < 20 => a), Err(Some(42)));
    assert_eq!(try_match!(None::<u32>, Some(a) if a < 20 => a), Err(None));
}

#[cfg(feature = "implicit_map")]
#[test]
fn guards_implicit_map() {
    assert_eq!(try_match!(Some(12), Some(a) if a < 20), Ok(12));
    assert_eq!(try_match!(Some(42), Some(a) if a < 20), Err(Some(42)));
    assert_eq!(try_match!(None::<u32>, Some(a) if a < 20), Err(None));
}

#[test]
fn struct_expr() {
    struct A {
        _x: i32,
    }
    assert!(matches!(A { _x: 42 }, A { .. }));
    assert_eq!(match_ok!(A { _x: 42 }, A { .. } => 42), Some(42));
}

#[cfg(feature = "implicit_map")]
#[test]
fn struct_expr_implicit_map() {
    struct A {
        _x: i32,
    }
    assert_eq!(match_ok!(A { _x: 42 }, A { .. }), Some(()));
}

#[test]
#[allow(unused_variables)]
fn struct_field_shorthand() {
    struct A {
        x: i32,
    }
    assert!(matches!(A { x: 42 }, A { x }));
    assert_eq!(match_ok!(A { x: 42 }, A { x } => x), Some(42));
    assert_eq!(match_ok!(A { x: 42 }, A { ref x } => x), Some(&42));
}

#[cfg(feature = "implicit_map")]
#[test]
fn struct_field_shorthand_implicit_map() {
    struct A {
        x: i32,
    }
    assert_eq!(match_ok!(A { x: 42 }, A { x }), Some(42));
    assert_eq!(match_ok!(A { x: 42 }, A { ref x }), Some(&42));
}

#[test]
#[allow(unused_variables)]
#[allow(clippy::diverging_sub_expression)]
fn return_in_guard() {
    assert_eq!(try_match!(Some(12), Some(a) if return => a), Ok(42));
    unreachable!();
}

#[cfg(feature = "implicit_map")]
#[test]
#[allow(unused_variables)]
#[allow(clippy::diverging_sub_expression)]
fn return_in_guard_implicit_map() {
    assert_eq!(try_match!(Some(12), Some(a) if return), Ok(42));
    unreachable!();
}

#[cfg(feature = "implicit_map")]
#[test]
fn input_evaled_only_once_implicit_map() {
    #[derive(Debug)]
    struct A;
    let input = A;

    // `A: !Copy`, so the following line should fail to compile if
    // `input` is evaluated twice
    unwrap_match!(input, A);
}

#[cfg(feature = "implicit_map")]
#[test]
fn unwrap_option() {
    assert_eq!(try_match!(Some(42), Some(a)), Ok(42));
    assert_eq!(try_match!(None::<u32>, Some(a)), Err(None));

    let some = MyOption::Some(42);
    let a = try_match!(some, MyOption::Some(a));
    assert_eq!(a, Ok(42));
    assert_eq!(
        try_match!(MyOption::None, MyOption::Some(a)),
        Err(MyOption::None)
    );
    assert_eq!(try_match!(some, MyOption::Some(a)), Ok(42));
    assert_eq!(
        try_match!(MyOption::None, MyOption::Some(a)),
        Err(MyOption::None)
    );
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum MyOption {
    Some(u32),
    None,
}

#[cfg(feature = "implicit_map")]
#[test]
fn unwrap_result() {
    assert_eq!(try_match!(Ok(42), Ok(a) | Err(a)), Ok(42));
    assert_eq!(try_match!(Err(42), Ok(a) | Err(a)), Ok(42));

    assert_eq!(try_match!(Ok::<_, &_>(42), Ok(_0) | Err(&_0)), Ok(42));
    assert_eq!(try_match!(Err::<&_, _>(42), Ok(&_0) | Err(_0)), Ok(42));
}

#[cfg(feature = "implicit_map")]
#[test]
#[should_panic = "assertion failed: '42' does not match 'x if x < 20'"]
fn unwrap_match_msg_default1() {
    unwrap_match!(42, x if x < 20);
}

#[cfg(feature = "implicit_map")]
#[test]
#[should_panic = "assertion failed: '42' does not match 'x if x < 20'"]
fn unwrap_match_msg_default2() {
    unwrap_match!(42, x if x < 20,);
}

#[test]
#[should_panic = "assertion failed: '42' does not match 'x if x < 20'"]
fn unwrap_match_msg_default3() {
    unwrap_match!(42, x if x < 20 => ());
}

#[test]
#[should_panic = "assertion failed: '42' does not match 'x if x < 20'"]
fn unwrap_match_msg_default4() {
    unwrap_match!(42, x if x < 20 => (),);
}

#[test]
#[should_panic = "poneyland"]
fn unwrap_match_msg1() {
    unwrap_match!(42, x if x < 20 => (), "poney{}", "land");
}

#[test]
#[should_panic = "poneyland"]
fn unwrap_match_msg2() {
    unwrap_match!(42, x if x < 20 => (), "poney{}", "land",);
}

#[test]
fn match_or_default_explicit() {
    assert_eq!(match_or_default!(Ok::<(), ()>(()), Ok(_) => 42), 42);
    assert_eq!(match_or_default!(Err::<(), ()>(()), Ok(_) => 42), 0);
}

#[cfg(feature = "implicit_map")]
#[test]
#[allow(clippy::let_unit_value)]
fn match_or_default_implicit_unit() {
    () = match_or_default!(Ok::<_, ()>(()), Ok(()));
    () = match_or_default!(Err::<(), _>(()), Ok(()));
}

#[cfg(feature = "implicit_map")]
#[test]
fn match_or_default_implicit_tuple() {
    assert_eq!(
        match_or_default!(Ok::<(i8, i8), (i8, i8)>((1, 2)), Ok((_0, _1))),
        (1, 2)
    );
    assert_eq!(
        match_or_default!(Err::<(i8, i8), (i8, i8)>((1, 2)), Ok((_0, _1))),
        (0, 0)
    );
}

#[cfg(feature = "implicit_map")]
#[test]
fn match_or_default_implicit_struct() {
    assert_eq!(
        match_or_default!(Ok::<(i8, i8), (i8, i8)>((1, 2)), Ok((a, b))).b,
        2
    );
    assert_eq!(
        match_or_default!(Err::<(i8, i8), (i8, i8)>((1, 2)), Ok((a, b))).b,
        0
    );
}