forked from getkin/kin-openapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunmarshal_test.go
156 lines (137 loc) · 4.05 KB
/
unmarshal_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
package jsoninfo
import (
"errors"
"testing"
"github.com/stretchr/testify/require"
)
func TestNewObjectDecoder(t *testing.T) {
data := []byte(`
{
"field1": 1,
"field2": 2
}
`)
t.Run("test new object decoder", func(t *testing.T) {
decoder, err := NewObjectDecoder(data)
require.NoError(t, err)
require.NotNil(t, decoder)
require.Equal(t, data, decoder.Data)
require.Equal(t, 2, len(decoder.DecodeExtensionMap()))
})
}
type mockStrictStruct struct {
EncodeWithFn func(encoder *ObjectEncoder, value interface{}) error
DecodeWithFn func(decoder *ObjectDecoder, value interface{}) error
}
func (m *mockStrictStruct) EncodeWith(encoder *ObjectEncoder, value interface{}) error {
return m.EncodeWithFn(encoder, value)
}
func (m *mockStrictStruct) DecodeWith(decoder *ObjectDecoder, value interface{}) error {
return m.DecodeWithFn(decoder, value)
}
func TestUnmarshalStrictStruct(t *testing.T) {
data := []byte(`
{
"field1": 1,
"field2": 2
}
`)
t.Run("test unmarshal with StrictStruct without err", func(t *testing.T) {
decodeWithFnCalled := 0
mockStruct := &mockStrictStruct{
EncodeWithFn: func(encoder *ObjectEncoder, value interface{}) error {
return nil
},
DecodeWithFn: func(decoder *ObjectDecoder, value interface{}) error {
decodeWithFnCalled++
return nil
},
}
err := UnmarshalStrictStruct(data, mockStruct)
require.NoError(t, err)
require.Equal(t, 1, decodeWithFnCalled)
})
t.Run("test unmarshal with StrictStruct with err", func(t *testing.T) {
decodeWithFnCalled := 0
mockStruct := &mockStrictStruct{
EncodeWithFn: func(encoder *ObjectEncoder, value interface{}) error {
return nil
},
DecodeWithFn: func(decoder *ObjectDecoder, value interface{}) error {
decodeWithFnCalled++
return errors.New("unable to decode the value")
},
}
err := UnmarshalStrictStruct(data, mockStruct)
require.Error(t, err)
require.Equal(t, 1, decodeWithFnCalled)
})
}
func TestDecodeStructFieldsAndExtensions(t *testing.T) {
data := []byte(`
{
"field1": "field1",
"field2": "field2"
}
`)
decoder, err := NewObjectDecoder(data)
require.NoError(t, err)
require.NotNil(t, decoder)
t.Run("value is not pointer", func(t *testing.T) {
var value interface{}
require.Panics(t, func() {
_ = decoder.DecodeStructFieldsAndExtensions(value)
}, "value is not a pointer")
})
t.Run("value is nil", func(t *testing.T) {
var value *string = nil
require.Panics(t, func() {
_ = decoder.DecodeStructFieldsAndExtensions(value)
}, "value is nil")
})
t.Run("value is not struct", func(t *testing.T) {
var value = "simple string"
require.Panics(t, func() {
_ = decoder.DecodeStructFieldsAndExtensions(&value)
}, "value is not struct")
})
t.Run("successfully decoded with all fields", func(t *testing.T) {
d, err := NewObjectDecoder(data)
require.NoError(t, err)
require.NotNil(t, d)
var value = struct {
Field1 string `json:"field1"`
Field2 string `json:"field2"`
}{}
err = d.DecodeStructFieldsAndExtensions(&value)
require.NoError(t, err)
require.Equal(t, "field1", value.Field1)
require.Equal(t, "field2", value.Field2)
require.Equal(t, 0, len(d.DecodeExtensionMap()))
})
t.Run("successfully decoded with renaming field", func(t *testing.T) {
d, err := NewObjectDecoder(data)
require.NoError(t, err)
require.NotNil(t, d)
var value = struct {
Field1 string `json:"field1"`
}{}
err = d.DecodeStructFieldsAndExtensions(&value)
require.NoError(t, err)
require.Equal(t, "field1", value.Field1)
require.Equal(t, 1, len(d.DecodeExtensionMap()))
})
t.Run("un-successfully decoded due to data mismatch", func(t *testing.T) {
d, err := NewObjectDecoder(data)
require.NoError(t, err)
require.NotNil(t, d)
var value = struct {
Field1 int `json:"field1"`
}{}
err = d.DecodeStructFieldsAndExtensions(&value)
require.Error(t, err)
require.EqualError(t, err, `failed to unmarshal property "field1" (*int): json: cannot unmarshal string into Go value of type int`)
require.Equal(t, 0, value.Field1)
require.Equal(t, 2, len(d.DecodeExtensionMap()))
})
}