1 use paste::paste;
2
3 #[test]
test_shared_hygiene()4 fn test_shared_hygiene() {
5 paste! {
6 let [<a a>] = 1;
7 assert_eq!([<a a>], 1);
8 }
9 }
10
11 #[test]
test_repeat()12 fn test_repeat() {
13 const ROCKET_A: &'static str = "/a";
14 const ROCKET_B: &'static str = "/b";
15
16 macro_rules! routes {
17 ($($route:ident),*) => {{
18 paste! {
19 vec![$( [<ROCKET_ $route>] ),*]
20 }
21 }}
22 }
23
24 let routes = routes!(A, B);
25 assert_eq!(routes, vec!["/a", "/b"]);
26 }
27
28 #[test]
test_integer()29 fn test_integer() {
30 const CONST0: &'static str = "const0";
31
32 let pasted = paste!([<CONST 0>]);
33 assert_eq!(pasted, CONST0);
34 }
35
36 #[test]
test_underscore()37 fn test_underscore() {
38 paste! {
39 const A_B: usize = 0;
40 assert_eq!([<A _ B>], 0);
41 }
42 }
43
44 #[test]
test_lifetime()45 fn test_lifetime() {
46 paste! {
47 #[allow(dead_code)]
48 struct S<[<'d e>]> {
49 q: &[<'d e>] str,
50 }
51 }
52 }
53
54 #[test]
test_keyword()55 fn test_keyword() {
56 paste! {
57 struct [<F move>];
58
59 let _ = Fmove;
60 }
61 }
62
63 #[test]
test_literal_str()64 fn test_literal_str() {
65 paste! {
66 #[allow(non_camel_case_types)]
67 struct [<Foo "Bar-Baz">];
68
69 let _ = FooBar_Baz;
70 }
71 }
72
73 #[test]
test_env_literal()74 fn test_env_literal() {
75 paste! {
76 struct [<Lib env bar>];
77
78 let _ = Libenvbar;
79 }
80 }
81
82 #[test]
test_env_present()83 fn test_env_present() {
84 paste! {
85 struct [<Lib "paste">];
86
87 let _ = Libpaste;
88 }
89 }
90
91 #[test]
test_raw_identifier()92 fn test_raw_identifier() {
93 paste! {
94 struct [<F r#move>];
95
96 let _ = Fmove;
97 }
98 }
99
100 #[test]
test_false_start()101 fn test_false_start() {
102 trait Trait {
103 fn f() -> usize;
104 }
105
106 struct S;
107
108 impl Trait for S {
109 fn f() -> usize {
110 0
111 }
112 }
113
114 paste! {
115 let x = [<S as Trait>::f()];
116 assert_eq!(x[0], 0);
117 }
118 }
119
120 #[test]
test_local_variable()121 fn test_local_variable() {
122 let yy = 0;
123
124 paste! {
125 assert_eq!([<y y>], 0);
126 }
127 }
128
129 #[test]
test_empty()130 fn test_empty() {
131 paste! {
132 assert_eq!(stringify!([<y y>]), "yy");
133 assert_eq!(stringify!([<>]).replace(' ', ""), "[<>]");
134 }
135 }
136
137 #[test]
test_env_to_lower()138 fn test_env_to_lower() {
139 paste! {
140 struct [<Lib "paste":lower>];
141
142 let _ = Libpaste;
143 }
144 }
145
146 #[test]
test_env_to_upper()147 fn test_env_to_upper() {
148 paste! {
149 const [<LIB "paste":upper>]: &str = "libpaste";
150
151 let _ = LIBPASTE;
152 }
153 }
154
155 #[test]
test_env_to_snake()156 fn test_env_to_snake() {
157 paste! {
158 const [<LIB "paste":snake:upper>]: &str = "libpaste";
159
160 let _ = LIBPASTE;
161 }
162 }
163
164 #[test]
test_env_to_camel()165 fn test_env_to_camel() {
166 paste! {
167 #[allow(non_upper_case_globals)]
168 const [<LIB "paste":camel>]: &str = "libpaste";
169
170 let _ = LIBPaste;
171 }
172 }
173
174 mod test_x86_feature_literal {
175 // work around https://github.com/rust-lang/rust/issues/72726
176
177 use paste::paste;
178
179 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
180 macro_rules! my_is_x86_feature_detected {
181 ($feat:literal) => {
182 paste! {
183 #[test]
184 fn test() {
185 let _ = is_x86_feature_detected!($feat);
186 }
187 }
188 };
189 }
190
191 #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
192 macro_rules! my_is_x86_feature_detected {
193 ($feat:literal) => {
194 #[ignore]
195 #[test]
196 fn test() {}
197 };
198 }
199
200 my_is_x86_feature_detected!("mmx");
201 }
202
203 #[rustversion::since(1.46)]
204 mod test_local_setter {
205 // https://github.com/dtolnay/paste/issues/7
206
207 use paste::paste;
208
209 #[derive(Default)]
210 struct Test {
211 val: i32,
212 }
213
214 impl Test {
set_val(&mut self, arg: i32)215 fn set_val(&mut self, arg: i32) {
216 self.val = arg;
217 }
218 }
219
220 macro_rules! setter {
221 ($obj:expr, $field:ident, $value:expr) => {
222 paste! { $obj.[<set_ $field>]($value); }
223 };
224
225 ($field:ident, $value:expr) => {{
226 let mut new = Test::default();
227 setter!(new, val, $value);
228 new
229 }};
230 }
231
232 #[test]
test_local_setter()233 fn test_local_setter() {
234 let a = setter!(val, 42);
235 assert_eq!(a.val, 42);
236 }
237 }
238