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

Test pr branch #101

Closed
wants to merge 5 commits into from
Closed
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
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
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()
}
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ require (
go 1.22

toolchain go1.22.0

replace (
github.com/hexdigest/gowrap => ../gowrap
)
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
Loading