-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui_test.go
More file actions
182 lines (167 loc) · 4.33 KB
/
Copy pathui_test.go
File metadata and controls
182 lines (167 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package main
import (
"reflect"
"testing"
)
// TestWrapText tests the text wrapping function with various inputs
func TestWrapText(t *testing.T) {
// Table-driven tests - the Go way!
tests := []struct {
name string
text string
maxWidth int
want []string
}{
{
name: "text fits within width",
text: "short text",
maxWidth: 20,
want: []string{"short text"},
},
{
name: "exact width match",
text: "exactly twenty chars",
maxWidth: 20,
want: []string{"exactly twenty chars"},
},
{
name: "wrap on word boundary",
text: "this is a very long commit message that needs wrapping",
maxWidth: 20,
want: []string{"this is a very long", "commit message that", "needs wrapping"},
},
{
name: "single long word",
text: "supercalifragilisticexpialidocious",
maxWidth: 15,
want: []string{"supercalifragil", "isticexpialidoc", "ious"},
},
{
name: "multiple spaces",
text: "word1 word2 word3",
maxWidth: 15,
want: []string{"word1 word2", "word3"},
},
{
name: "empty string",
text: "",
maxWidth: 10,
want: []string{""},
},
{
name: "invalid width zero",
text: "some text",
maxWidth: 0,
want: []string{"some text"},
},
{
name: "invalid width negative",
text: "some text",
maxWidth: -5,
want: []string{"some text"},
},
{
name: "wrapping removes leading spaces",
text: "first line and second line here",
maxWidth: 15,
want: []string{"first line and", "second line", "here"},
},
{
name: "text with newlines gets wrapped",
text: "this has a\nnewline in it",
maxWidth: 10,
want: []string{"this has", "a\nnewline", "in it"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := wrapText(tt.text, tt.maxWidth)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("wrapText() = %v, want %v", got, tt.want)
}
})
}
}
// TestWrapTextEdgeCases tests specific edge cases
func TestWrapTextEdgeCases(t *testing.T) {
t.Run("width of 1", func(t *testing.T) {
got := wrapText("abc", 1)
if len(got) != 3 {
t.Errorf("expected 3 lines, got %d", len(got))
}
})
t.Run("very long text", func(t *testing.T) {
// Build a long string
longText := ""
for i := 0; i < 100; i++ {
longText += "another "
}
got := wrapText(longText, 50)
// Should produce multiple lines
if len(got) < 10 {
t.Errorf("expected many lines for long text, got %d", len(got))
}
})
}
// BenchmarkWrapText benchmarks the wrapping function
func BenchmarkWrapText(b *testing.B) {
text := "This is a reasonably long commit message that will need to be wrapped across multiple lines for display"
for i := 0; i < b.N; i++ {
wrapText(text, 40)
}
}
// BenchmarkWrapTextLong benchmarks with very long text
func BenchmarkWrapTextLong(b *testing.B) {
text := ""
for i := 0; i < 1000; i++ {
text += "word "
}
for i := 0; i < b.N; i++ {
wrapText(text, 80)
}
}
// TestConstants ensures our layout constants are correct
func TestConstants(t *testing.T) {
tests := []struct {
name string
value int
want int
}{
{"hashLength", hashLength, 7},
{"prefixWidth", prefixWidth, 2},
{"hashSpacing", hashSpacing, 1},
{"leftMargin", leftMargin, 10}, // 2 + 7 + 1
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.value != tt.want {
t.Errorf("%s = %d, want %d", tt.name, tt.value, tt.want)
}
})
}
// Test that indentSpaces matches leftMargin
if len(indentSpaces) != leftMargin {
t.Errorf("indentSpaces length = %d, want %d (should match leftMargin)",
len(indentSpaces), leftMargin)
}
// Test that leftMargin calculation is correct
calculatedMargin := prefixWidth + hashLength + hashSpacing
if leftMargin != calculatedMargin {
t.Errorf("leftMargin = %d, but calculated as %d",
leftMargin, calculatedMargin)
}
}
// TestCommitDelegateType tests the delegate type
func TestCommitDelegateType(t *testing.T) {
delegate := commitDelegate{}
if delegate.Height() != 1 {
t.Errorf("Height() = %d, want 1", delegate.Height())
}
if delegate.Spacing() != 0 {
t.Errorf("Spacing() = %d, want 0", delegate.Spacing())
}
// Test that Update returns nil
if cmd := delegate.Update(nil, nil); cmd != nil {
t.Errorf("Update() returned non-nil cmd: %v", cmd)
}
}