This repository has been archived by the owner on Apr 28, 2023. It is now read-only.
/
table_test.go
260 lines (247 loc) · 5.54 KB
/
table_test.go
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package table
import (
"fmt"
"strings"
"testing"
"time"
"github.com/makkes/gitlab-cli/api"
)
func checkColumn(t *testing.T, cols map[string]int, col string, width int) {
if cols[col] != width {
t.Errorf("'%s' column has unexpected width %d", col, cols[col])
}
}
func TestPipelineColumnWidths(t *testing.T) {
var pipelineColumnWidthTests = []struct {
name string
in []api.PipelineDetails
out map[string]int
}{
{
"empty input",
[]api.PipelineDetails{},
map[string]int{
"id": 20,
"status": 20,
"duration": 10,
"url": 50,
},
},
{
"nil input",
nil,
map[string]int{
"id": 20,
"status": 20,
"duration": 10,
"url": 50,
},
},
{
"happy path",
[]api.PipelineDetails{
{
ID: 99,
Status: "this is a status with more than 20 characters",
URL: "This is a uniform resource locator with more than 50 characters",
RecordedDuration: func() *int { i := int(50); return &i }(),
},
},
map[string]int{
"id": 20,
"status": 45,
"url": 63,
"duration": 10,
},
},
}
for _, tt := range pipelineColumnWidthTests {
t.Run(tt.name, func(t *testing.T) {
res := calcPipelineColumnWidths(tt.in, time.Now())
for k, v := range tt.out {
checkColumn(t, res, k, v)
}
})
}
}
func TestProjectColumnWidths(t *testing.T) {
var projectColumnWidthTests = []struct {
name string
in []api.Project
out map[string]int
}{
{
"empty input",
[]api.Project{},
map[string]int{
"id": 15,
"name": 40,
"url": 50,
"clone": 50,
},
},
{
"nil input",
nil,
map[string]int{
"id": 15,
"name": 40,
"url": 50,
"clone": 50,
},
},
{
"happy path",
[]api.Project{
{
ID: 99,
Name: "this is a name with more than 40 characters",
URL: "This is a uniform resource locator with more than 50 characters",
},
},
map[string]int{
"id": 15,
"name": 43,
"url": 63,
"clone": 50,
},
},
}
for _, tt := range projectColumnWidthTests {
t.Run(tt.name, func(t *testing.T) {
res := calcProjectColumnWidths(tt.in)
if len(res) != len(tt.out) {
t.Errorf("Expected map with %d entries but got %d", len(tt.out), len(res))
}
for k, v := range tt.out {
checkColumn(t, res, k, v)
}
})
}
}
func TestIssuesTable(t *testing.T) {
var issuesTableTests = []struct {
name string
writer *strings.Builder
issues []api.Issue
out string
}{
{
"empty input",
&strings.Builder{},
[]api.Issue{},
"ID TITLE STATE URL \n",
},
{
"nil input",
&strings.Builder{},
nil,
"ID TITLE STATE URL \n",
},
{
"happy path",
&strings.Builder{},
[]api.Issue{
{
ID: 99,
Title: "this is a name with more than 40 characters",
State: "this is a loong state",
URL: "This is a uniform resource locator with more than 50 characters",
},
},
`ID TITLE STATE URL
0:99 this is a name with more than… this is a loong state This is a uniform resource locator with more than 50 characters
`,
},
}
for _, tt := range issuesTableTests {
t.Run(tt.name, func(t *testing.T) {
PrintIssues(tt.writer, tt.issues)
if tt.writer.String() != tt.out {
t.Errorf("Unexpected result: '%s'", tt.writer.String())
}
})
}
}
func TestVarsTable(t *testing.T) {
varsTableTests := []struct {
name string
writer *strings.Builder
vars []api.Var
out string
}{
{
"empty input",
&strings.Builder{},
[]api.Var{},
"KEY VALUE PROTECTED\n",
},
{
"nil input",
&strings.Builder{},
nil,
"KEY VALUE PROTECTED\n",
},
{
"happy path",
&strings.Builder{},
[]api.Var{
{
Key: "key 1",
Value: "value 1",
Protected: false,
},
{
Key: "",
Value: "",
Protected: true,
},
{
Key: "",
Value: "some value",
Protected: false,
},
{
Key: "some key",
Value: "",
Protected: false,
},
},
`KEY VALUE PROTECTED
key 1 value 1 false
true
some value false
some key false
`,
},
}
for _, tt := range varsTableTests {
t.Run(tt.name, func(t *testing.T) {
PrintVars(tt.writer, tt.vars)
if tt.writer.String() != tt.out {
t.Errorf("Unexpected result: '%s'", tt.writer.String())
}
})
}
}
func TestPad(t *testing.T) {
var padTable = []struct {
s string
w int
out string
}{
{"", 0, ""},
{"", 10, " "},
{"don't shorten me", 0, "don't shorten me"},
{"i'm too short", 20, "i'm too short "},
{"not padded when negative", -100, "not padded when negative"},
}
for _, tt := range padTable {
t.Run(fmt.Sprintf("'%s':%d", tt.s, tt.w), func(t *testing.T) {
res := pad(tt.s, tt.w)
if res != tt.out {
t.Errorf("Expected '%s' to be '%s'", res, tt.out)
}
})
}
}