Skip to content

Commit

Permalink
fix parse replace type
Browse files Browse the repository at this point in the history
  • Loading branch information
RangelReale committed Feb 8, 2023
1 parent 604a581 commit 3862af7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
11 changes: 8 additions & 3 deletions pkg/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -930,8 +930,13 @@ func parseReplaceType(t string) replaceType {
ret.alias = r[0]
t = r[1]
}
lastInd := strings.LastIndex(t, ".")
ret.pkg = t[:lastInd]
ret.typ = t[lastInd+1:]
lastDot := strings.LastIndex(t, ".")
lastSlash := strings.LastIndex(t, "/")
if lastDot == -1 || (lastSlash > -1 && lastDot < lastSlash) {
ret.pkg = t
} else {
ret.pkg = t[:lastDot]
ret.typ = t[lastDot+1:]
}
return ret
}
25 changes: 25 additions & 0 deletions pkg/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2775,3 +2775,28 @@ func TestGeneratorSuite(t *testing.T) {
generatorSuite := new(GeneratorSuite)
suite.Run(t, generatorSuite)
}

func TestParseReplaceType(t *testing.T) {
tests := []struct {
value string
expected replaceType
}{
{
value: "github.com/vektra/mockery/v2/pkg/fixtures/example_project/baz/internal/foo.InternalBaz",
expected: replaceType{alias: "", pkg: "github.com/vektra/mockery/v2/pkg/fixtures/example_project/baz/internal/foo", typ: "InternalBaz"},
},
{
value: "baz:github.com/vektra/mockery/v2/pkg/fixtures/example_project/baz.Baz",
expected: replaceType{alias: "baz", pkg: "github.com/vektra/mockery/v2/pkg/fixtures/example_project/baz", typ: "Baz"},
},
{
value: "github.com/vektra/mockery/v2/pkg/fixtures/example_project/baz",
expected: replaceType{alias: "", pkg: "github.com/vektra/mockery/v2/pkg/fixtures/example_project/baz", typ: ""},
},
}

for _, test := range tests {
actual := parseReplaceType(test.value)
assert.Equal(t, test.expected, actual)
}
}

0 comments on commit 3862af7

Please sign in to comment.