use super::*; #[track_caller] fn same(fmt: &'static str, p: &[Piece<'static>]) { let parser = Parser::new(fmt, None, None, false, ParseMode::Format); assert_eq!(parser.collect::>>(), p); } fn fmtdflt() -> FormatSpec<'static> { return FormatSpec { fill: None, fill_span: None, align: AlignUnknown, sign: None, alternate: false, zero_pad: false, debug_hex: None, precision: CountImplied, width: CountImplied, precision_span: None, width_span: None, ty: "", ty_span: None, }; } fn musterr(s: &str) { let mut p = Parser::new(s, None, None, false, ParseMode::Format); p.next(); assert!(!p.errors.is_empty()); } #[test] fn simple() { same("asdf", &[String("asdf")]); same("a{{b", &[String("a"), String("{b")]); same("a}}b", &[String("a"), String("}b")]); same("a}}", &[String("a"), String("}")]); same("}}", &[String("}")]); same("\\}}", &[String("\\"), String("}")]); } #[test] fn invalid01() { musterr("{") } #[test] fn invalid02() { musterr("}") } #[test] fn invalid04() { musterr("{3a}") } #[test] fn invalid05() { musterr("{:|}") } #[test] fn invalid06() { musterr("{:>>>}") } #[test] fn invalid_position() { musterr("{18446744073709551616}"); } #[test] fn invalid_width() { musterr("{:18446744073709551616}"); } #[test] fn invalid_precision() { musterr("{:.18446744073709551616}"); } #[test] fn format_nothing() { same( "{}", &[NextArgument(Box::new(Argument { position: ArgumentImplicitlyIs(0), position_span: InnerSpan { start: 2, end: 2 }, format: fmtdflt(), }))], ); } #[test] fn format_position() { same( "{3}", &[NextArgument(Box::new(Argument { position: ArgumentIs(3), position_span: InnerSpan { start: 2, end: 3 }, format: fmtdflt(), }))], ); } #[test] fn format_position_nothing_else() { same( "{3:}", &[NextArgument(Box::new(Argument { position: ArgumentIs(3), position_span: InnerSpan { start: 2, end: 3 }, format: fmtdflt(), }))], ); } #[test] fn format_named() { same( "{name}", &[NextArgument(Box::new(Argument { position: ArgumentNamed("name"), position_span: InnerSpan { start: 2, end: 6 }, format: fmtdflt(), }))], ) } #[test] fn format_type() { same( "{3:x}", &[NextArgument(Box::new(Argument { position: ArgumentIs(3), position_span: InnerSpan { start: 2, end: 3 }, format: FormatSpec { fill: None, fill_span: None, align: AlignUnknown, sign: None, alternate: false, zero_pad: false, debug_hex: None, precision: CountImplied, width: CountImplied, precision_span: None, width_span: None, ty: "x", ty_span: None, }, }))], ); } #[test] fn format_align_fill() { same( "{3:>}", &[NextArgument(Box::new(Argument { position: ArgumentIs(3), position_span: InnerSpan { start: 2, end: 3 }, format: FormatSpec { fill: None, fill_span: None, align: AlignRight, sign: None, alternate: false, zero_pad: false, debug_hex: None, precision: CountImplied, width: CountImplied, precision_span: None, width_span: None, ty: "", ty_span: None, }, }))], ); same( "{3:0<}", &[NextArgument(Box::new(Argument { position: ArgumentIs(3), position_span: InnerSpan { start: 2, end: 3 }, format: FormatSpec { fill: Some('0'), fill_span: Some(InnerSpan::new(4, 5)), align: AlignLeft, sign: None, alternate: false, zero_pad: false, debug_hex: None, precision: CountImplied, width: CountImplied, precision_span: None, width_span: None, ty: "", ty_span: None, }, }))], ); same( "{3:*