-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9f0dcda
commit 805f24e
Showing
16 changed files
with
685 additions
and
90 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
language: go | ||
|
||
go: | ||
- 1.18 | ||
- stable | ||
- master | ||
|
||
script: | ||
- go test -v ./... | ||
- go test -v -race ./... |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package examples | ||
|
||
import ( | ||
"fmt" | ||
"github.com/ompluscator/genjector" | ||
"testing" | ||
) | ||
|
||
type AnnotationInterface interface { | ||
String() string | ||
} | ||
|
||
type AnnotationStruct struct { | ||
value string | ||
} | ||
|
||
func (s *AnnotationStruct) Init() { | ||
firstChild := genjector.MustInitialize[*AnnotationChildStruct](genjector.AnnotatedWith("first")) | ||
secondChild := genjector.MustInitialize[*AnnotationChildStruct](genjector.AnnotatedWith("second")) | ||
s.value = fmt.Sprintf("%s | %s", firstChild.value, secondChild.value) | ||
} | ||
|
||
func (s *AnnotationStruct) String() string { | ||
return s.value | ||
} | ||
|
||
type AnnotationChildStruct struct { | ||
value string | ||
} | ||
|
||
func (s *AnnotationChildStruct) Init() { | ||
s.value = "value provided inside the AnnotationChildStruct" | ||
} | ||
|
||
func TestAsAnnotation(t *testing.T) { | ||
t.Run("Take values from inner 2 child objects defined with proper annotation", func(t *testing.T) { | ||
genjector.Clean() | ||
|
||
err := genjector.Bind(genjector.AsReference[AnnotationInterface, *AnnotationStruct]()) | ||
if err != nil { | ||
t.Error("binding should not cause an error") | ||
} | ||
|
||
err = genjector.Bind(genjector.AsProvider[*AnnotationChildStruct](func() (*AnnotationChildStruct, error) { | ||
return &AnnotationChildStruct{ | ||
value: "value from the first child", | ||
}, nil | ||
}), genjector.WithAnnotation("first")) | ||
if err != nil { | ||
t.Error("binding should not cause an error") | ||
} | ||
|
||
err = genjector.Bind(genjector.AsProvider[*AnnotationChildStruct](func() (*AnnotationChildStruct, error) { | ||
return &AnnotationChildStruct{ | ||
value: "value from the second child", | ||
}, nil | ||
}), genjector.WithAnnotation("second")) | ||
if err != nil { | ||
t.Error("binding should not cause an error") | ||
} | ||
|
||
instance, err := genjector.Initialize[AnnotationInterface]() | ||
if err != nil { | ||
t.Error("initialization should not cause an error") | ||
} | ||
|
||
value := instance.String() | ||
if value != "value from the first child | value from the second child" { | ||
t.Errorf(`unexpected value received: "%s"`, value) | ||
} | ||
}) | ||
} |
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,56 @@ | ||
package examples | ||
|
||
import ( | ||
"github.com/ompluscator/genjector" | ||
"testing" | ||
) | ||
|
||
type ContainerInterface interface { | ||
String() string | ||
} | ||
|
||
type ContainerStruct struct { | ||
value string | ||
} | ||
|
||
func (s *ContainerStruct) Init() { | ||
s.value = "value provided inside the ContainerStruct" | ||
} | ||
|
||
func (s *ContainerStruct) String() string { | ||
return s.value | ||
} | ||
|
||
func TestAsContainer(t *testing.T) { | ||
t.Run("Store binding inside the customer container and not the global one", func(t *testing.T) { | ||
genjector.Clean() | ||
|
||
customContainer := genjector.NewContainer() | ||
|
||
err := genjector.Bind( | ||
genjector.AsReference[ContainerInterface, *ContainerStruct](), | ||
genjector.WithContainer(customContainer), | ||
) | ||
if err != nil { | ||
t.Error("binding should not cause an error") | ||
} | ||
|
||
instance, err := genjector.Initialize[ContainerInterface]() | ||
if err == nil { | ||
t.Error("expected an error, but got nil") | ||
} | ||
if instance != nil { | ||
t.Errorf(`unexpected instance received: "%s"`, instance) | ||
} | ||
|
||
instance, err = genjector.Initialize[ContainerInterface](genjector.WithContainer(customContainer)) | ||
if err != nil { | ||
t.Error("initialization should not cause an error") | ||
} | ||
|
||
value := instance.String() | ||
if value != "value provided inside the ContainerStruct" { | ||
t.Errorf(`unexpected value received: "%s"`, value) | ||
} | ||
}) | ||
} |
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,67 @@ | ||
package examples | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/ompluscator/genjector" | ||
) | ||
|
||
type InstanceInterface interface { | ||
String() string | ||
} | ||
|
||
type InstanceStruct struct { | ||
value string | ||
} | ||
|
||
func (s *InstanceStruct) Init() { | ||
s.value = "value provided inside the InstanceStruct" | ||
} | ||
|
||
func (s *InstanceStruct) String() string { | ||
return s.value | ||
} | ||
|
||
func TestAsInstance(t *testing.T) { | ||
t.Run("Bind a reference to a struct as an implementation for an interface", func(t *testing.T) { | ||
genjector.Clean() | ||
|
||
err := genjector.Bind(genjector.AsInstance[InstanceInterface](&InstanceStruct{ | ||
value: "value provided inside the Test method", | ||
})) | ||
if err != nil { | ||
t.Error("binding should not cause an error") | ||
} | ||
|
||
instance, err := genjector.Initialize[InstanceInterface]() | ||
if err != nil { | ||
t.Error("initialization should not cause an error") | ||
} | ||
|
||
value := instance.String() | ||
if value != "value provided inside the Test method" { | ||
t.Errorf(`unexpected value received: "%s"`, value) | ||
} | ||
}) | ||
|
||
t.Run("Bind a reference to a struct as an implementation for the reference of that struct", func(t *testing.T) { | ||
genjector.Clean() | ||
|
||
err := genjector.Bind(genjector.AsInstance[*InstanceStruct](&InstanceStruct{ | ||
value: "value provided inside the Test method", | ||
})) | ||
if err != nil { | ||
t.Error("binding should not cause an error") | ||
} | ||
|
||
instance, err := genjector.Initialize[*InstanceStruct]() | ||
if err != nil { | ||
t.Error("initialization should not cause an error") | ||
} | ||
|
||
value := instance.String() | ||
if value != "value provided inside the Test method" { | ||
t.Errorf(`unexpected value received: "%s"`, value) | ||
} | ||
}) | ||
} |
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,75 @@ | ||
package examples | ||
|
||
import ( | ||
"github.com/ompluscator/genjector" | ||
"testing" | ||
) | ||
|
||
type MapInterface interface { | ||
String() string | ||
} | ||
|
||
type MapStruct struct { | ||
value string | ||
} | ||
|
||
func (s *MapStruct) Init() { | ||
s.value = "value provided inside the MapStruct" | ||
} | ||
|
||
func (s *MapStruct) String() string { | ||
return s.value | ||
} | ||
|
||
func TestAsMap(t *testing.T) { | ||
t.Run("Bind multiple references to a struct as an implementations for an interface", func(t *testing.T) { | ||
genjector.Clean() | ||
|
||
err := genjector.Bind( | ||
genjector.InMap("first", genjector.AsReference[MapInterface, *MapStruct]()), | ||
) | ||
if err != nil { | ||
t.Error("binding should not cause an error") | ||
} | ||
|
||
err = genjector.Bind( | ||
genjector.InMap("second", genjector.AsProvider[MapInterface](func() (*MapStruct, error) { | ||
return &MapStruct{ | ||
value: "value provided inside the ProviderMethod", | ||
}, nil | ||
})), | ||
) | ||
if err != nil { | ||
t.Error("binding should not cause an error") | ||
} | ||
|
||
err = genjector.Bind( | ||
genjector.InMap("third", genjector.AsInstance[MapInterface](&MapStruct{ | ||
value: "value provided inside the Test method", | ||
})), | ||
) | ||
if err != nil { | ||
t.Error("binding should not cause an error") | ||
} | ||
|
||
instance, err := genjector.Initialize[map[string]MapInterface]() | ||
if err != nil { | ||
t.Error("initialization should not cause an error") | ||
} | ||
|
||
value := instance["first"].String() | ||
if value != "value provided inside the MapStruct" { | ||
t.Errorf(`unexpected value received: "%s"`, value) | ||
} | ||
|
||
value = instance["second"].String() | ||
if value != "value provided inside the ProviderMethod" { | ||
t.Errorf(`unexpected value received: "%s"`, value) | ||
} | ||
|
||
value = instance["third"].String() | ||
if value != "value provided inside the Test method" { | ||
t.Errorf(`unexpected value received: "%s"`, value) | ||
} | ||
}) | ||
} |
Oops, something went wrong.