]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_parse_format/src/tests.rs
Rollup merge of #96658 - Folyd:es6, r=GuillaumeGomez
[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     same(
148         "{:10x}",
149         &[NextArgument(Argument {
150             position: ArgumentImplicitlyIs(0),
151             format: FormatSpec {
152                 fill: None,
153                 align: AlignUnknown,
154                 flags: 0,
155                 precision: CountImplied,
156                 width: CountIs(10),
157                 precision_span: None,
158                 width_span: None,
159                 ty: "x",
160                 ty_span: None,
161             },
162         })],
163     );
164     same(
165         "{:10$.10x}",
166         &[NextArgument(Argument {
167             position: ArgumentImplicitlyIs(0),
168             format: FormatSpec {
169                 fill: None,
170                 align: AlignUnknown,
171                 flags: 0,
172                 precision: CountIs(10),
173                 width: CountIsParam(10),
174                 precision_span: None,
175                 width_span: Some(InnerSpan::new(3, 6)),
176                 ty: "x",
177                 ty_span: None,
178             },
179         })],
180     );
181     same(
182         "{:.*x}",
183         &[NextArgument(Argument {
184             position: ArgumentImplicitlyIs(1),
185             format: FormatSpec {
186                 fill: None,
187                 align: AlignUnknown,
188                 flags: 0,
189                 precision: CountIsParam(0),
190                 width: CountImplied,
191                 precision_span: Some(InnerSpan::new(3, 5)),
192                 width_span: None,
193                 ty: "x",
194                 ty_span: None,
195             },
196         })],
197     );
198     same(
199         "{:.10$x}",
200         &[NextArgument(Argument {
201             position: ArgumentImplicitlyIs(0),
202             format: FormatSpec {
203                 fill: None,
204                 align: AlignUnknown,
205                 flags: 0,
206                 precision: CountIsParam(10),
207                 width: CountImplied,
208                 precision_span: Some(InnerSpan::new(3, 7)),
209                 width_span: None,
210                 ty: "x",
211                 ty_span: None,
212             },
213         })],
214     );
215     same(
216         "{:a$.b$?}",
217         &[NextArgument(Argument {
218             position: ArgumentImplicitlyIs(0),
219             format: FormatSpec {
220                 fill: None,
221                 align: AlignUnknown,
222                 flags: 0,
223                 precision: CountIsName("b", InnerSpan::new(6, 7)),
224                 width: CountIsName("a", InnerSpan::new(4, 4)),
225                 precision_span: None,
226                 width_span: None,
227                 ty: "?",
228                 ty_span: None,
229             },
230         })],
231     );
232 }
233 #[test]
234 fn format_flags() {
235     same(
236         "{:-}",
237         &[NextArgument(Argument {
238             position: ArgumentImplicitlyIs(0),
239             format: FormatSpec {
240                 fill: None,
241                 align: AlignUnknown,
242                 flags: (1 << FlagSignMinus as u32),
243                 precision: CountImplied,
244                 width: CountImplied,
245                 precision_span: None,
246                 width_span: None,
247                 ty: "",
248                 ty_span: None,
249             },
250         })],
251     );
252     same(
253         "{:+#}",
254         &[NextArgument(Argument {
255             position: ArgumentImplicitlyIs(0),
256             format: FormatSpec {
257                 fill: None,
258                 align: AlignUnknown,
259                 flags: (1 << FlagSignPlus as u32) | (1 << FlagAlternate as u32),
260                 precision: CountImplied,
261                 width: CountImplied,
262                 precision_span: None,
263                 width_span: None,
264                 ty: "",
265                 ty_span: None,
266             },
267         })],
268     );
269 }
270 #[test]
271 fn format_mixture() {
272     same(
273         "abcd {3:x} efg",
274         &[
275             String("abcd "),
276             NextArgument(Argument {
277                 position: ArgumentIs(3),
278                 format: FormatSpec {
279                     fill: None,
280                     align: AlignUnknown,
281                     flags: 0,
282                     precision: CountImplied,
283                     width: CountImplied,
284                     precision_span: None,
285                     width_span: None,
286                     ty: "x",
287                     ty_span: None,
288                 },
289             }),
290             String(" efg"),
291         ],
292     );
293 }