Skip to content

Commit

Permalink
fix: improve some edge diffing cases
Browse files Browse the repository at this point in the history
  • Loading branch information
gkampitakis committed Jun 12, 2023
1 parent 1c1786c commit 008ee36
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
12 changes: 12 additions & 0 deletions examples/__snapshots__/matchSnapshot_test.snap
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,15 @@ map[string]interface {}{
[TestMatchSnapshot/withConfig - 1]
this should use the default config
---

[TestMatchSnapshot/regression - 1]
dasda
---

[TestMatchSnapshot/regression - 2]
---

[TestMatchSnapshot/regression - 3]
test and another test
test another
---
6 changes: 6 additions & 0 deletions examples/matchSnapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ func TestMatchSnapshot(t *testing.T) {
snaps.MatchSnapshot(t, "ignore regex patterns on names")
})

t.Run("regression", func(t *testing.T) {
snaps.MatchSnapshot(t, "")
snaps.MatchSnapshot(t, "")
snaps.MatchSnapshot(t, "")
})

t.Run("withConfig", func(t *testing.T) {
t.Run("should allow changing filename", func(t *testing.T) {
snaps.WithConfig(
Expand Down
6 changes: 5 additions & 1 deletion snaps/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ func isSingleline(s string) bool {
// shouldPrintHighlights checks if the two strings are going to be presented with
// inline highlights
func shouldPrintHighlights(a, b string) bool {
return !colors.NOCOLOR && a != "\n" && b != "\n" && isSingleline(a) && isSingleline(b)
return !colors.NOCOLOR && a != "" && b != "" && isSingleline(a) && isSingleline(b)
// return !colors.NOCOLOR && a != "\n" && b != "\n" && isSingleline(a) && isSingleline(b)
}

// Compare two sequences of lines; generate the delta as a unified diff.
Expand Down Expand Up @@ -84,6 +85,9 @@ func getUnifiedDiff(a, b string) (string, int, int) {

if c.Tag == difflib.OpEqual {
for _, line := range aLines[i1:i2] {
if line == "\n" {
line = enterSymbol + "\n"
}
colors.FprintEqual(&s, line)
}

Expand Down
1 change: 1 addition & 0 deletions snaps/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const (
updateSymbol = "✎ "
skipSymbol = "⟳ "
enterSymbol = "↳ "
newLineSymbol = "↵"

snapsExt = ".snap"
endSequence = "---"
Expand Down

0 comments on commit 008ee36

Please sign in to comment.