Skip to content

Commit

Permalink
up godyno version (#71)
Browse files Browse the repository at this point in the history
Co-authored-by: Dmitrii Ovechkin <[email protected]>
  • Loading branch information
ovechkin-dm and Dmitrii Ovechkin authored Jun 12, 2024
1 parent d1d7bbc commit becfc50
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 27 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ module github.com/ovechkin-dm/mockio

go 1.21

require github.com/ovechkin-dm/go-dyno v0.1.3
require github.com/ovechkin-dm/go-dyno v0.2.0

require github.com/petermattis/goid v0.0.0-20230904192822-1876fd5063bc
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
github.com/ovechkin-dm/go-dyno v0.1.3 h1:rC9S5WikOSLPJ24kve0Tz0f1AZkfxo3TtjHa3aOfEy0=
github.com/ovechkin-dm/go-dyno v0.1.3/go.mod h1:CcJNuo7AbePMoRNpM3i1jC1Rp9kHEMyWozNdWzR+0ys=
github.com/ovechkin-dm/go-dyno v0.2.0 h1:KAh6OFiXYWZZpItdBJDILiklmmov0Txk+O8GFqH785g=
github.com/ovechkin-dm/go-dyno v0.2.0/go.mod h1:CcJNuo7AbePMoRNpM3i1jC1Rp9kHEMyWozNdWzR+0ys=
github.com/petermattis/goid v0.0.0-20230904192822-1876fd5063bc h1:8bQZVK1X6BJR/6nYUPxQEP+ReTsceJTKizeuwjWOPUA=
github.com/petermattis/goid v0.0.0-20230904192822-1876fd5063bc/go.mod h1:pxMtw7cyUw6B2bRH0ZBANSPg+AoSud1I1iyJHI69jH4=
26 changes: 12 additions & 14 deletions registry/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"reflect"
"sync"

"github.com/ovechkin-dm/go-dyno/pkg/dyno"

"github.com/ovechkin-dm/mockio/matchers"
)

Expand All @@ -17,7 +15,7 @@ type invocationHandler struct {
instanceType reflect.Type
}

func (h *invocationHandler) Handle(method *dyno.Method, values []reflect.Value) []reflect.Value {
func (h *invocationHandler) Handle(method reflect.Method, values []reflect.Value) []reflect.Value {
h.lock.Lock()
defer h.lock.Unlock()
values = h.refineValues(method, values)
Expand Down Expand Up @@ -60,24 +58,24 @@ func (h *invocationHandler) DoAnswer(c *MethodCall) []reflect.Value {

ansWrapper := mm.popAnswer()
if ansWrapper == nil {
return createDefaultReturnValues(c.Method.Type)
return createDefaultReturnValues(c.Method)
}

retValues := ansWrapper.ans(ifaces)

h.ctx.getState().whenAnswer = ansWrapper
h.ctx.getState().whenMethodMatch = mm

if !h.validateReturnValues(retValues, c.Method.Type) {
h.ctx.reporter.ReportInvalidReturnValues(h.instanceType, c.Method.Type, retValues)
return createDefaultReturnValues(c.Method.Type)
if !h.validateReturnValues(retValues, c.Method) {
h.ctx.reporter.ReportInvalidReturnValues(h.instanceType, c.Method, retValues)
return createDefaultReturnValues(c.Method)
}

result := interfaceSliceToValueSlice(retValues, c.Method.Type)
result := interfaceSliceToValueSlice(retValues, c.Method)
return result
}
}
return createDefaultReturnValues(c.Method.Type)
return createDefaultReturnValues(c.Method)
}

func (h *invocationHandler) When() matchers.ReturnerAll {
Expand Down Expand Up @@ -148,7 +146,7 @@ func (h *invocationHandler) DoVerifyMethod(call *MethodCall) []reflect.Value {
h.ctx.getState().verifyState = false

if !matchersOk {
return createDefaultReturnValues(call.Method.Type)
return createDefaultReturnValues(call.Method)
}

rec := h.methods[call.Method.Name]
Expand Down Expand Up @@ -186,7 +184,7 @@ func (h *invocationHandler) DoVerifyMethod(call *MethodCall) []reflect.Value {
h.ctx.reporter.ReportVerifyMethodError(
true,
h.instanceType,
call.Method.Type,
call.Method,
matchedInvocations,
argMatchers,
h.methods[call.Method.Name],
Expand All @@ -201,7 +199,7 @@ func (h *invocationHandler) DoVerifyMethod(call *MethodCall) []reflect.Value {
}
}
}
return createDefaultReturnValues(call.Method.Type)
return createDefaultReturnValues(call.Method)
}

func newHandler[T any](holder *mockContext) *invocationHandler {
Expand Down Expand Up @@ -285,8 +283,8 @@ func (h *invocationHandler) VerifyNoMoreInteractions(tearDown bool) {
}
}

func (h *invocationHandler) refineValues(method *dyno.Method, values []reflect.Value) []reflect.Value {
tp := method.Type.Type
func (h *invocationHandler) refineValues(method reflect.Method, values []reflect.Value) []reflect.Value {
tp := method.Type
if tp.IsVariadic() {
result := make([]reflect.Value, 0)
for i := 0; i < tp.NumIn()-1; i++ {
Expand Down
2 changes: 1 addition & 1 deletion registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TearDown() {
func Mock[T any]() T {
return withCheck[T](func() T {
handler := newHandler[T](getInstance().mockContext)
t, err := dyno.Dynamic[T](handler)
t, err := dyno.Dynamic[T](handler.Handle)
if err != nil {
getInstance().mockContext.reporter.FailNow(fmt.Errorf("error creating mock: %w", err))
var zero T
Expand Down
12 changes: 6 additions & 6 deletions registry/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,22 +93,22 @@ func (e *EnrichedReporter) ReportInvalidUseOfMatchers(instanceType reflect.Type,
matcherArgs[i] = m[i].matcher.Description()
}
matchersString := strings.Join(matcherArgs, ",")
tp := call.Method.Type.Type
tp := call.Method.Type
inArgs := make([]string, 0)
methodSig := prettyPrintMethodSignature(instanceType, call.Method.Type)
methodSig := prettyPrintMethodSignature(instanceType, call.Method)
for i := 0; i < tp.NumIn(); i++ {
inArgs = append(inArgs, tp.In(i).String())
}
inArgsStr := strings.Join(inArgs, ",")
numExpected := call.Method.Type.Type.NumIn()
numExpected := call.Method.Type.NumIn()
numActual := len(m)
declarationLines := make([]string, 0)
for i := range m {
declarationLines = append(declarationLines, "\t\t"+m[i].stackTrace.CallerLine())
}
decl := strings.Join(declarationLines, "\n")
expectedStr := fmt.Sprintf("%v expected, %v recorded:\n", numExpected, numActual)
if call.Method.Type.Type.IsVariadic() {
if call.Method.Type.IsVariadic() {
expectedStr = ""
}
e.StackTraceFatalf(`Invalid use of matchers
Expand Down Expand Up @@ -160,7 +160,7 @@ func (e *EnrichedReporter) ReportVerifyMethodError(
for i := range c.Values {
callArgs[i] = fmt.Sprintf("%v", c.Values[i])
}
pretty := PrettyPrintMethodInvocation(tp, c.Method.Type, callArgs)
pretty := PrettyPrintMethodInvocation(tp, c.Method, callArgs)
other.WriteString(fmt.Sprintf("\t\t%s at %s", pretty, c.StackTrace.CallerLine()))
if j != len(recorder.calls)-1 {
other.WriteString("\n")
Expand Down Expand Up @@ -308,7 +308,7 @@ func (e *EnrichedReporter) ReportNoMoreInteractionsExpected(fatal bool, instance
for _, v := range c.Values {
args = append(args, fmt.Sprintf("%v", v))
}
s := PrettyPrintMethodInvocation(instanceType, c.Method.Type, args)
s := PrettyPrintMethodInvocation(instanceType, c.Method, args)
line := fmt.Sprintf("\t\t%s at %s", s, c.StackTrace.CallerLine())
sb.WriteString(line)
if i != len(calls)-1 {
Expand Down
4 changes: 1 addition & 3 deletions registry/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"sync"
"sync/atomic"

"github.com/ovechkin-dm/go-dyno/pkg/dyno"

"github.com/ovechkin-dm/mockio/config"
"github.com/ovechkin-dm/mockio/matchers"
"github.com/ovechkin-dm/mockio/threadlocal"
Expand Down Expand Up @@ -121,7 +119,7 @@ func newMockContext(reporter *EnrichedReporter, cfg *config.MockConfig) *mockCon
}

type MethodCall struct {
Method *dyno.Method
Method reflect.Method
Values []reflect.Value
WhenCall bool
Verified bool
Expand Down

0 comments on commit becfc50

Please sign in to comment.