]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/macros/macro-comma-support.rs
Rollup merge of #60959 - petrochenkov:sassert, r=estebank
[rust.git] / src / test / run-pass / macros / macro-comma-support.rs
1 // run-pass
2 // This is meant to be a comprehensive test of invocations with/without
3 // trailing commas (or other, similar optionally-trailing separators).
4 // Every macro is accounted for, even those not tested in this file.
5 // (There will be a note indicating why).
6
7 // std and core are both tested because they may contain separate
8 // implementations for some macro_rules! macros as an implementation
9 // detail.
10
11 // ignore-pretty issue #37195
12
13 // compile-flags: --test -C debug_assertions=yes
14 // revisions: std core
15
16 #![cfg_attr(core, no_std)]
17
18 #![feature(concat_idents)]
19
20 #[cfg(std)] use std::fmt;
21 #[cfg(core)] use core::fmt;
22
23 #[test]
24 fn assert() {
25     assert!(true);
26     assert!(true,);
27     assert!(true, "hello");
28     assert!(true, "hello",);
29     assert!(true, "hello {}", "world");
30     assert!(true, "hello {}", "world",);
31 }
32
33 #[test]
34 fn assert_eq() {
35     assert_eq!(1, 1);
36     assert_eq!(1, 1,);
37     assert_eq!(1, 1, "hello");
38     assert_eq!(1, 1, "hello",);
39     assert_eq!(1, 1, "hello {}", "world");
40     assert_eq!(1, 1, "hello {}", "world",);
41 }
42
43 #[test]
44 fn assert_ne() {
45     assert_ne!(1, 2);
46     assert_ne!(1, 2,);
47     assert_ne!(1, 2, "hello");
48     assert_ne!(1, 2, "hello",);
49     assert_ne!(1, 2, "hello {}", "world");
50     assert_ne!(1, 2, "hello {}", "world",);
51 }
52
53 #[test]
54 fn cfg() {
55     let _ = cfg!(pants);
56     let _ = cfg!(pants,);
57     let _ = cfg!(pants = "pants");
58     let _ = cfg!(pants = "pants",);
59     let _ = cfg!(all(pants));
60     let _ = cfg!(all(pants),);
61     let _ = cfg!(all(pants,));
62     let _ = cfg!(all(pants,),);
63 }
64
65 #[test]
66 fn column() {
67     let _ = column!();
68 }
69
70 // compile_error! is in a companion to this test in compile-fail
71
72 #[test]
73 fn concat() {
74     let _ = concat!();
75     let _ = concat!("hello");
76     let _ = concat!("hello",);
77     let _ = concat!("hello", " world");
78     let _ = concat!("hello", " world",);
79 }
80
81 #[test]
82 fn concat_idents() {
83     fn foo() {}
84     fn foobar() {}
85
86     concat_idents!(foo)();
87     concat_idents!(foo,)();
88     concat_idents!(foo, bar)();
89     concat_idents!(foo, bar,)();
90 }
91
92 #[test]
93 fn debug_assert() {
94     debug_assert!(true);
95     debug_assert!(true, );
96     debug_assert!(true, "hello");
97     debug_assert!(true, "hello",);
98     debug_assert!(true, "hello {}", "world");
99     debug_assert!(true, "hello {}", "world",);
100 }
101
102 #[test]
103 fn debug_assert_eq() {
104     debug_assert_eq!(1, 1);
105     debug_assert_eq!(1, 1,);
106     debug_assert_eq!(1, 1, "hello");
107     debug_assert_eq!(1, 1, "hello",);
108     debug_assert_eq!(1, 1, "hello {}", "world");
109     debug_assert_eq!(1, 1, "hello {}", "world",);
110 }
111
112 #[test]
113 fn debug_assert_ne() {
114     debug_assert_ne!(1, 2);
115     debug_assert_ne!(1, 2,);
116     debug_assert_ne!(1, 2, "hello");
117     debug_assert_ne!(1, 2, "hello",);
118     debug_assert_ne!(1, 2, "hello {}", "world");
119     debug_assert_ne!(1, 2, "hello {}", "world",);
120 }
121
122 #[test]
123 fn env() {
124     let _ = env!("PATH");
125     let _ = env!("PATH",);
126     let _ = env!("PATH", "not found");
127     let _ = env!("PATH", "not found",);
128 }
129
130 #[cfg(std)]
131 #[test]
132 fn eprint() {
133     eprint!("hello");
134     eprint!("hello",);
135     eprint!("hello {}", "world");
136     eprint!("hello {}", "world",);
137 }
138
139 #[cfg(std)]
140 #[test]
141 fn eprintln() {
142     eprintln!();
143     eprintln!("hello");
144     eprintln!("hello",);
145     eprintln!("hello {}", "world");
146     eprintln!("hello {}", "world",);
147 }
148
149 #[test]
150 fn file() {
151     let _ = file!();
152 }
153
154 #[cfg(std)]
155 #[test]
156 fn format() {
157     let _ = format!("hello");
158     let _ = format!("hello",);
159     let _ = format!("hello {}", "world");
160     let _ = format!("hello {}", "world",);
161 }
162
163 #[test]
164 fn format_args() {
165     let _ = format_args!("hello");
166     let _ = format_args!("hello",);
167     let _ = format_args!("hello {}", "world");
168     let _ = format_args!("hello {}", "world",);
169 }
170
171 #[test]
172 fn include() {
173     let _ = include!("auxiliary/macro-comma-support.rs");
174     let _ = include!("auxiliary/macro-comma-support.rs",);
175 }
176
177 #[test]
178 fn include_bytes() {
179     let _ = include_bytes!("auxiliary/macro-comma-support.rs");
180     let _ = include_bytes!("auxiliary/macro-comma-support.rs",);
181 }
182
183 #[test]
184 fn include_str() {
185     let _ = include_str!("auxiliary/macro-comma-support.rs");
186     let _ = include_str!("auxiliary/macro-comma-support.rs",);
187 }
188
189 #[test]
190 fn line() {
191     let _ = line!();
192 }
193
194 #[test]
195 fn module_path() {
196     let _ = module_path!();
197 }
198
199 #[test]
200 fn option_env() {
201     let _ = option_env!("PATH");
202     let _ = option_env!("PATH",);
203 }
204
205 #[test]
206 fn panic() {
207     // prevent 'unreachable code' warnings
208     let falsum = || false;
209
210     if falsum() { panic!(); }
211     if falsum() { panic!("hello"); }
212     if falsum() { panic!("hello",); }
213     if falsum() { panic!("hello {}", "world"); }
214     if falsum() { panic!("hello {}", "world",); }
215 }
216
217 #[cfg(std)]
218 #[test]
219 fn print() {
220     print!("hello");
221     print!("hello",);
222     print!("hello {}", "world");
223     print!("hello {}", "world",);
224 }
225
226 #[cfg(std)]
227 #[test]
228 fn println() {
229     println!();
230     println!("hello");
231     println!("hello",);
232     println!("hello {}", "world");
233     println!("hello {}", "world",);
234 }
235
236 // stringify! is N/A
237
238 #[cfg(std)]
239 #[test]
240 fn thread_local() {
241     // this has an optional trailing *semicolon*
242     thread_local! {
243         #[allow(unused)] pub static A: () = ()
244     }
245
246     thread_local! {
247         #[allow(unused)] pub static AA: () = ();
248     }
249
250     thread_local! {
251         #[allow(unused)] pub static AAA: () = ();
252         #[allow(unused)] pub static AAAA: () = ()
253     }
254
255     thread_local! {
256         #[allow(unused)] pub static AAAAG: () = ();
257         #[allow(unused)] pub static AAAAGH: () = ();
258     }
259 }
260
261 #[test]
262 fn try() {
263     fn inner() -> Result<(), ()> {
264         try!(Ok(()));
265         try!(Ok(()),);
266         Ok(())
267     }
268
269     inner().unwrap();
270 }
271
272 #[test]
273 fn unimplemented() {
274     // prevent 'unreachable code' warnings
275     let falsum = || false;
276
277     if falsum() { unimplemented!(); }
278     if falsum() { unimplemented!("hello"); }
279     if falsum() { unimplemented!("hello",); }
280     if falsum() { unimplemented!("hello {}", "world"); }
281     if falsum() { unimplemented!("hello {}", "world",); }
282 }
283
284 #[test]
285 fn unreachable() {
286     // prevent 'unreachable code' warnings
287     let falsum = || false;
288
289     if falsum() { unreachable!(); }
290     if falsum() { unreachable!("hello"); }
291     if falsum() { unreachable!("hello",); }
292     if falsum() { unreachable!("hello {}", "world"); }
293     if falsum() { unreachable!("hello {}", "world",); }
294 }
295
296 #[cfg(std)]
297 #[test]
298 fn vec() {
299     let _: Vec<()> = vec![];
300     let _ = vec![0];
301     let _ = vec![0,];
302     let _ = vec![0, 1];
303     let _ = vec![0, 1,];
304 }
305
306 // give a test body access to a fmt::Formatter, which seems
307 // to be the easiest way to use 'write!' on core.
308 macro_rules! test_with_formatter {
309     (
310         #[test]
311         fn $fname:ident($f:ident: &mut fmt::Formatter) $block:block
312     ) => {
313         #[test]
314         fn $fname() {
315             struct Struct;
316             impl fmt::Display for Struct {
317                 fn fmt(&self, $f: &mut fmt::Formatter) -> fmt::Result {
318                     Ok($block)
319                 }
320             }
321
322             // suppress "unused"
323             assert!(true, "{}", Struct);
324         }
325     };
326 }
327
328 test_with_formatter! {
329     #[test]
330     fn write(f: &mut fmt::Formatter) {
331         let _ = write!(f, "hello");
332         let _ = write!(f, "hello",);
333         let _ = write!(f, "hello {}", "world");
334         let _ = write!(f, "hello {}", "world",);
335     }
336 }
337
338 test_with_formatter! {
339     #[test]
340     fn writeln(f: &mut fmt::Formatter) {
341         let _ = writeln!(f);
342         let _ = writeln!(f,);
343         let _ = writeln!(f, "hello");
344         let _ = writeln!(f, "hello",);
345         let _ = writeln!(f, "hello {}", "world");
346         let _ = writeln!(f, "hello {}", "world",);
347     }
348 }