Skip to content

Commit

Permalink
fix default mock interface name to %sMock
Browse files Browse the repository at this point in the history
  • Loading branch information
sonalys committed Jun 10, 2024
1 parent efb11d5 commit 8d167a4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
4 changes: 2 additions & 2 deletions generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
)

func Test_Generate(t *testing.T) {
output := t.TempDir()
// output := "out"
// output := t.TempDir()
output := "out"
os.RemoveAll(output) // no caching
Run([]string{"testdata"}, output, nil)
g, err := NewGenerator("mocks", "testdata")
Expand Down
16 changes: 10 additions & 6 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,13 @@ func (i *ParsedInterface) writeGenericsNameHeader() string {
return fmt.Sprintf("[%s]", strings.Join(i.GenericsNames, ", "))
}

func (i *ParsedInterface) getMockName() string {
return fmt.Sprintf("%sMock", i.Name)
}

func (i *ParsedInterface) writeStruct(w io.Writer) {
// Write struct definition implementing the interface
fmt.Fprintf(w, "type %s%s struct {\n", i.Name, i.writeGenericsHeader())
fmt.Fprintf(w, "type %s%s struct {\n", i.getMockName(), i.writeGenericsHeader())
for _, field := range i.ListFields() {
fmt.Fprintf(w, "\tsetup%s mockSetup.Mock[", field.Name)
i.PrintMethodHeader(w, "func", field)
Expand All @@ -213,8 +217,8 @@ func (i *ParsedInterface) writeStruct(w io.Writer) {

func (i *ParsedInterface) writeInitializer(w io.Writer) {
genericsNameHeader := i.writeGenericsNameHeader()
fmt.Fprintf(w, "func New%s%s(t *testing.T) *%s%s {\n", i.Name, i.writeGenericsHeader(), i.Name, genericsNameHeader)
fmt.Fprintf(w, "\treturn &%s%s{\n", i.Name, genericsNameHeader)
fmt.Fprintf(w, "func New%s%s(t *testing.T) *%s%s {\n", i.getMockName(), i.writeGenericsHeader(), i.getMockName(), genericsNameHeader)
fmt.Fprintf(w, "\treturn &%s%s{\n", i.getMockName(), genericsNameHeader)
for _, field := range i.ListFields() {
fmt.Fprintf(w, "\t\tsetup%s: mockSetup.NewMock[", field.Name)
i.PrintMethodHeader(w, "func", field)
Expand All @@ -226,7 +230,7 @@ func (i *ParsedInterface) writeInitializer(w io.Writer) {

func (i *ParsedInterface) writeAssertExpectations(w io.Writer) {
genericsTypeHeader := i.writeGenericsNameHeader()
fmt.Fprintf(w, "func (s *%s%s) AssertExpectations(t *testing.T) bool {\n", i.Name, genericsTypeHeader)
fmt.Fprintf(w, "func (s *%s%s) AssertExpectations(t *testing.T) bool {\n", i.getMockName(), genericsTypeHeader)
fmt.Fprintf(w, "\treturn ")
for _, field := range i.ListFields() {
fmt.Fprintf(w, "s.setup%s.AssertExpectations(t) &&\n\t\t", field.Name)
Expand All @@ -236,15 +240,15 @@ func (i *ParsedInterface) writeAssertExpectations(w io.Writer) {
}

func (i *ParsedInterface) writeOnMethod(w io.Writer, methodName string, f *ParsedField) {
fmt.Fprintf(w, "func (s *%s%s) On%s(funcs ...", i.Name, i.writeGenericsNameHeader(), methodName)
fmt.Fprintf(w, "func (s *%s%s) On%s(funcs ...", i.getMockName(), i.writeGenericsNameHeader(), methodName)
i.PrintMethodHeader(w, "func", f)
fmt.Fprintf(w, ") mockSetup.Config {\n")
fmt.Fprintf(w, "\treturn s.setup%s.Append(funcs...)\n", methodName)
fmt.Fprintf(w, "}\n\n")
}

func (i *ParsedInterface) writeMethod(w io.Writer, methodName string, f *ParsedField) {
fmt.Fprintf(w, "func (s *%s%s) ", i.Name, i.writeGenericsNameHeader())
fmt.Fprintf(w, "func (s *%s%s) ", i.getMockName(), i.writeGenericsNameHeader())
i.PrintMethodHeader(w, methodName, f)
fmt.Fprintf(w, "{\n")
var callingNames []string
Expand Down

0 comments on commit 8d167a4

Please sign in to comment.