Skip to content

Commit

Permalink
test: add some tests for MustSet function
Browse files Browse the repository at this point in the history
  • Loading branch information
BoynChan committed Aug 14, 2020
1 parent c2889d7 commit ea4c44f
Showing 1 changed file with 42 additions and 14 deletions.
56 changes: 42 additions & 14 deletions defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,21 +143,49 @@ type Emmbeded struct {
}

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

MustSet(sample)
go func() {
if err := recover(); err != nil {
t.Fatalf("it should not panic error: %v", err)
}
}()
t.Log("it works.")
t.Run("right way", func(t *testing.T) {
defer func() {
if err := recover(); err != nil {
t.Fatalf("it should not panic error: %v", err)
}
}()
sample := &Sample{
NonInitialString: "string",
NonInitialSlice: []int{1, 2, 3},
NonInitialStruct: Struct{Foo: 123},
NonInitialStructPtr: &Struct{Foo: 123},
DeepSliceOfStructsWithNoTag: [][][]Struct{{{{Foo: 123}}}},
}
MustSet(sample)
})

t.Run("not struct", func(t *testing.T) {
defer func() {
if err := recover(); err != nil {
t.Logf("panic error: %v", err)
}
}()
var a int
MustSet(&a)
})

t.Run("not pointer", func(t *testing.T) {
defer func() {
if err := recover(); err != nil {
t.Logf("panic error: %v", err)
}
}()
sample := Sample{
NonInitialString: "string",
NonInitialSlice: []int{1, 2, 3},
NonInitialStruct: Struct{Foo: 123},
NonInitialStructPtr: &Struct{Foo: 123},
DeepSliceOfStructsWithNoTag: [][][]Struct{{{{Foo: 123}}}},
}
MustSet(sample)
})

}

func TestInit(t *testing.T) {
Expand Down

0 comments on commit ea4c44f

Please sign in to comment.