-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathformatter_test.go
More file actions
71 lines (62 loc) · 1.35 KB
/
formatter_test.go
File metadata and controls
71 lines (62 loc) · 1.35 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
// FIXME(thaJeztah): remove once we are a module; the go:build directive prevents go from downgrading language version to go1.16:
//go:build go1.25
package formatter
import (
"bytes"
"testing"
"gotest.tools/v3/assert"
)
func TestFormat(t *testing.T) {
f := Format("json")
assert.Assert(t, f.IsJSON())
assert.Assert(t, !f.IsTable())
f = Format("table")
assert.Assert(t, !f.IsJSON())
assert.Assert(t, f.IsTable())
f = Format("other")
assert.Assert(t, !f.IsJSON())
assert.Assert(t, !f.IsTable())
}
type fakeSubContext struct {
Name string
}
func (fakeSubContext) FullHeader() any {
return map[string]string{"Name": "NAME"}
}
func TestContext(t *testing.T) {
testCases := []struct {
name string
format string
expected string
}{
{
name: "json format",
format: JSONFormatKey,
expected: `{"Name":"test"}
`,
},
{
name: "table format",
format: `table {{.Name}}`,
expected: `NAME
test
`,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
buf := bytes.NewBuffer(nil)
ctx := Context{
Format: Format(tc.format),
Output: buf,
}
subContext := fakeSubContext{Name: "test"}
subFormat := func(f func(sub SubContext) error) error {
return f(subContext)
}
err := ctx.Write(&subContext, subFormat)
assert.NilError(t, err)
assert.Equal(t, buf.String(), tc.expected)
})
}
}