Skip to content

Commit

Permalink
add test for global instance
Browse files Browse the repository at this point in the history
  • Loading branch information
miladrahimi committed Mar 22, 2022
1 parent 699d831 commit f982dce
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
4 changes: 2 additions & 2 deletions container.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"unsafe"
)

// container is the global repository of bindings
// container is the global instance.
var container = New()

// Singleton binds an abstraction to concrete for further singleton resolves.
Expand Down Expand Up @@ -77,7 +77,7 @@ func (b binding) resolve(c Container) (interface{}, error) {
return c.invoke(b.resolver)
}

// Container holds all of the declared bindings
// Container holds the declared bindings
type Container map[reflect.Type]map[string]binding

// New creates a new instance of the Container
Expand Down
47 changes: 47 additions & 0 deletions container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,53 @@ func (m MySQL) Connect() bool {

var instance = container.New()

func TestContainer_With_The_Global_Instance(t *testing.T) {
err := container.Singleton(func() Shape {
return &Circle{a: 13}
})
assert.NoError(t, err)

err = container.Call(func(s Shape) {})
assert.NoError(t, err)

var sh Shape
err = container.Resolve(&sh)
assert.NoError(t, err)

err = container.Transient(func() Shape {
return &Circle{a: 13}
})
assert.NoError(t, err)

err = container.Resolve(&sh)
assert.NoError(t, err)

err = container.NamedSingleton("theCircle", func() Shape {
return &Circle{a: 13}
})
assert.NoError(t, err)

err = container.NamedResolve(&sh, "theCircle")
assert.NoError(t, err)

err = container.NamedTransient("theCircle", func() Shape {
return &Circle{a: 13}
})
assert.NoError(t, err)

err = container.NamedResolve(&sh, "theCircle")
assert.NoError(t, err)

myApp := struct {
S Shape `container:"type"`
}{}

err = container.Fill(&myApp)
assert.NoError(t, err)

container.Reset()
}

func TestContainer_Singleton(t *testing.T) {
err := instance.Singleton(func() Shape {
return &Circle{a: 13}
Expand Down

0 comments on commit f982dce

Please sign in to comment.