]>
Commit | Line | Data |
---|---|---|
dc9dc135 XL |
1 | use super::*; |
2 | ||
f2b60f7d | 3 | #[track_caller] |
dc9dc135 | 4 | fn same(fmt: &'static str, p: &[Piece<'static>]) { |
f9f354fc | 5 | let parser = Parser::new(fmt, None, None, false, ParseMode::Format); |
60c5eb7d | 6 | assert_eq!(parser.collect::<Vec<Piece<'static>>>(), p); |
dc9dc135 XL |
7 | } |
8 | ||
9 | fn fmtdflt() -> FormatSpec<'static> { | |
10 | return FormatSpec { | |
11 | fill: None, | |
12 | align: AlignUnknown, | |
13 | flags: 0, | |
14 | precision: CountImplied, | |
15 | width: CountImplied, | |
416331ca XL |
16 | precision_span: None, |
17 | width_span: None, | |
dc9dc135 | 18 | ty: "", |
60c5eb7d | 19 | ty_span: None, |
dc9dc135 XL |
20 | }; |
21 | } | |
22 | ||
23 | fn musterr(s: &str) { | |
f9f354fc | 24 | let mut p = Parser::new(s, None, None, false, ParseMode::Format); |
dc9dc135 XL |
25 | p.next(); |
26 | assert!(!p.errors.is_empty()); | |
27 | } | |
28 | ||
29 | #[test] | |
30 | fn simple() { | |
31 | same("asdf", &[String("asdf")]); | |
32 | same("a{{b", &[String("a"), String("{b")]); | |
33 | same("a}}b", &[String("a"), String("}b")]); | |
34 | same("a}}", &[String("a"), String("}")]); | |
35 | same("}}", &[String("}")]); | |
36 | same("\\}}", &[String("\\"), String("}")]); | |
37 | } | |
38 | ||
39 | #[test] | |
40 | fn invalid01() { | |
41 | musterr("{") | |
42 | } | |
43 | #[test] | |
44 | fn invalid02() { | |
45 | musterr("}") | |
46 | } | |
47 | #[test] | |
48 | fn invalid04() { | |
49 | musterr("{3a}") | |
50 | } | |
51 | #[test] | |
52 | fn invalid05() { | |
53 | musterr("{:|}") | |
54 | } | |
55 | #[test] | |
56 | fn invalid06() { | |
57 | musterr("{:>>>}") | |
58 | } | |
59 | ||
2b03887a FG |
60 | #[test] |
61 | fn invalid_position() { | |
62 | musterr("{18446744073709551616}"); | |
63 | } | |
64 | ||
65 | #[test] | |
66 | fn invalid_width() { | |
67 | musterr("{:18446744073709551616}"); | |
68 | } | |
69 | ||
70 | #[test] | |
71 | fn invalid_precision() { | |
72 | musterr("{:.18446744073709551616}"); | |
73 | } | |
74 | ||
dc9dc135 XL |
75 | #[test] |
76 | fn format_nothing() { | |
064997fb FG |
77 | same( |
78 | "{}", | |
6522a427 | 79 | &[NextArgument(Box::new(Argument { |
064997fb FG |
80 | position: ArgumentImplicitlyIs(0), |
81 | position_span: InnerSpan { start: 2, end: 2 }, | |
82 | format: fmtdflt(), | |
6522a427 | 83 | }))], |
064997fb | 84 | ); |
dc9dc135 XL |
85 | } |
86 | #[test] | |
87 | fn format_position() { | |
064997fb FG |
88 | same( |
89 | "{3}", | |
6522a427 | 90 | &[NextArgument(Box::new(Argument { |
064997fb FG |
91 | position: ArgumentIs(3), |
92 | position_span: InnerSpan { start: 2, end: 3 }, | |
93 | format: fmtdflt(), | |
6522a427 | 94 | }))], |
064997fb | 95 | ); |
dc9dc135 XL |
96 | } |
97 | #[test] | |
98 | fn format_position_nothing_else() { | |
064997fb FG |
99 | same( |
100 | "{3:}", | |
6522a427 | 101 | &[NextArgument(Box::new(Argument { |
064997fb FG |
102 | position: ArgumentIs(3), |
103 | position_span: InnerSpan { start: 2, end: 3 }, | |
104 | format: fmtdflt(), | |
6522a427 | 105 | }))], |
064997fb FG |
106 | ); |
107 | } | |
108 | #[test] | |
109 | fn format_named() { | |
110 | same( | |
111 | "{name}", | |
6522a427 | 112 | &[NextArgument(Box::new(Argument { |
064997fb FG |
113 | position: ArgumentNamed("name"), |
114 | position_span: InnerSpan { start: 2, end: 6 }, | |
115 | format: fmtdflt(), | |
6522a427 | 116 | }))], |
064997fb | 117 | ) |
dc9dc135 XL |
118 | } |
119 | #[test] | |
120 | fn format_type() { | |
416331ca | 121 | same( |
60c5eb7d | 122 | "{3:x}", |
6522a427 | 123 | &[NextArgument(Box::new(Argument { |
416331ca | 124 | position: ArgumentIs(3), |
064997fb | 125 | position_span: InnerSpan { start: 2, end: 3 }, |
416331ca XL |
126 | format: FormatSpec { |
127 | fill: None, | |
128 | align: AlignUnknown, | |
129 | flags: 0, | |
130 | precision: CountImplied, | |
131 | width: CountImplied, | |
132 | precision_span: None, | |
133 | width_span: None, | |
60c5eb7d XL |
134 | ty: "x", |
135 | ty_span: None, | |
416331ca | 136 | }, |
6522a427 | 137 | }))], |
dfeec247 | 138 | ); |
dc9dc135 XL |
139 | } |
140 | #[test] | |
141 | fn format_align_fill() { | |
416331ca XL |
142 | same( |
143 | "{3:>}", | |
6522a427 | 144 | &[NextArgument(Box::new(Argument { |
416331ca | 145 | position: ArgumentIs(3), |
064997fb | 146 | position_span: InnerSpan { start: 2, end: 3 }, |
416331ca XL |
147 | format: FormatSpec { |
148 | fill: None, | |
149 | align: AlignRight, | |
150 | flags: 0, | |
151 | precision: CountImplied, | |
152 | width: CountImplied, | |
153 | precision_span: None, | |
154 | width_span: None, | |
155 | ty: "", | |
60c5eb7d | 156 | ty_span: None, |
416331ca | 157 | }, |
6522a427 | 158 | }))], |
dfeec247 | 159 | ); |
416331ca XL |
160 | same( |
161 | "{3:0<}", | |
6522a427 | 162 | &[NextArgument(Box::new(Argument { |
416331ca | 163 | position: ArgumentIs(3), |
064997fb | 164 | position_span: InnerSpan { start: 2, end: 3 }, |
416331ca XL |
165 | format: FormatSpec { |
166 | fill: Some('0'), | |
167 | align: AlignLeft, | |
168 | flags: 0, | |
169 | precision: CountImplied, | |
170 | width: CountImplied, | |
171 | precision_span: None, | |
172 | width_span: None, | |
173 | ty: "", | |
60c5eb7d | 174 | ty_span: None, |
416331ca | 175 | }, |
6522a427 | 176 | }))], |
dfeec247 | 177 | ); |
416331ca XL |
178 | same( |
179 | "{3:*<abcd}", | |
6522a427 | 180 | &[NextArgument(Box::new(Argument { |
416331ca | 181 | position: ArgumentIs(3), |
064997fb | 182 | position_span: InnerSpan { start: 2, end: 3 }, |
416331ca XL |
183 | format: FormatSpec { |
184 | fill: Some('*'), | |
185 | align: AlignLeft, | |
186 | flags: 0, | |
187 | precision: CountImplied, | |
188 | width: CountImplied, | |
189 | precision_span: None, | |
190 | width_span: None, | |
191 | ty: "abcd", | |
60c5eb7d | 192 | ty_span: Some(InnerSpan::new(6, 10)), |
416331ca | 193 | }, |
6522a427 | 194 | }))], |
dfeec247 | 195 | ); |
dc9dc135 XL |
196 | } |
197 | #[test] | |
198 | fn format_counts() { | |
04454e1e FG |
199 | same( |
200 | "{:10x}", | |
6522a427 | 201 | &[NextArgument(Box::new(Argument { |
04454e1e | 202 | position: ArgumentImplicitlyIs(0), |
064997fb | 203 | position_span: InnerSpan { start: 2, end: 2 }, |
04454e1e FG |
204 | format: FormatSpec { |
205 | fill: None, | |
206 | align: AlignUnknown, | |
207 | flags: 0, | |
208 | precision: CountImplied, | |
04454e1e | 209 | precision_span: None, |
f2b60f7d FG |
210 | width: CountIs(10), |
211 | width_span: Some(InnerSpan { start: 3, end: 5 }), | |
04454e1e FG |
212 | ty: "x", |
213 | ty_span: None, | |
214 | }, | |
6522a427 | 215 | }))], |
04454e1e FG |
216 | ); |
217 | same( | |
218 | "{:10$.10x}", | |
6522a427 | 219 | &[NextArgument(Box::new(Argument { |
04454e1e | 220 | position: ArgumentImplicitlyIs(0), |
064997fb | 221 | position_span: InnerSpan { start: 2, end: 2 }, |
04454e1e FG |
222 | format: FormatSpec { |
223 | fill: None, | |
224 | align: AlignUnknown, | |
225 | flags: 0, | |
226 | precision: CountIs(10), | |
f2b60f7d | 227 | precision_span: Some(InnerSpan { start: 6, end: 9 }), |
04454e1e | 228 | width: CountIsParam(10), |
f2b60f7d | 229 | width_span: Some(InnerSpan { start: 3, end: 6 }), |
04454e1e FG |
230 | ty: "x", |
231 | ty_span: None, | |
232 | }, | |
6522a427 | 233 | }))], |
04454e1e | 234 | ); |
064997fb FG |
235 | same( |
236 | "{1:0$.10x}", | |
6522a427 | 237 | &[NextArgument(Box::new(Argument { |
064997fb FG |
238 | position: ArgumentIs(1), |
239 | position_span: InnerSpan { start: 2, end: 3 }, | |
240 | format: FormatSpec { | |
241 | fill: None, | |
242 | align: AlignUnknown, | |
243 | flags: 0, | |
244 | precision: CountIs(10), | |
f2b60f7d | 245 | precision_span: Some(InnerSpan { start: 6, end: 9 }), |
064997fb | 246 | width: CountIsParam(0), |
f2b60f7d | 247 | width_span: Some(InnerSpan { start: 4, end: 6 }), |
064997fb FG |
248 | ty: "x", |
249 | ty_span: None, | |
250 | }, | |
6522a427 | 251 | }))], |
064997fb | 252 | ); |
04454e1e FG |
253 | same( |
254 | "{:.*x}", | |
6522a427 | 255 | &[NextArgument(Box::new(Argument { |
04454e1e | 256 | position: ArgumentImplicitlyIs(1), |
064997fb | 257 | position_span: InnerSpan { start: 2, end: 2 }, |
04454e1e FG |
258 | format: FormatSpec { |
259 | fill: None, | |
260 | align: AlignUnknown, | |
261 | flags: 0, | |
f2b60f7d FG |
262 | precision: CountIsStar(0), |
263 | precision_span: Some(InnerSpan { start: 3, end: 5 }), | |
04454e1e | 264 | width: CountImplied, |
04454e1e FG |
265 | width_span: None, |
266 | ty: "x", | |
267 | ty_span: None, | |
268 | }, | |
6522a427 | 269 | }))], |
04454e1e FG |
270 | ); |
271 | same( | |
272 | "{:.10$x}", | |
6522a427 | 273 | &[NextArgument(Box::new(Argument { |
04454e1e | 274 | position: ArgumentImplicitlyIs(0), |
064997fb | 275 | position_span: InnerSpan { start: 2, end: 2 }, |
04454e1e FG |
276 | format: FormatSpec { |
277 | fill: None, | |
278 | align: AlignUnknown, | |
279 | flags: 0, | |
280 | precision: CountIsParam(10), | |
281 | width: CountImplied, | |
282 | precision_span: Some(InnerSpan::new(3, 7)), | |
283 | width_span: None, | |
284 | ty: "x", | |
285 | ty_span: None, | |
286 | }, | |
6522a427 | 287 | }))], |
04454e1e FG |
288 | ); |
289 | same( | |
290 | "{:a$.b$?}", | |
6522a427 | 291 | &[NextArgument(Box::new(Argument { |
04454e1e | 292 | position: ArgumentImplicitlyIs(0), |
064997fb | 293 | position_span: InnerSpan { start: 2, end: 2 }, |
04454e1e FG |
294 | format: FormatSpec { |
295 | fill: None, | |
296 | align: AlignUnknown, | |
297 | flags: 0, | |
f2b60f7d FG |
298 | precision: CountIsName("b", InnerSpan { start: 6, end: 7 }), |
299 | precision_span: Some(InnerSpan { start: 5, end: 8 }), | |
300 | width: CountIsName("a", InnerSpan { start: 3, end: 4 }), | |
301 | width_span: Some(InnerSpan { start: 3, end: 5 }), | |
04454e1e FG |
302 | ty: "?", |
303 | ty_span: None, | |
304 | }, | |
6522a427 | 305 | }))], |
04454e1e | 306 | ); |
f2b60f7d FG |
307 | same( |
308 | "{:.4}", | |
6522a427 | 309 | &[NextArgument(Box::new(Argument { |
f2b60f7d FG |
310 | position: ArgumentImplicitlyIs(0), |
311 | position_span: InnerSpan { start: 2, end: 2 }, | |
312 | format: FormatSpec { | |
313 | fill: None, | |
314 | align: AlignUnknown, | |
315 | flags: 0, | |
316 | precision: CountIs(4), | |
317 | precision_span: Some(InnerSpan { start: 3, end: 5 }), | |
318 | width: CountImplied, | |
319 | width_span: None, | |
320 | ty: "", | |
321 | ty_span: None, | |
322 | }, | |
6522a427 | 323 | }))], |
f2b60f7d | 324 | ) |
dc9dc135 XL |
325 | } |
326 | #[test] | |
327 | fn format_flags() { | |
416331ca XL |
328 | same( |
329 | "{:-}", | |
6522a427 | 330 | &[NextArgument(Box::new(Argument { |
416331ca | 331 | position: ArgumentImplicitlyIs(0), |
064997fb | 332 | position_span: InnerSpan { start: 2, end: 2 }, |
416331ca XL |
333 | format: FormatSpec { |
334 | fill: None, | |
335 | align: AlignUnknown, | |
336 | flags: (1 << FlagSignMinus as u32), | |
337 | precision: CountImplied, | |
338 | width: CountImplied, | |
339 | precision_span: None, | |
340 | width_span: None, | |
341 | ty: "", | |
60c5eb7d | 342 | ty_span: None, |
416331ca | 343 | }, |
6522a427 | 344 | }))], |
dfeec247 | 345 | ); |
416331ca XL |
346 | same( |
347 | "{:+#}", | |
6522a427 | 348 | &[NextArgument(Box::new(Argument { |
416331ca | 349 | position: ArgumentImplicitlyIs(0), |
064997fb | 350 | position_span: InnerSpan { start: 2, end: 2 }, |
416331ca XL |
351 | format: FormatSpec { |
352 | fill: None, | |
353 | align: AlignUnknown, | |
354 | flags: (1 << FlagSignPlus as u32) | (1 << FlagAlternate as u32), | |
355 | precision: CountImplied, | |
356 | width: CountImplied, | |
357 | precision_span: None, | |
358 | width_span: None, | |
359 | ty: "", | |
60c5eb7d | 360 | ty_span: None, |
416331ca | 361 | }, |
6522a427 | 362 | }))], |
dfeec247 | 363 | ); |
dc9dc135 XL |
364 | } |
365 | #[test] | |
366 | fn format_mixture() { | |
416331ca | 367 | same( |
60c5eb7d | 368 | "abcd {3:x} efg", |
416331ca XL |
369 | &[ |
370 | String("abcd "), | |
6522a427 | 371 | NextArgument(Box::new(Argument { |
416331ca | 372 | position: ArgumentIs(3), |
064997fb | 373 | position_span: InnerSpan { start: 7, end: 8 }, |
416331ca XL |
374 | format: FormatSpec { |
375 | fill: None, | |
376 | align: AlignUnknown, | |
377 | flags: 0, | |
378 | precision: CountImplied, | |
379 | width: CountImplied, | |
380 | precision_span: None, | |
381 | width_span: None, | |
60c5eb7d XL |
382 | ty: "x", |
383 | ty_span: None, | |
416331ca | 384 | }, |
6522a427 | 385 | })), |
416331ca XL |
386 | String(" efg"), |
387 | ], | |
388 | ); | |
dc9dc135 | 389 | } |
064997fb FG |
390 | #[test] |
391 | fn format_whitespace() { | |
392 | same( | |
393 | "{ }", | |
6522a427 | 394 | &[NextArgument(Box::new(Argument { |
064997fb FG |
395 | position: ArgumentImplicitlyIs(0), |
396 | position_span: InnerSpan { start: 2, end: 3 }, | |
397 | format: fmtdflt(), | |
6522a427 | 398 | }))], |
064997fb FG |
399 | ); |
400 | same( | |
401 | "{ }", | |
6522a427 | 402 | &[NextArgument(Box::new(Argument { |
064997fb FG |
403 | position: ArgumentImplicitlyIs(0), |
404 | position_span: InnerSpan { start: 2, end: 4 }, | |
405 | format: fmtdflt(), | |
6522a427 | 406 | }))], |
064997fb FG |
407 | ); |
408 | } |