From ae93d9a24066fcc83cf163c7e2776f683f94ef3a Mon Sep 17 00:00:00 2001 From: Francisco Miamoto Date: Sat, 13 Feb 2021 23:58:13 -0300 Subject: [PATCH 1/3] parse arrays with const length correctly (#294) --- .../tests/const_array_length/input.go | 8 ++++ .../internal/tests/const_array_length/mock.go | 48 +++++++++++++++++++ mockgen/parse.go | 11 ++++- mockgen/parse_test.go | 24 ++++++++++ 4 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 mockgen/internal/tests/const_array_length/input.go create mode 100644 mockgen/internal/tests/const_array_length/mock.go diff --git a/mockgen/internal/tests/const_array_length/input.go b/mockgen/internal/tests/const_array_length/input.go new file mode 100644 index 00000000..c98482a4 --- /dev/null +++ b/mockgen/internal/tests/const_array_length/input.go @@ -0,0 +1,8 @@ +//go:generate mockgen -package const_length -destination mock.go -source input.go +package const_length + +const C = 2 + +type I interface { + F() [C]int +} diff --git a/mockgen/internal/tests/const_array_length/mock.go b/mockgen/internal/tests/const_array_length/mock.go new file mode 100644 index 00000000..9a407c23 --- /dev/null +++ b/mockgen/internal/tests/const_array_length/mock.go @@ -0,0 +1,48 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: input.go + +// Package const_length is a generated GoMock package. +package const_length + +import ( + reflect "reflect" + + gomock "github.com/golang/mock/gomock" +) + +// MockI is a mock of I interface. +type MockI struct { + ctrl *gomock.Controller + recorder *MockIMockRecorder +} + +// MockIMockRecorder is the mock recorder for MockI. +type MockIMockRecorder struct { + mock *MockI +} + +// NewMockI creates a new mock instance. +func NewMockI(ctrl *gomock.Controller) *MockI { + mock := &MockI{ctrl: ctrl} + mock.recorder = &MockIMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockI) EXPECT() *MockIMockRecorder { + return m.recorder +} + +// F mocks base method. +func (m *MockI) F() [2]int { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "F") + ret0, _ := ret[0].([2]int) + return ret0 +} + +// F indicates an expected call of F. +func (mr *MockIMockRecorder) F() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "F", reflect.TypeOf((*MockI)(nil).F)) +} diff --git a/mockgen/parse.go b/mockgen/parse.go index ef8a9595..cdb82a34 100644 --- a/mockgen/parse.go +++ b/mockgen/parse.go @@ -410,7 +410,16 @@ func (p *fileParser) parseType(pkg string, typ ast.Expr) (model.Type, error) { case *ast.ArrayType: ln := -1 if v.Len != nil { - x, err := strconv.Atoi(v.Len.(*ast.BasicLit).Value) + var value string + switch val := v.Len.(type) { + case (*ast.BasicLit): + value = val.Value + case (*ast.Ident): + // when the length is a const + value = val.Obj.Decl.(*ast.ValueSpec).Values[0].(*ast.BasicLit).Value + } + + x, err := strconv.Atoi(value) if err != nil { return nil, p.errorf(v.Len.Pos(), "bad array size: %v", err) } diff --git a/mockgen/parse_test.go b/mockgen/parse_test.go index b0cdbb13..e39f02ea 100644 --- a/mockgen/parse_test.go +++ b/mockgen/parse_test.go @@ -263,3 +263,27 @@ func TestParsePackageImport_FallbackMultiGoPath(t *testing.T) { t.Errorf("expect %s, got %s", expected, pkgPath) } } + +func TestParseArrayWithConstLength(t *testing.T) { + fs := token.NewFileSet() + + file, err := parser.ParseFile(fs, "internal/tests/const_array_length/input.go", nil, 0) + if err != nil { + t.Fatalf("Unexpected error: %v", err) + } + + p := fileParser{ + fileSet: fs, + } + + pkg, err := p.parseFile("", file) + if err != nil { + t.Fatalf("Unexpected error: %v", err) + } + + expect := "[2]int" + got := pkg.Interfaces[0].Methods[0].Out[0].Type.String(nil, "") + if got != expect { + t.Fatalf("Expected return type %v but got %v", expect, got) + } +} From 38deb921e94eecfb8d0834b7e4b93b098790f469 Mon Sep 17 00:00:00 2001 From: Francisco Miamoto Date: Tue, 23 Feb 2021 19:58:09 -0300 Subject: [PATCH 2/3] add additional function to test interface --- .../tests/const_array_length/input.go | 6 +++-- .../internal/tests/const_array_length/mock.go | 26 ++++++++++++++----- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/mockgen/internal/tests/const_array_length/input.go b/mockgen/internal/tests/const_array_length/input.go index c98482a4..008d57e3 100644 --- a/mockgen/internal/tests/const_array_length/input.go +++ b/mockgen/internal/tests/const_array_length/input.go @@ -1,8 +1,10 @@ -//go:generate mockgen -package const_length -destination mock.go -source input.go package const_length +//go:generate mockgen -package const_length -destination mock.go -source input.go + const C = 2 type I interface { - F() [C]int + Foo() [C]int + Bar() [2]int } diff --git a/mockgen/internal/tests/const_array_length/mock.go b/mockgen/internal/tests/const_array_length/mock.go index 9a407c23..71853b7e 100644 --- a/mockgen/internal/tests/const_array_length/mock.go +++ b/mockgen/internal/tests/const_array_length/mock.go @@ -33,16 +33,30 @@ func (m *MockI) EXPECT() *MockIMockRecorder { return m.recorder } -// F mocks base method. -func (m *MockI) F() [2]int { +// Bar mocks base method. +func (m *MockI) Bar() [2]int { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "F") + ret := m.ctrl.Call(m, "Bar") ret0, _ := ret[0].([2]int) return ret0 } -// F indicates an expected call of F. -func (mr *MockIMockRecorder) F() *gomock.Call { +// Bar indicates an expected call of Bar. +func (mr *MockIMockRecorder) Bar() *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "F", reflect.TypeOf((*MockI)(nil).F)) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Bar", reflect.TypeOf((*MockI)(nil).Bar)) +} + +// Foo mocks base method. +func (m *MockI) Foo() [2]int { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Foo") + ret0, _ := ret[0].([2]int) + return ret0 +} + +// Foo indicates an expected call of Foo. +func (mr *MockIMockRecorder) Foo() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Foo", reflect.TypeOf((*MockI)(nil).Foo)) } From 4f8f6b7987b1fe1783ba4614280f42a2f80a090b Mon Sep 17 00:00:00 2001 From: Francisco Miamoto Date: Tue, 23 Feb 2021 19:58:16 -0300 Subject: [PATCH 3/3] change fmt of error message --- mockgen/parse_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mockgen/parse_test.go b/mockgen/parse_test.go index e39f02ea..6230df58 100644 --- a/mockgen/parse_test.go +++ b/mockgen/parse_test.go @@ -284,6 +284,6 @@ func TestParseArrayWithConstLength(t *testing.T) { expect := "[2]int" got := pkg.Interfaces[0].Methods[0].Out[0].Type.String(nil, "") if got != expect { - t.Fatalf("Expected return type %v but got %v", expect, got) + t.Fatalf("got %v; expected %v", got, expect) } } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy