forked from coder/coder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable_test.go
More file actions
354 lines (292 loc) · 8.69 KB
/
table_test.go
File metadata and controls
354 lines (292 loc) · 8.69 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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package cliui_test
import (
"fmt"
"log"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/coder/coder/cli/cliui"
)
type stringWrapper struct {
str string
}
var _ fmt.Stringer = stringWrapper{}
func (s stringWrapper) String() string {
return s.str
}
type tableTest1 struct {
Name string `table:"name,default_sort"`
NotIncluded string // no table tag
Age int `table:"age"`
Roles []string `table:"roles"`
Sub1 tableTest2 `table:"sub_1,recursive"`
Sub2 *tableTest2 `table:"sub_2,recursive"`
Sub3 tableTest3 `table:"sub 3,recursive"`
Sub4 tableTest2 `table:"sub 4"` // not recursive
// Types with special formatting.
Time time.Time `table:"time"`
TimePtr *time.Time `table:"time_ptr"`
}
type tableTest2 struct {
Name stringWrapper `table:"name,default_sort"`
Age int `table:"age"`
NotIncluded string `table:"-"`
}
type tableTest3 struct {
NotIncluded string // no table tag
Sub tableTest2 `table:"inner,recursive,default_sort"`
}
func Test_DisplayTable(t *testing.T) {
t.Parallel()
someTime := time.Date(2022, 8, 2, 15, 49, 10, 0, time.UTC)
// Not sorted by name or age to test sorting.
in := []tableTest1{
{
Name: "bar",
Age: 20,
Roles: []string{"a"},
Sub1: tableTest2{
Name: stringWrapper{str: "bar1"},
Age: 21,
},
Sub2: nil,
Sub3: tableTest3{
Sub: tableTest2{
Name: stringWrapper{str: "bar3"},
Age: 23,
},
},
Sub4: tableTest2{
Name: stringWrapper{str: "bar4"},
Age: 24,
},
Time: someTime,
TimePtr: nil,
},
{
Name: "foo",
Age: 10,
Roles: []string{"a", "b", "c"},
Sub1: tableTest2{
Name: stringWrapper{str: "foo1"},
Age: 11,
},
Sub2: &tableTest2{
Name: stringWrapper{str: "foo2"},
Age: 12,
},
Sub3: tableTest3{
Sub: tableTest2{
Name: stringWrapper{str: "foo3"},
Age: 13,
},
},
Sub4: tableTest2{
Name: stringWrapper{str: "foo4"},
Age: 14,
},
Time: someTime,
TimePtr: &someTime,
},
{
Name: "baz",
Age: 30,
Roles: nil,
Sub1: tableTest2{
Name: stringWrapper{str: "baz1"},
Age: 31,
},
Sub2: nil,
Sub3: tableTest3{
Sub: tableTest2{
Name: stringWrapper{str: "baz3"},
Age: 33,
},
},
Sub4: tableTest2{
Name: stringWrapper{str: "baz4"},
Age: 34,
},
Time: someTime,
TimePtr: nil,
},
}
// This test tests skipping fields without table tags, recursion, pointer
// dereferencing, and nil pointer skipping.
t.Run("OK", func(t *testing.T) {
t.Parallel()
expected := `
NAME AGE ROLES SUB 1 NAME SUB 1 AGE SUB 2 NAME SUB 2 AGE SUB 3 INNER NAME SUB 3 INNER AGE SUB 4 TIME TIME PTR
bar 20 [a] bar1 21 <nil> <nil> bar3 23 {bar4 24 } 2022-08-02T15:49:10Z <nil>
baz 30 [] baz1 31 <nil> <nil> baz3 33 {baz4 34 } 2022-08-02T15:49:10Z <nil>
foo 10 [a b c] foo1 11 foo2 12 foo3 13 {foo4 14 } 2022-08-02T15:49:10Z 2022-08-02T15:49:10Z
`
// Test with non-pointer values.
out, err := cliui.DisplayTable(in, "", nil)
log.Println("rendered table:\n" + out)
require.NoError(t, err)
compareTables(t, expected, out)
// Test with pointer values.
inPtr := make([]*tableTest1, len(in))
for i, v := range in {
v := v
inPtr[i] = &v
}
out, err = cliui.DisplayTable(inPtr, "", nil)
require.NoError(t, err)
compareTables(t, expected, out)
})
t.Run("CustomSort", func(t *testing.T) {
t.Parallel()
expected := `
NAME AGE ROLES SUB 1 NAME SUB 1 AGE SUB 2 NAME SUB 2 AGE SUB 3 INNER NAME SUB 3 INNER AGE SUB 4 TIME TIME PTR
foo 10 [a b c] foo1 11 foo2 12 foo3 13 {foo4 14 } 2022-08-02T15:49:10Z 2022-08-02T15:49:10Z
bar 20 [a] bar1 21 <nil> <nil> bar3 23 {bar4 24 } 2022-08-02T15:49:10Z <nil>
baz 30 [] baz1 31 <nil> <nil> baz3 33 {baz4 34 } 2022-08-02T15:49:10Z <nil>
`
out, err := cliui.DisplayTable(in, "age", nil)
log.Println("rendered table:\n" + out)
require.NoError(t, err)
compareTables(t, expected, out)
})
t.Run("Filter", func(t *testing.T) {
t.Parallel()
expected := `
NAME SUB 1 NAME SUB 3 INNER NAME TIME
bar bar1 bar3 2022-08-02T15:49:10Z
baz baz1 baz3 2022-08-02T15:49:10Z
foo foo1 foo3 2022-08-02T15:49:10Z
`
out, err := cliui.DisplayTable(in, "", []string{"name", "sub_1_name", "sub_3 inner name", "time"})
log.Println("rendered table:\n" + out)
require.NoError(t, err)
compareTables(t, expected, out)
})
// This test ensures that safeties against invalid use of `table` tags
// causes errors (even without data).
t.Run("Errors", func(t *testing.T) {
t.Parallel()
t.Run("NotSlice", func(t *testing.T) {
t.Parallel()
var in string
_, err := cliui.DisplayTable(in, "", nil)
require.Error(t, err)
})
t.Run("BadSortColumn", func(t *testing.T) {
t.Parallel()
_, err := cliui.DisplayTable(in, "bad_column_does_not_exist", nil)
require.Error(t, err)
})
t.Run("BadFilterColumns", func(t *testing.T) {
t.Parallel()
_, err := cliui.DisplayTable(in, "", []string{"name", "bad_column_does_not_exist"})
require.Error(t, err)
})
t.Run("Interfaces", func(t *testing.T) {
t.Parallel()
t.Run("WithoutData", func(t *testing.T) {
t.Parallel()
var in []any
_, err := cliui.DisplayTable(in, "", nil)
require.Error(t, err)
})
t.Run("WithData", func(t *testing.T) {
t.Parallel()
in := []any{tableTest1{}}
_, err := cliui.DisplayTable(in, "", nil)
require.Error(t, err)
})
})
t.Run("NotStruct", func(t *testing.T) {
t.Parallel()
t.Run("WithoutData", func(t *testing.T) {
t.Parallel()
var in []string
_, err := cliui.DisplayTable(in, "", nil)
require.Error(t, err)
})
t.Run("WithData", func(t *testing.T) {
t.Parallel()
in := []string{"foo", "bar", "baz"}
_, err := cliui.DisplayTable(in, "", nil)
require.Error(t, err)
})
})
t.Run("NoTableTags", func(t *testing.T) {
t.Parallel()
type noTableTagsTest struct {
Field string `json:"field"`
}
t.Run("WithoutData", func(t *testing.T) {
t.Parallel()
var in []noTableTagsTest
_, err := cliui.DisplayTable(in, "", nil)
require.Error(t, err)
})
t.Run("WithData", func(t *testing.T) {
t.Parallel()
in := []noTableTagsTest{{Field: "hi"}}
_, err := cliui.DisplayTable(in, "", nil)
require.Error(t, err)
})
})
t.Run("InvalidTag/NoName", func(t *testing.T) {
t.Parallel()
type noNameTest struct {
Field string `table:""`
}
t.Run("WithoutData", func(t *testing.T) {
t.Parallel()
var in []noNameTest
_, err := cliui.DisplayTable(in, "", nil)
require.Error(t, err)
})
t.Run("WithData", func(t *testing.T) {
t.Parallel()
in := []noNameTest{{Field: "test"}}
_, err := cliui.DisplayTable(in, "", nil)
require.Error(t, err)
})
})
t.Run("InvalidTag/BadSyntax", func(t *testing.T) {
t.Parallel()
type invalidSyntaxTest struct {
Field string `table:"asda,asdjada"`
}
t.Run("WithoutData", func(t *testing.T) {
t.Parallel()
var in []invalidSyntaxTest
_, err := cliui.DisplayTable(in, "", nil)
require.Error(t, err)
})
t.Run("WithData", func(t *testing.T) {
t.Parallel()
in := []invalidSyntaxTest{{Field: "test"}}
_, err := cliui.DisplayTable(in, "", nil)
require.Error(t, err)
})
})
})
}
// compareTables normalizes the incoming table lines
func compareTables(t *testing.T, expected, out string) {
t.Helper()
expectedLines := strings.Split(strings.TrimSpace(expected), "\n")
gotLines := strings.Split(strings.TrimSpace(out), "\n")
assert.Equal(t, len(expectedLines), len(gotLines), "expected line count does not match generated line count")
// Map the expected and got lines to normalize them.
expectedNormalized := make([]string, len(expectedLines))
gotNormalized := make([]string, len(gotLines))
normalizeLine := func(s string) string {
return strings.Join(strings.Fields(strings.TrimSpace(s)), " ")
}
for i, s := range expectedLines {
expectedNormalized[i] = normalizeLine(s)
}
for i, s := range gotLines {
gotNormalized[i] = normalizeLine(s)
}
require.Equal(t, expectedNormalized, gotNormalized, "expected lines to match generated lines")
}