Skip to content

Commit

Permalink
nil handling for Call() (#40)
Browse files Browse the repository at this point in the history
* nil handling for Call()

* added a testcase
  • Loading branch information
Place1 authored Apr 16, 2022
1 parent 71ca5b8 commit 9432853
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions container.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ func (c Container) Call(function interface{}) error {
if len(result) == 0 {
return nil
} else if len(result) == 1 && result[0].CanInterface() {
if result[0].IsNil() {
return nil
}
if err, ok := result[0].Interface().(error); ok {
return err
}
Expand Down
14 changes: 14 additions & 0 deletions container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,20 @@ func TestContainer_Call_With_A_Returning_Error(t *testing.T) {
assert.EqualError(t, err, "app: some context error")
}

func TestContainer_Call_With_A_Returning_Nil_Error(t *testing.T) {
instance.Reset()

err := instance.Singleton(func() Shape {
return &Circle{}
})
assert.NoError(t, err)

err = instance.Call(func(s Shape) error {
return nil
})
assert.Nil(t, err)
}

func TestContainer_Call_With_Invalid_Signature(t *testing.T) {
instance.Reset()

Expand Down

0 comments on commit 9432853

Please sign in to comment.