Skip to content

Commit

Permalink
unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ompluscator committed Oct 16, 2022
1 parent a6d0f27 commit 9f0dcda
Show file tree
Hide file tree
Showing 7 changed files with 1,150 additions and 40 deletions.
20 changes: 10 additions & 10 deletions binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "fmt"

type bindingSource[T any] struct {
binding Binding
keySource *baseKeySource[T]
keySource baseKeySource[T]
}

func (s *bindingSource[T]) Binding() (Binding, error) {
Expand All @@ -26,7 +26,7 @@ func (s *bindingSource[T]) Key() Key {

type valueBinding[S any] struct{}

func (*valueBinding[S]) Instance(initialize bool) (interface{}, error) {
func (valueBinding[S]) Instance(initialize bool) (interface{}, error) {
initial := *new(S)
var instance interface{} = &initial
if !initialize {
Expand All @@ -42,8 +42,8 @@ func (*valueBinding[S]) Instance(initialize bool) (interface{}, error) {

func AsValue[T any, S any]() BindingSource[T] {
return &bindingSource[T]{
binding: &valueBinding[S]{},
keySource: &baseKeySource[T]{},
binding: valueBinding[S]{},
keySource: baseKeySource[T]{},
}
}

Expand All @@ -53,7 +53,7 @@ type initializable interface {

type referenceBinding[R any] struct{}

func (*referenceBinding[R]) Instance(initialize bool) (interface{}, error) {
func (referenceBinding[R]) Instance(initialize bool) (interface{}, error) {
var instance interface{} = new(R)
if !initialize {
return instance, nil
Expand All @@ -68,8 +68,8 @@ func (*referenceBinding[R]) Instance(initialize bool) (interface{}, error) {

func AsReference[T any, S *R, R any]() BindingSource[T] {
return &bindingSource[T]{
binding: &referenceBinding[R]{},
keySource: &baseKeySource[T]{},
binding: referenceBinding[R]{},
keySource: baseKeySource[T]{},
}
}

Expand All @@ -82,7 +82,7 @@ func (b ProviderBinding[S]) Instance(bool) (interface{}, error) {
func AsProvider[T any, S any](provider ProviderBinding[S]) BindingSource[T] {
return &bindingSource[T]{
binding: provider,
keySource: &baseKeySource[T]{},
keySource: baseKeySource[T]{},
}
}

Expand All @@ -99,7 +99,7 @@ func AsInstance[T any, S any](instance S) BindingSource[T] {
binding: &instanceBinding[S]{
instance: instance,
},
keySource: &baseKeySource[T]{},
keySource: baseKeySource[T]{},
}
}

Expand Down Expand Up @@ -148,7 +148,7 @@ func AsSingleton() BindingOption {
parent: binding,
}, nil
},
keyOption: &sameKeyOption{},
keyOption: sameKeyOption{},
}
}

Expand Down
25 changes: 13 additions & 12 deletions binding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
)

type testBinding struct {
value string
instance func(initialize bool) (interface{}, error)
}

Expand Down Expand Up @@ -107,7 +108,7 @@ func Test_bindingSource_Binding_success(t *testing.T) {

func Test_bindingSource_Key(t *testing.T) {
source := &bindingSource[int]{
keySource: &baseKeySource[int]{},
keySource: baseKeySource[int]{},
}

result := source.Key()
Expand Down Expand Up @@ -183,16 +184,16 @@ func Test_valueBinding_Instance_initialize(t *testing.T) {
func TestAsValue(t *testing.T) {
result := AsValue[string, string]()
if !reflect.DeepEqual(result, &bindingSource[string]{
binding: &valueBinding[string]{},
keySource: &baseKeySource[string]{},
binding: valueBinding[string]{},
keySource: baseKeySource[string]{},
}) {
t.Error("binding sources are different")
}

result = AsValue[interface{}, testStruct]()
if !reflect.DeepEqual(result, &bindingSource[interface{}]{
binding: &valueBinding[testStruct]{},
keySource: &baseKeySource[interface{}]{},
binding: valueBinding[testStruct]{},
keySource: baseKeySource[interface{}]{},
}) {
t.Error("binding sources are different")
}
Expand Down Expand Up @@ -266,16 +267,16 @@ func Test_referenceBinding_Instance_initialize(t *testing.T) {
func TestAsReference(t *testing.T) {
result := AsReference[*string, *string]()
if !reflect.DeepEqual(result, &bindingSource[*string]{
binding: &referenceBinding[string]{},
keySource: &baseKeySource[*string]{},
binding: referenceBinding[string]{},
keySource: baseKeySource[*string]{},
}) {
t.Error("binding sources are different")
}

result = AsReference[interface{}, *testStruct]()
if !reflect.DeepEqual(result, &bindingSource[interface{}]{
binding: &referenceBinding[testStruct]{},
keySource: &baseKeySource[interface{}]{},
binding: referenceBinding[testStruct]{},
keySource: baseKeySource[interface{}]{},
}) {
t.Error("binding sources are different")
}
Expand Down Expand Up @@ -396,7 +397,7 @@ func TestAsInstance(t *testing.T) {
binding: &instanceBinding[string]{
instance: "value",
},
keySource: &baseKeySource[string]{},
keySource: baseKeySource[string]{},
}) {
t.Error("binding sources are different")
}
Expand All @@ -410,7 +411,7 @@ func TestAsInstance(t *testing.T) {
a: "test",
},
},
keySource: &baseKeySource[interface{}]{},
keySource: baseKeySource[interface{}]{},
}) {
t.Error("binding sources are different")
}
Expand Down Expand Up @@ -527,7 +528,7 @@ func TestAsSingleton(t *testing.T) {
result.(*bindingOption).bindingFunc = nil

if !reflect.DeepEqual(result, &bindingOption{
keyOption: &sameKeyOption{},
keyOption: sameKeyOption{},
}) {
t.Error("binding options are different")
}
Expand Down
22 changes: 13 additions & 9 deletions chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (b *sliceBinding[T]) Instance(initialize bool) (interface{}, error) {
}

type sliceBindingSource[T any] struct {
parent Binding
previous Binding
source BindingSource[T]
keySource KeySource
}
Expand All @@ -48,13 +48,17 @@ func (s *sliceBindingSource[T]) Binding() (Binding, error) {
return nil, err
}

instance, _ := binding.Instance(false)
instance, err := binding.Instance(false)
if err != nil {
return nil, err
}

if _, ok := instance.(T); !ok {
var initial T
return nil, fmt.Errorf(`binding is not possible for "%v" and "%v"`, initial, instance)
}

previous, ok := s.parent.(*sliceBinding[T])
previous, ok := s.previous.(*sliceBinding[T])
if !ok {
return &sliceBinding[T]{
current: binding,
Expand All @@ -68,7 +72,7 @@ func (s *sliceBindingSource[T]) Binding() (Binding, error) {
}

func (s *sliceBindingSource[T]) SetPrevious(binding Binding) {
s.parent = binding
s.previous = binding
}

func (s *sliceBindingSource[T]) Key() Key {
Expand All @@ -78,7 +82,7 @@ func (s *sliceBindingSource[T]) Key() Key {
func InSlice[T any](source BindingSource[T]) BindingSource[T] {
return &sliceBindingSource[T]{
source: source,
keySource: &sliceKeySource[T]{},
keySource: sliceKeySource[T]{},
}
}

Expand Down Expand Up @@ -118,7 +122,7 @@ func (b *mapBinding[K, T]) Instance(initialize bool) (interface{}, error) {
}

type mapBindingSource[K comparable, T any] struct {
parent Binding
previous Binding
source BindingSource[T]
key K
keySource KeySource
Expand All @@ -136,7 +140,7 @@ func (s *mapBindingSource[K, T]) Binding() (Binding, error) {
return nil, fmt.Errorf(`binding is not possible for "%v" and "%v"`, initial, instance)
}

previous, ok := s.parent.(*mapBinding[K, T])
previous, ok := s.previous.(*mapBinding[K, T])
if !ok {
return &mapBinding[K, T]{
key: s.key,
Expand All @@ -152,7 +156,7 @@ func (s *mapBindingSource[K, T]) Binding() (Binding, error) {
}

func (s *mapBindingSource[K, T]) SetPrevious(binding Binding) {
s.parent = binding
s.previous = binding
}

func (s *mapBindingSource[K, T]) Key() Key {
Expand All @@ -163,6 +167,6 @@ func InMap[K comparable, T any](key K, source BindingSource[T]) BindingSource[T]
return &mapBindingSource[K, T]{
key: key,
source: source,
keySource: &mapKeySource[K, T]{},
keySource: mapKeySource[K, T]{},
}
}
Loading

0 comments on commit 9f0dcda

Please sign in to comment.