Skip to content

Commit

Permalink
Fix issue with custom types shadowing basic types (#163)
Browse files Browse the repository at this point in the history
When a custom type has a name that can shadow an in-built type, append
MoqParam to the name. Example: var name stringMoqParam for null.String.
  • Loading branch information
dedalusj committed Dec 18, 2021
1 parent ef8b268 commit 514e306
Show file tree
Hide file tree
Showing 5 changed files with 1,038 additions and 1 deletion.
7 changes: 6 additions & 1 deletion internal/registry/var.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ func varName(vr *types.Var, suffix string) string {
switch name {
case "mock", "callInfo", "break", "default", "func", "interface", "select", "case", "defer", "go", "map", "struct",
"chan", "else", "goto", "package", "switch", "const", "fallthrough", "if", "range", "type", "continue", "for",
"import", "return", "var":
"import", "return", "var",
// avoid shadowing basic types
"string", "bool", "byte", "rune", "uintptr",
"int", "int8", "int16", "int32", "int64",
"uint", "uint8", "uint16", "uint32", "uint64",
"float32", "float64", "complex64", "complex128":
name += "MoqParam"
}

Expand Down
6 changes: 6 additions & 0 deletions pkg/moq/moq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,12 @@ func TestMockGolden(t *testing.T) {
interfaces: []string{"Example"},
goldenFile: filepath.Join("testpackages/anonimport", "iface_moq.golden.go"),
},
{
name: "ShadowTypes",
cfg: Config{SrcDir: "testpackages/shadowtypes"},
interfaces: []string{"ShadowTypes"},
goldenFile: filepath.Join("testpackages/shadowtypes", "shadowtypes_moq.golden.go"),
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
Expand Down
35 changes: 35 additions & 0 deletions pkg/moq/testpackages/shadowtypes/shadowtypes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package shadowtypes

import (
"github.com/matryer/moq/pkg/moq/testpackages/shadowtypes/types"
)

type ShadowTypes interface {
ShadowString(string, types.String)

ShadowInt(int, types.Int)
ShadowInt8(int8, types.Int8)
ShadowInt16(int16, types.Int16)
ShadowInt32(int32, types.Int32)
ShadowInt64(int64, types.Int64)

ShadowUint(uint, types.Uint)
ShadowUint8(uint8, types.Uint8)
ShadowUint16(uint16, types.Uint16)
ShadowUint32(uint32, types.Uint32)
ShadowUint64(uint64, types.Uint64)

ShadowFloat32(float32, types.Float32)
ShadowFloat64(float64, types.Float64)

ShadowByte(byte, types.Byte)

ShadowRune(rune, types.Rune)

ShadowBool(bool, types.Bool)

ShadowComplex64(complex64, types.Complex64)
ShadowComplex128(complex128, types.Complex128)

ShadowUintptr(uintptr, types.Uintptr)
}
Loading

0 comments on commit 514e306

Please sign in to comment.