Skip to content

Commit

Permalink
Trim on diff (#445)
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko authored Feb 19, 2024
1 parent 0d9c6c2 commit ffc3a9b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to insta and cargo-insta are documented here.

## 1.35.1

- Fixed a bug with diffs showing bogus newlines.

## 1.35.0

- Fixed a crash when a file named `.config` was in the root.
Expand Down
17 changes: 9 additions & 8 deletions src/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ impl Snapshot {

/// The snapshot contents as a &str
pub fn contents_str(&self) -> &str {
&self.snapshot.0
self.snapshot.as_str()
}

fn serialize_snapshot(&self, md: &MetaData) -> String {
Expand Down Expand Up @@ -510,6 +510,13 @@ impl SnapshotContents {
SnapshotContents(get_inline_snapshot_value(value))
}

/// Returns the snapshot contents as string with surrounding whitespace removed.
pub fn as_str(&self) -> &str {
self.0
.trim_start_matches(|x| x == '\r' || x == '\n')
.trim_end()
}

pub fn to_inline(&self, indentation: usize) -> String {
let contents = &self.0;
let mut out = String::new();
Expand Down Expand Up @@ -576,13 +583,7 @@ impl From<SnapshotContents> for String {

impl PartialEq for SnapshotContents {
fn eq(&self, other: &Self) -> bool {
self.0
.trim_start_matches(|x| x == '\r' || x == '\n')
.trim_end()
== other
.0
.trim_start_matches(|x| x == '\r' || x == '\n')
.trim_end()
self.as_str() == other.as_str()
}
}

Expand Down

0 comments on commit ffc3a9b

Please sign in to comment.