Skip to content

Commit

Permalink
Slice of structs support ensured and test added
Browse files Browse the repository at this point in the history
  • Loading branch information
SpectrumQT authored Jan 28, 2019
1 parent 20fa5b4 commit 5c5d0f3
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,11 @@ type Sample struct {
NoDefault *string `default:"-"`
NoDefaultStruct Struct `default:"-"`

MapWithNoTag map[string]int
SliceWithNoTag []string
StructPtrWithNoTag *Struct
StructWithNoTag Struct
MapWithNoTag map[string]int
SliceWithNoTag []string
StructPtrWithNoTag *Struct
StructWithNoTag Struct
DeepSliceOfStructsWithNoTag [][][]Struct

NonInitialString string `default:"foo"`
NonInitialSlice []int `default:"[123]"`
Expand All @@ -110,10 +111,11 @@ type Emmbeded struct {

func TestInit(t *testing.T) {
sample := &Sample{
NonInitialString: "string",
NonInitialSlice: []int{1, 2, 3},
NonInitialStruct: Struct{Foo: 123},
NonInitialStructPtr: &Struct{Foo: 123},
NonInitialString: "string",
NonInitialSlice: []int{1, 2, 3},
NonInitialStruct: Struct{Foo: 123},
NonInitialStructPtr: &Struct{Foo: 123},
DeepSliceOfStructsWithNoTag: [][][]Struct{{{{Foo: 123}}}},
}

if err := Set(sample); err != nil {
Expand Down Expand Up @@ -337,15 +339,15 @@ func TestInit(t *testing.T) {
if sample.MapWithNoTag != nil {
t.Errorf("it should not initialize pointer type (map)")
}
if sample.SliceWithNoTag != nil {
t.Errorf("it should not initialize pointer type (slice)")
}
if sample.StructPtrWithNoTag != nil {
t.Errorf("it should not initialize pointer type (struct)")
}
if sample.StructWithNoTag.WithDefault != "foo" {
t.Errorf("it should automatically recurse into a struct even without a tag")
}
if !reflect.DeepEqual(sample.DeepSliceOfStructsWithNoTag, [][][]Struct{{{{Emmbeded: Emmbeded{Int: 1}, Foo: 123, Bar: 456, WithDefault: "foo"}}}}) {
t.Errorf("it should automatically recurse into a slice of structs even without a tag")
}
})

t.Run("opt-out", func(t *testing.T) {
Expand Down

0 comments on commit 5c5d0f3

Please sign in to comment.