Skip to content

Commit dee3da2

Browse files
committed
Add test cases for embedded structs
1 parent 9d5b0bb commit dee3da2

2 files changed

Lines changed: 152 additions & 0 deletions

File tree

pkg/decodini/decode_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,59 @@ func TestDecode_ShallowSlice_to_ShallowSlice(t *testing.T) {
153153
a.EqualValues(expected, actual)
154154
a.NoError(err)
155155
}
156+
157+
func TestDecode_OneEmbeddedStruct(t *testing.T) {
158+
type (
159+
Embedded struct {
160+
A string `decodini:"a"`
161+
B int
162+
C bool `decodini:"-"`
163+
}
164+
Outer struct {
165+
Embedded
166+
}
167+
)
168+
169+
a := assert.New(t)
170+
171+
from := map[string]any{
172+
"a": "foo",
173+
"B": 42,
174+
}
175+
tr := Encode(nil, from)
176+
177+
to, err := Decode[Outer](nil, tr)
178+
a.NoError(err)
179+
180+
expected := Outer{Embedded: Embedded{A: "foo", B: 42}}
181+
a.Equal(expected, to)
182+
}
183+
184+
func TestDecode_TwoEmbeddedStructs(t *testing.T) {
185+
type (
186+
EmbeddedA struct {
187+
A string `decodini:"a"`
188+
}
189+
EmbeddedB struct {
190+
B int `decodini:"b"`
191+
}
192+
Outer struct {
193+
EmbeddedA
194+
EmbeddedB
195+
}
196+
)
197+
198+
a := assert.New(t)
199+
200+
from := map[string]any{
201+
"a": "foo",
202+
"b": 7,
203+
}
204+
tr := Encode(nil, from)
205+
206+
to, err := Decode[Outer](nil, tr)
207+
a.NoError(err)
208+
209+
expected := Outer{EmbeddedA: EmbeddedA{A: "foo"}, EmbeddedB: EmbeddedB{B: 7}}
210+
a.Equal(expected, to)
211+
}

pkg/decodini/encode_test.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,102 @@ func TestEncode_ShallowMap(t *testing.T) {
178178
}
179179
}
180180

181+
func TestEncode_OneEmbeddedStruct(t *testing.T) {
182+
type (
183+
Embedded struct {
184+
A string `decodini:"a"`
185+
B int
186+
C bool `decodini:"-"`
187+
}
188+
Outer struct {
189+
Embedded
190+
}
191+
)
192+
193+
a := assert.New(t)
194+
195+
val := Outer{
196+
Embedded: Embedded{
197+
A: "foo",
198+
B: 42,
199+
C: true,
200+
},
201+
}
202+
203+
tr := Encode(nil, val)
204+
a.NotNil(tr)
205+
206+
a.Nil(tr.Child("Embedded"))
207+
208+
children := slices.Collect(tr.Children())
209+
a.Len(children, 2)
210+
a.Equal("a", children[0].Name())
211+
a.Equal("B", children[1].Name())
212+
213+
{
214+
childA := tr.Child("a")
215+
a.NotNil(childA)
216+
a.Equal(reflect.String, childA.Value().Kind())
217+
a.Equal(val.A, childA.Value().String())
218+
a.Equal(uint(0), childA.NumChildren())
219+
}
220+
221+
{
222+
childB := tr.Child("B")
223+
a.NotNil(childB)
224+
a.Equal(reflect.Int, childB.Value().Kind())
225+
a.Equal(val.B, int(childB.Value().Int()))
226+
a.Equal(uint(0), childB.NumChildren())
227+
}
228+
}
229+
230+
func TestEncode_TwoEmbeddedStructs(t *testing.T) {
231+
type (
232+
EmbeddedA struct {
233+
A string `decodini:"a"`
234+
}
235+
EmbeddedB struct {
236+
B int `decodini:"b"`
237+
}
238+
Outer struct {
239+
EmbeddedA
240+
EmbeddedB
241+
}
242+
)
243+
244+
a := assert.New(t)
245+
246+
val := Outer{
247+
EmbeddedA: EmbeddedA{A: "foo"},
248+
EmbeddedB: EmbeddedB{B: 7},
249+
}
250+
251+
tr := Encode(nil, val)
252+
a.NotNil(tr)
253+
254+
a.Nil(tr.Child("EmbeddedA"))
255+
a.Nil(tr.Child("EmbeddedB"))
256+
257+
children := slices.Collect(tr.Children())
258+
a.Len(children, 2)
259+
a.Equal("a", children[0].Name())
260+
a.Equal("b", children[1].Name())
261+
262+
{
263+
childA := tr.Child("a")
264+
a.NotNil(childA)
265+
a.Equal(reflect.String, childA.Value().Kind())
266+
a.Equal(val.A, childA.Value().String())
267+
}
268+
269+
{
270+
childB := tr.Child("b")
271+
a.NotNil(childB)
272+
a.Equal(reflect.Int, childB.Value().Kind())
273+
a.Equal(val.B, int(childB.Value().Int()))
274+
}
275+
}
276+
181277
func TestTree_DepthFirst(t *testing.T) {
182278
t.Run("Singleton", func(t *testing.T) {
183279
type testStruct struct {

0 commit comments

Comments
 (0)