Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/local-app/pkg/prettyprint/prettyprint_test.go
2500 views
1
// Copyright (c) 2023 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package prettyprint
6
7
import (
8
"bytes"
9
"testing"
10
11
"github.com/google/go-cmp/cmp"
12
)
13
14
func TestWriterWrite(t *testing.T) {
15
type R struct {
16
Foo string `print:"foo"`
17
Number int `print:"number"`
18
DiffName string `print:"foobar"`
19
}
20
type Expectation struct {
21
Error string
22
Out string
23
}
24
tests := []struct {
25
Name string
26
Expectation Expectation
27
Format WriterFormat
28
Field string
29
Data []R
30
}{
31
{
32
Name: "wide format with data",
33
Expectation: Expectation{
34
Out: "FOO NUMBER FOOBAR \nfoo 42 bar \n",
35
},
36
Format: WriterFormatWide,
37
Data: []R{
38
{Foo: "foo", Number: 42, DiffName: "bar"},
39
},
40
},
41
{
42
Name: "narrow format with data",
43
Expectation: Expectation{
44
Out: "Foo: foo\nNumber: 42\nFoobar: bar\n",
45
},
46
Format: WriterFormatNarrow,
47
Data: []R{
48
{Foo: "foo", Number: 42, DiffName: "bar"},
49
},
50
},
51
{
52
Name: "empty",
53
Expectation: Expectation{
54
Out: "",
55
},
56
Format: WriterFormatNarrow,
57
Field: "",
58
Data: nil,
59
},
60
{
61
Name: "empty wide",
62
Expectation: Expectation{
63
Out: "FOO NUMBER FOOBAR \n",
64
},
65
Format: WriterFormatWide,
66
},
67
{
68
Name: "empty field",
69
Expectation: Expectation{},
70
Format: WriterFormatNarrow,
71
Field: "foo",
72
Data: nil,
73
},
74
{
75
Name: "empty field wide",
76
Expectation: Expectation{},
77
Format: WriterFormatWide,
78
Field: "foo",
79
Data: nil,
80
},
81
{
82
Name: "empty field with data",
83
Expectation: Expectation{},
84
Format: WriterFormatNarrow,
85
Field: "foo",
86
Data: []R{{}},
87
},
88
{
89
Name: "empty field with data wide",
90
Expectation: Expectation{},
91
Format: WriterFormatWide,
92
Field: "foo",
93
Data: []R{{}},
94
},
95
}
96
97
for _, test := range tests {
98
t.Run(test.Name, func(t *testing.T) {
99
var act Expectation
100
101
out := bytes.NewBuffer(nil)
102
w := Writer[R]{
103
Out: out,
104
Format: test.Format,
105
Field: test.Field,
106
}
107
err := w.Write(test.Data)
108
if err != nil {
109
act.Error = err.Error()
110
}
111
act.Out = out.String()
112
113
if diff := cmp.Diff(test.Expectation, act); diff != "" {
114
t.Errorf("Write() mismatch (-want +got):\n%s", diff)
115
}
116
})
117
}
118
}
119
120