-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathprint_test.go
More file actions
162 lines (149 loc) · 3.26 KB
/
print_test.go
File metadata and controls
162 lines (149 loc) · 3.26 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
package main
import (
"io"
"os"
"testing"
"time"
"github.com/gookit/color"
)
func TestGetDay(t *testing.T) {
tests := []struct {
name string
day int
want string
}{
{
name: "Monday",
day: 1,
want: "Mon",
},
{
name: "Wednesday",
day: 3,
want: "Wed",
},
{
name: "Friday",
day: 5,
want: "Fri",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := getDay(tt.day); got != tt.want {
t.Errorf("getDay() = %v, want %v", got, tt.want)
}
})
}
}
func TestBuildHeader(t *testing.T) {
type args struct {
start time.Time
end time.Time
}
tests := []struct {
name string
args args
want string
}{
{
name: "test 1",
args: args{
start: time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC),
end: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC),
},
want: " Jan ",
},
{
name: "test 2",
args: args{
start: time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC),
end: time.Date(2023, 3, 1, 0, 0, 0, 0, time.UTC),
},
want: " Jan Feb ",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := buildHeader(tt.args.start, tt.args.end); got != tt.want {
t.Errorf("buildHeader() = %q, want %q", got, tt.want)
}
})
}
}
func TestPrintCell(t *testing.T) {
testCases := []struct {
description string
val int
maxValue int
expectedMessage string
}{
{
description: "Zero value",
val: 0,
maxValue: 10,
expectedMessage: color.New(color.FgLightWhite, color.BgBlack).Sprintf(" - "),
},
{
description: "Lower bound value (/8)",
val: 1,
maxValue: 10,
expectedMessage: color.New(color.FgBlack, color.BgLightCyan).Sprintf(" 1 "),
},
{
description: "Lower bound value (/4)",
val: 2,
maxValue: 10,
expectedMessage: color.New(color.FgBlack, color.BgLightCyan).Sprintf(" 2 "),
},
{
description: "Middle value",
val: 5,
maxValue: 10,
expectedMessage: color.New(color.FgBlack, color.BgHiCyan).Sprintf(" 5 "),
},
{
description: "Upper bound value 1",
val: 7,
maxValue: 10,
expectedMessage: color.New(color.FgBlack, color.BgHiBlue).Sprintf(" 7 "),
},
{
description: "Upper bound value 2",
val: 10,
maxValue: 10,
expectedMessage: color.New(color.FgBlack, color.BgBlue).Sprintf(" 10 "),
},
}
for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
actualMessage := printCell(tc.val, tc.maxValue)
if actualMessage != tc.expectedMessage {
t.Errorf("Expected message: %q, got: %q", tc.expectedMessage, actualMessage)
}
})
}
}
func TestPrintTable(t *testing.T) {
commits := map[int]int{
0: 5,
1: 8,
}
b := Boundary{
Since: time.Date(2024, 2, 7, 0, 0, 0, 0, time.UTC),
Until: time.Date(2024, 2, 19, 0, 0, 0, 0, time.UTC),
}
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
printTable(commits, b)
w.Close()
dat, err := io.ReadAll(r)
if err != nil {
t.Errorf("Error reading from pipe: %s", err.Error())
}
os.Stdout = oldStdout
if len(dat) == 0 {
t.Error("Expected some output from printTable, got nothing")
}
}