-
Notifications
You must be signed in to change notification settings - Fork 11
/
yaml_test.go
147 lines (135 loc) · 2.8 KB
/
yaml_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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package yaml
import (
"fmt"
"reflect"
"testing"
"github.com/coveooss/gotemplate/v3/collections"
)
func Test_list_String(t *testing.T) {
t.Parallel()
tests := []struct {
name string
l yamlList
want string
}{
{"Nil", nil, "[]\n"},
{"Empty List", yamlList{}, "[]\n"},
{"List of int", yamlList{1, 2, 3}, collections.UnIndent(`
- 1
- 2
- 3
`)[1:]},
{"List of string", strFixture, collections.UnIndent(`
- Hello
- World,
- I'm
- Foo
- Bar!
`)[1:]},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.l.String(); got != tt.want {
t.Errorf("yamlList.String():\ngot:\n%v\nwant:\n%v", got, tt.want)
}
})
}
}
func Test_dict_String(t *testing.T) {
t.Parallel()
tests := []struct {
name string
d yamlDict
want string
}{
{"nil", nil, "{}\n"},
{"Map", dictFixture, collections.UnIndent(`
float: 1.23
int: 123
list:
- 1
- two
listInt:
- 1
- 2
- 3
map:
sub1: 1
sub2: two
mapInt:
"1": 1
"2": two
string: Foo bar
`)[1:]},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.d.String(); got != tt.want {
t.Errorf("yamlDict.String():\ngot:\n%v\nwant:\n%v", got, tt.want)
}
})
}
}
func TestUnmarshal(t *testing.T) {
t.Parallel()
tests := []struct {
name string
yaml string
want interface{}
}{
{"nil", "{}\n", yamlDict{}},
{"Map", fmt.Sprint(dictFixture), dictFixture},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var out interface{}
err := Unmarshal([]byte(tt.yaml), &out)
if err == nil && !reflect.DeepEqual(out, tt.want) {
t.Errorf("Unmarshal:\n got %[1]v (%[1]T)\nwant %[2]v (%[2]T)", out, tt.want)
}
})
}
}
func TestUnmarshalWithError(t *testing.T) {
t.Parallel()
tests := []struct {
name string
yaml string
}{
{"Error", "Invalid"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var out map[string]interface{}
err := Unmarshal([]byte(tt.yaml), &out)
if err == nil {
t.Errorf("Unmarshal() expected error")
}
})
}
}
func TestUnmarshalStrict(t *testing.T) {
t.Parallel()
tests := []struct {
name string
yaml string
want interface{}
wantErr bool
}{
{"nil", "{}\n", map[string]interface{}{}, false},
{"Map", fmt.Sprint(dictFixture), dictFixture.Native(), false},
{"Error", "Invalid", nil, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var out map[string]interface{}
err := Unmarshal([]byte(tt.yaml), &out)
if (err != nil) != tt.wantErr {
t.Errorf("Unmarshal() error = %v, wantErr %v", err, tt.wantErr)
}
if err == nil && !reflect.DeepEqual(out, tt.want) {
t.Errorf("Unmarshal:\n got %[1]v (%[1]T)\nwant %[2]v (%[2]T)", out, tt.want)
}
})
}
}