9
"github.com/stretchr/testify/assert"
12
func TestList(t *testing.T) {
24
wantErr assert.ErrorAssertionFunc
27
name: "print plain json",
31
wantOutput: fmt.Sprintf("[%s,%s,%s]\n", shortEntityPlainJSON, longEntityPlainJSON, emptyEntityPlainJSON),
32
wantErr: assert.NoError,
35
name: "print indented json",
39
wantOutput: fmt.Sprintf("[\n%s,\n%s,\n%s\n]\n", shortEntityIndentedJSON, longEntityIndentedJSON, emptyEntityIndentedJSON),
40
wantErr: assert.NoError,
43
name: "print empty json",
48
wantErr: assert.NoError,
51
name: "print ascii table",
55
wantOutput: fmt.Sprintf("%s\n%s\n%s\n%s\n%s\n",
56
testEntityASCIIHeader,
60
testEntityASCIISeparator,
62
wantErr: assert.NoError,
65
name: "print empty ascii table",
70
wantErr: assert.NoError,
73
name: "print tabbed table",
77
wantOutput: fmt.Sprintf("%s\n%s\n%s\n%s\n",
78
testEntityTabbedHeader,
83
wantErr: assert.NoError,
86
name: "print empty tabbed table",
91
wantErr: assert.NoError,
94
for _, tt := range tests {
95
t.Run(tt.name, func(t *testing.T) {
96
output := new(bytes.Buffer)
97
err := List(tt.entities, output, tt.pretty, tt.json)
98
t.Logf("actual output:\n%s<end>", output)
99
if !tt.wantErr(t, err) {
102
assert.Equal(t, tt.wantOutput, output.String())
107
type testEntity struct {
109
APretty string `json:"-"`
111
CJSONOnly string `json:"c,omitempty"`
114
func (e testEntity) PrintableFields() []TableField {
118
PrettyName: "A (pretty)",
120
PrettyValue: &e.APretty,
124
PrettyName: "B (pretty)",
132
testEntityASCIISeparator = "+---+----------------------------------------------------------+------------+"
134
testEntityASCIIHeader = strings.Join(
136
testEntityASCIISeparator,
137
"| # | A (PRETTY) | B (PRETTY) |",
138
testEntityASCIISeparator,
143
testEntityTabbedHeader = "a b"
147
shortEntity = testEntity{
149
APretty: "Thu, 23 Aug 2023 09:26:25",
154
shortEntityPlainJSON = `{"a":"1692753495","b":"123","c":"kek"}`
156
shortEntityIndentedJSON = ` {
162
shortEntityASCIIRow = `| 0 | Thu, 23 Aug 2023 09:26:25 | 123 |`
164
shortEntityTabbedRow = `1692753495 123`
168
longValue = "lorem ipsum dolor sit amet consectetur adipiscing elit"
169
prettyLongValue = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
171
longEntity = testEntity{
173
APretty: prettyLongValue,
175
CJSONOnly: "details",
178
longEntityPlainJSON = `{"a":"lorem ipsum dolor sit amet consectetur adipiscing elit","b":"some value","c":"details"}`
180
longEntityIndentedJSON = ` {
181
"a": "lorem ipsum dolor sit amet consectetur adipiscing elit",
186
longEntityASCIIRow = `| 1 | Lorem ipsum dolor sit amet, consectetur adipiscing elit. | some value |`
188
longEntityTabbedRow = `lorem ipsum dolor sit amet consectetur adipiscing elit some value`
192
emptyEntity = testEntity{
199
emptyEntityPlainJSON = `{"a":"","b":""}`
201
emptyEntityIndentedJSON = ` {
206
emptyEntityASCIIRow = `| 2 | | |`
208
emptyEntityTabbedRow = ` `