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