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