Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Akshay Shah committed May 25, 2017
1 parent 2dd34a6 commit c81338a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
39 changes: 39 additions & 0 deletions container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -593,3 +593,42 @@ func Test_ZeroValueObjectValidationError(t *testing.T) {
})
require.Nil(t, err)
}

func TestZeroValueOnInvoke(t *testing.T) {
type missing struct{}
type present struct{}

t.Parallel()
c := New()
c.Provide(func() *present {
return &present{}
})

var called int
assert.NoError(t, c.Invoke(func(m *missing) {
assert.Nil(t, m, "expected zero value for missing deps")
called++
}), "unexpected failure invoking with missing deps")

assert.NoError(t, c.Invoke(func(p *present, m *missing) {
assert.Nil(t, m, "expected zero value for missing dep")
assert.NotNil(t, p, "expected non-zero value for present dep")
called++
}), "unexpected failure invoking with a mix of present and missing deps")

assert.Equal(t, 2, called, "didn't run invokes")
}

func TestZeroValueOnProvide(t *testing.T) {
type missing struct{}
type provided struct{}

c := New()
assert.NoError(t, c.Provide(func(m *missing) (*provided, error) {
assert.Nil(t, m, "expected zero value for missing dep")
return &provided{}, nil
}), "unexpected failure providing with missing deps")

var p *provided
assert.NoError(t, c.Resolve(&p), "unexpected error resolving provided type")
}
2 changes: 1 addition & 1 deletion internal/graph/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func (g *Graph) validateGraph(ct reflect.Type) (reflect.Value, error) {
for _, dep := range node.dependencies() {
// check that the dependency is a registered objNode
if _, ok := g.nodes[dep]; !ok {
return reflect.Zero(ct), fmt.Errorf("%v dependency of type %v is not registered", ct, dep)
return reflect.Zero(ct), nil
}
}
}
Expand Down

0 comments on commit c81338a

Please sign in to comment.