forked from helmfile/vals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
io_test.go
129 lines (117 loc) · 2.47 KB
/
io_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
package vals
import (
"bytes"
"strings"
"testing"
)
func Test_InputOutput(t *testing.T) {
baseDocument := `foo:
bar:
- baz
`
tests := []struct {
name string
input string
format string
expected string
}{
{
name: "single document yaml",
input: baseDocument,
format: "yaml",
expected: "foo:\n bar:\n - baz\n",
},
{
name: "multi document yaml",
input: baseDocument + "---\nbar: baz\n",
format: "yaml",
expected: "foo:\n bar:\n - baz\n---\nbar: baz\n",
},
{
name: "single document json",
input: baseDocument,
format: "json",
expected: "{\"foo\":{\"bar\":[\"baz\"]}}\n",
},
{
name: "multi document json",
input: baseDocument + "---\nbar: baz\n",
format: "json",
expected: "{\"foo\":{\"bar\":[\"baz\"]}}\n---\n{\"bar\":\"baz\"}\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
nodes, err := nodesFromReader(strings.NewReader(tt.input))
if err != nil {
t.Fatal(err)
}
buf := &bytes.Buffer{}
err = Output(buf, tt.format, nodes)
if err != nil {
t.Fatal(err)
}
if buf.String() != tt.expected {
t.Errorf("Expected %q, got %q", tt.expected, buf.String())
}
nodesRoundTrip, err := nodesFromReader(buf)
if err != nil {
t.Fatal(err)
}
bufRoundTrip := &bytes.Buffer{}
err = Output(bufRoundTrip, "yaml", nodesRoundTrip)
if err != nil {
t.Fatal(err)
}
if bufRoundTrip.String() != tt.input {
t.Errorf("Expected %q, got %q", tt.input, bufRoundTrip.String())
}
})
}
}
func Test_NodesFromReader(t *testing.T) {
simpleDocument := "---\nfoo: bar\n"
commentDocument := "---\n# comment\n"
tests := []struct {
name string
input string
nodes int
}{
{
name: "single document",
input: simpleDocument,
nodes: 1,
},
{
name: "multi document",
input: simpleDocument + simpleDocument,
nodes: 2,
},
{
name: "single comment document",
input: commentDocument,
nodes: 0,
},
{
name: "multiple comment document",
input: commentDocument + commentDocument,
nodes: 0,
},
{
name: "mixed documents",
input: simpleDocument + commentDocument,
nodes: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
nodes, err := nodesFromReader(strings.NewReader(tt.input))
if err != nil {
t.Fatal(err)
}
if len(nodes) != tt.nodes {
t.Errorf("Expected %v nodes, got %v", tt.nodes, len(nodes))
}
})
}
}