]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/macro-pat.rs
7a3e55322c8e1cb5cb1c87d6bd96e147df3c4a4b
[rust.git] / src / test / run-pass / macro-pat.rs
1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 macro_rules! mypat {
12     () => (
13         Some('y')
14     )
15 }
16
17 macro_rules! char_x {
18     () => (
19         'x'
20     )
21 }
22
23 macro_rules! some {
24     ($x:pat) => (
25         Some($x)
26     )
27 }
28
29 macro_rules! indirect {
30     () => (
31         some!(char_x!())
32     )
33 }
34
35 macro_rules! ident_pat {
36     ($x:ident) => (
37         $x
38     )
39 }
40
41 fn f(c: Option<char>) -> uint {
42     match c {
43         Some('x') => 1,
44         mypat!() => 2,
45         _ => 3,
46     }
47 }
48
49 pub fn main() {
50     assert_eq!(1, f(Some('x')));
51     assert_eq!(2, f(Some('y')));
52     assert_eq!(3, f(None));
53
54     assert_eq!(1, match Some('x') {
55         Some(char_x!()) => 1,
56         _ => 2,
57     });
58
59     assert_eq!(1, match Some('x') {
60         some!(char_x!()) => 1,
61         _ => 2,
62     });
63
64     assert_eq!(1, match Some('x') {
65         indirect!() => 1,
66         _ => 2,
67     });
68
69     assert_eq!(3, {
70         let ident_pat!(x) = 2;
71         x+1
72     });
73 }