cosmos-sdk/depinject/invoke_test.go
Aaron Craelius 7728516abf
refactor(depinject)!: require exported functions & types (#12797)
* refactor(depinject)!: require exported functions

* unexport ProviderDescriptor

* WIP on tests

* fix tests and check for bound instance methods

* address merge issues

* WIP on checking valid types

* WIP on checking valid types

* WIP

* tests passing

* revert changes outside module

* docs

* docs

* docs

* add comment

* revert

* update depinject go.mod versions

* remove go.work

* add go.work back

* go mod tidy

* fix docs

Co-authored-by: Julien Robert <julien@rbrt.fr>
2022-08-31 17:37:01 +00:00

89 lines
2.1 KiB
Go

package depinject_test
import (
"testing"
"github.com/regen-network/gocuke"
"gotest.tools/v3/assert"
"cosmossdk.io/depinject"
)
func TestInvoke(t *testing.T) {
gocuke.NewRunner(t, &InvokeSuite{}).
Path("features/invoke.feature").
Step("an int provider returning 5", (*InvokeSuite).AnIntProviderReturning5).
Step(`a string pointer provider pointing to "foo"`, (*InvokeSuite).AStringPointerProviderPointingToFoo).
Run()
}
type InvokeSuite struct {
gocuke.TestingT
configs []depinject.Config
i int
sp *string
}
func (s *InvokeSuite) AnInvokerRequestingAnIntAndStringPointer() {
s.configs = append(s.configs,
depinject.Supply(s),
depinject.Invoke((*InvokeSuite).IntStringPointerInvoker),
)
}
func (s *InvokeSuite) IntStringPointerInvoker(i int, sp *string) {
s.i = i
s.sp = sp
}
func (s *InvokeSuite) TheContainerIsBuilt() {
assert.NilError(s, depinject.Inject(depinject.Configs(s.configs...)))
}
func (s *InvokeSuite) TheInvokerWillGetTheIntParameterSetTo(a int64) {
assert.Equal(s, int(a), s.i)
}
func (s *InvokeSuite) TheInvokerWillGetTheStringPointerParameterSetToNil() {
if s.sp != nil {
s.Fatalf("expected a nil string pointer, got %s", *s.sp)
}
}
func IntProvider5() int { return 5 }
func (s *InvokeSuite) AnIntProviderReturning5() {
s.configs = append(s.configs, depinject.Provide(IntProvider5))
}
func StringPtrProviderFoo() *string {
x := "foo"
return &x
}
func (s *InvokeSuite) AStringPointerProviderPointingToFoo() {
s.configs = append(s.configs, depinject.Provide(StringPtrProviderFoo))
}
func (s *InvokeSuite) TheInvokerWillGetTheStringPointerParameterSetTo(a string) {
if s.sp == nil {
s.Fatalf("expected a non-nil string pointer")
}
assert.Equal(s, a, *s.sp)
}
func (s *InvokeSuite) AnInvokerRequestingAnIntAndStringPointerRunInModule(a string) {
s.configs = append(s.configs,
depinject.Supply(s),
depinject.InvokeInModule(a, (*InvokeSuite).IntStringPointerInvoker),
)
}
func ProvideLenModuleKey(key depinject.ModuleKey) int {
return len(key.Name())
}
func (s *InvokeSuite) AModulescopedIntProviderWhichReturnsTheLengthOfTheModuleName() {
s.configs = append(s.configs, depinject.Provide(ProvideLenModuleKey))
}