|
1 | 1 | package decoder_test |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "reflect" |
4 | 5 | "testing" |
5 | 6 |
|
6 | 7 | "github.com/abemedia/go-don/decoder" |
@@ -80,62 +81,83 @@ func TestDecode(t *testing.T) { |
80 | 81 | }, |
81 | 82 | } |
82 | 83 |
|
83 | | - dec := decoder.New("field") |
84 | | - |
85 | | - actual := &test{} |
86 | | - if err := dec.Decode(in, actual); err != nil { |
87 | | - t.Fatal(err) |
88 | | - } |
89 | | - |
90 | | - if diff := cmp.Diff(expected, actual); diff != "" { |
91 | | - t.Errorf(diff) |
92 | | - } |
93 | | -} |
94 | | - |
95 | | -func TestCached(t *testing.T) { |
96 | | - type test struct { |
97 | | - String string `field:"string"` |
98 | | - } |
99 | | - |
100 | | - in := decoder.Map{"string": {"string"}} |
101 | | - expected := &test{String: "string"} |
102 | | - |
103 | | - dec, err := decoder.NewCached(test{}, "field") |
104 | | - if err != nil { |
105 | | - t.Fatal(err) |
106 | | - } |
107 | | - |
108 | | - actual := &test{} |
109 | | - |
110 | | - if err = dec.Decode(in, actual); err != nil { |
111 | | - t.Fatal(err) |
112 | | - } |
113 | | - |
114 | | - if diff := cmp.Diff(expected, actual); diff != "" { |
115 | | - t.Errorf(diff) |
116 | | - } |
| 84 | + t.Run("Decoder", func(t *testing.T) { |
| 85 | + dec := decoder.New("field") |
| 86 | + actual := &test{} |
| 87 | + if err := dec.Decode(in, actual); err != nil { |
| 88 | + t.Fatal(err) |
| 89 | + } |
| 90 | + if diff := cmp.Diff(expected, actual); diff != "" { |
| 91 | + t.Errorf(diff) |
| 92 | + } |
| 93 | + }) |
| 94 | + |
| 95 | + t.Run("CachedDecoder", func(t *testing.T) { |
| 96 | + dec, err := decoder.NewCached(test{}, "field") |
| 97 | + if err != nil { |
| 98 | + t.Fatal(err) |
| 99 | + } |
| 100 | + |
| 101 | + actual := &test{} |
| 102 | + if err := dec.Decode(in, actual); err != nil { |
| 103 | + t.Fatal(err) |
| 104 | + } |
| 105 | + if diff := cmp.Diff(expected, actual); diff != "" { |
| 106 | + t.Errorf(diff) |
| 107 | + } |
| 108 | + |
| 109 | + actual = &test{} |
| 110 | + val := reflect.ValueOf(actual).Elem() |
| 111 | + if err = dec.DecodeValue(in, val); err != nil { |
| 112 | + t.Fatal(err) |
| 113 | + } |
| 114 | + if diff := cmp.Diff(expected, actual); diff != "" { |
| 115 | + t.Errorf(diff) |
| 116 | + } |
| 117 | + }) |
| 118 | + |
| 119 | + t.Run("CachedDecoder_NilPointer", func(t *testing.T) { |
| 120 | + dec, err := decoder.NewCached(&test{}, "field") |
| 121 | + if err != nil { |
| 122 | + t.Fatal(err) |
| 123 | + } |
| 124 | + |
| 125 | + var actual *test |
| 126 | + if err := dec.Decode(in, &actual); err != nil { |
| 127 | + t.Fatal(err) |
| 128 | + } |
| 129 | + if diff := cmp.Diff(expected, actual); diff != "" { |
| 130 | + t.Errorf(diff) |
| 131 | + } |
| 132 | + }) |
117 | 133 | } |
118 | 134 |
|
119 | | -func TestCachedNil(t *testing.T) { |
120 | | - type test struct { |
121 | | - String string `field:"string"` |
| 135 | +func TestDecodeError(t *testing.T) { |
| 136 | + type noTag struct { |
| 137 | + Test string `json:"test"` |
122 | 138 | } |
123 | | - |
124 | | - in := decoder.Map{"string": {"string"}} |
125 | | - expected := &test{String: "string"} |
126 | | - |
127 | | - dec, err := decoder.NewCached(&test{}, "field") |
128 | | - if err != nil { |
129 | | - t.Fatal(err) |
130 | | - } |
131 | | - |
132 | | - var actual *test |
133 | | - |
134 | | - if err = dec.Decode(in, &actual); err != nil { |
135 | | - t.Fatal(err) |
136 | | - } |
137 | | - |
138 | | - if diff := cmp.Diff(expected, actual); diff != "" { |
139 | | - t.Errorf(diff) |
| 139 | + type unsupportedType struct { |
| 140 | + Test chan string `field:"test"` |
140 | 141 | } |
| 142 | + s := "" |
| 143 | + tests := []any{"", &s, 1, noTag{}, &unsupportedType{}} |
| 144 | + |
| 145 | + t.Run("Decoder", func(t *testing.T) { |
| 146 | + for _, test := range tests { |
| 147 | + dec := decoder.New("field") |
| 148 | + err := dec.Decode(nil, test) |
| 149 | + if err == nil { |
| 150 | + t.Errorf("should return error for %T", test) |
| 151 | + } |
| 152 | + } |
| 153 | + }) |
| 154 | + |
| 155 | + t.Run("CachedDecoder", func(t *testing.T) { |
| 156 | + for _, test := range tests { |
| 157 | + _, err := decoder.NewCached(test, "field") |
| 158 | + if err == nil { |
| 159 | + t.Errorf("should return error for %T", test) |
| 160 | + } |
| 161 | + } |
| 162 | + }) |
141 | 163 | } |
0 commit comments