changelog generator & diff tool
stormlightlabs.github.io/git-storm/
changelog
changeset
markdown
golang
git
1package diff
2
3import (
4 "strings"
5 "testing"
6)
7
8func TestSideBySideFormatter_CompressUnchangedBlocks(t *testing.T) {
9 tests := []struct {
10 name string
11 edits []Edit
12 expectedCompressed, expectedTotal int
13 }{
14 {
15 name: "no compression for small unchanged blocks",
16 edits: makeEqualEdits(5),
17 expectedCompressed: 0,
18 expectedTotal: 5,
19 },
20 {
21 name: "compress large unchanged block",
22 edits: makeEqualEdits(20),
23 expectedCompressed: 1,
24 expectedTotal: 7,
25 },
26 {
27 name: "compress unchanged between changes",
28 edits: []Edit{
29 {Kind: Insert, AIndex: -1, BIndex: 0, Content: "new line"},
30 {Kind: Equal, AIndex: 0, BIndex: 1, Content: "unchanged 1"},
31 {Kind: Equal, AIndex: 1, BIndex: 2, Content: "unchanged 2"},
32 {Kind: Equal, AIndex: 2, BIndex: 3, Content: "unchanged 3"},
33 {Kind: Equal, AIndex: 3, BIndex: 4, Content: "unchanged 4"},
34 {Kind: Equal, AIndex: 4, BIndex: 5, Content: "unchanged 5"},
35 {Kind: Equal, AIndex: 5, BIndex: 6, Content: "unchanged 6"},
36 {Kind: Equal, AIndex: 6, BIndex: 7, Content: "unchanged 7"},
37 {Kind: Equal, AIndex: 7, BIndex: 8, Content: "unchanged 8"},
38 {Kind: Equal, AIndex: 8, BIndex: 9, Content: "unchanged 9"},
39 {Kind: Equal, AIndex: 9, BIndex: 10, Content: "unchanged 10"},
40 {Kind: Equal, AIndex: 10, BIndex: 11, Content: "unchanged 11"},
41 {Kind: Equal, AIndex: 11, BIndex: 12, Content: "unchanged 12"},
42 {Kind: Equal, AIndex: 12, BIndex: 13, Content: "unchanged 13"},
43 {Kind: Delete, AIndex: 13, BIndex: -1, Content: "removed line"},
44 },
45 expectedCompressed: 1,
46 expectedTotal: 9,
47 },
48 }
49
50 for _, tt := range tests {
51 t.Run(tt.name, func(t *testing.T) {
52 formatter := &SideBySideFormatter{}
53
54 result := formatter.compressUnchangedBlocks(tt.edits)
55
56 if len(result) != tt.expectedTotal {
57 t.Errorf("Expected %d total edits after compression, got %d", tt.expectedTotal, len(result))
58 }
59
60 compressed := countCompressedBlocks(result)
61 if compressed != tt.expectedCompressed {
62 t.Errorf("Expected %d compressed blocks, got %d", tt.expectedCompressed, compressed)
63 }
64 })
65 }
66}
67
68func TestSideBySideFormatter_Expanded(t *testing.T) {
69 edits := makeEqualEdits(20)
70
71 compressedFormatter := &SideBySideFormatter{
72 TerminalWidth: 100,
73 ShowLineNumbers: true,
74 Expanded: false,
75 }
76
77 expandedFormatter := &SideBySideFormatter{
78 TerminalWidth: 100,
79 ShowLineNumbers: true,
80 Expanded: true,
81 }
82
83 compressedOutput := compressedFormatter.Format(edits)
84 expandedOutput := expandedFormatter.Format(edits)
85
86 compressedLines := strings.Split(compressedOutput, "\n")
87 expandedLines := strings.Split(expandedOutput, "\n")
88
89 if len(expandedLines) <= len(compressedLines) {
90 t.Errorf("Expanded output (%d lines) should have more lines than compressed (%d lines)",
91 len(expandedLines), len(compressedLines))
92 }
93
94 if !strings.Contains(compressedOutput, compressedIndicator) {
95 t.Error("Compressed output should contain compression indicator")
96 }
97
98 if strings.Contains(expandedOutput, compressedIndicator) {
99 t.Error("Expanded output should not contain compression indicator")
100 }
101}
102
103func TestSideBySideFormatter_CompressedIndicatorStyling(t *testing.T) {
104 formatter := &SideBySideFormatter{
105 TerminalWidth: 100,
106 ShowLineNumbers: true,
107 Expanded: false,
108 }
109
110 edits := makeEqualEdits(20)
111 output := formatter.Format(edits)
112
113 if !strings.Contains(output, "unchanged lines") {
114 t.Error("Compressed output should mention 'unchanged lines'")
115 }
116}
117
118func TestMultipleCompressedBlocks(t *testing.T) {
119 formatter := &SideBySideFormatter{}
120
121 edits := []Edit{}
122 edits = append(edits, makeEqualEdits(20)...)
123 edits = append(edits, Edit{Kind: Insert, AIndex: -1, BIndex: 0, Content: "change 1"})
124 edits = append(edits, makeEqualEdits(20)...)
125 edits = append(edits, Edit{Kind: Delete, AIndex: 0, BIndex: -1, Content: "change 2"})
126 edits = append(edits, makeEqualEdits(20)...)
127
128 result := formatter.compressUnchangedBlocks(edits)
129
130 compressed := countCompressedBlocks(result)
131 if compressed != 3 {
132 t.Errorf("Expected 3 compressed blocks, got %d", compressed)
133 }
134}
135
136func makeEqualEdits(count int) []Edit {
137 edits := make([]Edit, count)
138 for i := range count {
139 edits[i] = Edit{
140 Kind: Equal,
141 AIndex: i,
142 BIndex: i,
143 Content: "unchanged line",
144 }
145 }
146 return edits
147}
148
149func countCompressedBlocks(edits []Edit) int {
150 count := 0
151 for _, edit := range edits {
152 if edit.AIndex == -2 && edit.BIndex == -2 {
153 count++
154 }
155 }
156 return count
157}