-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allows registration of struct dependencies (#18)
* Allows registration of struct dependencies
- Loading branch information
1 parent
b8d5f56
commit fd95efe
Showing
6 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
internal/tests/registration/registry_register_struct_dependency_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package registration | ||
|
||
import ( | ||
"context" | ||
"github.com/matzefriedrich/parsley/pkg/registration" | ||
"github.com/matzefriedrich/parsley/pkg/resolving" | ||
"github.com/matzefriedrich/parsley/pkg/types" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func Test_Register_struct_dependency(t *testing.T) { | ||
|
||
// Arrange | ||
registry := registration.NewServiceRegistry() | ||
|
||
config := someConfig{b: true} | ||
registryErr := registration.RegisterInstance[someConfig](registry, config) | ||
|
||
resolver := resolving.NewResolver(registry) | ||
ctx := resolving.NewScopedContext(context.Background()) | ||
|
||
// Act | ||
actual, err := resolving.ResolveRequiredService[someConfig](resolver, ctx) | ||
|
||
// Arrange | ||
assert.NoError(t, registryErr) | ||
assert.NoError(t, err) | ||
assert.True(t, actual.b) | ||
} | ||
|
||
func Test_Register_service_with_struct_dependency(t *testing.T) { | ||
|
||
// Arrange | ||
registry := registration.NewServiceRegistry() | ||
registry.Register(newAppWithStructDependency, types.LifetimeTransient) | ||
|
||
config := someConfig{b: true} | ||
registryErr := registration.RegisterInstance[someConfig](registry, config) | ||
|
||
resolver := resolving.NewResolver(registry) | ||
ctx := resolving.NewScopedContext(context.Background()) | ||
|
||
// Act | ||
actual, err := resolving.ResolveRequiredService[*appWithStructDependency](resolver, ctx) | ||
|
||
// Arrange | ||
assert.NoError(t, registryErr) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, actual) | ||
assert.True(t, actual.config.b) | ||
} | ||
|
||
func Test_Register_immutable_service_with_struct_dependency(t *testing.T) { | ||
|
||
// Arrange | ||
registry := registration.NewServiceRegistry() | ||
registry.Register(newImmutableAppWithStructDependency, types.LifetimeTransient) | ||
|
||
config := someConfig{b: true} | ||
registryErr := registration.RegisterInstance[someConfig](registry, config) | ||
|
||
resolver := resolving.NewResolver(registry) | ||
ctx := resolving.NewScopedContext(context.Background()) | ||
|
||
// Act | ||
actual, err := resolving.ResolveRequiredService[appWithStructDependency](resolver, ctx) | ||
|
||
// Arrange | ||
assert.NoError(t, registryErr) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, actual) | ||
assert.True(t, actual.config.b) | ||
} | ||
|
||
type someConfig struct { | ||
b bool | ||
} | ||
|
||
type appWithStructDependency struct { | ||
config someConfig | ||
} | ||
|
||
func newAppWithStructDependency(config someConfig) *appWithStructDependency { | ||
return &appWithStructDependency{config: config} | ||
} | ||
|
||
func newImmutableAppWithStructDependency(config someConfig) appWithStructDependency { | ||
return appWithStructDependency{config: config} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters