Skip to content

Commit 66f7c22

Browse files
cppforlifemaximilien
authored andcommitted
added ui/test package for test helpers
Signed-off-by: Michael Maximilien <[email protected]>
1 parent dd4bc44 commit 66f7c22

File tree

4 files changed

+117
-6
lines changed

4 files changed

+117
-6
lines changed

ui/json_ui.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@ import (
1111

1212
type JSONUI struct {
1313
parent UI
14-
uiResp uiResp
14+
uiResp JSONUIResp
1515

1616
logTag string
1717
logger ExternalLogger
1818
}
1919

20-
type uiResp struct {
21-
Tables []tableResp
20+
type JSONUIResp struct {
21+
Tables []JSONUITableResp
2222
Blocks []string
2323
Lines []string
2424
}
2525

26-
type tableResp struct {
26+
type JSONUITableResp struct {
2727
Content string
2828
Header map[string]string
2929
Rows []map[string]string
@@ -88,7 +88,7 @@ func (ui *JSONUI) PrintTable(table Table) {
8888
table.Header = rawHeaders
8989
}
9090

91-
resp := tableResp{
91+
resp := JSONUITableResp{
9292
Content: table.Content,
9393
Header: header,
9494
Rows: ui.stringRows(table.Header, table.AsRows()),
@@ -121,7 +121,7 @@ func (ui *JSONUI) IsInteractive() bool {
121121
func (ui *JSONUI) Flush() {
122122
defer ui.parent.Flush()
123123

124-
if !reflect.DeepEqual(ui.uiResp, uiResp{}) {
124+
if !reflect.DeepEqual(ui.uiResp, JSONUIResp{}) {
125125
bytes, err := json.MarshalIndent(ui.uiResp, "", " ")
126126
if err != nil {
127127
ui.logger.Error(ui.logTag, "Failed to marshal UI response")

ui/test/json_ui.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package test
2+
3+
import (
4+
"encoding/json"
5+
"testing"
6+
7+
"github.com/cppforlife/go-cli-ui/ui"
8+
)
9+
10+
func JSONUIFromBytes(t *testing.T, bytes []byte) ui.JSONUIResp {
11+
resp := ui.JSONUIResp{}
12+
13+
err := json.Unmarshal(bytes, &resp)
14+
if err != nil {
15+
t.Fatalf("Expected to successfully unmarshal JSON UI: %s", err)
16+
}
17+
18+
return resp
19+
}

ui/test/json_ui_test.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package test_test
2+
3+
import (
4+
. "github.com/onsi/ginkgo"
5+
. "github.com/onsi/gomega"
6+
7+
"github.com/cppforlife/go-cli-ui/ui"
8+
. "github.com/cppforlife/go-cli-ui/ui/test"
9+
)
10+
11+
var _ = Describe("JSONUIFromBytes", func() {
12+
const (
13+
example = `
14+
{
15+
"Tables": [
16+
{
17+
"Content": "services",
18+
"Header": {
19+
"created_at": "Created At",
20+
"domain": "Domain",
21+
"internal_domain": "Internal Domain",
22+
"name": "Name"
23+
},
24+
"Rows": [
25+
{
26+
"created_at": "2018-07-31T12:27:45-07:00",
27+
"domain": "foo3.default.example.com",
28+
"internal_domain": "foo3.default.svc.cluster.local",
29+
"name": "foo3"
30+
},
31+
{
32+
"created_at": "2018-07-26T16:47:39-07:00",
33+
"domain": "helloworld-go.default.example.com",
34+
"internal_domain": "helloworld-go.default.svc.cluster.local",
35+
"name": "helloworld-go"
36+
}
37+
],
38+
"Notes": null
39+
}
40+
],
41+
"Blocks": null,
42+
"Lines": null
43+
}
44+
`
45+
)
46+
47+
It("properly parses JSON UI output", func() {
48+
resp := JSONUIFromBytes(nil, []byte(example))
49+
Expect(resp).To(Equal(ui.JSONUIResp{
50+
Tables: []ui.JSONUITableResp{
51+
{
52+
Content: "services",
53+
Header: map[string]string{
54+
"domain": "Domain",
55+
"internal_domain": "Internal Domain",
56+
"name": "Name",
57+
"created_at": "Created At",
58+
},
59+
Rows: []map[string]string{
60+
{
61+
"internal_domain": "foo3.default.svc.cluster.local",
62+
"name": "foo3",
63+
"created_at": "2018-07-31T12:27:45-07:00",
64+
"domain": "foo3.default.example.com",
65+
},
66+
{
67+
"name": "helloworld-go",
68+
"created_at": "2018-07-26T16:47:39-07:00",
69+
"domain": "helloworld-go.default.example.com",
70+
"internal_domain": "helloworld-go.default.svc.cluster.local",
71+
},
72+
},
73+
Notes: nil,
74+
},
75+
},
76+
Blocks: nil,
77+
Lines: nil,
78+
}))
79+
})
80+
})

ui/test/suite_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package test_test
2+
3+
import (
4+
. "github.com/onsi/ginkgo"
5+
. "github.com/onsi/gomega"
6+
"testing"
7+
)
8+
9+
func TestReg(t *testing.T) {
10+
RegisterFailHandler(Fail)
11+
RunSpecs(t, "ui/test")
12+
}

0 commit comments

Comments
 (0)