]> git.lizzy.rs Git - rust.git/blob - src/libfmt_macros/tests.rs
Rollup merge of #66226 - lzutao:asm-usage-link, r=Centril
[rust.git] / src / libfmt_macros / tests.rs
1 use super::*;
2
3 fn same(fmt: &'static str, p: &[Piece<'static>]) {
4     let parser = Parser::new(fmt, None, vec![], false);
5     assert_eq!(parser.collect::<Vec<Piece<'static>>>(), p);
6 }
7
8 fn fmtdflt() -> FormatSpec<'static> {
9     return FormatSpec {
10         fill: None,
11         align: AlignUnknown,
12         flags: 0,
13         precision: CountImplied,
14         width: CountImplied,
15         precision_span: None,
16         width_span: None,
17         ty: "",
18         ty_span: None,
19     };
20 }
21
22 fn musterr(s: &str) {
23     let mut p = Parser::new(s, None, vec![], false);
24     p.next();
25     assert!(!p.errors.is_empty());
26 }
27
28 #[test]
29 fn simple() {
30     same("asdf", &[String("asdf")]);
31     same("a{{b", &[String("a"), String("{b")]);
32     same("a}}b", &[String("a"), String("}b")]);
33     same("a}}", &[String("a"), String("}")]);
34     same("}}", &[String("}")]);
35     same("\\}}", &[String("\\"), String("}")]);
36 }
37
38 #[test]
39 fn invalid01() {
40     musterr("{")
41 }
42 #[test]
43 fn invalid02() {
44     musterr("}")
45 }
46 #[test]
47 fn invalid04() {
48     musterr("{3a}")
49 }
50 #[test]
51 fn invalid05() {
52     musterr("{:|}")
53 }
54 #[test]
55 fn invalid06() {
56     musterr("{:>>>}")
57 }
58
59 #[test]
60 fn format_nothing() {
61     same("{}",
62          &[NextArgument(Argument {
63                position: ArgumentImplicitlyIs(0),
64                format: fmtdflt(),
65            })]);
66 }
67 #[test]
68 fn format_position() {
69     same("{3}",
70          &[NextArgument(Argument {
71                position: ArgumentIs(3),
72                format: fmtdflt(),
73            })]);
74 }
75 #[test]
76 fn format_position_nothing_else() {
77     same("{3:}",
78          &[NextArgument(Argument {
79                position: ArgumentIs(3),
80                format: fmtdflt(),
81            })]);
82 }
83 #[test]
84 fn format_type() {
85     same(
86         "{3:x}",
87         &[NextArgument(Argument {
88             position: ArgumentIs(3),
89             format: FormatSpec {
90                 fill: None,
91                 align: AlignUnknown,
92                 flags: 0,
93                 precision: CountImplied,
94                 width: CountImplied,
95                 precision_span: None,
96                 width_span: None,
97                 ty: "x",
98                 ty_span: None,
99             },
100         })]);
101 }
102 #[test]
103 fn format_align_fill() {
104     same(
105         "{3:>}",
106         &[NextArgument(Argument {
107             position: ArgumentIs(3),
108             format: FormatSpec {
109                 fill: None,
110                 align: AlignRight,
111                 flags: 0,
112                 precision: CountImplied,
113                 width: CountImplied,
114                 precision_span: None,
115                 width_span: None,
116                 ty: "",
117                 ty_span: None,
118             },
119         })]);
120     same(
121         "{3:0<}",
122         &[NextArgument(Argument {
123             position: ArgumentIs(3),
124             format: FormatSpec {
125                 fill: Some('0'),
126                 align: AlignLeft,
127                 flags: 0,
128                 precision: CountImplied,
129                 width: CountImplied,
130                 precision_span: None,
131                 width_span: None,
132                 ty: "",
133                 ty_span: None,
134             },
135         })]);
136     same(
137         "{3:*<abcd}",
138         &[NextArgument(Argument {
139             position: ArgumentIs(3),
140             format: FormatSpec {
141                 fill: Some('*'),
142                 align: AlignLeft,
143                 flags: 0,
144                 precision: CountImplied,
145                 width: CountImplied,
146                 precision_span: None,
147                 width_span: None,
148                 ty: "abcd",
149                 ty_span: Some(InnerSpan::new(6, 10)),
150             },
151         })]);
152 }
153 #[test]
154 fn format_counts() {
155     use syntax_pos::{GLOBALS, Globals, edition};
156     GLOBALS.set(&Globals::new(edition::DEFAULT_EDITION), || {
157     same(
158         "{:10x}",
159         &[NextArgument(Argument {
160             position: ArgumentImplicitlyIs(0),
161             format: FormatSpec {
162                 fill: None,
163                 align: AlignUnknown,
164                 flags: 0,
165                 precision: CountImplied,
166                 width: CountIs(10),
167                 precision_span: None,
168                 width_span: None,
169                 ty: "x",
170                 ty_span: None,
171             },
172         })]);
173     same(
174         "{:10$.10x}",
175         &[NextArgument(Argument {
176             position: ArgumentImplicitlyIs(0),
177             format: FormatSpec {
178                 fill: None,
179                 align: AlignUnknown,
180                 flags: 0,
181                 precision: CountIs(10),
182                 width: CountIsParam(10),
183                 precision_span: None,
184                 width_span: Some(InnerSpan::new(3, 6)),
185                 ty: "x",
186                 ty_span: None,
187             },
188         })]);
189     same(
190         "{:.*x}",
191         &[NextArgument(Argument {
192             position: ArgumentImplicitlyIs(1),
193             format: FormatSpec {
194                 fill: None,
195                 align: AlignUnknown,
196                 flags: 0,
197                 precision: CountIsParam(0),
198                 width: CountImplied,
199                 precision_span: Some(InnerSpan::new(3, 5)),
200                 width_span: None,
201                 ty: "x",
202                 ty_span: None,
203             },
204         })]);
205     same(
206         "{:.10$x}",
207         &[NextArgument(Argument {
208             position: ArgumentImplicitlyIs(0),
209             format: FormatSpec {
210                 fill: None,
211                 align: AlignUnknown,
212                 flags: 0,
213                 precision: CountIsParam(10),
214                 width: CountImplied,
215                 precision_span: Some(InnerSpan::new(3, 7)),
216                 width_span: None,
217                 ty: "x",
218                 ty_span: None,
219             },
220         })]);
221     same(
222         "{:a$.b$?}",
223         &[NextArgument(Argument {
224             position: ArgumentImplicitlyIs(0),
225             format: FormatSpec {
226                 fill: None,
227                 align: AlignUnknown,
228                 flags: 0,
229                 precision: CountIsName(Symbol::intern("b")),
230                 width: CountIsName(Symbol::intern("a")),
231                 precision_span: None,
232                 width_span: None,
233                 ty: "?",
234                 ty_span: None,
235             },
236         })]);
237     });
238 }
239 #[test]
240 fn format_flags() {
241     same(
242         "{:-}",
243         &[NextArgument(Argument {
244             position: ArgumentImplicitlyIs(0),
245             format: FormatSpec {
246                 fill: None,
247                 align: AlignUnknown,
248                 flags: (1 << FlagSignMinus as u32),
249                 precision: CountImplied,
250                 width: CountImplied,
251                 precision_span: None,
252                 width_span: None,
253                 ty: "",
254                 ty_span: None,
255             },
256         })]);
257     same(
258         "{:+#}",
259         &[NextArgument(Argument {
260             position: ArgumentImplicitlyIs(0),
261             format: FormatSpec {
262                 fill: None,
263                 align: AlignUnknown,
264                 flags: (1 << FlagSignPlus as u32) | (1 << FlagAlternate as u32),
265                 precision: CountImplied,
266                 width: CountImplied,
267                 precision_span: None,
268                 width_span: None,
269                 ty: "",
270                 ty_span: None,
271             },
272         })]);
273 }
274 #[test]
275 fn format_mixture() {
276     same(
277         "abcd {3:x} efg",
278         &[
279             String("abcd "),
280             NextArgument(Argument {
281                 position: ArgumentIs(3),
282                 format: FormatSpec {
283                     fill: None,
284                     align: AlignUnknown,
285                     flags: 0,
286                     precision: CountImplied,
287                     width: CountImplied,
288                     precision_span: None,
289                     width_span: None,
290                     ty: "x",
291                     ty_span: None,
292                 },
293             }),
294             String(" efg"),
295         ],
296     );
297 }