Skip to content

Commit

Permalink
improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pmendelski committed Feb 28, 2023
1 parent e4d4bdf commit 6ca4ae5
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 51 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 1 addition & 2 deletions test/cyclic_dependency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
13 changes: 6 additions & 7 deletions test/dependency_registration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) },
},
}

Expand All @@ -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)
})
}
Expand Down
12 changes: 3 additions & 9 deletions test/duplicated_registration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
14 changes: 6 additions & 8 deletions test/lazy_dependency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) },
},
}

Expand All @@ -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)
})
}
Expand All @@ -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)
}

Expand Down
12 changes: 4 additions & 8 deletions test/named_dependency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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)
}

Expand Down
21 changes: 7 additions & 14 deletions test/parameter_injection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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))
Expand All @@ -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))
Expand All @@ -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))
Expand Down
3 changes: 1 addition & 2 deletions test/skip_dependency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 6ca4ae5

Please sign in to comment.