fix(text): remove trailing newline from single-line Display trait impl (#1320)

This commit is contained in:
Lucas Pickering 2024-08-11 23:30:36 -04:00 committed by GitHub
parent 8b624f5952
commit fdd5d8c092
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -633,7 +633,7 @@ impl<T: fmt::Display> ToText for T {
impl fmt::Display for Text<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for (position, line) in self.iter().with_position() {
if position == Position::Last {
if position == Position::Last || position == Position::Only {
write!(f, "{line}")?;
} else {
writeln!(f, "{line}")?;
@ -945,11 +945,12 @@ mod tests {
);
}
#[test]
fn display_raw_text() {
let text = Text::raw("The first line\nThe second line");
assert_eq!(format!("{text}"), "The first line\nThe second line");
#[rstest]
#[case::one_line("The first line")]
#[case::multiple_lines("The first line\nThe second line")]
fn display_raw_text(#[case] value: &str) {
let text = Text::raw(value);
assert_eq!(format!("{text}"), value);
}
#[test]