Skip to content

Commit

Permalink
Merge pull request #38 from ovechkin-dm/33-better-failure-report
Browse files Browse the repository at this point in the history
verbose error reporting
  • Loading branch information
ovechkin-dm authored Jan 16, 2024
2 parents b50b2ff + f40cda7 commit e8cca63
Show file tree
Hide file tree
Showing 8 changed files with 428 additions and 119 deletions.
62 changes: 25 additions & 37 deletions registry/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,33 @@ import (
"github.com/ovechkin-dm/mockio/matchers"
"reflect"
"sync"
"sync/atomic"
)

type invocationHandler struct {
ctx *mockContext
calls []*methodRecorder
lock sync.Mutex
instanceType reflect.Type
ctx *mockContext
methods []*methodRecorder
lock sync.Mutex
instanceType reflect.Type
}

func (h *invocationHandler) Handle(method *dyno.Method, values []reflect.Value) []reflect.Value {
h.lock.Lock()
defer h.lock.Unlock()

call := &MethodCall{
Method: method,
Values: values,
Method: method,
Values: values,
StackTrace: NewStackTrace(),
}
if h.ctx.getState().verifyState {
return h.DoVerifyMethod(call)
}
h.calls[method.Num].calls = append(h.calls[method.Num].calls, call)
h.methods[method.Num].calls = append(h.methods[method.Num].calls, call)
return h.DoAnswer(call)
}

func (h *invocationHandler) DoAnswer(c *MethodCall) []reflect.Value {
rec := h.calls[c.Method.Num]
rec := h.methods[c.Method.Num]
h.ctx.getState().whenHandler = h
h.ctx.getState().whenCall = c
var matched bool
Expand Down Expand Up @@ -108,10 +108,10 @@ func (h *invocationHandler) When() matchers.ReturnerAll {
}

if !h.validateMatchers(whenCall) {
return nil
return NewEmptyReturner()
}

rec := h.calls[whenCall.Method.Num]
rec := h.methods[whenCall.Method.Num]

argMatchers := h.ctx.getState().matchers

Expand Down Expand Up @@ -157,8 +157,8 @@ func (h *invocationHandler) DoVerifyMethod(call *MethodCall) []reflect.Value {
return createDefaultReturnValues(call.Method.Type)
}

numMethodCalls := 0
rec := h.calls[call.Method.Num]
rec := h.methods[call.Method.Num]
matchedInvocations := make([]*MethodCall, 0)
for _, c := range rec.calls {
if c.WhenCall {
continue
Expand All @@ -177,16 +177,16 @@ func (h *invocationHandler) DoVerifyMethod(call *MethodCall) []reflect.Value {

if matches {
c.Verified = true
numMethodCalls += 1
matchedInvocations = append(matchedInvocations, c)
}
}
verifyData := &matchers.MethodVerificationData{
NumMethodCalls: numMethodCalls,
NumMethodCalls: len(matchedInvocations),
}
err := h.ctx.getState().methodVerifier.Verify(verifyData)
h.ctx.getState().methodVerifier = nil
if err != nil {
h.ctx.reporter.ReportVerifyMethodError(call, err)
h.ctx.reporter.ReportVerifyMethodError(h.instanceType, call, matchedInvocations, argMatchers, h.methods[call.Method.Num], err)
}
return createDefaultReturnValues(call.Method.Type)
}
Expand All @@ -202,9 +202,9 @@ func newHandler[T any](holder *mockContext) *invocationHandler {
}
}
return &invocationHandler{
ctx: holder,
calls: recorders,
instanceType: tp,
ctx: holder,
methods: recorders,
instanceType: tp,
}
}

Expand Down Expand Up @@ -271,31 +271,19 @@ func (h *invocationHandler) validateVerifyMatchers(call *MethodCall) bool {
return true
}

func (h *invocationHandler) CheckUnusedStubs() {
for _, rec := range h.calls {
calls := make([]*MethodCall, 0)
for i := range rec.calls {
if !rec.calls[i].WhenCall {
calls = append(calls, rec.calls[i])
}
}
for _, m := range rec.methodMatches {
if atomic.LoadInt64(&m.invocations) == 0 {
h.ctx.reporter.ReportWantedButNotInvoked(h.instanceType, rec.methodType, m, calls)
}
}
}
}

func (h *invocationHandler) VerifyNoMoreInteractions() {
for _, rec := range h.calls {
unexpected := make([]*MethodCall, 0)
for _, rec := range h.methods {
for _, call := range rec.calls {
if call.WhenCall {
continue
}
if !call.Verified {
h.ctx.reporter.ReportNoMoreInteractionsExpected(h.instanceType, call)
unexpected = append(unexpected, call)
}
}
}
if len(unexpected) > 0 {
h.ctx.reporter.ReportNoMoreInteractionsExpected(h.instanceType, unexpected)
}
}
2 changes: 2 additions & 0 deletions registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func AddMatcher[T any](m matchers.Matcher[T]) {
w := &matcherWrapper{
matcher: untypedMatcher(m),
rec: nil,
stackTrace: NewStackTrace(),
}
getInstance().mockContext.getState().matchers = append(getInstance().mockContext.getState().matchers, w)
return nil
Expand All @@ -78,6 +79,7 @@ func AddCaptor[T any](c *captorImpl[T]) {
return true
}),
rec: c,
stackTrace: NewStackTrace(),
}
getInstance().mockContext.getState().matchers = append(getInstance().mockContext.getState().matchers, w)
return nil
Expand Down
Loading

0 comments on commit e8cca63

Please sign in to comment.