-
Notifications
You must be signed in to change notification settings - Fork 1
/
print_test.go
150 lines (136 loc) · 2.58 KB
/
print_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
package cli
import (
"bytes"
"encoding/json"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"
)
func TestPrintJSON(t *testing.T) {
type someType struct {
Name string
Age int
}
foo := someType{
Name: "Test",
Age: 42,
}
out := new(bytes.Buffer)
require.NoError(t, PrintWriter("json", foo, out))
assert.NotEmpty(t, out)
t.Log("\n" + out.String())
var bar someType
require.NoError(t, json.Unmarshal(out.Bytes(), &bar))
assert.Equal(t, foo, bar)
}
func TestPrintYAML(t *testing.T) {
type someType struct {
Name string
Age int
}
foo := someType{
Name: "Test",
Age: 42,
}
out := new(bytes.Buffer)
require.NoError(t, PrintWriter("yaml", foo, out))
assert.NotEmpty(t, out)
t.Log("\n" + out.String())
var bar someType
require.NoError(t, yaml.Unmarshal(out.Bytes(), &bar))
assert.Equal(t, foo, bar)
}
func TestPrintTable(t *testing.T) {
cases := map[string]struct {
instance interface{}
expected []string
}{
"no tags": {
instance: struct {
Name string
Age int
Value bool
}{
Name: "Test",
Age: 42,
Value: true,
},
expected: []string{
"NAME AGE VALUE",
"Test 42 true ",
},
},
"with ignore tags": {
instance: struct {
Name string
Age int
Value bool `table:"-"`
}{
Name: "Test",
Age: 42,
Value: true,
},
expected: []string{
"NAME AGE",
"Test 42 ",
},
},
"rename columns": {
instance: struct {
Name string `table:"key"`
Age int `table:"age"`
Value bool `table:"-"`
}{
Name: "Test",
Age: 42,
Value: true,
},
expected: []string{
"key age",
"Test 42 ",
},
},
"slice": {
instance: []struct {
Name string
Age int
Value bool
}{
{Name: "Foo", Age: 1, Value: true},
{Name: "Bar", Age: 2, Value: false},
{Name: "Baz", Age: 3, Value: false},
},
expected: []string{
"NAME AGE VALUE",
"Foo 1 true ",
"Bar 2 false ",
"Baz 3 false ",
},
},
"slice of strings": {
instance: []string{"A", "B", "C"},
expected: []string{
"A",
"B",
"C",
},
},
"slice of ints": {
instance: []int{1, 2, 3},
expected: []string{
"1",
"2",
"3",
},
},
}
for name, c := range cases {
t.Run(name, func(t *testing.T) {
out := new(bytes.Buffer)
require.NoError(t, PrintWriter("table", c.instance, out))
assert.Equal(t, strings.Join(c.expected, "\n")+"\n", out.String())
})
}
}