Skip to content

Commit

Permalink
feat: verify number of times method called
Browse files Browse the repository at this point in the history
Added the ability to verify the number of times a method has been called. To do this I swapped the order of `Times()` vs the action methods so that you don't have to specify a result when trying to verify a method has been called.
  • Loading branch information
adamconnelly committed Dec 1, 2023
1 parent e7df4ce commit 85e714f
Show file tree
Hide file tree
Showing 8 changed files with 507 additions and 217 deletions.
86 changes: 60 additions & 26 deletions cmd/kelpie/mock.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -118,71 +118,105 @@ func {{ $method.Name }}{{ if $method.Parameters }}[{{ template "matcherTypeParam
return &result
}

type {{ $method.Name }}Times struct {
matcher *{{ $method.Name }}MethodMatcher
}

// Times allows you to restrict the number of times a particular expectation can be matched.
func (m *{{ $method.Name }}MethodMatcher) Times(times uint) *{{ $method.Name }}Times {
m.matcher.Times = &times

return &{{ $method.Name }}Times{
matcher: m,
}
}

// Once specifies that the expectation will only match once.
func (m *{{ $method.Name }}MethodMatcher) Once() *{{ $method.Name }}Times {
return m.Times(1)
}

// Never specifies that the method has not been called. This is mainly useful for verification
// rather than mocking.
func (m *{{ $method.Name }}MethodMatcher) Never() *{{ $method.Name }}Times {
return m.Times(0)
}

{{- if $method.Results }}

// Return returns the specified results when the method is called.
func (a *{{ $method.Name }}MethodMatcher) Return({{ template "resultWithTypeList" $method.Results }}) *{{ $method.Name }}Action {
func (t *{{ $method.Name }}Times) Return({{ template "resultWithTypeList" $method.Results }}) *{{ $method.Name }}Action {
return &{{ $method.Name }}Action{
expectation: mocking.Expectation{
MethodMatcher: &a.matcher,
MethodMatcher: &t.matcher.matcher,
Returns: []any{ {{- template "resultList" $method.Results -}} },
},
}
}
{{- end }}

// Panic panics using the specified argument when the method is called.
func (a *{{ $method.Name }}MethodMatcher) Panic(arg any) *{{ $method.Name }}Action {
func (t *{{ $method.Name }}Times) Panic(arg any) *{{ $method.Name }}Action {
return &{{ $method.Name }}Action{
expectation: mocking.Expectation{
MethodMatcher: &a.matcher,
MethodMatcher: &t.matcher.matcher,
PanicArg: arg,
},
}
}

// When calls the specified observe callback when the method is called.
func (a *{{ $method.Name }}MethodMatcher) When(observe {{ template "observationCallback" $method }}) *{{ $method.Name }}Action {
func (t *{{ $method.Name }}Times) When(observe {{ template "observationCallback" $method }}) *{{ $method.Name }}Action {
return &{{ $method.Name }}Action{
expectation: mocking.Expectation{
MethodMatcher: &a.matcher,
MethodMatcher: &t.matcher.matcher,
ObserveFn: observe,
},
}
}

type {{ $method.Name }}Action struct {
expectation mocking.Expectation
}

func (a *{{ $method.Name }}Action) CreateExpectation() *mocking.Expectation {
return &a.expectation
func (t *{{ $method.Name }}Times) CreateMethodMatcher() *mocking.MethodMatcher {
return &t.matcher.matcher
}

// Times allows you to restrict the number of times a particular expectation can be matched.
func (a *{{ $method.Name }}Action) Times(times int) *{{ $method.Name }}Times {
a.expectation.MethodMatcher.Times = &times
{{- if $method.Results }}

return &{{ $method.Name }}Times{
expectation: a.expectation,
// Return returns the specified results when the method is called.
func (m *{{ $method.Name }}MethodMatcher) Return({{ template "resultWithTypeList" $method.Results }}) *{{ $method.Name }}Action {
return &{{ $method.Name }}Action{
expectation: mocking.Expectation{
MethodMatcher: &m.matcher,
Returns: []any{ {{- template "resultList" $method.Results -}} },
},
}
}
{{- end }}

// Once specifies that the expectation will only match once.
func (a *{{ $method.Name }}Action) Once() *{{ $method.Name }}Times {
times := 1
a.expectation.MethodMatcher.Times = &times
// Panic panics using the specified argument when the method is called.
func (m *{{ $method.Name }}MethodMatcher) Panic(arg any) *{{ $method.Name }}Action {
return &{{ $method.Name }}Action{
expectation: mocking.Expectation{
MethodMatcher: &m.matcher,
PanicArg: arg,
},
}
}

return &{{ $method.Name }}Times{
expectation: a.expectation,
// When calls the specified observe callback when the method is called.
func (m *{{ $method.Name }}MethodMatcher) When(observe {{ template "observationCallback" $method }}) *{{ $method.Name }}Action {
return &{{ $method.Name }}Action{
expectation: mocking.Expectation{
MethodMatcher: &m.matcher,
ObserveFn: observe,
},
}
}

type {{ $method.Name }}Times struct {
type {{ $method.Name }}Action struct {
expectation mocking.Expectation
}

func (t *{{ $method.Name }}Times) CreateExpectation() *mocking.Expectation {
return &t.expectation
func (a *{{ $method.Name }}Action) CreateExpectation() *mocking.Expectation {
return &a.expectation
}
{{- end }}
Loading

0 comments on commit 85e714f

Please sign in to comment.