From 56504f1087e1776812c08210b8aabd541351ca3e Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Wed, 27 Apr 2022 00:15:36 -0700 Subject: [PATCH] Use inline block format also for strings of form "foo\n" (#225) This changes the formatting for strings with a trailing newline to block format. --- src/snapshot.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/snapshot.rs b/src/snapshot.rs index bbee2dc0..76f27d59 100644 --- a/src/snapshot.rs +++ b/src/snapshot.rs @@ -317,12 +317,12 @@ impl SnapshotContents { pub fn to_inline(&self, indentation: usize) -> String { let contents = &self.0; let mut out = String::new(); - let is_escape = contents.lines().count() > 1 || contents.contains(&['\\', '"'][..]); + let is_escape = contents.contains(&['\n', '\\', '"'][..]); out.push_str(if is_escape { "r###\"" } else { "\"" }); // if we have more than one line we want to change into the block // representation mode - if contents.lines().count() > 1 { + if contents.contains('\n') { out.extend( contents .lines() @@ -512,6 +512,16 @@ b"[1..]; \"###" ); + let t = &" + ab +"[1..]; + assert_eq!( + SnapshotContents(t.to_string()).to_inline(0), + "r###\" + ab +\"###" + ); + let t = "ab"; assert_eq!(SnapshotContents(t.to_string()).to_inline(0), r##""ab""##); }