This repository has been archived by the owner on Oct 6, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
test.rs
327 lines (268 loc) · 5.57 KB
/
test.rs
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
mod test_basic {
struct Struct;
paste::item! {
impl Struct {
fn [<a b c>]() {}
}
}
#[test]
fn test() {
Struct::abc();
}
}
mod test_in_impl {
struct Struct;
impl Struct {
paste::item! {
fn [<a b c>]() {}
}
}
#[test]
fn test() {
Struct::abc();
}
}
#[test]
fn test_shared_hygiene() {
paste::expr! {
let [<a a>] = 1;
assert_eq!([<a a>], 1);
}
}
#[test]
fn test_repeat() {
const ROCKET_A: &'static str = "/a";
const ROCKET_B: &'static str = "/b";
macro_rules! routes {
($($route:ident),*) => {{
paste::expr! {
vec![$( [<ROCKET_ $route>] ),*]
}
}}
}
let routes = routes!(A, B);
assert_eq!(routes, vec!["/a", "/b"]);
}
#[test]
fn test_integer() {
const CONST0: &'static str = "const0";
let pasted = paste::expr!([<CONST 0>]);
assert_eq!(pasted, CONST0);
}
#[test]
fn test_underscore() {
paste::expr! {
const A_B: usize = 0;
assert_eq!([<A _ B>], 0);
}
}
#[test]
fn test_lifetime() {
paste::expr! {
#[allow(dead_code)]
struct S<[<'d e>]> {
q: &[<'d e>] str,
}
}
}
#[test]
fn test_keyword() {
paste::expr! {
struct [<F move>];
let _ = Fmove;
}
}
#[test]
fn test_literal_str() {
paste::expr! {
struct [<Foo "Bar-Baz">];
let _ = FooBar_Baz;
}
}
#[test]
fn test_env_literal() {
paste::expr! {
struct [<Lib env bar>];
let _ = Libenvbar;
}
}
#[test]
fn test_env_present() {
paste::expr! {
struct [<Lib env!("CARGO_PKG_NAME")>];
let _ = Libpaste;
}
}
#[test]
fn test_raw_identifier() {
paste::expr! {
struct [<F r#move>];
let _ = Fmove;
}
}
#[test]
fn test_false_start() {
trait Trait {
fn f() -> usize;
}
struct S;
impl Trait for S {
fn f() -> usize {
0
}
}
paste::expr! {
let x = [<S as Trait>::f()];
assert_eq!(x[0], 0);
}
}
#[test]
fn test_local_variable() {
let yy = 0;
paste::expr! {
assert_eq!([<y y>], 0);
}
}
mod test_none_delimited_single_ident {
macro_rules! m {
($id:ident) => {
paste::item! {
fn f() -> &'static str {
stringify!($id)
}
}
};
}
m!(i32x4);
#[test]
fn test() {
assert_eq!(f(), "i32x4");
}
}
mod test_none_delimited_single_lifetime {
macro_rules! m {
($life:lifetime) => {
paste::item! {
pub struct S;
impl<$life> S {
fn f() {}
}
}
};
}
m!('a);
#[test]
fn test() {
S::f();
}
}
mod test_to_lower {
macro_rules! m {
($id:ident) => {
paste::item! {
fn [<my_ $id:lower _here>](_arg: u8) -> &'static str {
stringify!([<$id:lower>])
}
}
};
}
m!(Test);
#[test]
fn test_to_lower() {
assert_eq!(my_test_here(0), "test");
}
}
#[test]
fn test_env_to_lower() {
paste::expr! {
struct [<Lib env!("CARGO_PKG_NAME"):lower>];
let _ = Libpaste;
}
}
mod test_to_upper {
macro_rules! m {
($id:ident) => {
paste::item! {
const [<MY_ $id:upper _HERE>]: &str = stringify!([<$id:upper>]);
}
};
}
m!(Test);
#[test]
fn test_to_upper() {
assert_eq!(MY_TEST_HERE, "TEST");
}
}
#[test]
fn test_env_to_upper() {
paste::expr! {
const [<LIB env!("CARGO_PKG_NAME"):upper>]: &str = "libpaste";
let _ = LIBPASTE;
}
}
mod test_to_snake {
macro_rules! m {
($id:ident) => {
paste::item! {
const DEFAULT_SNAKE: &str = stringify!([<$id:snake>]);
const LOWER_SNAKE: &str = stringify!([<$id:snake:lower>]);
const UPPER_SNAKE: &str = stringify!([<$id:snake:upper>]);
}
};
}
m!(ThisIsButATest);
#[test]
fn test_to_snake() {
assert_eq!(DEFAULT_SNAKE, "this_is_but_a_test");
assert_eq!(LOWER_SNAKE, "this_is_but_a_test");
assert_eq!(UPPER_SNAKE, "THIS_IS_BUT_A_TEST");
}
}
#[test]
fn test_env_to_snake() {
paste::expr! {
const [<LIB env!("CARGO_PKG_NAME"):snake:upper>]: &str = "libpaste";
let _ = LIBPASTE;
}
}
mod test_to_camel {
macro_rules! m {
($id:ident) => {
paste::item! {
const DEFAULT_CAMEL: &str = stringify!([<$id:camel>]);
const LOWER_CAMEL: &str = stringify!([<$id:camel:lower>]);
const UPPER_CAMEL: &str = stringify!([<$id:camel:upper>]);
}
};
}
m!(this_is_but_a_test);
#[test]
fn test_to_camel() {
assert_eq!(DEFAULT_CAMEL, "ThisIsButATest");
assert_eq!(LOWER_CAMEL, "thisisbutatest");
assert_eq!(UPPER_CAMEL, "THISISBUTATEST");
}
}
#[test]
fn test_env_to_camel() {
paste::expr! {
const [<LIB env!("CARGO_PKG_NAME"):camel>]: &str = "libpaste";
let _ = LIBPaste;
}
}
mod test_doc_expr {
// https://github.com/dtolnay/paste/issues/29
macro_rules! doc_expr {
($doc:expr) => {
paste::item! {
#[doc = $doc]
pub struct S;
}
};
}
doc_expr!(stringify!());
#[test]
fn test_doc_expr() {
let _: S;
}
}