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

Commit

Permalink
mockgen: handle more cases of "duplicate" imports (#405)
Browse files Browse the repository at this point in the history
Improved handling of embedded interfaces in source-mode parser.
  • Loading branch information
stevendanna authored Apr 6, 2020
1 parent d476d65 commit 8a3d595
Show file tree
Hide file tree
Showing 14 changed files with 479 additions and 52 deletions.
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190311183353-d8887717615a h1:oWX7TPOiFAMXLq8o0ikBYfCJVlRHBcsciT5bXOrH628=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down
35 changes: 35 additions & 0 deletions mockgen/internal/tests/import_embedded_interface/bugreport.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2020 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//go:generate mockgen -destination bugreport_mock.go -package bugreport -source=bugreport.go

package bugreport

import (
"log"

"github.com/golang/mock/mockgen/internal/tests/import_embedded_interface/ersatz"
"github.com/golang/mock/mockgen/internal/tests/import_embedded_interface/faux"
)

// Source is an interface w/ an embedded foreign interface
type Source interface {
ersatz.Embedded
faux.Foreign
}

func CallForeignMethod(s Source) {
log.Println(s.Ersatz())
log.Println(s.OtherErsatz())
}
63 changes: 63 additions & 0 deletions mockgen/internal/tests/import_embedded_interface/bugreport_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions mockgen/internal/tests/import_embedded_interface/bugreport_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2020 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bugreport

import (
"testing"

"github.com/golang/mock/gomock"
)

// TestValidInterface assesses whether or not the generated mock is valid
func TestValidInterface(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

s := NewMockSource(ctrl)
s.EXPECT().Ersatz().Return("")
s.EXPECT().OtherErsatz().Return("")
CallForeignMethod(s)
}
20 changes: 20 additions & 0 deletions mockgen/internal/tests/import_embedded_interface/ersatz/ersatz.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2020 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ersatz

type Embedded interface {
Ersatz() Return
}

type Return interface{}
20 changes: 20 additions & 0 deletions mockgen/internal/tests/import_embedded_interface/faux/conflict.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2020 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package faux

import "github.com/golang/mock/mockgen/internal/tests/import_embedded_interface/other/log"

func Conflict1() {
log.Foo()
}
28 changes: 28 additions & 0 deletions mockgen/internal/tests/import_embedded_interface/faux/faux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2020 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package faux

import (
"log"

"github.com/golang/mock/mockgen/internal/tests/import_embedded_interface/other/ersatz"
)

type Foreign interface {
ersatz.Embedded
}

func Conflict0() {
log.Println()
}
25 changes: 25 additions & 0 deletions mockgen/internal/tests/import_embedded_interface/net.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//go:generate mockgen -destination net_mock.go -package bugreport -source=net.go
package bugreport

import "net/http"

type Net interface {
http.ResponseWriter
}

func CallResponseWriterMethods(n Net) {
n.WriteHeader(10)
}
75 changes: 75 additions & 0 deletions mockgen/internal/tests/import_embedded_interface/net_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions mockgen/internal/tests/import_embedded_interface/net_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2020 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bugreport

import (
"testing"

"github.com/golang/mock/gomock"
)

// TestValidInterface assesses whether or not the generated mock is valid
func TestValidNetInterface(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

s := NewMockNet(ctrl)
s.EXPECT().WriteHeader(10)
CallResponseWriterMethods(s)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2020 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ersatz

type Embedded interface {
OtherErsatz() Return
}

type Return interface{}
16 changes: 16 additions & 0 deletions mockgen/internal/tests/import_embedded_interface/other/log/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2020 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package log

func Foo() {}
Loading

0 comments on commit 8a3d595

Please sign in to comment.