Skip to content

Commit

Permalink
Add edge tests around Provide and Invoke
Browse files Browse the repository at this point in the history
  • Loading branch information
anuptalwalkar committed May 31, 2017
1 parent 863426c commit eaaea66
Showing 1 changed file with 44 additions and 3 deletions.
47 changes: 44 additions & 3 deletions dig_test.go
Expand Up @@ -233,7 +233,35 @@ func TestEndToEndSuccess(t *testing.T) {
require.NoError(t, c.Invoke(func(a *A, b *B) {}), "AB invoke failed")
require.Equal(t, 1, count, "Constructor must be called once")
})
t.Run("multiple-type constructor returns multiple objects of same type", func(t *testing.T) {
c := New()
type A struct{}
constructor := func() (*A, *A, error) {
return &A{}, &A{}, nil
}
require.Error(t, c.Provide(constructor), "provide failed")
})
t.Run("method invocation inside Invoke", func(t *testing.T) {
c := New()
type A struct{}
type B struct{}
cA := func() (*A, error) {
return &A{}, nil
}
cB := func() (*B, error) {
return &B{}, nil
}
getA := func(a *A) {
c.Invoke(func(b *B) {
assert.NotNil(t, b, "got nil B")
})
assert.NotNil(t, a, "got nil A")
}

require.NoError(t, c.Provide(cA), "provide failed")
require.NoError(t, c.Provide(cB), "provide failed")
require.NoError(t, c.Invoke(getA), "A invoke failed")
})
t.Run("collections and instances of same type", func(t *testing.T) {
c := New()
require.NoError(t, c.Provide(func() []*bytes.Buffer {
Expand Down Expand Up @@ -289,9 +317,22 @@ func TestCantProvideErrors(t *testing.T) {

func TestProvideKnownTypesFails(t *testing.T) {
t.Parallel()
c := New()
assert.NoError(t, c.Provide(func() *bytes.Buffer { return nil }))
assert.Error(t, c.Provide(func() *bytes.Buffer { return nil }))

t.Run("provide constructor twice", func(t *testing.T) {
c := New()
assert.NoError(t, c.Provide(func() *bytes.Buffer { return nil }))
assert.Error(t, c.Provide(func() *bytes.Buffer { return nil }))
})
t.Run("provide instance and constructor fails", func(t *testing.T) {
c := New()
assert.NoError(t, c.Provide(&bytes.Buffer{}))
assert.Error(t, c.Provide(func() *bytes.Buffer { return nil }))
})
t.Run("provide constructor then object instance fails", func(t *testing.T) {
c := New()
assert.NoError(t, c.Provide(func() *bytes.Buffer { return nil }))
assert.Error(t, c.Provide(&bytes.Buffer{}))
})
}

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

0 comments on commit eaaea66

Please sign in to comment.