6 func TestDecode_NilValue(t *testing.T) {
7 input := map[string]interface{}{
13 err := Decode(input, &result)
15 t.Fatalf("should not error: %s", err)
18 if result.Vfoo != "" {
19 t.Fatalf("value should be default: %s", result.Vfoo)
22 if result.Vother != nil {
23 t.Fatalf("Vother should be nil: %s", result.Vother)
28 func TestDecode_mapInterfaceInterface(t *testing.T) {
29 input := map[interface{}]interface{}{
35 err := Decode(input, &result)
37 t.Fatalf("should not error: %s", err)
40 if result.Vfoo != "" {
41 t.Fatalf("value should be default: %s", result.Vfoo)
44 if result.Vother != nil {
45 t.Fatalf("Vother should be nil: %s", result.Vother)
50 func TestNestedTypePointerWithDefaults(t *testing.T) {
53 input := map[string]interface{}{
55 "vbar": map[string]interface{}{
62 result := NestedPointer{
67 err := Decode(input, &result)
69 t.Fatalf("got an err: %s", err.Error())
72 if result.Vfoo != "foo" {
73 t.Errorf("vfoo value should be 'foo': %#v", result.Vfoo)
76 if result.Vbar.Vstring != "foo" {
77 t.Errorf("vstring value should be 'foo': %#v", result.Vbar.Vstring)
80 if result.Vbar.Vint != 42 {
81 t.Errorf("vint value should be 42: %#v", result.Vbar.Vint)
84 if result.Vbar.Vbool != true {
85 t.Errorf("vbool value should be true: %#v", result.Vbar.Vbool)
88 if result.Vbar.Vextra != "" {
89 t.Errorf("vextra value should be empty: %#v", result.Vbar.Vextra)
93 if result.Vbar.Vuint != 42 {
94 t.Errorf("vuint value should be 42: %#v", result.Vbar.Vuint)
99 type NestedSlice struct {
106 func TestNestedTypeSliceWithDefaults(t *testing.T) {
109 input := map[string]interface{}{
111 "vbars": []map[string]interface{}{
112 {"vstring": "foo", "vint": 42, "vbool": true},
113 {"vint": 42, "vbool": true},
115 "vempty": []map[string]interface{}{
116 {"vstring": "foo", "vint": 42, "vbool": true},
117 {"vint": 42, "vbool": true},
121 result := NestedSlice{
127 err := Decode(input, &result)
129 t.Fatalf("got an err: %s", err.Error())
132 if result.Vfoo != "foo" {
133 t.Errorf("vfoo value should be 'foo': %#v", result.Vfoo)
136 if result.Vbars[0].Vstring != "foo" {
137 t.Errorf("vstring value should be 'foo': %#v", result.Vbars[0].Vstring)
140 if result.Vbars[0].Vuint != 42 {
141 t.Errorf("vuint value should be 42: %#v", result.Vbars[0].Vuint)
146 func TestNestedTypeWithDefaults(t *testing.T) {
149 input := map[string]interface{}{
151 "vbar": map[string]interface{}{
163 err := Decode(input, &result)
165 t.Fatalf("got an err: %s", err.Error())
168 if result.Vfoo != "foo" {
169 t.Errorf("vfoo value should be 'foo': %#v", result.Vfoo)
172 if result.Vbar.Vstring != "foo" {
173 t.Errorf("vstring value should be 'foo': %#v", result.Vbar.Vstring)
176 if result.Vbar.Vint != 42 {
177 t.Errorf("vint value should be 42: %#v", result.Vbar.Vint)
180 if result.Vbar.Vbool != true {
181 t.Errorf("vbool value should be true: %#v", result.Vbar.Vbool)
184 if result.Vbar.Vextra != "" {
185 t.Errorf("vextra value should be empty: %#v", result.Vbar.Vextra)
189 if result.Vbar.Vuint != 42 {
190 t.Errorf("vuint value should be 42: %#v", result.Vbar.Vuint)
195 // #67 panic() on extending slices (decodeSlice with disabled ZeroValues)
196 func TestDecodeSliceToEmptySliceWOZeroing(t *testing.T) {
199 type TestStruct struct {
203 decode := func(m interface{}, rawVal interface{}) error {
204 config := &DecoderConfig{
210 decoder, err := NewDecoder(config)
215 return decoder.Decode(m)
219 input := map[string]interface{}{
220 "vfoo": []string{"1"},
223 result := &TestStruct{}
225 err := decode(input, &result)
227 t.Fatalf("got an err: %s", err.Error())
232 input := map[string]interface{}{
233 "vfoo": []string{"1"},
236 result := &TestStruct{
240 err := decode(input, &result)
242 t.Fatalf("got an err: %s", err.Error())
247 input := map[string]interface{}{
248 "vfoo": []string{"2", "3"},
251 result := &TestStruct{
255 err := decode(input, &result)
257 t.Fatalf("got an err: %s", err.Error())