Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fix and improve test for call validation (#52) #52

Merged
merged 3 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ go 1.20
require (
github.com/golang/mock v1.6.0
github.com/h2non/gock v1.2.0
github.com/huandu/go-clone v1.5.0
github.com/huandu/go-clone v1.6.0
github.com/stretchr/testify v1.8.4
golang.org/x/exp v0.0.0-20230725093048-515e97ebf090
golang.org/x/exp v0.0.0-20230801115018-d63ba01acd4b
golang.org/x/text v0.11.0
golang.org/x/tools v0.11.0
golang.org/x/tools v0.11.1
)

require (
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslC
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542/go.mod h1:Ow0tF8D4Kplbc8s8sSb3V2oUCygFHVp8gC3Dn6U4MNI=
github.com/huandu/go-assert v1.1.5 h1:fjemmA7sSfYHJD7CUqs9qTwwfdNAx7/j2/ZlHXzNB3c=
github.com/huandu/go-assert v1.1.5/go.mod h1:yOLvuqZwmcHIC5rIzrBhT7D3Q9c3GFnd0JrPVhn/06U=
github.com/huandu/go-clone v1.5.0 h1:vXzaNv7gjTsP6dT3KSv98wrVbC+WLhKVfoM+gvHBxbk=
github.com/huandu/go-clone v1.5.0/go.mod h1:ReGivhG6op3GYr+UY3lS6mxjKp7MIGTknuU5TbTVaXE=
github.com/huandu/go-clone v1.6.0 h1:HMo5uvg4wgfiy5FoGOqlFLQED/VGRm2D9Pi8g1FXPGc=
github.com/huandu/go-clone v1.6.0/go.mod h1:ReGivhG6op3GYr+UY3lS6mxjKp7MIGTknuU5TbTVaXE=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
Expand All @@ -33,8 +33,8 @@ github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXl
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/exp v0.0.0-20230725093048-515e97ebf090 h1:Di6/M8l0O2lCLc6VVRWhgCiApHV8MnQurBnFSHsQtNY=
golang.org/x/exp v0.0.0-20230725093048-515e97ebf090/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
golang.org/x/exp v0.0.0-20230801115018-d63ba01acd4b h1:r+vk0EmXNmekl0S0BascoeeoHk/L7wmaW2QF90K+kYI=
golang.org/x/exp v0.0.0-20230801115018-d63ba01acd4b/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
Expand All @@ -59,8 +59,8 @@ golang.org/x/text v0.11.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.11.0 h1:EMCa6U9S2LtZXLAMoWiR/R8dAQFRqbAitmbJ2UKhoi8=
golang.org/x/tools v0.11.0/go.mod h1:anzJrxPjNtfgiYQYirP2CPGzGLxrH2u2QBhn6Bf3qY8=
golang.org/x/tools v0.11.1 h1:ojD5zOW8+7dOGzdnNgersm8aPfcDjhMp12UfG93NIMc=
golang.org/x/tools v0.11.1/go.mod h1:anzJrxPjNtfgiYQYirP2CPGzGLxrH2u2QBhn6Bf3qY8=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
99 changes: 83 additions & 16 deletions mock/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package mock_test

import (
"errors"
"os"
"path"
"strings"
"testing"

Expand Down Expand Up @@ -69,30 +71,79 @@ func CallC(input string) mock.SetupFunc {
}
}

func MockSetup(t gomock.TestReporter, mockSetup mock.SetupFunc) *mock.Mocks {
return mock.NewMocks(t).Expect(mockSetup)
}
var (
// Generic source directory for caller path evaluation.
SourceDir = func() string {
dir, err := os.Getwd()
if err != nil {
panic(err)
}
return dir
}()
// CallerCallA provides the file with the line number of the `CallA` call
// in mock.
CallerCallA = path.Join(SourceDir, "mock/mock_test.go:35")
)

type MockParams struct {
mockSetup mock.SetupFunc
call func(*mock.Mocks)
failSetup func(test.Test, *mock.Mocks) mock.SetupFunc
call func(test.Test, *mock.Mocks)
}

var testMockParams = map[string]MockParams{
"single mock with single call": {
mockSetup: mock.Setup(
CallA("okay"),
CallA("ok"),
),
call: func(_ test.Test, mocks *mock.Mocks) {
mock.Get(mocks, NewMockIFace).CallA("ok")
},
},
"single mock with two calls": {
mockSetup: mock.Setup(
CallA("ok"), CallA("okay"),
),
call: func(mocks *mock.Mocks) {
call: func(_ test.Test, mocks *mock.Mocks) {
mock.Get(mocks, NewMockIFace).CallA("ok")
mock.Get(mocks, NewMockIFace).CallA("okay")
},
},
"single mock with missing calls": {
mockSetup: mock.Setup(
CallA("ok"), CallA("okay"),
),
failSetup: test.MissingCalls(CallA("okay")),
call: func(_ test.Test, mocks *mock.Mocks) {
mock.Get(mocks, NewMockIFace).CallA("ok")
},
},
"single mock with unexpected call": {
failSetup: test.UnexpectedCall(NewMockIFace,
"CallA", path.Join(SourceDir, "mock_test.go:125"), "ok"),
call: func(_ test.Test, mocks *mock.Mocks) {
mock.Get(mocks, NewMockIFace).CallA("ok")
},
},
"single mock with more than expected calls": {
mockSetup: mock.Setup(
CallA("ok"),
),
failSetup: test.ConsumedCall(NewMockIFace,
"CallA", path.Join(SourceDir, "mock_test.go:137"),
path.Join(SourceDir, "mock_test.go:37"), "ok"),
call: func(_ test.Test, mocks *mock.Mocks) {
mock.Get(mocks, NewMockIFace).CallA("ok")
mock.Get(mocks, NewMockIFace).CallA("ok")
},
},

"single mock with many calls": {
mockSetup: mock.Setup(
CallA("okay"),
CallB("okay", "okay"),
),
call: func(mocks *mock.Mocks) {
call: func(_ test.Test, mocks *mock.Mocks) {
mock.Get(mocks, NewMockIFace).CallA("okay")
mock.Get(mocks, NewMockIFace).CallB("okay")
},
Expand All @@ -103,7 +154,7 @@ var testMockParams = map[string]MockParams{
CallB("okay", "okay"),
CallC("okay"),
),
call: func(mocks *mock.Mocks) {
call: func(_ test.Test, mocks *mock.Mocks) {
mock.Get(mocks, NewMockIFace).CallA("okay")
mock.Get(mocks, NewMockIFace).CallB("okay")
mock.Get(mocks, NewMockXFace).CallC("okay")
Expand All @@ -112,16 +163,32 @@ var testMockParams = map[string]MockParams{
}

func TestMocks(t *testing.T) {
test.Map(t, testMockParams).Run(func(t test.Test, param MockParams) {
// Given
mocks := MockSetup(t, param.mockSetup)
test.Map(t, testMockParams).
RunSeq(func(t test.Test, param MockParams) {
// Given
mocks := mock.NewMocks(t)

// When
param.call(mocks)
// When
test.InRun(test.Success, func(tt test.Test) {
// Given
imocks := mock.NewMocks(tt)
if param.failSetup != nil {
mocks.Expect(param.failSetup(tt, imocks))
}
imocks.Expect(param.mockSetup)

// Then
mocks.Wait()
})
// Connect the mock controller directly to the isolated parent
// test environment to capture the mock controller failure.
imocks.Ctrl.T = t

// When
param.call(tt, imocks)
})(t)
})
}

func MockSetup(t gomock.TestReporter, mockSetup mock.SetupFunc) *mock.Mocks {
return mock.NewMocks(t).Expect(mockSetup)
}

func MockValidate(
Expand Down
17 changes: 17 additions & 0 deletions test/caller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package test_test
// File contains the split out logic to calculate the caller for automated
// testing of test failure validation.
import (
"os"
"path"
"runtime"
"strconv"
"testing"
Expand Down Expand Up @@ -81,4 +83,19 @@ var (
CallerPanic = getCaller(func(t test.Reporter) {
t.Panic("fail")
})

// Generic source directory for caller path evaluation.
SourceDir = func() string {
dir, err := os.Getwd()
if err != nil {
panic(err)
}
return dir
}()
// CallerTestErrorf provides the file with the line number of the `Errorf`
// call in testing.
CallerTestErrorf = path.Join(SourceDir, "testing.go:175")
// CallerGomockErrorf provides the file with the line number of the
// `Errorf` call in gomock.
CallerGomockErrorf = path.Join(SourceDir, "gomock.go:61")
)
93 changes: 83 additions & 10 deletions test/gomock.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/tkrop/go-testing/mock"
)

// Validator a test failure validator based on the thest reporter interface.
// Validator a test failure validator based on the test reporter interface.
type Validator struct {
ctrl *gomock.Controller
recorder *Recorder
Expand Down Expand Up @@ -58,8 +58,7 @@ func (r *Recorder) Errorf(format string, args ...any) *gomock.Call {
func Errorf(format string, args ...any) mock.SetupFunc {
return func(mocks *mock.Mocks) any {
return mock.Get(mocks, NewValidator).EXPECT().
Errorf(format, args...).
Do(mocks.Do(Reporter.Errorf))
Errorf(format, args...).Do(mocks.Do(Reporter.Errorf))
}
}

Expand Down Expand Up @@ -125,18 +124,63 @@ func (r *Recorder) Panic(arg any) *gomock.Call {
func Panic(arg any) mock.SetupFunc {
return func(mocks *mock.Mocks) any {
return mock.Get(mocks, NewValidator).EXPECT().
Panic(Error(arg)).Do(mocks.Do(Reporter.Panic))
Panic(EqError(arg)).Do(mocks.Do(Reporter.Panic))
}
}

// errorMatcher is an error matcher to improve capabilities of matching errors.
// UnexpectedCall creates expectation for unexpected calls. We only support one
// unexpected call since the test execution stops in this case.
func UnexpectedCall[T any](
creator func(*gomock.Controller) *T,
method, caller string, args ...any,
) func(Test, *mock.Mocks) mock.SetupFunc {
return func(t Test, mocks *mock.Mocks) mock.SetupFunc {
return Fatalf("Unexpected call to %T.%v(%v) at %s because: %s",
mock.Get(mocks, creator), method, args, caller,
fmt.Errorf("there are no expected calls "+ //nolint:goerr113 // necessary
"of the method \"%s\" for that receiver", method))
}
}

func ConsumedCall[T any](
creator func(*gomock.Controller) *T,
method, caller, ecaller string, args ...any,
) func(Test, *mock.Mocks) mock.SetupFunc {
return func(t Test, mocks *mock.Mocks) mock.SetupFunc {
return Fatalf("Unexpected call to %T.%v(%v) at %s because: %s",
mock.Get(mocks, creator), method, args, caller,
fmt.Errorf("\nexpected call at %s has "+ //nolint:goerr113 // necessary
"already been called the max number of times", ecaller))
}
}

// MissingCalls creates an expectation for all missing calls.
func MissingCalls(
setups ...mock.SetupFunc,
) func(Test, *mock.Mocks) mock.SetupFunc {
return func(t Test, _ *mock.Mocks) mock.SetupFunc {
// Creates a new mock controller and test environment to isolate the
// validator used for sub-call creation/registration from the validator
// used for execution.
mocks := mock.NewMocks(NewTester(t, false))
calls := make([]func(*mock.Mocks) any, 0, len(setups))
for _, setup := range setups {
calls = append(calls,
Errorf("missing call(s) to %v", EqCall(setup(mocks))))
}
calls = append(calls, Errorf("aborting test due to missing call(s)"))
return mock.Chain(calls...)
}
}

// errorMatcher is a matcher to improve capabilities of matching errors.
type errorMatcher struct {
x any
}

// Error creates a new error matcher that allows to match either the error or
// EqError creates a new error matcher that allows to match either the error or
// alternatively the string describing the error.
func Error(x any) gomock.Matcher {
func EqError(x any) gomock.Matcher {
return &errorMatcher{x: x}
}

Expand All @@ -146,14 +190,14 @@ func (m *errorMatcher) Matches(x any) bool {
case string:
switch b := x.(type) {
case string:
return gomock.Eq(a).Matches(b)
return a == b
case error:
return gomock.Eq(a).Matches(b.Error())
return a == b.Error()
}
case error:
switch b := x.(type) {
case string:
return gomock.Eq(a.Error()).Matches(b)
return a.Error() == b
case error:
return gomock.Eq(a).Matches(b)
}
Expand All @@ -165,3 +209,32 @@ func (m *errorMatcher) Matches(x any) bool {
func (m *errorMatcher) String() string {
return fmt.Sprintf("is equal to %v (%T)", m.x, m.x)
}

// callMatcher is a matcher that supports matching of calls. Calls contain
// actions consisting of functions that cannot be matched successfully using
// [reflect.DeepEquals].
type callMatcher struct {
x any
}

// EqCall creates a new call matcher that allows to match calls by translating
// them to the string containing the core information instead of using the
// standard matcher using [reflect.DeepEquals] that fails for the contained
// actions.
func EqCall(x any) gomock.Matcher {
return &callMatcher{x: x}
}

// Matches executes the extended call matching algorithms.
func (m *callMatcher) Matches(x any) bool {
a, aok := m.x.(*gomock.Call)
if b, bok := x.(*gomock.Call); aok && bok {
return a.String() == b.String()
}
return gomock.Eq(m.x).Matches(x)
}

// String creates a string of the expectation to match.
func (m *callMatcher) String() string {
return fmt.Sprintf("is equal to %v (%T)", m.x, m.x)
}
Loading