Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
// AddFunc is a convenience method to add a singleton service descriptor to the ContainerBuilder.
// AddFuncByLookupKey is a convenience method to add a singleton service descriptor to the ContainerBuilder.
  • Loading branch information
ghstahl committed Apr 17, 2024
1 parent f5544eb commit 3dbf48b
Show file tree
Hide file tree
Showing 4 changed files with 209 additions and 137 deletions.
14 changes: 14 additions & 0 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,20 @@ func AddSingleton[T any](cb ContainerBuilder, ctor any, implementedInterfaceType
cb.Add(Singleton[T](ctor, implementedInterfaceTypes...))
}

// AddFunc is a convenience method to add a singleton service descriptor to the ContainerBuilder.
func AddFunc[T any](cb ContainerBuilder, ctor any) {
AddSingleton[T](cb, ctor)
}

// AddFuncByLookupKey is a convenience method to add a singleton service descriptor to the ContainerBuilder.
func AddFuncWithLookupKeys[T any](cb ContainerBuilder,
ctor any,
lookupKeys []string,
metadata map[string]interface{},
) {
AddSingletonWithLookupKeys[T](cb, ctor, lookupKeys, metadata)
}

// Add a singleton service descriptor to the ContainerBuilder.
// T is the service type,
// cb is the ContainerBuilder,
Expand Down
47 changes: 47 additions & 0 deletions funcs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package di

import (
"testing"

"github.com/stretchr/testify/require"
)

type (
MyFunc func() string
)

func AddMyFunc(b ContainerBuilder) {
AddFunc[MyFunc](b, func() MyFunc {
return func() string {
return "Hello"
}
})
}
func AddMyFuncByName(b ContainerBuilder, name string) {
AddFuncWithLookupKeys[MyFunc](b,
func() MyFunc {
return func() string {
return "Hello: " + name
}
}, []string{name}, map[string]interface{}{})
}
func TestAddMyFunc(t *testing.T) {
b := Builder()
// Build the container
AddMyFunc(b)
c := b.Build()

myFunc := Get[MyFunc](c)
require.NotNil(t, myFunc)
require.Equal(t, "Hello", myFunc())
}
func TestAddMyFuncByName(t *testing.T) {
b := Builder()
// Build the container
AddMyFuncByName(b, "test")
c := b.Build()

myFunc := GetByLookupKey[MyFunc](c, "test")
require.NotNil(t, myFunc)
require.Equal(t, "Hello: test", myFunc())
}
148 changes: 148 additions & 0 deletions lookup_keys_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package di

import (
"fmt"
"reflect"
"testing"

"github.com/stretchr/testify/require"
)

func AddSingletonEmployeesWithLookupKeys(b ContainerBuilder) {
AddSingletonWithLookupKeys[*employee](b,
func() *employee {
return &employee{Name: "1"}
}, []string{"1"}, map[string]interface{}{"name": "1"},
reflect.TypeOf((*IEmployee)(nil)))
AddSingletonWithLookupKeys[*employee](b,
func() *employee {
return &employee{Name: "2a"}
}, []string{"2"}, map[string]interface{}{"name": "2a"},
reflect.TypeOf((*IEmployee)(nil)))
AddSingletonWithLookupKeys[*employee](b,
func() *employee {
return &employee{Name: "2"}
}, []string{"2"}, map[string]interface{}{"name": "2"},
reflect.TypeOf((*IEmployee)(nil)))
}
func AddTransientEmployeesWithLookupKeys(b ContainerBuilder) {
AddTransientWithLookupKeys[*employee](b,
func() *employee {
return &employee{Name: "1"}
}, []string{"1"},
map[string]interface{}{"name": "1"},
reflect.TypeOf((*IEmployee)(nil)))
AddTransientWithLookupKeys[*employee](b,
func() *employee {
return &employee{Name: "2"}
}, []string{"2"},
map[string]interface{}{"name": "2"},
reflect.TypeOf((*IEmployee)(nil)))
}
func AddInstanceEmployeesWithLookupKeys(b ContainerBuilder) {
AddInstanceWithLookupKeys[*employee](b,
&employee{Name: "1"}, []string{"1"},
map[string]interface{}{"name": "1"},
reflect.TypeOf((*IEmployee)(nil)))
AddInstanceWithLookupKeys[*employee](b,
&employee{Name: "2"}, []string{"2"},
map[string]interface{}{"name": "2"},
reflect.TypeOf((*IEmployee)(nil)))
}
func AddScopedHandlersWithLookupKeys(b ContainerBuilder) {
AddScopedWithLookupKeys[*handler](b,
func() *handler {
return &handler{path: "1"}
}, []string{"1"}, map[string]interface{}{"name": "1"},
reflect.TypeOf((*IHandler)(nil)))
AddScopedWithLookupKeys[*handler](b,
func() *handler {
return &handler{path: "2"}
}, []string{"2"}, map[string]interface{}{"name": "2"},
reflect.TypeOf((*IHandler)(nil)))
}

func TestManyWithScopeWithLookupKeys(t *testing.T) {
b := Builder()
// Build the container
AddScopedHandlersWithLookupKeys(b)
c := b.Build()
scopeFactory := Get[ScopeFactory](c)
scope1 := scopeFactory.CreateScope()
ctn := scope1.Container()
descriptors := ctn.GetDescriptors()
require.Equal(t, 2, len(descriptors))
for _, d := range descriptors {
require.NotEmpty(t, d.Metadata)
_, ok := d.Metadata["name"]
require.True(t, ok)
_, ok = d.Metadata["name"].(string)
require.True(t, ok)

fmt.Println(d)
}
handlers := Get[[]IHandler](ctn)
require.Equal(t, 2, len(handlers))
require.NotPanics(t, func() {
h := GetByLookupKey[IHandler](c, "1")
require.NotNil(t, h)
require.Equal(t, "1", h.GetPath())
})
}
func TestManyWithSingletonWithLookupKeys(t *testing.T) {
b := Builder()
// Build the container
AddSingletonEmployeesWithLookupKeys(b)
c := b.Build()
scopeFactory := Get[ScopeFactory](c)
scope1 := scopeFactory.CreateScope()
employees := Get[[]IEmployee](scope1.Container())
require.Equal(t, 3, len(employees))
require.NotPanics(t, func() {
h := GetByLookupKey[IEmployee](c, "1")
require.NotNil(t, h)
require.Equal(t, "1", h.GetName())
})
require.NotPanics(t, func() {
h := GetByLookupKey[IEmployee](c, "2")
require.NotNil(t, h)
require.Equal(t, "2", h.GetName())
})
}
func TestManyWithTransientWithLookupKeys(t *testing.T) {
b := Builder()
// Build the container
AddTransientEmployeesWithLookupKeys(b)
c := b.Build()
scopeFactory := Get[ScopeFactory](c)
scope1 := scopeFactory.CreateScope()
employees := Get[[]IEmployee](scope1.Container())
require.Equal(t, 2, len(employees))
require.NotPanics(t, func() {
h := GetByLookupKey[IEmployee](c, "1")
require.NotNil(t, h)
require.Equal(t, "1", h.GetName())
})
}
func TestManyWithInstanceWithLookupKeys(t *testing.T) {
b := Builder()
// Build the container
AddInstanceEmployeesWithLookupKeys(b)
c := b.Build()
scopeFactory := Get[ScopeFactory](c)
scope1 := scopeFactory.CreateScope()
employees := Get[[]IEmployee](scope1.Container())
require.Equal(t, 2, len(employees))
require.NotPanics(t, func() {
h := GetByLookupKey[IEmployee](c, "1")
require.NotNil(t, h)
require.Equal(t, "1", h.GetName())
})
employeePtrs := Get[[]*employee](scope1.Container())
require.Equal(t, 2, len(employeePtrs))
require.NotPanics(t, func() {
h := GetByLookupKey[*employee](c, "1")
require.NotNil(t, h)
require.Equal(t, "1", h.GetName())
})
}
137 changes: 0 additions & 137 deletions many_interfaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,59 +80,6 @@ func AddSingletonTime(b ContainerBuilder) {
return &myTime{}
})
}
func AddSingletonEmployeesWithLookupKeys(b ContainerBuilder) {
AddSingletonWithLookupKeys[*employee](b,
func() *employee {
return &employee{Name: "1"}
}, []string{"1"}, map[string]interface{}{"name": "1"},
reflect.TypeOf((*IEmployee)(nil)))
AddSingletonWithLookupKeys[*employee](b,
func() *employee {
return &employee{Name: "2a"}
}, []string{"2"}, map[string]interface{}{"name": "2a"},
reflect.TypeOf((*IEmployee)(nil)))
AddSingletonWithLookupKeys[*employee](b,
func() *employee {
return &employee{Name: "2"}
}, []string{"2"}, map[string]interface{}{"name": "2"},
reflect.TypeOf((*IEmployee)(nil)))
}
func AddTransientEmployeesWithLookupKeys(b ContainerBuilder) {
AddTransientWithLookupKeys[*employee](b,
func() *employee {
return &employee{Name: "1"}
}, []string{"1"},
map[string]interface{}{"name": "1"},
reflect.TypeOf((*IEmployee)(nil)))
AddTransientWithLookupKeys[*employee](b,
func() *employee {
return &employee{Name: "2"}
}, []string{"2"},
map[string]interface{}{"name": "2"},
reflect.TypeOf((*IEmployee)(nil)))
}
func AddInstanceEmployeesWithLookupKeys(b ContainerBuilder) {
AddInstanceWithLookupKeys[*employee](b,
&employee{Name: "1"}, []string{"1"},
map[string]interface{}{"name": "1"},
reflect.TypeOf((*IEmployee)(nil)))
AddInstanceWithLookupKeys[*employee](b,
&employee{Name: "2"}, []string{"2"},
map[string]interface{}{"name": "2"},
reflect.TypeOf((*IEmployee)(nil)))
}
func AddScopedHandlersWithLookupKeys(b ContainerBuilder) {
AddScopedWithLookupKeys[*handler](b,
func() *handler {
return &handler{path: "1"}
}, []string{"1"}, map[string]interface{}{"name": "1"},
reflect.TypeOf((*IHandler)(nil)))
AddScopedWithLookupKeys[*handler](b,
func() *handler {
return &handler{path: "2"}
}, []string{"2"}, map[string]interface{}{"name": "2"},
reflect.TypeOf((*IHandler)(nil)))
}

func AddSingletonDepartments(b ContainerBuilder, names ...string) {
// pointer to interface type
Expand Down Expand Up @@ -313,91 +260,7 @@ func TestSingleton(t *testing.T) {
require.Equal(t, "IT", company.GetDepartment().GetName())
require.Equal(t, "Contoso", company.GetName())
}
func TestManyWithScopeWithLookupKeys(t *testing.T) {
b := Builder()
// Build the container
AddScopedHandlersWithLookupKeys(b)
c := b.Build()
scopeFactory := Get[ScopeFactory](c)
scope1 := scopeFactory.CreateScope()
ctn := scope1.Container()
descriptors := ctn.GetDescriptors()
require.Equal(t, 2, len(descriptors))
for _, d := range descriptors {
require.NotEmpty(t, d.Metadata)
_, ok := d.Metadata["name"]
require.True(t, ok)
_, ok = d.Metadata["name"].(string)
require.True(t, ok)

fmt.Println(d)
}
handlers := Get[[]IHandler](ctn)
require.Equal(t, 2, len(handlers))
require.NotPanics(t, func() {
h := GetByLookupKey[IHandler](c, "1")
require.NotNil(t, h)
require.Equal(t, "1", h.GetPath())
})
}

func TestManyWithSingletonWithLookupKeys(t *testing.T) {
b := Builder()
// Build the container
AddSingletonEmployeesWithLookupKeys(b)
c := b.Build()
scopeFactory := Get[ScopeFactory](c)
scope1 := scopeFactory.CreateScope()
employees := Get[[]IEmployee](scope1.Container())
require.Equal(t, 3, len(employees))
require.NotPanics(t, func() {
h := GetByLookupKey[IEmployee](c, "1")
require.NotNil(t, h)
require.Equal(t, "1", h.GetName())
})
require.NotPanics(t, func() {
h := GetByLookupKey[IEmployee](c, "2")
require.NotNil(t, h)
require.Equal(t, "2", h.GetName())
})
}
func TestManyWithTransientWithLookupKeys(t *testing.T) {
b := Builder()
// Build the container
AddTransientEmployeesWithLookupKeys(b)
c := b.Build()
scopeFactory := Get[ScopeFactory](c)
scope1 := scopeFactory.CreateScope()
employees := Get[[]IEmployee](scope1.Container())
require.Equal(t, 2, len(employees))
require.NotPanics(t, func() {
h := GetByLookupKey[IEmployee](c, "1")
require.NotNil(t, h)
require.Equal(t, "1", h.GetName())
})
}
func TestManyWithInstanceWithLookupKeys(t *testing.T) {
b := Builder()
// Build the container
AddInstanceEmployeesWithLookupKeys(b)
c := b.Build()
scopeFactory := Get[ScopeFactory](c)
scope1 := scopeFactory.CreateScope()
employees := Get[[]IEmployee](scope1.Container())
require.Equal(t, 2, len(employees))
require.NotPanics(t, func() {
h := GetByLookupKey[IEmployee](c, "1")
require.NotNil(t, h)
require.Equal(t, "1", h.GetName())
})
employeePtrs := Get[[]*employee](scope1.Container())
require.Equal(t, 2, len(employeePtrs))
require.NotPanics(t, func() {
h := GetByLookupKey[*employee](c, "1")
require.NotNil(t, h)
require.Equal(t, "1", h.GetName())
})
}
func TestManyWithScope(t *testing.T) {
b := Builder()
AddSingletonTime(b)
Expand Down

0 comments on commit 3dbf48b

Please sign in to comment.