1
Fork 0

Fix libfmt_macros tests

This commit is contained in:
Esteban Küber 2019-11-05 16:02:12 -08:00
parent c271db284b
commit 543fe5b413
2 changed files with 40 additions and 26 deletions

View file

@ -35,7 +35,7 @@ impl InnerOffset {
/// A piece is a portion of the format string which represents the next part /// A piece is a portion of the format string which represents the next part
/// to emit. These are emitted as a stream by the `Parser` class. /// to emit. These are emitted as a stream by the `Parser` class.
#[derive(Copy, Clone, PartialEq)] #[derive(Copy, Clone, Debug, PartialEq)]
pub enum Piece<'a> { pub enum Piece<'a> {
/// A literal string which should directly be emitted /// A literal string which should directly be emitted
String(&'a str), String(&'a str),
@ -45,7 +45,7 @@ pub enum Piece<'a> {
} }
/// Representation of an argument specification. /// Representation of an argument specification.
#[derive(Copy, Clone, PartialEq)] #[derive(Copy, Clone, Debug, PartialEq)]
pub struct Argument<'a> { pub struct Argument<'a> {
/// Where to find this argument /// Where to find this argument
pub position: Position, pub position: Position,
@ -54,7 +54,7 @@ pub struct Argument<'a> {
} }
/// Specification for the formatting of an argument in the format string. /// Specification for the formatting of an argument in the format string.
#[derive(Copy, Clone, PartialEq)] #[derive(Copy, Clone, Debug, PartialEq)]
pub struct FormatSpec<'a> { pub struct FormatSpec<'a> {
/// Optionally specified character to fill alignment with. /// Optionally specified character to fill alignment with.
pub fill: Option<char>, pub fill: Option<char>,
@ -79,7 +79,7 @@ pub struct FormatSpec<'a> {
} }
/// Enum describing where an argument for a format can be located. /// Enum describing where an argument for a format can be located.
#[derive(Copy, Clone, PartialEq)] #[derive(Copy, Clone, Debug, PartialEq)]
pub enum Position { pub enum Position {
/// The argument is implied to be located at an index /// The argument is implied to be located at an index
ArgumentImplicitlyIs(usize), ArgumentImplicitlyIs(usize),
@ -99,7 +99,7 @@ impl Position {
} }
/// Enum of alignments which are supported. /// Enum of alignments which are supported.
#[derive(Copy, Clone, PartialEq)] #[derive(Copy, Clone, Debug, PartialEq)]
pub enum Alignment { pub enum Alignment {
/// The value will be aligned to the left. /// The value will be aligned to the left.
AlignLeft, AlignLeft,
@ -113,7 +113,7 @@ pub enum Alignment {
/// Various flags which can be applied to format strings. The meaning of these /// Various flags which can be applied to format strings. The meaning of these
/// flags is defined by the formatters themselves. /// flags is defined by the formatters themselves.
#[derive(Copy, Clone, PartialEq)] #[derive(Copy, Clone, Debug, PartialEq)]
pub enum Flag { pub enum Flag {
/// A `+` will be used to denote positive numbers. /// A `+` will be used to denote positive numbers.
FlagSignPlus, FlagSignPlus,
@ -133,7 +133,7 @@ pub enum Flag {
/// A count is used for the precision and width parameters of an integer, and /// A count is used for the precision and width parameters of an integer, and
/// can reference either an argument or a literal integer. /// can reference either an argument or a literal integer.
#[derive(Copy, Clone, PartialEq)] #[derive(Copy, Clone, Debug, PartialEq)]
pub enum Count { pub enum Count {
/// The count is specified explicitly. /// The count is specified explicitly.
CountIs(usize), CountIs(usize),
@ -572,10 +572,11 @@ impl<'a> Parser<'a> {
} else { } else {
spec.ty = self.word(); spec.ty = self.word();
let ty_span_end = self.cur.peek().map(|(pos, _)| *pos); let ty_span_end = self.cur.peek().map(|(pos, _)| *pos);
let this = self; if !spec.ty.is_empty() {
spec.ty_span = ty_span_start spec.ty_span = ty_span_start
.and_then(|s| ty_span_end.map(|e| (s, e))) .and_then(|s| ty_span_end.map(|e| (s, e)))
.map(|(start, end)| this.to_span_index(start).to(this.to_span_index(end))); .map(|(start, end)| self.to_span_index(start).to(self.to_span_index(end)));
}
} }
spec spec
} }

View file

@ -2,7 +2,7 @@ use super::*;
fn same(fmt: &'static str, p: &[Piece<'static>]) { fn same(fmt: &'static str, p: &[Piece<'static>]) {
let parser = Parser::new(fmt, None, vec![], false); let parser = Parser::new(fmt, None, vec![], false);
assert!(parser.collect::<Vec<Piece<'static>>>() == p); assert_eq!(parser.collect::<Vec<Piece<'static>>>(), p);
} }
fn fmtdflt() -> FormatSpec<'static> { fn fmtdflt() -> FormatSpec<'static> {
@ -15,6 +15,7 @@ fn fmtdflt() -> FormatSpec<'static> {
precision_span: None, precision_span: None,
width_span: None, width_span: None,
ty: "", ty: "",
ty_span: None,
}; };
} }
@ -82,7 +83,7 @@ fn format_position_nothing_else() {
#[test] #[test]
fn format_type() { fn format_type() {
same( same(
"{3:a}", "{3:x}",
&[NextArgument(Argument { &[NextArgument(Argument {
position: ArgumentIs(3), position: ArgumentIs(3),
format: FormatSpec { format: FormatSpec {
@ -93,7 +94,8 @@ fn format_type() {
width: CountImplied, width: CountImplied,
precision_span: None, precision_span: None,
width_span: None, width_span: None,
ty: "a", ty: "x",
ty_span: None,
}, },
})]); })]);
} }
@ -112,6 +114,7 @@ fn format_align_fill() {
precision_span: None, precision_span: None,
width_span: None, width_span: None,
ty: "", ty: "",
ty_span: None,
}, },
})]); })]);
same( same(
@ -127,6 +130,7 @@ fn format_align_fill() {
precision_span: None, precision_span: None,
width_span: None, width_span: None,
ty: "", ty: "",
ty_span: None,
}, },
})]); })]);
same( same(
@ -142,6 +146,7 @@ fn format_align_fill() {
precision_span: None, precision_span: None,
width_span: None, width_span: None,
ty: "abcd", ty: "abcd",
ty_span: Some(InnerSpan::new(6, 10)),
}, },
})]); })]);
} }
@ -150,7 +155,7 @@ fn format_counts() {
use syntax_pos::{GLOBALS, Globals, edition}; use syntax_pos::{GLOBALS, Globals, edition};
GLOBALS.set(&Globals::new(edition::DEFAULT_EDITION), || { GLOBALS.set(&Globals::new(edition::DEFAULT_EDITION), || {
same( same(
"{:10s}", "{:10x}",
&[NextArgument(Argument { &[NextArgument(Argument {
position: ArgumentImplicitlyIs(0), position: ArgumentImplicitlyIs(0),
format: FormatSpec { format: FormatSpec {
@ -161,11 +166,12 @@ fn format_counts() {
width: CountIs(10), width: CountIs(10),
precision_span: None, precision_span: None,
width_span: None, width_span: None,
ty: "s", ty: "x",
ty_span: None,
}, },
})]); })]);
same( same(
"{:10$.10s}", "{:10$.10x}",
&[NextArgument(Argument { &[NextArgument(Argument {
position: ArgumentImplicitlyIs(0), position: ArgumentImplicitlyIs(0),
format: FormatSpec { format: FormatSpec {
@ -176,11 +182,12 @@ fn format_counts() {
width: CountIsParam(10), width: CountIsParam(10),
precision_span: None, precision_span: None,
width_span: Some(InnerSpan::new(3, 6)), width_span: Some(InnerSpan::new(3, 6)),
ty: "s", ty: "x",
ty_span: None,
}, },
})]); })]);
same( same(
"{:.*s}", "{:.*x}",
&[NextArgument(Argument { &[NextArgument(Argument {
position: ArgumentImplicitlyIs(1), position: ArgumentImplicitlyIs(1),
format: FormatSpec { format: FormatSpec {
@ -191,11 +198,12 @@ fn format_counts() {
width: CountImplied, width: CountImplied,
precision_span: Some(InnerSpan::new(3, 5)), precision_span: Some(InnerSpan::new(3, 5)),
width_span: None, width_span: None,
ty: "s", ty: "x",
ty_span: None,
}, },
})]); })]);
same( same(
"{:.10$s}", "{:.10$x}",
&[NextArgument(Argument { &[NextArgument(Argument {
position: ArgumentImplicitlyIs(0), position: ArgumentImplicitlyIs(0),
format: FormatSpec { format: FormatSpec {
@ -206,11 +214,12 @@ fn format_counts() {
width: CountImplied, width: CountImplied,
precision_span: Some(InnerSpan::new(3, 7)), precision_span: Some(InnerSpan::new(3, 7)),
width_span: None, width_span: None,
ty: "s", ty: "x",
ty_span: None,
}, },
})]); })]);
same( same(
"{:a$.b$s}", "{:a$.b$?}",
&[NextArgument(Argument { &[NextArgument(Argument {
position: ArgumentImplicitlyIs(0), position: ArgumentImplicitlyIs(0),
format: FormatSpec { format: FormatSpec {
@ -221,7 +230,8 @@ fn format_counts() {
width: CountIsName(Symbol::intern("a")), width: CountIsName(Symbol::intern("a")),
precision_span: None, precision_span: None,
width_span: None, width_span: None,
ty: "s", ty: "?",
ty_span: None,
}, },
})]); })]);
}); });
@ -241,6 +251,7 @@ fn format_flags() {
precision_span: None, precision_span: None,
width_span: None, width_span: None,
ty: "", ty: "",
ty_span: None,
}, },
})]); })]);
same( same(
@ -256,13 +267,14 @@ fn format_flags() {
precision_span: None, precision_span: None,
width_span: None, width_span: None,
ty: "", ty: "",
ty_span: None,
}, },
})]); })]);
} }
#[test] #[test]
fn format_mixture() { fn format_mixture() {
same( same(
"abcd {3:a} efg", "abcd {3:x} efg",
&[ &[
String("abcd "), String("abcd "),
NextArgument(Argument { NextArgument(Argument {
@ -275,7 +287,8 @@ fn format_mixture() {
width: CountImplied, width: CountImplied,
precision_span: None, precision_span: None,
width_span: None, width_span: None,
ty: "a", ty: "x",
ty_span: None,
}, },
}), }),
String(" efg"), String(" efg"),