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