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