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

fix: panic when method has no results #4

Merged
merged 1 commit into from
Nov 28, 2023
Merged
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
12 changes: 10 additions & 2 deletions cmd/kelpie/mock.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
{{- end -}}

{{- define "observationCallback" -}}
func({{ template "parameterWithTypeList" .Parameters }}) {{ template "resultTypeList" .Results }}
func({{ template "parameterWithTypeList" .Parameters }}){{ if .Results }} {{ template "resultTypeList" .Results }}{{ end }}
{{- end -}}

{{- define "matcherTypeParams" -}}
Expand Down Expand Up @@ -58,12 +58,17 @@ type Instance struct {

{{- range $method := .Methods }}

func (m *Instance) {{ $method.Name }}({{ template "parameterWithTypeList" $method.Parameters }}) ({{ template "resultWithTypeList" $method.Results }}) {
func (m *Instance) {{ $method.Name }}({{ template "parameterWithTypeList" $method.Parameters }}){{ if $method.Results }} ({{ template "resultWithTypeList" $method.Results }}){{ end }} {
expectation := m.mock.Call("{{ $method.Name }}", {{ template "parameterList" $method.Parameters }})
if expectation != nil {
if expectation.ObserveFn != nil {
observe := expectation.ObserveFn.({{ template "observationCallback" $method }})
{{- if $method.Results }}
return observe({{ template "parameterList" $method.Parameters }})
{{- else }}
observe({{ template "parameterList" $method.Parameters }})
return
{{- end }}
}

if expectation.PanicArg != nil {
Expand Down Expand Up @@ -121,6 +126,8 @@ func (e *{{ $method.Name }}Expectation) CreateExpectation() *mocking.Expectation
return &e.expectation
}

{{- if $method.Results }}

func (a *{{ $method.Name }}MethodMatcher) Return({{ template "resultWithTypeList" $method.Results }}) *{{ $method.Name }}Expectation {
return &{{ $method.Name }}Expectation{
expectation: mocking.Expectation{
Expand All @@ -129,6 +136,7 @@ func (a *{{ $method.Name }}MethodMatcher) Return({{ template "resultWithTypeList
},
}
}
{{- end }}

func (a *{{ $method.Name }}MethodMatcher) Panic(arg any) *{{ $method.Name }}Expectation {
return &{{ $method.Name }}Expectation{
Expand Down
68 changes: 68 additions & 0 deletions examples/mocks/accountservice/accountservice.go

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

17 changes: 17 additions & 0 deletions examples/result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package examples
import (
"testing"

"github.com/adamconnelly/kelpie"
"github.com/adamconnelly/kelpie/examples/mocks/accountservice"
"github.com/stretchr/testify/suite"
)

//go:generate go run ../cmd/kelpie generate --source-file result_test.go --package github.com/adamconnelly/kelpie/examples --interfaces AccountService
type AccountService interface {
SendActivationEmail(emailAddress string) bool
DisableAccount(id uint)
}

type ResultTests struct {
Expand Down Expand Up @@ -53,6 +55,21 @@ func (t *ResultTests) Test_CustomAction() {
t.Equal("[email protected]", recipientAddress)
}

func (t *ResultTests) Test_CanMockMethodsWithNoReturnArgs() {
// Arrange
var accountID uint
mock := accountservice.NewMock()
mock.Setup(accountservice.DisableAccount(kelpie.Any[uint]()).When(func(id uint) {
accountID = id
}))

// Act
mock.Instance().DisableAccount(uint(123))

// Assert
t.Equal(uint(123), accountID)
}

func TestResults(t *testing.T) {
suite.Run(t, new(ResultTests))
}
18 changes: 10 additions & 8 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,20 @@ func Parse(reader io.Reader, packageName string, filter InterfaceFilter) ([]Mock
}
}

for _, result := range funcType.Results.List {
if len(result.Names) > 0 {
for _, resultName := range result.Names {
if funcType.Results != nil {
for _, result := range funcType.Results.List {
if len(result.Names) > 0 {
for _, resultName := range result.Names {
methodDefinition.Results = append(methodDefinition.Results, ResultDefinition{
Name: resultName.Name,
Type: result.Type.(*ast.Ident).Name,
})
}
} else {
methodDefinition.Results = append(methodDefinition.Results, ResultDefinition{
Name: resultName.Name,
Type: result.Type.(*ast.Ident).Name,
})
}
} else {
methodDefinition.Results = append(methodDefinition.Results, ResultDefinition{
Type: result.Type.(*ast.Ident).Name,
})
}
}

Expand Down
34 changes: 34 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,40 @@ type NotificationService interface {
t.Equal("error", broadcastNotification.Results[1].Type)
}

func (t *ParserTests) Test_Parse_SupportsMethodsWithNoResults() {
// Arrange
input := `package test

type NotificationService interface {
Block(recipient string)
}`

t.interfaceFilter.Setup(interfacefilter.Include("github.com/adamconnelly/kelpie/tests.UserService").Return(false))

// Act
result, err := parser.Parse(strings.NewReader(input), "github.com/adamconnelly/kelpie/tests", t.interfaceFilter.Instance())

// Assert
t.NoError(err)
t.Len(result, 1)

notificationService := slices.FirstOrPanic(result, func(mock parser.MockedInterface) bool {
return mock.Name == "NotificationService"
})
t.Equal("notificationservice", notificationService.PackageName)
t.Len(notificationService.Methods, 1)

block := slices.FirstOrPanic(notificationService.Methods, func(method parser.MethodDefinition) bool {
return method.Name == "Block"
})

t.Len(block.Parameters, 1)
t.Equal("recipient", block.Parameters[0].Name)
t.Equal("string", block.Parameters[0].Type)

t.Len(block.Results, 0)
}

// TODO: what about empty interfaces? Return a warning?

func TestParser(t *testing.T) {
Expand Down
Loading