Skip to content

Commit

Permalink
fixes #39
Browse files Browse the repository at this point in the history
  • Loading branch information
hexdigest committed Jun 17, 2019
1 parent e3ffc7a commit 48589a7
Show file tree
Hide file tree
Showing 5 changed files with 335 additions and 324 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ require (
github.com/pkg/errors v0.8.0
github.com/pmezard/go-difflib v1.0.0
github.com/stretchr/testify v1.2.2
golang.org/x/tools v0.0.0-20190130014116-16909d206f00
golang.org/x/tools v0.0.0-20190614205625-5aca471b1d59
)
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,25 @@ github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793 h1:u+LnwYTOOW7Ukr/fppxEb1Nwz0AtPflrblfvUudpo+I=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190125091013-d26f9f9a57f3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5 h1:mzjBh+S5frKOsOBobWIMAbXavqjmgO17k/2puhcFR94=
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20181105230042-78dc5bac0cac/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190130014116-16909d206f00 h1:G7fMnjzTJ53Qk/TDEjQOl2NB416tfTomU1cZNmSXLx0=
golang.org/x/tools v0.0.0-20190130014116-16909d206f00/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190614205625-5aca471b1d59 h1:QjA/9ArTfVTLfEhClDCG7SGrZkZixxWpwNCDiwJfh88=
golang.org/x/tools v0.0.0-20190614205625-5aca471b1d59/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
110 changes: 56 additions & 54 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ const (
}
{{ range $method := $.Interface.Methods }}
{{ $m := (printf "mm%s" $method.Name) }}
type m{{$mock}}{{$method.Name}} struct {
mock *{{$mock}}
defaultExpectation *{{$mock}}{{$method.Name}}Expectation
Expand Down Expand Up @@ -79,66 +81,66 @@ const (
{{end}}
// Expect sets up expected params for {{$.Interface.Name}}.{{$method.Name}}
func (m *m{{$mock}}{{$method.Name}}) Expect({{$method.Params}}) *m{{$mock}}{{$method.Name}} {
if m.mock.func{{$method.Name}} != nil {
m.mock.t.Fatalf("{{$mock}}.{{$method.Name}} mock is already set by Set")
func ({{$m}} *m{{$mock}}{{$method.Name}}) Expect({{$method.Params}}) *m{{$mock}}{{$method.Name}} {
if {{$m}}.mock.func{{$method.Name}} != nil {
{{$m}}.mock.t.Fatalf("{{$mock}}.{{$method.Name}} mock is already set by Set")
}
if m.defaultExpectation == nil {
m.defaultExpectation = &{{$mock}}{{$method.Name}}Expectation{}
if {{$m}}.defaultExpectation == nil {
{{$m}}.defaultExpectation = &{{$mock}}{{$method.Name}}Expectation{}
}
{{if $method.HasParams }}
m.defaultExpectation.params = &{{$mock}}{{$method.Name}}Params{ {{ $method.ParamsNames }} }
for _, e := range m.expectations {
if minimock.Equal(e.params, m.defaultExpectation.params) {
m.mock.t.Fatalf("Expectation set by When has same params: %#v", *m.defaultExpectation.params)
{{$m}}.defaultExpectation.params = &{{$mock}}{{$method.Name}}Params{ {{ $method.ParamsNames }} }
for _, e := range {{$m}}.expectations {
if minimock.Equal(e.params, {{$m}}.defaultExpectation.params) {
{{$m}}.mock.t.Fatalf("Expectation set by When has same params: %#v", *{{$m}}.defaultExpectation.params)
}
}
{{end}}
return m
return {{$m}}
}
// Return sets up results that will be returned by {{$.Interface.Name}}.{{$method.Name}}
func (m *m{{$mock}}{{$method.Name}}) Return({{$method.Results}}) *{{$mock}} {
if m.mock.func{{$method.Name}} != nil {
m.mock.t.Fatalf("{{$mock}}.{{$method.Name}} mock is already set by Set")
func ({{$m}} *m{{$mock}}{{$method.Name}}) Return({{$method.Results}}) *{{$mock}} {
if {{$m}}.mock.func{{$method.Name}} != nil {
{{$m}}.mock.t.Fatalf("{{$mock}}.{{$method.Name}} mock is already set by Set")
}
if m.defaultExpectation == nil {
m.defaultExpectation = &{{$mock}}{{$method.Name}}Expectation{mock: m.mock}
if {{$m}}.defaultExpectation == nil {
{{$m}}.defaultExpectation = &{{$mock}}{{$method.Name}}Expectation{mock: {{$m}}.mock}
}
{{if $method.HasResults }} m.defaultExpectation.results = &{{$mock}}{{$method.Name}}Results{ {{ $method.ResultsNames }} } {{end}}
return m.mock
{{if $method.HasResults }} {{$m}}.defaultExpectation.results = &{{$mock}}{{$method.Name}}Results{ {{ $method.ResultsNames }} } {{end}}
return {{$m}}.mock
}
//Set uses given function f to mock the {{$.Interface.Name}}.{{$method.Name}} method
func (m *m{{$mock}}{{$method.Name}}) Set(f func{{$method.Signature}}) *{{$mock}}{
if m.defaultExpectation != nil {
m.mock.t.Fatalf("Default expectation is already set for the {{$.Interface.Name}}.{{$method.Name}} method")
func ({{$m}} *m{{$mock}}{{$method.Name}}) Set(f func{{$method.Signature}}) *{{$mock}}{
if {{$m}}.defaultExpectation != nil {
{{$m}}.mock.t.Fatalf("Default expectation is already set for the {{$.Interface.Name}}.{{$method.Name}} method")
}
if len(m.expectations) > 0 {
m.mock.t.Fatalf("Some expectations are already set for the {{$.Interface.Name}}.{{$method.Name}} method")
if len({{$m}}.expectations) > 0 {
{{$m}}.mock.t.Fatalf("Some expectations are already set for the {{$.Interface.Name}}.{{$method.Name}} method")
}
m.mock.func{{$method.Name}}= f
return m.mock
{{$m}}.mock.func{{$method.Name}}= f
return {{$m}}.mock
}
{{if (and $method.HasParams $method.HasResults)}}
// When sets expectation for the {{$.Interface.Name}}.{{$method.Name}} which will trigger the result defined by the following
// Then helper
func (m *m{{$mock}}{{$method.Name}}) When({{$method.Params}}) *{{$mock}}{{$method.Name}}Expectation {
if m.mock.func{{$method.Name}} != nil {
m.mock.t.Fatalf("{{$mock}}.{{$method.Name}} mock is already set by Set")
func ({{$m}} *m{{$mock}}{{$method.Name}}) When({{$method.Params}}) *{{$mock}}{{$method.Name}}Expectation {
if {{$m}}.mock.func{{$method.Name}} != nil {
{{$m}}.mock.t.Fatalf("{{$mock}}.{{$method.Name}} mock is already set by Set")
}
expectation := &{{$mock}}{{$method.Name}}Expectation{
mock: m.mock,
mock: {{$m}}.mock,
params: &{{$mock}}{{$method.Name}}Params{ {{ $method.ParamsNames }} },
}
m.expectations = append(m.expectations, expectation)
{{$m}}.expectations = append({{$m}}.expectations, expectation)
return expectation
}
Expand All @@ -150,72 +152,72 @@ const (
{{end}}
// {{$method.Name}} implements {{$.Interface.Type}}
func (m *{{$mock}}) {{$method.Declaration}} {
mm_atomic.AddUint64(&m.before{{$method.Name}}Counter, 1)
defer mm_atomic.AddUint64(&m.after{{$method.Name}}Counter, 1)
func ({{$m}} *{{$mock}}) {{$method.Declaration}} {
mm_atomic.AddUint64(&{{$m}}.before{{$method.Name}}Counter, 1)
defer mm_atomic.AddUint64(&{{$m}}.after{{$method.Name}}Counter, 1)
{{if $method.HasParams}}
params := &{{$mock}}{{$method.Name}}Params{ {{$method.ParamsNames}} }
// Record call args
m.{{$method.Name}}Mock.mutex.Lock()
m.{{$method.Name}}Mock.callArgs = append(m.{{$method.Name}}Mock.callArgs, params)
m.{{$method.Name}}Mock.mutex.Unlock()
{{$m}}.{{$method.Name}}Mock.mutex.Lock()
{{$m}} .{{$method.Name}}Mock.callArgs = append({{$m}}.{{$method.Name}}Mock.callArgs, params)
{{$m}}.{{$method.Name}}Mock.mutex.Unlock()
for _, e := range m.{{$method.Name}}Mock.expectations {
for _, e := range {{$m}}.{{$method.Name}}Mock.expectations {
if minimock.Equal(e.params, params) {
mm_atomic.AddUint64(&e.Counter, 1)
{{$method.ReturnStruct "e.results" -}}
}
}
{{end}}
if m.{{$method.Name}}Mock.defaultExpectation != nil {
mm_atomic.AddUint64(&m.{{$method.Name}}Mock.defaultExpectation.Counter, 1)
if {{$m}}.{{$method.Name}}Mock.defaultExpectation != nil {
mm_atomic.AddUint64(&{{$m}}.{{$method.Name}}Mock.defaultExpectation.Counter, 1)
{{- if $method.HasParams }}
want := m.{{$method.Name}}Mock.defaultExpectation.params
want := {{$m}}.{{$method.Name}}Mock.defaultExpectation.params
got := {{$mock}}{{$method.Name}}Params{ {{$method.ParamsNames}} }
if want != nil && !minimock.Equal(*want, got) {
m.t.Errorf("{{$mock}}.{{$method.Name}} got unexpected parameters, want: %#v, got: %#v%s\n", *want, got, minimock.Diff(*want, got))
{{$m}}.t.Errorf("{{$mock}}.{{$method.Name}} got unexpected parameters, want: %#v, got: %#v%s\n", *want, got, minimock.Diff(*want, got))
}
{{ end }}
{{if $method.HasResults }}
results := m.{{$method.Name}}Mock.defaultExpectation.results
results := {{$m}}.{{$method.Name}}Mock.defaultExpectation.results
if results == nil {
m.t.Fatal("No results are set for the {{$mock}}.{{$method.Name}}")
{{$m}}.t.Fatal("No results are set for the {{$mock}}.{{$method.Name}}")
}
{{$method.ReturnStruct "(*results)" -}}
{{else}}
return
{{ end }}
}
if m.func{{$method.Name}} != nil {
{{$method.Pass "m.func"}}
if {{$m}}.func{{$method.Name}} != nil {
{{$method.Pass (printf "%s.func" $m)}}
}
m.t.Fatalf("Unexpected call to {{$mock}}.{{$method.Name}}.{{range $method.Params}} %v{{end}}", {{ $method.ParamsNames }} )
{{$m}}.t.Fatalf("Unexpected call to {{$mock}}.{{$method.Name}}.{{range $method.Params}} %v{{end}}", {{ $method.ParamsNames }} )
{{if $method.HasResults}}return{{end}}
}
// {{$method.Name}}AfterCounter returns a count of finished {{$mock}}.{{$method.Name}} invocations
func (m *{{$mock}}) {{$method.Name}}AfterCounter() uint64 {
return mm_atomic.LoadUint64(&m.after{{$method.Name}}Counter)
func ({{$m}} *{{$mock}}) {{$method.Name}}AfterCounter() uint64 {
return mm_atomic.LoadUint64(&{{$m}}.after{{$method.Name}}Counter)
}
// {{$method.Name}}BeforeCounter returns a count of {{$mock}}.{{$method.Name}} invocations
func (m *{{$mock}}) {{$method.Name}}BeforeCounter() uint64 {
return mm_atomic.LoadUint64(&m.before{{$method.Name}}Counter)
func ({{$m}} *{{$mock}}) {{$method.Name}}BeforeCounter() uint64 {
return mm_atomic.LoadUint64(&{{$m}}.before{{$method.Name}}Counter)
}
{{ if $method.HasParams }}
// Calls returns a list of arguments used in each call to {{$mock}}.{{$method.Name}}.
// The list is in the same order as the calls were made (i.e. recent calls have a higher index)
func (m *m{{$mock}}{{$method.Name}}) Calls() []*{{$mock}}{{$method.Name}}Params {
m.mutex.RLock()
func ({{$m}} *m{{$mock}}{{$method.Name}}) Calls() []*{{$mock}}{{$method.Name}}Params {
{{$m}}.mutex.RLock()
argCopy := make([]*{{$mock}}{{$method.Name}}Params, len(m.callArgs))
copy(argCopy, m.callArgs)
argCopy := make([]*{{$mock}}{{$method.Name}}Params, len({{$m}}.callArgs))
copy(argCopy, {{$m}}.callArgs)
m.mutex.RUnlock()
{{$m}}.mutex.RUnlock()
return argCopy
}
Expand Down
Loading

0 comments on commit 48589a7

Please sign in to comment.