Skip to content

Commit 543fe5b

Browse files
committed
Fix libfmt_macros tests
1 parent c271db2 commit 543fe5b

File tree

2 files changed

+40
-26
lines changed

2 files changed

+40
-26
lines changed

src/libfmt_macros/lib.rs

+12-11
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl InnerOffset {
3535

3636
/// A piece is a portion of the format string which represents the next part
3737
/// to emit. These are emitted as a stream by the `Parser` class.
38-
#[derive(Copy, Clone, PartialEq)]
38+
#[derive(Copy, Clone, Debug, PartialEq)]
3939
pub enum Piece<'a> {
4040
/// A literal string which should directly be emitted
4141
String(&'a str),
@@ -45,7 +45,7 @@ pub enum Piece<'a> {
4545
}
4646

4747
/// Representation of an argument specification.
48-
#[derive(Copy, Clone, PartialEq)]
48+
#[derive(Copy, Clone, Debug, PartialEq)]
4949
pub struct Argument<'a> {
5050
/// Where to find this argument
5151
pub position: Position,
@@ -54,7 +54,7 @@ pub struct Argument<'a> {
5454
}
5555

5656
/// Specification for the formatting of an argument in the format string.
57-
#[derive(Copy, Clone, PartialEq)]
57+
#[derive(Copy, Clone, Debug, PartialEq)]
5858
pub struct FormatSpec<'a> {
5959
/// Optionally specified character to fill alignment with.
6060
pub fill: Option<char>,
@@ -79,7 +79,7 @@ pub struct FormatSpec<'a> {
7979
}
8080

8181
/// Enum describing where an argument for a format can be located.
82-
#[derive(Copy, Clone, PartialEq)]
82+
#[derive(Copy, Clone, Debug, PartialEq)]
8383
pub enum Position {
8484
/// The argument is implied to be located at an index
8585
ArgumentImplicitlyIs(usize),
@@ -99,7 +99,7 @@ impl Position {
9999
}
100100

101101
/// Enum of alignments which are supported.
102-
#[derive(Copy, Clone, PartialEq)]
102+
#[derive(Copy, Clone, Debug, PartialEq)]
103103
pub enum Alignment {
104104
/// The value will be aligned to the left.
105105
AlignLeft,
@@ -113,7 +113,7 @@ pub enum Alignment {
113113

114114
/// Various flags which can be applied to format strings. The meaning of these
115115
/// flags is defined by the formatters themselves.
116-
#[derive(Copy, Clone, PartialEq)]
116+
#[derive(Copy, Clone, Debug, PartialEq)]
117117
pub enum Flag {
118118
/// A `+` will be used to denote positive numbers.
119119
FlagSignPlus,
@@ -133,7 +133,7 @@ pub enum Flag {
133133

134134
/// A count is used for the precision and width parameters of an integer, and
135135
/// can reference either an argument or a literal integer.
136-
#[derive(Copy, Clone, PartialEq)]
136+
#[derive(Copy, Clone, Debug, PartialEq)]
137137
pub enum Count {
138138
/// The count is specified explicitly.
139139
CountIs(usize),
@@ -572,10 +572,11 @@ impl<'a> Parser<'a> {
572572
} else {
573573
spec.ty = self.word();
574574
let ty_span_end = self.cur.peek().map(|(pos, _)| *pos);
575-
let this = self;
576-
spec.ty_span = ty_span_start
577-
.and_then(|s| ty_span_end.map(|e| (s, e)))
578-
.map(|(start, end)| this.to_span_index(start).to(this.to_span_index(end)));
575+
if !spec.ty.is_empty() {
576+
spec.ty_span = ty_span_start
577+
.and_then(|s| ty_span_end.map(|e| (s, e)))
578+
.map(|(start, end)| self.to_span_index(start).to(self.to_span_index(end)));
579+
}
579580
}
580581
spec
581582
}

src/libfmt_macros/tests.rs

+28-15
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::*;
22

33
fn same(fmt: &'static str, p: &[Piece<'static>]) {
44
let parser = Parser::new(fmt, None, vec![], false);
5-
assert!(parser.collect::<Vec<Piece<'static>>>() == p);
5+
assert_eq!(parser.collect::<Vec<Piece<'static>>>(), p);
66
}
77

88
fn fmtdflt() -> FormatSpec<'static> {
@@ -15,6 +15,7 @@ fn fmtdflt() -> FormatSpec<'static> {
1515
precision_span: None,
1616
width_span: None,
1717
ty: "",
18+
ty_span: None,
1819
};
1920
}
2021

@@ -82,7 +83,7 @@ fn format_position_nothing_else() {
8283
#[test]
8384
fn format_type() {
8485
same(
85-
"{3:a}",
86+
"{3:x}",
8687
&[NextArgument(Argument {
8788
position: ArgumentIs(3),
8889
format: FormatSpec {
@@ -93,7 +94,8 @@ fn format_type() {
9394
width: CountImplied,
9495
precision_span: None,
9596
width_span: None,
96-
ty: "a",
97+
ty: "x",
98+
ty_span: None,
9799
},
98100
})]);
99101
}
@@ -112,6 +114,7 @@ fn format_align_fill() {
112114
precision_span: None,
113115
width_span: None,
114116
ty: "",
117+
ty_span: None,
115118
},
116119
})]);
117120
same(
@@ -127,6 +130,7 @@ fn format_align_fill() {
127130
precision_span: None,
128131
width_span: None,
129132
ty: "",
133+
ty_span: None,
130134
},
131135
})]);
132136
same(
@@ -142,6 +146,7 @@ fn format_align_fill() {
142146
precision_span: None,
143147
width_span: None,
144148
ty: "abcd",
149+
ty_span: Some(InnerSpan::new(6, 10)),
145150
},
146151
})]);
147152
}
@@ -150,7 +155,7 @@ fn format_counts() {
150155
use syntax_pos::{GLOBALS, Globals, edition};
151156
GLOBALS.set(&Globals::new(edition::DEFAULT_EDITION), || {
152157
same(
153-
"{:10s}",
158+
"{:10x}",
154159
&[NextArgument(Argument {
155160
position: ArgumentImplicitlyIs(0),
156161
format: FormatSpec {
@@ -161,11 +166,12 @@ fn format_counts() {
161166
width: CountIs(10),
162167
precision_span: None,
163168
width_span: None,
164-
ty: "s",
169+
ty: "x",
170+
ty_span: None,
165171
},
166172
})]);
167173
same(
168-
"{:10$.10s}",
174+
"{:10$.10x}",
169175
&[NextArgument(Argument {
170176
position: ArgumentImplicitlyIs(0),
171177
format: FormatSpec {
@@ -176,11 +182,12 @@ fn format_counts() {
176182
width: CountIsParam(10),
177183
precision_span: None,
178184
width_span: Some(InnerSpan::new(3, 6)),
179-
ty: "s",
185+
ty: "x",
186+
ty_span: None,
180187
},
181188
})]);
182189
same(
183-
"{:.*s}",
190+
"{:.*x}",
184191
&[NextArgument(Argument {
185192
position: ArgumentImplicitlyIs(1),
186193
format: FormatSpec {
@@ -191,11 +198,12 @@ fn format_counts() {
191198
width: CountImplied,
192199
precision_span: Some(InnerSpan::new(3, 5)),
193200
width_span: None,
194-
ty: "s",
201+
ty: "x",
202+
ty_span: None,
195203
},
196204
})]);
197205
same(
198-
"{:.10$s}",
206+
"{:.10$x}",
199207
&[NextArgument(Argument {
200208
position: ArgumentImplicitlyIs(0),
201209
format: FormatSpec {
@@ -206,11 +214,12 @@ fn format_counts() {
206214
width: CountImplied,
207215
precision_span: Some(InnerSpan::new(3, 7)),
208216
width_span: None,
209-
ty: "s",
217+
ty: "x",
218+
ty_span: None,
210219
},
211220
})]);
212221
same(
213-
"{:a$.b$s}",
222+
"{:a$.b$?}",
214223
&[NextArgument(Argument {
215224
position: ArgumentImplicitlyIs(0),
216225
format: FormatSpec {
@@ -221,7 +230,8 @@ fn format_counts() {
221230
width: CountIsName(Symbol::intern("a")),
222231
precision_span: None,
223232
width_span: None,
224-
ty: "s",
233+
ty: "?",
234+
ty_span: None,
225235
},
226236
})]);
227237
});
@@ -241,6 +251,7 @@ fn format_flags() {
241251
precision_span: None,
242252
width_span: None,
243253
ty: "",
254+
ty_span: None,
244255
},
245256
})]);
246257
same(
@@ -256,13 +267,14 @@ fn format_flags() {
256267
precision_span: None,
257268
width_span: None,
258269
ty: "",
270+
ty_span: None,
259271
},
260272
})]);
261273
}
262274
#[test]
263275
fn format_mixture() {
264276
same(
265-
"abcd {3:a} efg",
277+
"abcd {3:x} efg",
266278
&[
267279
String("abcd "),
268280
NextArgument(Argument {
@@ -275,7 +287,8 @@ fn format_mixture() {
275287
width: CountImplied,
276288
precision_span: None,
277289
width_span: None,
278-
ty: "a",
290+
ty: "x",
291+
ty_span: None,
279292
},
280293
}),
281294
String(" efg"),

0 commit comments

Comments
 (0)