1
1
package cliui_test
2
2
3
3
import (
4
+ "database/sql"
4
5
"fmt"
5
6
"log"
6
7
"strings"
@@ -11,6 +12,7 @@ import (
11
12
"github.com/stretchr/testify/require"
12
13
13
14
"github.com/coder/coder/v2/cli/cliui"
15
+ "github.com/coder/coder/v2/codersdk"
14
16
)
15
17
16
18
type stringWrapper struct {
@@ -24,18 +26,20 @@ func (s stringWrapper) String() string {
24
26
}
25
27
26
28
type tableTest1 struct {
27
- Name string `table:"name,default_sort"`
28
- NotIncluded string // no table tag
29
- Age int `table:"age"`
30
- Roles []string `table:"roles"`
31
- Sub1 tableTest2 `table:"sub_1,recursive"`
32
- Sub2 * tableTest2 `table:"sub_2,recursive"`
33
- Sub3 tableTest3 `table:"sub 3,recursive"`
34
- Sub4 tableTest2 `table:"sub 4"` // not recursive
29
+ Name string `table:"name,default_sort"`
30
+ AltName * stringWrapper `table:"alt_name"`
31
+ NotIncluded string // no table tag
32
+ Age int `table:"age"`
33
+ Roles []string `table:"roles"`
34
+ Sub1 tableTest2 `table:"sub_1,recursive"`
35
+ Sub2 * tableTest2 `table:"sub_2,recursive"`
36
+ Sub3 tableTest3 `table:"sub 3,recursive"`
37
+ Sub4 tableTest2 `table:"sub 4"` // not recursive
35
38
36
39
// Types with special formatting.
37
- Time time.Time `table:"time"`
38
- TimePtr * time.Time `table:"time_ptr"`
40
+ Time time.Time `table:"time"`
41
+ TimePtr * time.Time `table:"time_ptr"`
42
+ NullTime codersdk.NullTime `table:"null_time"`
39
43
}
40
44
41
45
type tableTest2 struct {
@@ -62,9 +66,10 @@ func Test_DisplayTable(t *testing.T) {
62
66
// Not sorted by name or age to test sorting.
63
67
in := []tableTest1 {
64
68
{
65
- Name : "bar" ,
66
- Age : 20 ,
67
- Roles : []string {"a" },
69
+ Name : "bar" ,
70
+ AltName : & stringWrapper {str : "bar alt" },
71
+ Age : 20 ,
72
+ Roles : []string {"a" },
68
73
Sub1 : tableTest2 {
69
74
Name : stringWrapper {str : "bar1" },
70
75
Age : 21 ,
@@ -82,6 +87,12 @@ func Test_DisplayTable(t *testing.T) {
82
87
},
83
88
Time : someTime ,
84
89
TimePtr : nil ,
90
+ NullTime : codersdk.NullTime {
91
+ NullTime : sql.NullTime {
92
+ Time : someTime ,
93
+ Valid : true ,
94
+ },
95
+ },
85
96
},
86
97
{
87
98
Name : "foo" ,
@@ -138,10 +149,10 @@ func Test_DisplayTable(t *testing.T) {
138
149
t .Parallel ()
139
150
140
151
expected := `
141
- 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
142
- bar 20 [a] bar1 21 <nil> <nil> bar3 23 {bar4 24 } 2022-08-02T15:49:10Z <nil>
143
- baz 30 [] baz1 31 <nil> <nil> baz3 33 {baz4 34 } 2022-08-02T15:49:10Z <nil>
144
- foo 10 [a, b, c] foo1 11 foo2 12 foo3 13 {foo4 14 } 2022-08-02T15:49:10Z 2022-08-02T15:49:10Z
152
+ NAME ALT 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 NULL TIME
153
+ bar bar alt 20 [a] bar1 21 <nil> <nil> bar3 23 {bar4 24 } 2022-08-02T15:49:10Z <nil> 2022-08-02T15:49:10Z
154
+ baz <nil> 30 [] baz1 31 <nil> <nil> baz3 33 {baz4 34 } 2022-08-02T15:49:10Z <nil> <nil>
155
+ foo <nil> 10 [a, b, c] foo1 11 foo2 12 foo3 13 {foo4 14 } 2022-08-02T15:49:10Z 2022-08-02T15:49:10Z <nil>
145
156
`
146
157
147
158
// Test with non-pointer values.
@@ -165,10 +176,10 @@ foo 10 [a, b, c] foo1 11 foo2 12 foo3
165
176
t .Parallel ()
166
177
167
178
expected := `
168
- 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
169
- foo 10 [a, b, c] foo1 11 foo2 12 foo3 13 {foo4 14 } 2022-08-02T15:49:10Z 2022-08-02T15:49:10Z
170
- bar 20 [a] bar1 21 <nil> <nil> bar3 23 {bar4 24 } 2022-08-02T15:49:10Z <nil>
171
- baz 30 [] baz1 31 <nil> <nil> baz3 33 {baz4 34 } 2022-08-02T15:49:10Z <nil>
179
+ NAME ALT 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 NULL TIME
180
+ foo <nil> 10 [a, b, c] foo1 11 foo2 12 foo3 13 {foo4 14 } 2022-08-02T15:49:10Z 2022-08-02T15:49:10Z <nil>
181
+ bar bar alt 20 [a] bar1 21 <nil> <nil> bar3 23 {bar4 24 } 2022-08-02T15:49:10Z <nil> 2022-08-02T15:49:10Z
182
+ baz <nil> 30 [] baz1 31 <nil> <nil> baz3 33 {baz4 34 } 2022-08-02T15:49:10Z <nil> <nil>
172
183
`
173
184
174
185
out , err := cliui .DisplayTable (in , "age" , nil )
@@ -235,12 +246,12 @@ Alice 25
235
246
t .Run ("WithSeparator" , func (t * testing.T ) {
236
247
t .Parallel ()
237
248
expected := `
238
- 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
239
- bar 20 [a] bar1 21 <nil> <nil> bar3 23 {bar4 24 } 2022-08-02T15:49:10Z <nil>
240
- ---------------------------------------------------------------------------------------------------------------------------------------------------------------
241
- baz 30 [] baz1 31 <nil> <nil> baz3 33 {baz4 34 } 2022-08-02T15:49:10Z <nil>
242
- ---------------------------------------------------------------------------------------------------------------------------------------------------------------
243
- foo 10 [a, b, c] foo1 11 foo2 12 foo3 13 {foo4 14 } 2022-08-02T15:49:10Z 2022-08-02T15:49:10Z
249
+ NAME ALT 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 NULL TIME
250
+ bar bar alt 20 [a] bar1 21 <nil> <nil> bar3 23 {bar4 24 } 2022-08-02T15:49:10Z <nil> 2022-08-02T15:49:10Z
251
+ -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
252
+ baz <nil> 30 [] baz1 31 <nil> <nil> baz3 33 {baz4 34 } 2022-08-02T15:49:10Z <nil> <nil>
253
+ -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
254
+ foo <nil> 10 [a, b, c] foo1 11 foo2 12 foo3 13 {foo4 14 } 2022-08-02T15:49:10Z 2022-08-02T15:49:10Z <nil>
244
255
`
245
256
246
257
var inlineIn []any
0 commit comments