1 #![allow(clippy::let_underscore_untyped)]
2 
3 mod test_basic {
4     use paste::paste;
5 
6     struct Struct;
7 
8     paste! {
9         impl Struct {
10             fn [<a b c>]() {}
11         }
12     }
13 
14     #[test]
test()15     fn test() {
16         Struct::abc();
17     }
18 }
19 
20 mod test_in_impl {
21     use paste::paste;
22 
23     struct Struct;
24 
25     impl Struct {
26         paste! {
27             fn [<a b c>]() {}
28         }
29     }
30 
31     #[test]
test()32     fn test() {
33         Struct::abc();
34     }
35 }
36 
37 mod test_none_delimited_single_ident {
38     use paste::paste;
39 
40     macro_rules! m {
41         ($id:ident) => {
42             paste! {
43                 fn f() -> &'static str {
44                     stringify!($id)
45                 }
46             }
47         };
48     }
49 
50     m!(i32x4);
51 
52     #[test]
test()53     fn test() {
54         assert_eq!(f(), "i32x4");
55     }
56 }
57 
58 mod test_none_delimited_single_lifetime {
59     use paste::paste;
60 
61     macro_rules! m {
62         ($life:lifetime) => {
63             paste! {
64                 pub struct S<$life>(&$life ());
65                 impl<$life> S<$life> {
66                     fn f() {}
67                 }
68             }
69         };
70     }
71 
72     m!('a);
73 
74     #[test]
test()75     fn test() {
76         S::f();
77     }
78 }
79 
80 mod test_to_lower {
81     use paste::paste;
82 
83     macro_rules! m {
84         ($id:ident) => {
85             paste! {
86                 fn [<my_ $id:lower _here>](_arg: u8) -> &'static str {
87                     stringify!([<$id:lower>])
88                 }
89             }
90         };
91     }
92 
93     m!(Test);
94 
95     #[test]
test_to_lower()96     fn test_to_lower() {
97         assert_eq!(my_test_here(0), "test");
98     }
99 }
100 
101 mod test_to_upper {
102     use paste::paste;
103 
104     macro_rules! m {
105         ($id:ident) => {
106             paste! {
107                 const [<MY_ $id:upper _HERE>]: &str = stringify!([<$id:upper>]);
108             }
109         };
110     }
111 
112     m!(Test);
113 
114     #[test]
test_to_upper()115     fn test_to_upper() {
116         assert_eq!(MY_TEST_HERE, "TEST");
117     }
118 }
119 
120 mod test_to_snake {
121     use paste::paste;
122 
123     macro_rules! m {
124         ($id:ident) => {
125             paste! {
126                 const DEFAULT_SNAKE: &str = stringify!([<$id:snake>]);
127                 const LOWER_SNAKE: &str = stringify!([<$id:snake:lower>]);
128                 const UPPER_SNAKE: &str = stringify!([<$id:snake:upper>]);
129             }
130         };
131     }
132 
133     m!(ThisIsButATest);
134 
135     #[test]
test_to_snake()136     fn test_to_snake() {
137         assert_eq!(DEFAULT_SNAKE, "this_is_but_a_test");
138         assert_eq!(LOWER_SNAKE, "this_is_but_a_test");
139         assert_eq!(UPPER_SNAKE, "THIS_IS_BUT_A_TEST");
140     }
141 }
142 
143 mod test_to_camel {
144     use paste::paste;
145 
146     macro_rules! m {
147         ($id:ident) => {
148             paste! {
149                 const DEFAULT_CAMEL: &str = stringify!([<$id:camel>]);
150                 const LOWER_CAMEL: &str = stringify!([<$id:camel:lower>]);
151                 const UPPER_CAMEL: &str = stringify!([<$id:camel:upper>]);
152             }
153         };
154     }
155 
156     m!(this_is_but_a_test);
157 
158     #[test]
test_to_camel()159     fn test_to_camel() {
160         assert_eq!(DEFAULT_CAMEL, "ThisIsButATest");
161         assert_eq!(LOWER_CAMEL, "thisisbutatest");
162         assert_eq!(UPPER_CAMEL, "THISISBUTATEST");
163     }
164 }
165 
166 mod test_doc_expr {
167     // https://github.com/dtolnay/paste/issues/29
168 
169     use paste::paste;
170 
171     macro_rules! doc_expr {
172         ($doc:expr) => {
173             paste! {
174                 #[doc = $doc]
175                 pub struct S;
176             }
177         };
178     }
179 
180     doc_expr!(stringify!());
181 
182     #[test]
test_doc_expr()183     fn test_doc_expr() {
184         let _: S;
185     }
186 }
187 
188 mod test_type_in_path {
189     // https://github.com/dtolnay/paste/issues/31
190 
191     use paste::paste;
192 
193     mod keys {
194         #[derive(Default)]
195         pub struct Mib<T = ()>(std::marker::PhantomData<T>);
196     }
197 
198     macro_rules! types {
199         ($mib:ty) => {
200             paste! {
201                 #[derive(Default)]
202                 pub struct S(pub keys::$mib);
203             }
204         };
205     }
206 
207     macro_rules! write {
208         ($fn:ident, $field:ty) => {
209             paste! {
210                 pub fn $fn() -> $field {
211                     $field::default()
212                 }
213             }
214         };
215     }
216 
217     types! {Mib<[usize; 2]>}
218     write! {get_a, keys::Mib}
219     write! {get_b, usize}
220 
221     #[test]
test_type_in_path()222     fn test_type_in_path() {
223         let _: S;
224         let _ = get_a;
225         let _ = get_b;
226     }
227 }
228 
229 mod test_type_in_fn_arg {
230     // https://github.com/dtolnay/paste/issues/38
231 
232     use paste::paste;
233 
_jit_address(_node: ())234     fn _jit_address(_node: ()) {}
235 
236     macro_rules! jit_reexport {
237         ($fn:ident, $arg:ident : $typ:ty) => {
238             paste! {
239                 pub fn $fn($arg: $typ) {
240                     [<_jit_ $fn>]($arg);
241                 }
242             }
243         };
244     }
245 
246     jit_reexport!(address, node: ());
247 
248     #[test]
test_type_in_fn_arg()249     fn test_type_in_fn_arg() {
250         let _ = address;
251     }
252 }
253 
254 mod test_pat_in_expr_position {
255     // https://github.com/xiph/rav1e/pull/2324/files
256 
257     use paste::paste;
258 
259     macro_rules! rav1e_bad {
260         ($e:pat) => {
261             paste! {
262                 #[test]
263                 fn test() {
264                     let _ = $e;
265                 }
266             }
267         };
268     }
269 
270     rav1e_bad!(std::fmt::Error);
271 }
272