Approval-based snapshot testing library for Go (mirror)
1package diff_test
2
3import (
4 "testing"
5
6 "github.com/ptdewey/shutter/internal/diff"
7)
8
9func TestHistogramEmpty(t *testing.T) {
10 both := diff.Histogram("", "")
11 if both != nil {
12 t.Errorf("empty strings should return nil, got %v", both)
13 }
14
15 oldEmpty := diff.Histogram("", "hello")
16 if oldEmpty == nil {
17 t.Errorf("new content should return non-nil")
18 }
19
20 newEmpty := diff.Histogram("hello", "")
21 if newEmpty == nil {
22 t.Errorf("old content should return non-nil")
23 }
24}
25
26func TestHistogramIdentical(t *testing.T) {
27 old := "line1\nline2\nline3"
28 new := "line1\nline2\nline3"
29
30 result := diff.Histogram(old, new)
31
32 if len(result) != 3 {
33 t.Errorf("expected 3 diff lines, got %d", len(result))
34 }
35
36 for i, dl := range result {
37 if dl.Kind != diff.DiffShared {
38 t.Errorf("line %d: expected DiffShared, got %v", i, dl.Kind)
39 }
40 expectedLineNum := i + 1
41 if dl.OldNumber != expectedLineNum {
42 t.Errorf("line %d: expected OldNumber=%d, got %d", i, expectedLineNum, dl.OldNumber)
43 }
44 if dl.NewNumber != expectedLineNum {
45 t.Errorf("line %d: expected NewNumber=%d, got %d", i, expectedLineNum, dl.NewNumber)
46 }
47 }
48}
49
50func TestHistogramCompletelyDifferent(t *testing.T) {
51 old := "old content"
52 new := "new content"
53
54 result := diff.Histogram(old, new)
55
56 if len(result) != 2 {
57 t.Errorf("expected 2 diff lines, got %d", len(result))
58 }
59
60 hasOld := false
61 hasNew := false
62 for _, dl := range result {
63 if dl.Kind == diff.DiffOld {
64 hasOld = true
65 }
66 if dl.Kind == diff.DiffNew {
67 hasNew = true
68 }
69 }
70
71 if !hasOld || !hasNew {
72 t.Error("expected both old and new diff kinds")
73 }
74}
75
76func TestHistogramSingleLineChange(t *testing.T) {
77 old := "line1\nline2\nline3"
78 new := "line1\nmodified\nline3"
79
80 result := diff.Histogram(old, new)
81
82 if len(result) < 3 {
83 t.Errorf("expected at least 3 diff lines, got %d", len(result))
84 }
85
86 if result[0].Kind != diff.DiffShared || result[0].Line != "line1" {
87 t.Errorf("line 0: expected shared 'line1', got %v %s", result[0].Kind, result[0].Line)
88 }
89
90 hasModified := false
91 for _, dl := range result {
92 if dl.Line == "modified" {
93 hasModified = true
94 if dl.Kind != diff.DiffNew {
95 t.Errorf("'modified' should be marked as new, got %v", dl.Kind)
96 }
97 }
98 }
99 if !hasModified {
100 t.Error("diff missing 'modified' line")
101 }
102}
103
104func TestHistogramAddLine(t *testing.T) {
105 old := "line1\nline2"
106 new := "line1\nline1.5\nline2"
107
108 result := diff.Histogram(old, new)
109
110 newCount := 0
111 for _, dl := range result {
112 if dl.Kind == diff.DiffNew {
113 newCount++
114 if dl.Line != "line1.5" {
115 t.Errorf("expected new line 'line1.5', got '%s'", dl.Line)
116 }
117 }
118 }
119
120 if newCount != 1 {
121 t.Errorf("expected 1 new line, got %d", newCount)
122 }
123}
124
125func TestHistogramRemoveLine(t *testing.T) {
126 old := "line1\nline2\nline3"
127 new := "line1\nline3"
128
129 result := diff.Histogram(old, new)
130
131 oldCount := 0
132 for _, dl := range result {
133 if dl.Kind == diff.DiffOld {
134 oldCount++
135 if dl.Line != "line2" {
136 t.Errorf("expected old line 'line2', got '%s'", dl.Line)
137 }
138 }
139 }
140
141 if oldCount != 1 {
142 t.Errorf("expected 1 old line, got %d", oldCount)
143 }
144}
145
146func TestHistogramLineNumbers(t *testing.T) {
147 old := "a\nb\nc"
148 new := "a\nb\nc"
149
150 result := diff.Histogram(old, new)
151
152 for i, dl := range result {
153 expectedLineNum := i + 1
154 if dl.OldNumber != expectedLineNum {
155 t.Errorf("line %d: expected OldNumber=%d, got %d", i, expectedLineNum, dl.OldNumber)
156 }
157 if dl.NewNumber != expectedLineNum {
158 t.Errorf("line %d: expected NewNumber=%d, got %d", i, expectedLineNum, dl.NewNumber)
159 }
160 }
161}
162
163func TestHistogramMultilineChanges(t *testing.T) {
164 old := "start\nmiddle\nend"
165 new := "start\nnew1\nnew2\nend"
166
167 result := diff.Histogram(old, new)
168
169 newCount := 0
170 for _, dl := range result {
171 if dl.Kind == diff.DiffNew {
172 newCount++
173 }
174 }
175
176 if newCount != 2 {
177 t.Errorf("expected 2 new lines, got %d", newCount)
178 }
179}
180
181func TestHistogramWithEmptyLines(t *testing.T) {
182 old := "line1\n\nline3"
183 new := "line1\nline2\nline3"
184
185 result := diff.Histogram(old, new)
186
187 if len(result) == 0 {
188 t.Error("expected non-empty diff result")
189 }
190}