Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

Commit

Permalink
refactor method names for reflect/source (#376)
Browse files Browse the repository at this point in the history
The reflect method should be renamed so it does not collide with
the reflect package name. As is, it makes import reflect in the
code non-idiomatic. Also renamed sourceMode to stay consistent.

Noticed this here: #371

Also, refactor slice equals method
  • Loading branch information
codyoss authored Jan 12, 2020
1 parent 1b95bd9 commit 0800f9a
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 26 deletions.
4 changes: 2 additions & 2 deletions mockgen/mockgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ func main() {
var pkg *model.Package
var err error
if *source != "" {
pkg, err = parseFile(*source)
pkg, err = sourceMode(*source)
} else {
if flag.NArg() != 2 {
usage()
log.Fatal("Expected exactly two arguments")
}
pkg, err = reflect(flag.Arg(0), strings.Split(flag.Arg(1), ","))
pkg, err = reflectMode(flag.Arg(0), strings.Split(flag.Arg(1), ","))
}
if err != nil {
log.Fatalf("Loading input failed: %v", err)
Expand Down
23 changes: 2 additions & 21 deletions mockgen/mockgen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"reflect"
"regexp"
"strings"
"testing"
Expand Down Expand Up @@ -327,29 +328,9 @@ func TestGetArgNames(t *testing.T) {
g := generator{}

result := g.getArgNames(testCase.method)
if !testEqSliceStr(t, result, testCase.expected) {
if !reflect.DeepEqual(result, testCase.expected) {
t.Fatalf("expected %s, got %s", result, testCase.expected)
}
})
}
}

func testEqSliceStr(t *testing.T, a, b []string) bool {
t.Helper()

if a == nil || b == nil {
return false
}

if len(a) != len(b) {
return false
}

for i := range a {
if a[i] != b[i] {
return false
}
}

return true
}
3 changes: 2 additions & 1 deletion mockgen/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ var (

// TODO: simplify error reporting

func parseFile(source string) (*model.Package, error) {
// sourceMode generates mocks via source file.
func sourceMode(source string) (*model.Package, error) {
srcDir, err := filepath.Abs(filepath.Dir(source))
if err != nil {
return nil, fmt.Errorf("failed getting source directory: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion mockgen/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,6 @@ func checkGreeterImports(t *testing.T, imports map[string]string) {
func Benchmark_parseFile(b *testing.B) {
source := "internal/tests/performance/big_interface/big_interface.go"
for n := 0; n < b.N; n++ {
parseFile(source)
sourceMode(source)
}
}
3 changes: 2 additions & 1 deletion mockgen/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ func runInDir(program []byte, dir string) (*model.Package, error) {
return run(filepath.Join(tmpDir, progBinary))
}

func reflect(importPath string, symbols []string) (*model.Package, error) {
// reflectMode generates mocks via reflection on an interface.
func reflectMode(importPath string, symbols []string) (*model.Package, error) {
// TODO: sanity check arguments

if *execOnly != "" {
Expand Down

0 comments on commit 0800f9a

Please sign in to comment.