Skip to content

Commit

Permalink
Merge pull request #102 from zcolleen/master
Browse files Browse the repository at this point in the history
Bugs/improvements on union interfaces, diff displaying and version setting
  • Loading branch information
hexdigest authored May 11, 2024
2 parents 6fbb32e + 21dcb10 commit f204e2d
Show file tree
Hide file tree
Showing 13 changed files with 634 additions and 45 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ generate:
go run ./cmd/minimock/minimock.go -i ./tests.genericSimpleUnion -o ./tests/generic_simple_union.go
go run ./cmd/minimock/minimock.go -i ./tests.genericComplexUnion -o ./tests/generic_complex_union.go
go run ./cmd/minimock/minimock.go -i ./tests.genericInlineUnion -o ./tests/generic_inline_union.go
go run ./cmd/minimock/minimock.go -i ./tests.genericInlineUnion -o ./tests/generic_inline_union.go
go run ./cmd/minimock/minimock.go -i ./tests.genericInlineUnionWithManyTypes -o ./tests/generic_inline_with_many_options.go
go run ./cmd/minimock/minimock.go -i ./tests.genericMultipleTypes -o ./tests/generic_multiple_args_with_different_types.go
go run ./cmd/minimock/minimock.go -i ./tests.contextAccepter -o ./tests/context_accepter_mock.go
go run ./cmd/minimock/minimock.go -i github.com/gojuno/minimock/v3.Tester -o ./tests/package_name_specified_test.go -p tests_test
go run ./cmd/minimock/minimock.go -i ./tests.actor -o ./tests/actor_mock.go
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,22 @@ readCloserMock := NewReadCloserMock(mc).ReadMock.Inspect(func(p []byte){

```

### Setting up a mock using ExpectParams helpers:

Let's say we have a mocking interface with function that has many arguments:
```go
type If interface {
Do(intArg int, stringArg string, floatArg float)
}
```

Imagine that you don't want to check all the arguments, just one or two of them.
Then we can use ExpectParams helpers, which are generated for each argument:
```go
mc := minimock.NewController(t)
ifMock := NewIfMock(mc).DoMock.ExpectIntArgParam1(10).ExpectFloatArgParam3(10.2).Return()
```

### Setting up a mock using When/Then helpers:
```go
mc := minimock.NewController(t)
Expand Down
50 changes: 48 additions & 2 deletions cmd/minimock/minimock.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"runtime/debug"
"strings"
"text/template"
"time"
Expand All @@ -22,11 +23,13 @@ import (
"golang.org/x/tools/go/packages"
)

const devVersion = "dev"

var (
//do not modify the following vars
//the values are being injected at the compile time by goreleaser
version = "dev"
commit = "dev"
version string
commit string
buildDate = time.Now().Format(time.RFC3339)
)

Expand Down Expand Up @@ -62,6 +65,49 @@ type (
}
)

func init() {
if buildInfo, ok := debug.ReadBuildInfo(); ok {
// if installing directly with go build/install
// take version and commit from buildInfo
version = getVersion(version, buildInfo)
commit = getCommit(commit, buildInfo)
}
// if goreleaser didn't set these vars,
// and we didn't find buildInfo then set
// them to 'dev'
if version == "" {
version = devVersion
}
if commit == "" {
commit = devVersion
}

}

func getCommit(commit string, buildInfo *debug.BuildInfo) string {
if commit != "" {
return commit
}
for _, setting := range buildInfo.Settings {
if setting.Key == "vcs.revision" {
return setting.Value
}
}

return ""
}

func getVersion(version string, buildInfo *debug.BuildInfo) string {
if version != "" {
return version
}
if buildInfo.Main.Version != "" {
return buildInfo.Main.Version
}

return ""
}

func main() {
opts, err := processArgs(os.Args[1:], os.Stdout, os.Stderr)
if err != nil {
Expand Down
44 changes: 44 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package minimock

import (
"context"
"reflect"
)

func copyValue(value interface{}) reflect.Value {
rValue := reflect.ValueOf(value)
newValue := reflect.New(rValue.Type()).Elem()
newValue.Set(rValue)

return newValue
}

func checkAnyContext(eFieldValue, aFieldValue interface{}) bool {
_, ok := eFieldValue.(anyContext)
if ok {
if ctx, ok := aFieldValue.(context.Context); ok && ctx != nil {
return true
}
}

return false
}

func setAnyContext(src, dst interface{}) interface{} {
srcp := copyValue(src)
dstp := copyValue(dst)

for i := 0; i < srcp.NumField(); i++ {
srcFieldValue := unexportedVal(srcp.Field(i))
dstFieldValue := unexportedVal(dstp.Field(i))

if checkAnyContext(srcFieldValue.Interface(), dstFieldValue.Interface()) {
// we set context field to anyContext because
// we don't want to display diff between two contexts in case
// of anyContext
dstFieldValue.Set(srcFieldValue)
}
}

return dstp.Interface()
}
27 changes: 13 additions & 14 deletions equal.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,16 @@ func Equal(a, b interface{}) bool {
}

if reflect.TypeOf(a).Kind() == reflect.Struct {
aV := reflect.ValueOf(a)
bV := reflect.ValueOf(b)

ap := reflect.New(aV.Type()).Elem()
bp := reflect.New(bV.Type()).Elem()

ap.Set(aV)
bp.Set(bV)
ap := copyValue(a)
bp := copyValue(b)

// for every field in a
for i := 0; i < reflect.TypeOf(a).NumField(); i++ {
aFieldValue := unexported(ap.Field(i))
bFieldValue := unexported(bp.Field(i))

_, ok := aFieldValue.(anyContext)
if ok {
if ctx, ok := bFieldValue.(context.Context); ok && ctx != nil {
continue
}
if checkAnyContext(aFieldValue, bFieldValue) {
continue
}

if !reflect.DeepEqual(aFieldValue, bFieldValue) {
Expand Down Expand Up @@ -82,6 +73,10 @@ func Diff(e, a interface{}) string {
return ""
}

if k == reflect.Struct {
a = setAnyContext(e, a)
}

es := dumpConf.Sdump(e)
as := dumpConf.Sdump(a)

Expand All @@ -101,5 +96,9 @@ func Diff(e, a interface{}) string {
}

func unexported(field reflect.Value) interface{} {
return reflect.NewAt(field.Type(), unsafe.Pointer(field.UnsafeAddr())).Elem().Interface()
return unexportedVal(field).Interface()
}

func unexportedVal(field reflect.Value) reflect.Value {
return reflect.NewAt(field.Type(), unsafe.Pointer(field.UnsafeAddr())).Elem()
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module github.com/gojuno/minimock/v3

require (
github.com/davecgh/go-spew v1.1.1
github.com/hexdigest/gowrap v1.3.6
github.com/hexdigest/gowrap v1.3.7
github.com/pkg/errors v0.9.1
github.com/pmezard/go-difflib v1.0.0
github.com/stretchr/testify v1.8.4
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaS
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/hexdigest/gowrap v1.3.6 h1:nW91fC0tjW9X6MjittAK1EmI/9lAXibjqL4ebA6y8F8=
github.com/hexdigest/gowrap v1.3.6/go.mod h1:5KTYxPjK1RRfD+9L4Oo9gjP3XNAs4rkoVK2E7eAEFyM=
github.com/hexdigest/gowrap v1.3.7 h1:vgfh7NQEZQyMe17PBaMX/d4+7hTLyxo2q4DFTfx3p1E=
github.com/hexdigest/gowrap v1.3.7/go.mod h1:5KTYxPjK1RRfD+9L4Oo9gjP3XNAs4rkoVK2E7eAEFyM=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
Expand Down
4 changes: 2 additions & 2 deletions tests/actor_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f204e2d

Please sign in to comment.