-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalue_test.go
140 lines (132 loc) · 3.42 KB
/
value_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
package ycat_test
import (
"encoding/json"
"strings"
"testing"
"github.com/alxarch/ycat"
yaml "gopkg.in/yaml.v2"
)
func TestMap_MarshalJSON(t *testing.T) {
m := ycat.NewMap
a := func(values ...interface{}) []interface{} {
return values
}
tests := []struct {
Map ycat.Map
wantJSON string
wantErr bool
}{
{nil, `null`, false},
{m(), `{}`, false},
{m("foo", m()), `{"foo":{}}`, false},
{m("foo", a(42)), `{"foo":[42]}`, false},
}
for _, tt := range tests {
t.Run(string(tt.wantJSON), func(t *testing.T) {
data, err := json.Marshal(tt.Map)
if (err != nil) != tt.wantErr {
t.Errorf("Map.MarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
}
if string(data) != tt.wantJSON {
t.Errorf("Map.MarshalJSON() %q != %q", data, tt.wantJSON)
}
})
}
}
func TestMap_UnmarshalJSON(t *testing.T) {
m := ycat.NewMap
a := func(values ...interface{}) []interface{} {
return values
}
tests := []struct {
Map ycat.Map
JSON string
wantErr bool
}{
{m("foo", m("bar", "baz")), `{"foo":{"bar":"baz"}}`, false},
{m("foo", a(1, 2, 3)), `{"foo":[1,2,3]}`, false},
{nil, `null`, false},
{m(), `{}`, false},
{m("foo", m()), `{"foo":{}}`, false},
}
for _, tt := range tests {
t.Run("json="+string(tt.JSON), func(t *testing.T) {
var v ycat.Map
err := json.Unmarshal([]byte(tt.JSON), &v)
if (err != nil) != tt.wantErr {
t.Errorf("map.UnmarshalJSON() error = %v, wanterr %v", err, tt.wantErr)
}
data, err := json.Marshal(v)
if (err != nil) != tt.wantErr {
t.Errorf("map.UnmarshalJSON() error = %v, wanterr %v", err, tt.wantErr)
}
if string(data) != tt.JSON {
t.Errorf("Map.UnmarshalJSON() %q != %q", string(data), tt.JSON)
}
})
}
}
func TestRawValue_UnmarshalYAML(t *testing.T) {
tests := []struct {
YAML string
wantValue ycat.RawValue
wantErr bool
}{
{`foo: {}`, `{"foo":{}}`, false},
{"{}", `{}`, false},
{`""`, `""`, false},
{"[]", `[]`, false},
{"42", "42", false},
{"foo: bar", `{"foo":"bar"}`, false},
{"null", ``, false},
{"[1,2,3]", `[1,2,3]`, false},
}
for _, tt := range tests {
t.Run(string(tt.wantValue), func(t *testing.T) {
var v ycat.RawValue
if err := yaml.Unmarshal([]byte(tt.YAML), &v); (err != nil) != tt.wantErr {
t.Errorf("RawValue.UnmarshalYAML() error = %v, wantErr %v", err, tt.wantErr)
}
v, err := v.Compact()
if (err != nil) != tt.wantErr {
t.Errorf("RawValue.Compact() error = %v, wantErr %v", err, tt.wantErr)
}
if v != tt.wantValue {
t.Errorf("RawValue.UnmarshalYAML() %q != %q", v, tt.wantValue)
}
})
}
}
func TestRawValue_MarshalYAML(t *testing.T) {
tests := []struct {
YAML string
value ycat.RawValue
wantErr bool
}{
{`foo: {}`, `{"foo":{}}`, false},
{"{}", `{}`, false},
{`""`, `""`, false},
{"[]", "[]", false},
{"answer: 42", `{"answer":42}`, false},
{"answer:\n- 42\n", `{"answer":[42]}`, false},
{"42", "42", false},
{"foo: bar", `{"foo":"bar"}`, false},
{"null", ``, false},
{"- 1\n- 2\n- 3", `[1,2,3]`, false},
}
for _, tt := range tests {
t.Run(string(tt.value), func(t *testing.T) {
data, err := yaml.Marshal(tt.value)
if (err != nil) != tt.wantErr {
t.Errorf("RawValue.MarshalYAML() error = %v, wantErr %v", err, tt.wantErr)
}
want := tt.YAML
if !strings.HasSuffix(want, "\n") {
want += "\n"
}
if string(data) != want {
t.Errorf("RawValue.MarsalYAML() %q != %q", string(data), want)
}
})
}
}