forked from miracl/conflate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
marshal_test.go
161 lines (127 loc) · 3.84 KB
/
marshal_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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package conflate
import (
"testing"
"github.com/stretchr/testify/assert"
)
// --------
func TestJSONMarshalAll(t *testing.T) {
data, err := jsonMarshalAll("a", "b", "c")
assert.Nil(t, err)
assert.Equal(t, 3, len(data))
assert.Equal(t, []byte("\"a\"\n"), data[0])
assert.Equal(t, []byte("\"b\"\n"), data[1])
assert.Equal(t, []byte("\"c\"\n"), data[2])
}
func TestJSONMarshalAll_Error(t *testing.T) {
mockMarshal := func(_ interface{}) ([]byte, error) {
return nil, errTest
}
data, err := jsonMarshalAll(mockMarshal, "a")
assert.NotNil(t, err)
assert.Nil(t, data)
assert.Contains(t, err.Error(), "the data could not be marshalled")
}
// --------
func TestJSONMarshalUnmarshal(t *testing.T) {
var out interface{}
err := jsonMarshalUnmarshal(testMarshalData, &out)
assert.Nil(t, err)
assert.Equal(t, testMarshalData, out)
}
func TestJSONMarshalUnmarshal_MarshalError(t *testing.T) {
var out interface{}
err := jsonMarshalUnmarshal(testMarshalDataInvalid, out)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "could not be marshalled to json")
}
func TestJSONMarshalUnmarshal_UnmarshalError(t *testing.T) {
err := jsonMarshalUnmarshal(testMarshalData, testMarshalDataInvalid)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "could not be unmarshalled as json")
}
// --------
func TestJSONUnmarshal(t *testing.T) {
var out interface{}
err := JSONUnmarshal(testMarshalJSON, &out)
assert.Nil(t, err)
assert.Equal(t, testMarshalData, out)
}
func TestJSONUnmarshal_Error(t *testing.T) {
var out interface{}
err := JSONUnmarshal(testMarshalInvalid, &out)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "could not be unmarshalled as json")
}
func TestYAMLUnmarshal(t *testing.T) {
var out interface{}
err := YAMLUnmarshal(testMarshalYAML, &out)
assert.Nil(t, err)
assert.Equal(t, testMarshalData, out)
}
func TestYAMLUnmarshal_Error(t *testing.T) {
var out interface{}
err := YAMLUnmarshal(testMarshalInvalid, &out)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "could not be unmarshalled as yaml")
}
func TestTOMLUnmarshal(t *testing.T) {
var out interface{}
err := TOMLUnmarshal(testMarshalTOML, &out)
assert.Nil(t, err)
assert.Equal(t, testMarshalData, out)
}
func TestTOMLUnmarshal_Error(t *testing.T) {
var out interface{}
err := TOMLUnmarshal(testMarshalInvalid, &out)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "could not be unmarshalled as toml")
}
// --------
func TestJSONMarshal(t *testing.T) {
out, err := jsonMarshal(testMarshalData)
assert.Nil(t, err)
assert.Equal(t, string(testMarshalJSON), string(out))
}
func TestJSONMarshal_Error(t *testing.T) {
out, err := jsonMarshal(testMarshalDataInvalid)
assert.NotNil(t, err)
assert.Nil(t, out)
assert.Contains(t, err.Error(), "marshalled to json")
}
func TestYAMLMarshal(t *testing.T) {
out, err := yamlMarshal(testMarshalData)
assert.Nil(t, err)
assert.Equal(t, string(testMarshalYAML), string(out))
}
func TestYAMLMarshal_Error(t *testing.T) {
out, err := yamlMarshal(testMarshalDataInvalid)
assert.NotNil(t, err)
assert.Nil(t, out)
assert.Contains(t, err.Error(), "marshalled to yaml")
}
func TestTOMLMarshal(t *testing.T) {
out, err := tomlMarshal(testMarshalData)
assert.Nil(t, err)
assert.Equal(t, string(testMarshalTOML), string(out))
}
func TestTOMLMarshal_PanicError(t *testing.T) {
out, err := tomlMarshal(testMarshalDataInvalid)
assert.NotNil(t, err)
assert.Nil(t, out)
assert.Contains(t, err.Error(), "marshalled to toml")
}
// --------
var (
testValue = `value!£$%^&*()_+-={}[]:@~;'#<>?,./|`
testMarshalData = map[string]interface{}{"key": testValue}
testMarshalDataInvalid = func() {}
testMarshalJSON = []byte(`{
"key": "` + testValue + `"
}
`)
testMarshalYAML = []byte(`key: ` + testValue + `
`)
testMarshalTOML = []byte(`key = "` + testValue + `"
`)
testMarshalInvalid = []byte(`{invalid`)
)