Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add edge tests around Provide and Invoke #76

Merged
merged 2 commits into from May 31, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 46 additions & 0 deletions dig_test.go
Expand Up @@ -247,7 +247,27 @@ 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("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 All @@ -259,6 +279,17 @@ func TestEndToEndSuccess(t *testing.T) {
})
}

func TestProvideConstructorErrors(t *testing.T) {
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")
})
}

func TestProvideRespectsConstructorErrors(t *testing.T) {
t.Run("constructor succeeds", func(t *testing.T) {
c := New()
Expand Down Expand Up @@ -349,6 +380,21 @@ func TestProvideKnownTypesFails(t *testing.T) {

})
}
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