Skip to content

Commit

Permalink
Moving to a parse.go
Browse files Browse the repository at this point in the history
  • Loading branch information
kliukovkin committed Sep 10, 2024
1 parent 5a7af37 commit 6cc01b1
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 127 deletions.
26 changes: 0 additions & 26 deletions mockgen/mockgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ func main() {
return
}


outputPackageName := *packageOut
if outputPackageName == "" {
// pkg.Name in reflect mode is the base name of the import path,
Expand Down Expand Up @@ -896,28 +895,3 @@ func parsePackageImport(srcDir string) (string, error) {
}
return "", errOutsideGoPath
}

func filterInterfaces(all []*model.Interface, requested []string) ([]*model.Interface, error) {
if len(requested) == 0 {
return nil, fmt.Errorf("no interfaces requested, other provide them or remove flag -interfaces")
}
requestedIfaces := make(map[string]struct{})
for _, iface := range requested {
requestedIfaces[iface] = struct{}{}
}
result := make([]*model.Interface, 0, len(all))
for _, iface := range all {
if _, ok := requestedIfaces[iface.Name]; ok {
result = append(result, iface)
delete(requestedIfaces, iface.Name)
}
}
if len(requestedIfaces) > 0 {
var missing []string
for iface := range requestedIfaces {
missing = append(missing, iface)
}
return nil, fmt.Errorf("missing interfaces: %s", strings.Join(missing, ", "))
}
return result, nil
}
101 changes: 1 addition & 100 deletions mockgen/mockgen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,103 +468,4 @@ func TestParseExcludeInterfaces(t *testing.T) {
}
}

func Test_filterInterfaces(t *testing.T) {
type args struct {
all []*model.Interface
requested []string
}
tests := []struct {
name string
args args
want []*model.Interface
wantErr bool
}{
{
name: "no filter",
args: args{
all: []*model.Interface{
{
Name: "Foo",
},
{
Name: "Bar",
},
},
requested: []string{},
},
want: nil,
wantErr: true,
},
{
name: "filter by Foo",
args: args{
all: []*model.Interface{
{
Name: "Foo",
},
{
Name: "Bar",
},
},
requested: []string{"Foo"},
},
want: []*model.Interface{
{
Name: "Foo",
},
},
wantErr: false,
},
{
name: "filter by Foo and Bar",
args: args{
all: []*model.Interface{
{
Name: "Foo",
},
{
Name: "Bar",
},
},
requested: []string{"Foo", "Bar"},
},
want: []*model.Interface{
{
Name: "Foo",
},
{
Name: "Bar",
},
},
wantErr: false,
},
{
name: "incorrect filter by Foo and Baz",
args: args{
all: []*model.Interface{
{
Name: "Foo",
},
{
Name: "Bar",
},
},
requested: []string{"Foo", "Baz"},
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := filterInterfaces(tt.args.all, tt.args.requested)
if (err != nil) != tt.wantErr {
t.Errorf("filterInterfaces() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("filterInterfaces() got = %v, want %v", got, tt.want)
}
})
}
}

27 changes: 26 additions & 1 deletion mockgen/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func sourceMode(source string) (*model.Package, error) {
}

}

return pkg, nil
}

Expand Down Expand Up @@ -811,4 +811,29 @@ func packageNameOfDir(srcDir string) (string, error) {
return packageImport, nil
}

func filterInterfaces(all []*model.Interface, requested []string) ([]*model.Interface, error) {
if len(requested) == 0 {
return nil, fmt.Errorf("no interfaces requested, other provide them or remove flag -interfaces")
}
requestedIfaces := make(map[string]struct{})
for _, iface := range requested {
requestedIfaces[iface] = struct{}{}
}
result := make([]*model.Interface, 0, len(all))
for _, iface := range all {
if _, ok := requestedIfaces[iface.Name]; ok {
result = append(result, iface)
delete(requestedIfaces, iface.Name)
}
}
if len(requestedIfaces) > 0 {
var missing []string
for iface := range requestedIfaces {
missing = append(missing, iface)
}
return nil, fmt.Errorf("missing interfaces: %s", strings.Join(missing, ", "))
}
return result, nil
}

var errOutsideGoPath = errors.New("source directory is outside GOPATH")
104 changes: 104 additions & 0 deletions mockgen/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"go/parser"
"go/token"
"testing"
"reflect"

"go.uber.org/mock/mockgen/model"
)

func TestFileParser_ParseFile(t *testing.T) {
Expand Down Expand Up @@ -143,3 +146,104 @@ func TestParseArrayWithConstLength(t *testing.T) {
}
}
}

func Test_filterInterfaces(t *testing.T) {
type args struct {
all []*model.Interface
requested []string
}
tests := []struct {
name string
args args
want []*model.Interface
wantErr bool
}{
{
name: "no filter",
args: args{
all: []*model.Interface{
{
Name: "Foo",
},
{
Name: "Bar",
},
},
requested: []string{},
},
want: nil,
wantErr: true,
},
{
name: "filter by Foo",
args: args{
all: []*model.Interface{
{
Name: "Foo",
},
{
Name: "Bar",
},
},
requested: []string{"Foo"},
},
want: []*model.Interface{
{
Name: "Foo",
},
},
wantErr: false,
},
{
name: "filter by Foo and Bar",
args: args{
all: []*model.Interface{
{
Name: "Foo",
},
{
Name: "Bar",
},
},
requested: []string{"Foo", "Bar"},
},
want: []*model.Interface{
{
Name: "Foo",
},
{
Name: "Bar",
},
},
wantErr: false,
},
{
name: "incorrect filter by Foo and Baz",
args: args{
all: []*model.Interface{
{
Name: "Foo",
},
{
Name: "Bar",
},
},
requested: []string{"Foo", "Baz"},
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := filterInterfaces(tt.args.all, tt.args.requested)
if (err != nil) != tt.wantErr {
t.Errorf("filterInterfaces() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("filterInterfaces() got = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 6cc01b1

Please sign in to comment.