From 6ca4ae5c819ec3bdaa91e99541a42be816499117 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Mendelski?= Date: Tue, 28 Feb 2023 23:48:31 +0100 Subject: [PATCH] improve tests --- Makefile | 2 +- test/cyclic_dependency_test.go | 3 +-- test/dependency_registration_test.go | 13 ++++++------- test/duplicated_registration_test.go | 12 +++--------- test/lazy_dependency_test.go | 14 ++++++-------- test/named_dependency_test.go | 12 ++++-------- test/parameter_injection_test.go | 21 +++++++-------------- test/skip_dependency_test.go | 3 +-- 8 files changed, 29 insertions(+), 51 deletions(-) diff --git a/Makefile b/Makefile index 83328af..cf882c4 100644 --- a/Makefile +++ b/Makefile @@ -57,7 +57,7 @@ coverage: ## Run tests and create coverage report $(call task,coverage) @rm -rf $(REPORT_DIR)/test @mkdir -p $(REPORT_DIR)/test - @go test -covermode=count -coverprofile=$(REPORT_DIR)/test/coverage.out -v ./... \ + @go test -cover -covermode=atomic -coverpkg=./... -coverprofile=$(REPORT_DIR)/test/coverage.out -v ./... \ | tee >($(GOJUNITREP_CMD) -set-exit-code > $(REPORT_DIR)/test/junit-report.xml) @go tool cover -html=$(REPORT_DIR)/test/coverage.out -o $(REPORT_DIR)/test/coverage.html diff --git a/test/cyclic_dependency_test.go b/test/cyclic_dependency_test.go index f5bb9e6..d14bf31 100644 --- a/test/cyclic_dependency_test.go +++ b/test/cyclic_dependency_test.go @@ -124,8 +124,7 @@ func (suite *CyclicDependencySuite) TestNoErrorThrownWhenNotRetrieved() { return &bar }) ctx := ctxb.Build() - result, err := di.GetOrErr[*Bar](ctx) - suite.Nil(err) + result := di.Get[*Bar](ctx) suite.Equal(&bar, result) } diff --git a/test/dependency_registration_test.go b/test/dependency_registration_test.go index a754c5e..c7285fb 100644 --- a/test/dependency_registration_test.go +++ b/test/dependency_registration_test.go @@ -17,23 +17,23 @@ type DependencyRegistrationSuite struct { func (suite *DependencyRegistrationSuite) TestGetByType() { tests := []struct { value any - get func(ctx *di.Context) (any, error) + get func(ctx *di.Context) any }{ { value: &foo, - get: func(ctx *di.Context) (any, error) { return di.GetOrErr[*Foo](ctx) }, + get: func(ctx *di.Context) any { return di.Get[*Foo](ctx) }, }, { value: foo, - get: func(ctx *di.Context) (any, error) { return di.GetOrErr[Foo](ctx) }, + get: func(ctx *di.Context) any { return di.Get[Foo](ctx) }, }, { value: 42, - get: func(ctx *di.Context) (any, error) { return di.GetOrErr[int](ctx) }, + get: func(ctx *di.Context) any { return di.Get[int](ctx) }, }, { value: "text", - get: func(ctx *di.Context) (any, error) { return di.GetOrErr[string](ctx) }, + get: func(ctx *di.Context) any { return di.Get[string](ctx) }, }, } @@ -43,8 +43,7 @@ func (suite *DependencyRegistrationSuite) TestGetByType() { ctxb := di.NewContextBuilder() ctxb.Add(tt.value) ctx := ctxb.Build() - result, err := tt.get(ctx) - suite.Nil(err) + result := tt.get(ctx) suite.Equal(tt.value, result) }) } diff --git a/test/duplicated_registration_test.go b/test/duplicated_registration_test.go index 97f82e9..095de26 100644 --- a/test/duplicated_registration_test.go +++ b/test/duplicated_registration_test.go @@ -18,16 +18,10 @@ func (suite *DuplicatedRegistartionSuite) TestWithFunction() { inits := 0 ctor := func() *Foo { inits++; return &Foo{} } ctxb := di.NewContextBuilder() - err := func() (err any) { - defer func() { - err = recover() - }() - ctxb.Add(ctor) - ctxb.Add(ctor) - return nil - }() + ctxb.Add(ctor) + err := ctxb.AddOrErr(ctor) suite.NotNil(err) - suite.Equal("duplicated registration", err.(error).Error()) + suite.Equal("duplicated registration", err.Error()) suite.Equal(0, inits) } diff --git a/test/lazy_dependency_test.go b/test/lazy_dependency_test.go index 35bec92..8cae0bc 100644 --- a/test/lazy_dependency_test.go +++ b/test/lazy_dependency_test.go @@ -60,25 +60,25 @@ func (suite *LazyDependencySuite) TestGetByInterface() { value any iface any provide func(ctxb *di.ContextBuilder) - get func(ctx *di.Context) (any, error) + get func(ctx *di.Context) any }{ { value: &foo, iface: new(Baz), provide: func(ctxb *di.ContextBuilder) { ctxb.Add(func() *Foo { return &foo }) }, - get: func(ctx *di.Context) (any, error) { return di.GetOrErr[Baz](ctx) }, + get: func(ctx *di.Context) any { return di.Get[Baz](ctx) }, }, { value: (*Foo)(nil), iface: new(Baz), provide: func(ctxb *di.ContextBuilder) { ctxb.Add(func() *Foo { return nil }) }, - get: func(ctx *di.Context) (any, error) { return di.GetOrErr[Baz](ctx) }, + get: func(ctx *di.Context) any { return di.Get[Baz](ctx) }, }, { value: bar, iface: new(Baz), provide: func(ctxb *di.ContextBuilder) { ctxb.Add(func() Bar { return bar }) }, - get: func(ctx *di.Context) (any, error) { return di.GetOrErr[Baz](ctx) }, + get: func(ctx *di.Context) any { return di.Get[Baz](ctx) }, }, } @@ -88,8 +88,7 @@ func (suite *LazyDependencySuite) TestGetByInterface() { ctxb := di.NewContextBuilder() ctxb.AddAs(tt.iface, tt.value) ctx := ctxb.Build() - result, err := tt.get(ctx) - suite.Nil(err) + result := tt.get(ctx) suite.Equal(tt.value, result) }) } @@ -102,8 +101,7 @@ func (suite *LazyDependencySuite) TestGetAllByType() { ctxb.Add(func() *Foo { return foo1 }) ctxb.Add(func() *Foo { return foo2 }) ctx := ctxb.Build() - result, err := di.GetAllOrErr[*Foo](ctx) - suite.Nil(err) + result := di.GetAll[*Foo](ctx) suite.Equal([]*Foo{foo1, foo2}, result) } diff --git a/test/named_dependency_test.go b/test/named_dependency_test.go index 648a057..a1ed570 100644 --- a/test/named_dependency_test.go +++ b/test/named_dependency_test.go @@ -20,11 +20,9 @@ func (suite *NamedDependencySuite) TestGetNamedDependencyByName() { ctxb.AddNamed("foo1", &foo1) ctxb.AddNamed("foo2", &foo2) ctx := ctxb.Build() - result, err := di.GetNamedOrErr[*Foo](ctx, "foo1") - suite.Nil(err) + result := di.GetNamed[*Foo](ctx, "foo1") suite.Equal(&foo1, result) - result, err = di.GetNamedOrErr[*Foo](ctx, "foo2") - suite.Nil(err) + result = di.GetNamed[*Foo](ctx, "foo2") suite.Equal(&foo2, result) } @@ -36,11 +34,9 @@ func (suite *NamedDependencySuite) TestGetNamedDependencyByType() { ctxb.AddNamed("foo2", &foo2) ctxb.Add(&foo) ctx := ctxb.Build() - result, err := di.GetOrErr[*Foo](ctx) - suite.Nil(err) + result := di.Get[*Foo](ctx) suite.Equal(&foo1, result) - all, err := di.GetAllOrErr[*Foo](ctx) - suite.Nil(err) + all := di.GetAll[*Foo](ctx) suite.Equal([]*Foo{&foo1, &foo2, &foo}, all) } diff --git a/test/parameter_injection_test.go b/test/parameter_injection_test.go index cd3c806..f7548a0 100644 --- a/test/parameter_injection_test.go +++ b/test/parameter_injection_test.go @@ -24,8 +24,7 @@ func (suite *ParameterInjectionSuite) TestInjectParams() { return &Boo{foo: pfoo, bar: pbar} }) ctx := ctxb.Build() - result, err := di.GetOrErr[*Boo](ctx) - suite.Nil(err) + result := di.Get[*Boo](ctx) suite.NotNil(result) suite.Equal(&foo, result.foo) suite.Equal(&bar, result.bar) @@ -41,8 +40,7 @@ func (suite *ParameterInjectionSuite) TestInjectCastedParam() { return &Boo{baz: baz} }) ctx := ctxb.Build() - result, err := di.GetOrErr[*Boo](ctx) - suite.Nil(err) + result := di.Get[*Boo](ctx) suite.NotNil(result) suite.Equal(&foo, result.baz) } @@ -62,8 +60,7 @@ func (suite *ParameterInjectionSuite) TestInjectContext() { } }) ctx := ctxb.Build() - result, err := di.GetOrErr[*Boo](ctx) - suite.Nil(err) + result := di.Get[*Boo](ctx) suite.NotNil(result) suite.Equal(&foo, result.foo) suite.Equal(&bar, result.bar) @@ -84,8 +81,7 @@ func (suite *ParameterInjectionSuite) TestInjectMixed() { } }) ctx := ctxb.Build() - result, err := di.GetOrErr[*Boo](ctx) - suite.Nil(err) + result := di.Get[*Boo](ctx) suite.NotNil(result) suite.Equal(&foo, result.foo) suite.Equal(&bar, result.bar) @@ -119,8 +115,7 @@ func (suite *ParameterInjectionSuite) TestInjectSliceOfInterfaces() { return &Boo{baz: baz} }) ctx := ctxb.Build() - result, err := di.GetOrErr[*Boo](ctx) - suite.Nil(err) + result := di.Get[*Boo](ctx) suite.NotNil(result) suite.NotNil(result.baz) suite.Equal(2, len(result.baz)) @@ -139,8 +134,7 @@ func (suite *ParameterInjectionSuite) TestInjectSliceOfStructs() { return &Boo{foo: foo} }) ctx := ctxb.Build() - result, err := di.GetOrErr[*Boo](ctx) - suite.Nil(err) + result := di.Get[*Boo](ctx) suite.NotNil(result) suite.NotNil(result.foo) suite.Equal(2, len(result.foo)) @@ -159,8 +153,7 @@ func (suite *ParameterInjectionSuite) TestInjectSliceOfStructPtrs() { return &Boo{foo: foo} }) ctx := ctxb.Build() - result, err := di.GetOrErr[*Boo](ctx) - suite.Nil(err) + result := di.Get[*Boo](ctx) suite.NotNil(result) suite.NotNil(result.foo) suite.Equal(2, len(result.foo)) diff --git a/test/skip_dependency_test.go b/test/skip_dependency_test.go index d472565..24a86a5 100644 --- a/test/skip_dependency_test.go +++ b/test/skip_dependency_test.go @@ -43,8 +43,7 @@ func (suite *SkipDependencySuite) TestResolveSliceWithNonSkippedDependnecies() { return &Boo{baz: baz} }) ctx := ctxb.Build() - result, err := di.GetOrErr[*Boo](ctx) - suite.Nil(err) + result := di.Get[*Boo](ctx) suite.NotNil(result) suite.NotNil(result.baz) suite.Equal(1, len(result.baz))