Skip to content

Commit

Permalink
Merge pull request #132 from ldmonster/master
Browse files Browse the repository at this point in the history
[template] add when/then for functions without results
  • Loading branch information
zcolleen authored Feb 5, 2025
2 parents 566b2ef + 18535b6 commit 6bd5c79
Show file tree
Hide file tree
Showing 12 changed files with 414 additions and 2 deletions.
10 changes: 8 additions & 2 deletions internal/templates/body.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func New{{$mock}}{{(params)}}(t minimock.Tester) *{{$mock}}{{(paramsRef)}} {
return {{$m}}.mock
}

{{if (and $method.HasParams $method.HasResults)}}
{{if $method.HasParams }}
// When sets expectation for the {{$.Interface.Name}}.{{$method.Name}} which will trigger the result defined by the following
// Then helper
func ({{$m}} *m{{$mock}}{{$method.Name}}{{(paramsRef)}}) When({{$method.Params}}) *{{$mock}}{{$method.Name}}Expectation{{(paramsRef)}} {
Expand All @@ -202,11 +202,17 @@ func New{{$mock}}{{(params)}}(t minimock.Tester) *{{$mock}}{{(paramsRef)}} {
return expectation
}

// Then sets up {{$.Interface.Name}}.{{$method.Name}} return parameters for the expectation previously defined by the When method
// Then sets up {{$.Interface.Name}}.{{$method.Name}} return parameters for the expectation previously defined by the When method{{if $method.HasResults }}
func (e *{{$mock}}{{$method.Name}}Expectation{{(paramsRef)}}) Then({{$method.Results}}) *{{$mock}}{{(paramsRef)}} {
e.results = &{{$mock}}{{$method.Name}}Results{{(paramsRef)}}{ {{ $method.ResultsNames }} }
return e.mock
}
{{end}}
{{if not $method.HasResults }}
func (e *{{$mock}}{{$method.Name}}Expectation{{(paramsRef)}}) Then() *{{$mock}}{{(paramsRef)}} {
return e.mock
}
{{end}}
{{end}}

// Times sets number of times {{$.Interface.Name}}.{{$method.Name}} should be invoked
Expand Down
22 changes: 22 additions & 0 deletions tests/context_accepter_mock.go

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

10 changes: 10 additions & 0 deletions tests/context_accepter_mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ func TestContextAccepterMock_WhenThenMatchAnycontext(t *testing.T) {
assert.Equal(t, 42, result)
}

func TestContextAccepterMock_WhenThenMatchAnycontextWithoutArgs(t *testing.T) {
tester := NewTesterMock(t)
tester.CleanupMock.Return().HelperMock.Return()

mock := NewContextAccepterMock(tester).
AcceptContextMock.When(minimock.AnyContext).Then()

mock.AcceptContext(context.TODO())
}

func TestContextAccepterMock_DiffWithoutAnyContext(t *testing.T) {
tester := NewTesterMock(t)
tester.CleanupMock.Return().HelperMock.Return()
Expand Down
22 changes: 22 additions & 0 deletions tests/generic_complex_union.go

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

22 changes: 22 additions & 0 deletions tests/generic_in.go

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

22 changes: 22 additions & 0 deletions tests/generic_inline_union.go

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

22 changes: 22 additions & 0 deletions tests/generic_inline_with_many_options.go

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

22 changes: 22 additions & 0 deletions tests/generic_multiple_args_with_different_types.go

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

22 changes: 22 additions & 0 deletions tests/generic_simple_union.go

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

22 changes: 22 additions & 0 deletions tests/generic_specific.go

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

Loading

0 comments on commit 6bd5c79

Please sign in to comment.