@@ -24,7 +24,7 @@ func (s stringWrapper) String() string {
24
24
}
25
25
26
26
type tableTest1 struct {
27
- Name string `table:"name"`
27
+ Name string `table:"name,default_sort "`
28
28
NotIncluded string // no table tag
29
29
Age int `table:"age"`
30
30
Roles []string `table:"roles"`
@@ -39,21 +39,45 @@ type tableTest1 struct {
39
39
}
40
40
41
41
type tableTest2 struct {
42
- Name stringWrapper `table:"name"`
42
+ Name stringWrapper `table:"name,default_sort "`
43
43
Age int `table:"age"`
44
44
NotIncluded string `table:"-"`
45
45
}
46
46
47
47
type tableTest3 struct {
48
48
NotIncluded string // no table tag
49
- Sub tableTest2 `table:"inner,recursive"`
49
+ Sub tableTest2 `table:"inner,recursive,default_sort "`
50
50
}
51
51
52
52
func Test_DisplayTable (t * testing.T ) {
53
53
t .Parallel ()
54
54
55
55
someTime := time .Date (2022 , 8 , 2 , 15 , 49 , 10 , 0 , time .UTC )
56
+
57
+ // Not sorted by name or age to test sorting.
56
58
in := []tableTest1 {
59
+ {
60
+ Name : "bar" ,
61
+ Age : 20 ,
62
+ Roles : []string {"a" },
63
+ Sub1 : tableTest2 {
64
+ Name : stringWrapper {str : "bar1" },
65
+ Age : 21 ,
66
+ },
67
+ Sub2 : nil ,
68
+ Sub3 : tableTest3 {
69
+ Sub : tableTest2 {
70
+ Name : stringWrapper {str : "bar3" },
71
+ Age : 23 ,
72
+ },
73
+ },
74
+ Sub4 : tableTest2 {
75
+ Name : stringWrapper {str : "bar4" },
76
+ Age : 24 ,
77
+ },
78
+ Time : someTime ,
79
+ TimePtr : nil ,
80
+ },
57
81
{
58
82
Name : "foo" ,
59
83
Age : 10 ,
@@ -79,28 +103,6 @@ func Test_DisplayTable(t *testing.T) {
79
103
Time : someTime ,
80
104
TimePtr : & someTime ,
81
105
},
82
- {
83
- Name : "bar" ,
84
- Age : 20 ,
85
- Roles : []string {"a" },
86
- Sub1 : tableTest2 {
87
- Name : stringWrapper {str : "bar1" },
88
- Age : 21 ,
89
- },
90
- Sub2 : nil ,
91
- Sub3 : tableTest3 {
92
- Sub : tableTest2 {
93
- Name : stringWrapper {str : "bar3" },
94
- Age : 23 ,
95
- },
96
- },
97
- Sub4 : tableTest2 {
98
- Name : stringWrapper {str : "bar4" },
99
- Age : 24 ,
100
- },
101
- Time : someTime ,
102
- TimePtr : nil ,
103
- },
104
106
{
105
107
Name : "baz" ,
106
108
Age : 30 ,
@@ -132,9 +134,9 @@ func Test_DisplayTable(t *testing.T) {
132
134
133
135
expected := `
134
136
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
135
- foo 10 [a b c] foo1 11 foo2 12 foo3 13 {foo4 14 } 2022-08-02T15:49:10Z 2022-08-02T15:49:10Z
136
137
bar 20 [a] bar1 21 <nil> <nil> bar3 23 {bar4 24 } 2022-08-02T15:49:10Z <nil>
137
138
baz 30 [] baz1 31 <nil> <nil> baz3 33 {baz4 34 } 2022-08-02T15:49:10Z <nil>
139
+ foo 10 [a b c] foo1 11 foo2 12 foo3 13 {foo4 14 } 2022-08-02T15:49:10Z 2022-08-02T15:49:10Z
138
140
`
139
141
140
142
// Test with non-pointer values.
@@ -154,17 +156,17 @@ baz 30 [] baz1 31 <nil> <nil> baz3
154
156
compareTables (t , expected , out )
155
157
})
156
158
157
- t .Run ("Sort " , func (t * testing.T ) {
159
+ t .Run ("CustomSort " , func (t * testing.T ) {
158
160
t .Parallel ()
159
161
160
162
expected := `
161
163
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
164
+ foo 10 [a b c] foo1 11 foo2 12 foo3 13 {foo4 14 } 2022-08-02T15:49:10Z 2022-08-02T15:49:10Z
162
165
bar 20 [a] bar1 21 <nil> <nil> bar3 23 {bar4 24 } 2022-08-02T15:49:10Z <nil>
163
166
baz 30 [] baz1 31 <nil> <nil> baz3 33 {baz4 34 } 2022-08-02T15:49:10Z <nil>
164
- foo 10 [a b c] foo1 11 foo2 12 foo3 13 {foo4 14 } 2022-08-02T15:49:10Z 2022-08-02T15:49:10Z
165
167
`
166
168
167
- out , err := cliui .DisplayTable (in , "name " , nil )
169
+ out , err := cliui .DisplayTable (in , "age " , nil )
168
170
log .Println ("rendered table:\n " + out )
169
171
require .NoError (t , err )
170
172
compareTables (t , expected , out )
@@ -175,9 +177,9 @@ foo 10 [a b c] foo1 11 foo2 12 foo3
175
177
176
178
expected := `
177
179
NAME SUB 1 NAME SUB 3 INNER NAME TIME
178
- foo foo1 foo3 2022-08-02T15:49:10Z
179
180
bar bar1 bar3 2022-08-02T15:49:10Z
180
181
baz baz1 baz3 2022-08-02T15:49:10Z
182
+ foo foo1 foo3 2022-08-02T15:49:10Z
181
183
`
182
184
183
185
out , err := cliui .DisplayTable (in , "" , []string {"name" , "sub_1_name" , "sub_3 inner name" , "time" })
@@ -327,28 +329,6 @@ baz baz1 baz3 2022-08-02T15:49:10Z
327
329
})
328
330
}
329
331
330
- func Test_TableHeaders (t * testing.T ) {
331
- t .Parallel ()
332
- s := []tableTest1 {}
333
- expectedFields := []string {
334
- "name" ,
335
- "age" ,
336
- "roles" ,
337
- "sub_1_name" ,
338
- "sub_1_age" ,
339
- "sub_2_name" ,
340
- "sub_2_age" ,
341
- "sub_3_inner_name" ,
342
- "sub_3_inner_age" ,
343
- "sub_4" ,
344
- "time" ,
345
- "time_ptr" ,
346
- }
347
- headers , err := cliui .TableHeaders (s )
348
- require .NoError (t , err )
349
- require .EqualValues (t , expectedFields , headers )
350
- }
351
-
352
332
// compareTables normalizes the incoming table lines
353
333
func compareTables (t * testing.T , expected , out string ) {
354
334
t .Helper ()
0 commit comments