-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisplay_test.go
More file actions
203 lines (185 loc) · 4.28 KB
/
display_test.go
File metadata and controls
203 lines (185 loc) · 4.28 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package main
import (
"os"
"testing"
)
func TestColorize(t *testing.T) {
// When not a terminal, colorize should return plain text
// (In tests, stdout is typically not a terminal)
result := colorize("hello", Red)
// The behavior depends on whether stdout is a terminal
if isTerminal() {
if result != Red+"hello"+Reset {
t.Errorf("expected colored output, got %q", result)
}
} else {
if result != "hello" {
t.Errorf("expected plain text, got %q", result)
}
}
}
func TestIsTerminal(t *testing.T) {
// In test environment, stdout is typically not a terminal
fi, err := os.Stdout.Stat()
if err != nil {
t.Skip("cannot stat stdout")
}
expected := fi.Mode()&os.ModeCharDevice != 0
if isTerminal() != expected {
t.Errorf("isTerminal() = %v, expected %v", isTerminal(), expected)
}
}
func TestTruncate(t *testing.T) {
tests := []struct {
input string
maxLen int
expected string
}{
{"hello", 10, "hello"},
{"hello world", 5, "hell…"},
{"hi", 2, "hi"},
{"a", 1, "a"},
{"hello", 3, "he…"},
}
for _, tt := range tests {
result := truncate(tt.input, tt.maxLen)
if result != tt.expected {
t.Errorf("truncate(%q, %d) = %q, expected %q",
tt.input, tt.maxLen, result, tt.expected)
}
}
}
func TestPadRight(t *testing.T) {
tests := []struct {
input string
width int
expected string
}{
{"hello", 10, "hello "},
{"hello", 5, "hello"},
{"hello", 3, "hello"},
}
for _, tt := range tests {
result := padRight(tt.input, tt.width)
if result != tt.expected {
t.Errorf("padRight(%q, %d) = %q, expected %q",
tt.input, tt.width, result, tt.expected)
}
}
}
func TestMakeBar(t *testing.T) {
tests := []struct {
value int
maxVal int
width int
filledLen int
}{
{10, 10, 10, 10}, // Full bar
{5, 10, 10, 5}, // Half bar
{0, 10, 10, 0}, // Empty bar
{1, 100, 10, 1}, // Minimum 1 when value > 0
}
for _, tt := range tests {
bar := makeBar(tt.value, tt.maxVal, tt.width)
filled := 0
for _, r := range bar {
if r == '█' {
filled++
}
}
if filled != tt.filledLen {
t.Errorf("makeBar(%d, %d, %d) filled=%d, expected %d (bar=%q)",
tt.value, tt.maxVal, tt.width, filled, tt.filledLen, bar)
}
if len([]rune(bar)) != tt.width {
t.Errorf("makeBar(%d, %d, %d) width=%d, expected %d",
tt.value, tt.maxVal, tt.width, len([]rune(bar)), tt.width)
}
}
}
func TestMakeBarZeroMax(t *testing.T) {
bar := makeBar(0, 0, 10)
filled := 0
for _, r := range bar {
if r == '█' {
filled++
}
}
if filled != 0 {
t.Errorf("makeBar with maxVal=0 should have no filled chars, got %d", filled)
}
}
func TestMin(t *testing.T) {
if min(3, 5) != 3 {
t.Error("min(3, 5) should be 3")
}
if min(7, 2) != 2 {
t.Error("min(7, 2) should be 2")
}
if min(4, 4) != 4 {
t.Error("min(4, 4) should be 4")
}
}
func TestMax(t *testing.T) {
if max(3, 5) != 5 {
t.Error("max(3, 5) should be 5")
}
if max(7, 2) != 7 {
t.Error("max(7, 2) should be 7")
}
if max(4, 4) != 4 {
t.Error("max(4, 4) should be 4")
}
}
func TestContributionCell(t *testing.T) {
// Zero count should return dim block
cell := contributionCell(0, 10)
if cell == "" {
t.Error("contributionCell(0, 10) should not be empty")
}
// High count should return bold green block
cell = contributionCell(10, 10)
if cell == "" {
t.Error("contributionCell(10, 10) should not be empty")
}
// Mid count
cell = contributionCell(5, 10)
if cell == "" {
t.Error("contributionCell(5, 10) should not be empty")
}
}
func TestHourColor(t *testing.T) {
tests := []struct {
hour int
expected string
}{
{3, Magenta}, // Night
{8, Yellow}, // Morning
{14, Green}, // Afternoon
{20, Cyan}, // Evening
}
for _, tt := range tests {
result := hourColor(tt.hour)
if result != tt.expected {
t.Errorf("hourColor(%d) = %q, expected %q", tt.hour, result, tt.expected)
}
}
}
func TestBarColor(t *testing.T) {
// High ratio -> Red
if barColor(90, 100) != Red {
t.Error("expected Red for high ratio")
}
// Medium ratio -> Yellow
if barColor(60, 100) != Yellow {
t.Error("expected Yellow for medium ratio")
}
// Low ratio -> Green
if barColor(30, 100) != Green {
t.Error("expected Green for low ratio")
}
// Very low -> Cyan
if barColor(10, 100) != Cyan {
t.Error("expected Cyan for very low ratio")
}
}