]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_parse_format/src/tests.rs
Auto merge of #89130 - nikic:update-llvm-2, r=cuviper
[rust.git] / compiler / rustc_parse_format / src / tests.rs
1 use super::*;
2
3 fn same(fmt: &'static str, p: &[Piece<'static>]) {
4     let parser = Parser::new(fmt, None, None, false, ParseMode::Format);
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, None, false, ParseMode::Format);
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("{}", &[NextArgument(Argument { position: ArgumentImplicitlyIs(0), format: fmtdflt() })]);
62 }
63 #[test]
64 fn format_position() {
65     same("{3}", &[NextArgument(Argument { position: ArgumentIs(3), format: fmtdflt() })]);
66 }
67 #[test]
68 fn format_position_nothing_else() {
69     same("{3:}", &[NextArgument(Argument { position: ArgumentIs(3), format: fmtdflt() })]);
70 }
71 #[test]
72 fn format_type() {
73     same(
74         "{3:x}",
75         &[NextArgument(Argument {
76             position: ArgumentIs(3),
77             format: FormatSpec {
78                 fill: None,
79                 align: AlignUnknown,
80                 flags: 0,
81                 precision: CountImplied,
82                 width: CountImplied,
83                 precision_span: None,
84                 width_span: None,
85                 ty: "x",
86                 ty_span: None,
87             },
88         })],
89     );
90 }
91 #[test]
92 fn format_align_fill() {
93     same(
94         "{3:>}",
95         &[NextArgument(Argument {
96             position: ArgumentIs(3),
97             format: FormatSpec {
98                 fill: None,
99                 align: AlignRight,
100                 flags: 0,
101                 precision: CountImplied,
102                 width: CountImplied,
103                 precision_span: None,
104                 width_span: None,
105                 ty: "",
106                 ty_span: None,
107             },
108         })],
109     );
110     same(
111         "{3:0<}",
112         &[NextArgument(Argument {
113             position: ArgumentIs(3),
114             format: FormatSpec {
115                 fill: Some('0'),
116                 align: AlignLeft,
117                 flags: 0,
118                 precision: CountImplied,
119                 width: CountImplied,
120                 precision_span: None,
121                 width_span: None,
122                 ty: "",
123                 ty_span: None,
124             },
125         })],
126     );
127     same(
128         "{3:*<abcd}",
129         &[NextArgument(Argument {
130             position: ArgumentIs(3),
131             format: FormatSpec {
132                 fill: Some('*'),
133                 align: AlignLeft,
134                 flags: 0,
135                 precision: CountImplied,
136                 width: CountImplied,
137                 precision_span: None,
138                 width_span: None,
139                 ty: "abcd",
140                 ty_span: Some(InnerSpan::new(6, 10)),
141             },
142         })],
143     );
144 }
145 #[test]
146 fn format_counts() {
147     rustc_span::create_default_session_globals_then(|| {
148         same(
149             "{:10x}",
150             &[NextArgument(Argument {
151                 position: ArgumentImplicitlyIs(0),
152                 format: FormatSpec {
153                     fill: None,
154                     align: AlignUnknown,
155                     flags: 0,
156                     precision: CountImplied,
157                     width: CountIs(10),
158                     precision_span: None,
159                     width_span: None,
160                     ty: "x",
161                     ty_span: None,
162                 },
163             })],
164         );
165         same(
166             "{:10$.10x}",
167             &[NextArgument(Argument {
168                 position: ArgumentImplicitlyIs(0),
169                 format: FormatSpec {
170                     fill: None,
171                     align: AlignUnknown,
172                     flags: 0,
173                     precision: CountIs(10),
174                     width: CountIsParam(10),
175                     precision_span: None,
176                     width_span: Some(InnerSpan::new(3, 6)),
177                     ty: "x",
178                     ty_span: None,
179                 },
180             })],
181         );
182         same(
183             "{:.*x}",
184             &[NextArgument(Argument {
185                 position: ArgumentImplicitlyIs(1),
186                 format: FormatSpec {
187                     fill: None,
188                     align: AlignUnknown,
189                     flags: 0,
190                     precision: CountIsParam(0),
191                     width: CountImplied,
192                     precision_span: Some(InnerSpan::new(3, 5)),
193                     width_span: None,
194                     ty: "x",
195                     ty_span: None,
196                 },
197             })],
198         );
199         same(
200             "{:.10$x}",
201             &[NextArgument(Argument {
202                 position: ArgumentImplicitlyIs(0),
203                 format: FormatSpec {
204                     fill: None,
205                     align: AlignUnknown,
206                     flags: 0,
207                     precision: CountIsParam(10),
208                     width: CountImplied,
209                     precision_span: Some(InnerSpan::new(3, 7)),
210                     width_span: None,
211                     ty: "x",
212                     ty_span: None,
213                 },
214             })],
215         );
216         same(
217             "{:a$.b$?}",
218             &[NextArgument(Argument {
219                 position: ArgumentImplicitlyIs(0),
220                 format: FormatSpec {
221                     fill: None,
222                     align: AlignUnknown,
223                     flags: 0,
224                     precision: CountIsName(Symbol::intern("b")),
225                     width: CountIsName(Symbol::intern("a")),
226                     precision_span: None,
227                     width_span: None,
228                     ty: "?",
229                     ty_span: None,
230                 },
231             })],
232         );
233     });
234 }
235 #[test]
236 fn format_flags() {
237     same(
238         "{:-}",
239         &[NextArgument(Argument {
240             position: ArgumentImplicitlyIs(0),
241             format: FormatSpec {
242                 fill: None,
243                 align: AlignUnknown,
244                 flags: (1 << FlagSignMinus as u32),
245                 precision: CountImplied,
246                 width: CountImplied,
247                 precision_span: None,
248                 width_span: None,
249                 ty: "",
250                 ty_span: None,
251             },
252         })],
253     );
254     same(
255         "{:+#}",
256         &[NextArgument(Argument {
257             position: ArgumentImplicitlyIs(0),
258             format: FormatSpec {
259                 fill: None,
260                 align: AlignUnknown,
261                 flags: (1 << FlagSignPlus as u32) | (1 << FlagAlternate as u32),
262                 precision: CountImplied,
263                 width: CountImplied,
264                 precision_span: None,
265                 width_span: None,
266                 ty: "",
267                 ty_span: None,
268             },
269         })],
270     );
271 }
272 #[test]
273 fn format_mixture() {
274     same(
275         "abcd {3:x} efg",
276         &[
277             String("abcd "),
278             NextArgument(Argument {
279                 position: ArgumentIs(3),
280                 format: FormatSpec {
281                     fill: None,
282                     align: AlignUnknown,
283                     flags: 0,
284                     precision: CountImplied,
285                     width: CountImplied,
286                     precision_span: None,
287                     width_span: None,
288                     ty: "x",
289                     ty_span: None,
290                 },
291             }),
292             String(" efg"),
293         ],
294     );
295 }