Skip to content

Commit

Permalink
Merge pull request #18 from percolate/add-stub-helpers
Browse files Browse the repository at this point in the history
Generate Helpers to Automate Stub Usage
  • Loading branch information
kevinbirch authored Apr 16, 2018
2 parents b7df4bb + 4f2561b commit a789b0d
Show file tree
Hide file tree
Showing 16 changed files with 961 additions and 235 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version: 2
jobs:
build:
docker:
- image: circleci/golang:1.9.2-stretch
- image: circleci/golang:1.10.1-stretch

working_directory: /go/src/github.com/percolate/charlatan

Expand Down
2 changes: 1 addition & 1 deletion VERSION.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.2
1.0.3
13 changes: 11 additions & 2 deletions generator.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"flag"
"fmt"
"go/ast"
"go/build"
Expand Down Expand Up @@ -255,9 +256,17 @@ func (g *Generator) Generate(interfaceNames []string) ([]byte, error) {
packageName = g.PackageOverride
}

argv := []string{"charlatan"}
var argv strings.Builder
argv.WriteString("charlatan")
flag.Visit(func(f *flag.Flag) {
fmt.Fprintf(&argv, " -%s=%s", f.Name, f.Value)
})
if flag.NArg() > 0 {
argv.WriteByte(' ')
argv.WriteString(strings.Join(flag.Args(), " "))
}
tmpl := Template{
CommandLine: strings.Join(append(argv, os.Args[1:]...), " "),
CommandLine: argv.String(),
PackageName: packageName,
Imports: g.imports.GetRequired(),
Interfaces: decls,
Expand Down
38 changes: 37 additions & 1 deletion template.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ type {{.Interface}}{{.Name}}Invocation struct {
{{end}}
}{{end}}
}
{{if and .Parameters .Results}}
// New{{.Interface}}{{.Name}}Invocation creates a new instance of {{.Interface}}{{.Name}}Invocation
func New{{.Interface}}{{.Name}}Invocation({{.ParametersDeclaration}}, {{.ResultsDeclaration}}) *{{.Interface}}{{.Name}}Invocation {
invocation := new({{.Interface}}{{.Name}}Invocation)
{{range .Parameters}} invocation.Parameters.{{.TitleCase}} = {{.Name}}
{{end}}
{{range .Results}}invocation.Results.{{.TitleCase}} = {{.Name}}
{{end}}
return invocation
}{{end}}
{{end}}{{/* end range .Methods */}}
// {{.Name}}TestingT represents the methods of "testing".T used by charlatan Fakes. It avoids importing the testing package.
Expand Down Expand Up @@ -122,6 +135,29 @@ func (f *Fake{{.Name}}) Reset() {
return
}{{end}}
{{if .Results}}
// Set{{.Name}}Stub configures {{.Interface}}.{{.Name}} to always return the given values
{{with $f := gensym}}func ({{$f}} *Fake{{$m.Interface}}) Set{{$m.Name}}Stub({{$m.ResultsDeclaration}}) {
{{$f}}.{{$m.Name}}Hook = func({{$m.ParametersSignature}}) ({{$m.ResultsSignature}}) {
return {{range $i, $r := $m.Results}}{{if $i}}, {{end}}{{$r.Name}}{{end}}
}
}{{end}}{{end}}{{/* end if .Results */}}
{{if and .Parameters .Results}}
// Set{{.Name}}Invocation configures {{.Interface}}.{{.Name}} to return the given results when called with the given parameters
// If no match is found for an invocation the result(s) of the fallback function are returned
{{with $f := gensym}}{{with $c := gensym}}{{with $d := gensym}}func ({{$f}} *Fake{{$m.Interface}}) Set{{$m.Name}}Invocation(calls{{$c}} []*{{$m.Interface}}{{$m.Name}}Invocation, fallback{{$d}} func() ({{$m.ResultsSignature}})) {
{{$f}}.{{$m.Name}}Hook = func({{$m.ParametersDeclaration}}) ({{$m.ResultsDeclaration}}) {
for _, call := range calls{{$c}} {
if {{range $i, $p := $m.Parameters}}{{if $i}} && {{end}}reflect.DeepEqual(call.Parameters.{{$p.TitleCase}}, {{$p.Name}}){{end}} {
{{range $m.Results}}{{.Name}} = call.Results.{{.TitleCase}}
{{end}}
return
}
}
return fallback{{$d}}()
}
}{{end}}{{end}}{{end}}{{end}}{{/* end if and .Parameters .Results */}}
// {{.Name}}Called returns true if Fake{{.Interface}}.{{.Name}} was called
func (f *Fake{{.Interface}}) {{.Name}}Called() bool {
Expand Down Expand Up @@ -241,7 +277,7 @@ func (f *Fake{{.Interface}}) Assert{{.Name}}CalledN(t {{.Interface}}TestingT, n
}
return
}{{end}}{{end}} {{/* end if .Results */}}
}{{end}}{{end}}{{/* end if len $m.Results */}}
{{end}}{{/* end if .Parameters */}}
{{end}}{{/* end range $m := .Methods */}}
{{end}}{{/* end range .Interfaces */}}
Expand Down
46 changes: 30 additions & 16 deletions testdata/array/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,13 @@ func (_f6 *FakeArray) ArrayReturn() (ident1 [3]string) {
return
}

// SetArrayReturnStub configures Array.ArrayReturn to always return the given values
func (_f7 *FakeArray) SetArrayReturnStub(ident1 [3]string) {
_f7.ArrayReturnHook = func() [3]string {
return ident1
}
}

// ArrayReturnCalled returns true if FakeArray.ArrayReturn was called
func (f *FakeArray) ArrayReturnCalled() bool {
return len(f.ArrayReturnCalls) != 0
Expand Down Expand Up @@ -334,17 +341,17 @@ func (f *FakeArray) AssertArrayReturnCalledN(t ArrayTestingT, n int) {
}
}

func (_f7 *FakeArray) SliceParameter(ident1 []string) {
if _f7.SliceParameterHook == nil {
func (_f8 *FakeArray) SliceParameter(ident1 []string) {
if _f8.SliceParameterHook == nil {
panic("Array.SliceParameter() called but FakeArray.SliceParameterHook is nil")
}

invocation := new(ArraySliceParameterInvocation)
_f7.SliceParameterCalls = append(_f7.SliceParameterCalls, invocation)
_f8.SliceParameterCalls = append(_f8.SliceParameterCalls, invocation)

invocation.Parameters.Ident1 = ident1

_f7.SliceParameterHook(ident1)
_f8.SliceParameterHook(ident1)

return
}
Expand Down Expand Up @@ -402,8 +409,8 @@ func (f *FakeArray) AssertSliceParameterCalledN(t ArrayTestingT, n int) {
}

// SliceParameterCalledWith returns true if FakeArray.SliceParameter was called with the given values
func (_f8 *FakeArray) SliceParameterCalledWith(ident1 []string) (found bool) {
for _, call := range _f8.SliceParameterCalls {
func (_f9 *FakeArray) SliceParameterCalledWith(ident1 []string) (found bool) {
for _, call := range _f9.SliceParameterCalls {
if reflect.DeepEqual(call.Parameters.Ident1, ident1) {
found = true
break
Expand All @@ -414,10 +421,10 @@ func (_f8 *FakeArray) SliceParameterCalledWith(ident1 []string) (found bool) {
}

// AssertSliceParameterCalledWith calls t.Error if FakeArray.SliceParameter was not called with the given values
func (_f9 *FakeArray) AssertSliceParameterCalledWith(t ArrayTestingT, ident1 []string) {
func (_f10 *FakeArray) AssertSliceParameterCalledWith(t ArrayTestingT, ident1 []string) {
t.Helper()
var found bool
for _, call := range _f9.SliceParameterCalls {
for _, call := range _f10.SliceParameterCalls {
if reflect.DeepEqual(call.Parameters.Ident1, ident1) {
found = true
break
Expand All @@ -430,9 +437,9 @@ func (_f9 *FakeArray) AssertSliceParameterCalledWith(t ArrayTestingT, ident1 []s
}

// SliceParameterCalledOnceWith returns true if FakeArray.SliceParameter was called exactly once with the given values
func (_f10 *FakeArray) SliceParameterCalledOnceWith(ident1 []string) bool {
func (_f11 *FakeArray) SliceParameterCalledOnceWith(ident1 []string) bool {
var count int
for _, call := range _f10.SliceParameterCalls {
for _, call := range _f11.SliceParameterCalls {
if reflect.DeepEqual(call.Parameters.Ident1, ident1) {
count++
}
Expand All @@ -442,10 +449,10 @@ func (_f10 *FakeArray) SliceParameterCalledOnceWith(ident1 []string) bool {
}

// AssertSliceParameterCalledOnceWith calls t.Error if FakeArray.SliceParameter was not called exactly once with the given values
func (_f11 *FakeArray) AssertSliceParameterCalledOnceWith(t ArrayTestingT, ident1 []string) {
func (_f12 *FakeArray) AssertSliceParameterCalledOnceWith(t ArrayTestingT, ident1 []string) {
t.Helper()
var count int
for _, call := range _f11.SliceParameterCalls {
for _, call := range _f12.SliceParameterCalls {
if reflect.DeepEqual(call.Parameters.Ident1, ident1) {
count++
}
Expand All @@ -456,21 +463,28 @@ func (_f11 *FakeArray) AssertSliceParameterCalledOnceWith(t ArrayTestingT, ident
}
}

func (_f12 *FakeArray) SliceReturn() (ident1 []string) {
if _f12.SliceReturnHook == nil {
func (_f13 *FakeArray) SliceReturn() (ident1 []string) {
if _f13.SliceReturnHook == nil {
panic("Array.SliceReturn() called but FakeArray.SliceReturnHook is nil")
}

invocation := new(ArraySliceReturnInvocation)
_f12.SliceReturnCalls = append(_f12.SliceReturnCalls, invocation)
_f13.SliceReturnCalls = append(_f13.SliceReturnCalls, invocation)

ident1 = _f12.SliceReturnHook()
ident1 = _f13.SliceReturnHook()

invocation.Results.Ident1 = ident1

return
}

// SetSliceReturnStub configures Array.SliceReturn to always return the given values
func (_f14 *FakeArray) SetSliceReturnStub(ident1 []string) {
_f14.SliceReturnHook = func() []string {
return ident1
}
}

// SliceReturnCalled returns true if FakeArray.SliceReturn was called
func (f *FakeArray) SliceReturnCalled() bool {
return len(f.SliceReturnCalls) != 0
Expand Down
Loading

0 comments on commit a789b0d

Please sign in to comment.