Skip to content

Commit

Permalink
Merge pull request #142 from codeactual/generator_test_checker
Browse files Browse the repository at this point in the history
Allow generator fixtures to use formatted source
  • Loading branch information
evanphx authored May 20, 2017
2 parents 876cfd3 + 42ce937 commit 4df6d1b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
SHELL=bash

all: clean fmt test install integration
all: clean fmt test fixture install integration

clean:
rm -rf mocks
Expand All @@ -11,6 +11,9 @@ fmt:
test:
go test ./...

fixture:
mockery -print -dir mockery/fixtures -name RequesterVariadic > mockery/fixtures/mocks/requester_variadic.go

install:
go install ./...

Expand Down
3 changes: 3 additions & 0 deletions mockery/fixtures/mocks/requester_variadic.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func (_m *RequesterVariadic) Get(values ...string) bool {

return r0
}

// MultiWriteToFile provides a mock function with given fields: filename, w
func (_m *RequesterVariadic) MultiWriteToFile(filename string, w ...io.Writer) string {
_va := make([]interface{}, len(w))
Expand All @@ -47,6 +48,7 @@ func (_m *RequesterVariadic) MultiWriteToFile(filename string, w ...io.Writer) s

return r0
}

// OneInterface provides a mock function with given fields: a
func (_m *RequesterVariadic) OneInterface(a ...interface{}) bool {
var _ca []interface{}
Expand All @@ -62,6 +64,7 @@ func (_m *RequesterVariadic) OneInterface(a ...interface{}) bool {

return r0
}

// Sprintf provides a mock function with given fields: format, a
func (_m *RequesterVariadic) Sprintf(format string, a ...interface{}) string {
var _ca []interface{}
Expand Down
29 changes: 26 additions & 3 deletions mockery/generator_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mockery

import (
"go/format"
"io/ioutil"
"path/filepath"
"strings"
Expand Down Expand Up @@ -52,8 +53,23 @@ func (s *GeneratorSuite) checkGeneration(
) *Generator {
generator := s.getGenerator(filepath, interfaceName, inPackage)
s.NoError(generator.Generate(), "The generator ran without errors.")

// Mirror the formatting done by normally done by golang.org/x/tools/imports in Generator.Write.
//
// While we could possibly reuse Generator.Write here in addition to Generator.Generate,
// it would require changing Write's signature to accept custom options, specifically to
// allow the fragments in preexisting cases. It's assumed that this approximation,
// just formatting the source, is sufficient for the needs of the current test styles.
var actual []byte
actual, fmtErr := format.Source(generator.buf.Bytes())
s.NoError(fmtErr, "The formatter ran without errors.")

// Compare lines for easier debugging via testify's slice diff output
expectedLines := strings.Split(expected, "\n")
actualLines := strings.Split(string(actual), "\n")

s.Equal(
expected, generator.buf.String(),
expectedLines, actualLines,
"The generator produced the expected output.",
)
return generator
Expand Down Expand Up @@ -614,8 +630,7 @@ func (s *GeneratorSuite) TestGeneratorVariadicArgs() {
// Read the expected output from a "golden" file that we can also import in CompatSuite
// to asserts its actual behavior.
//
// NOTE: After regenerating the mock, the blank lines between the method blocks currently
// need to be removed due to possible defect in the checker.
// To regenerate the golden file: make fixture
expectedBytes, err := ioutil.ReadFile(filepath.Join(fixturePath, "mocks", "requester_variadic.go"))
s.NoError(err)
expected := string(expectedBytes)
Expand All @@ -636,6 +651,7 @@ type Fooer struct {
func (_m *Fooer) Bar(f func([]int)) {
_m.Called(f)
}
// Baz provides a mock function with given fields: path
func (_m *Fooer) Baz(path string) func(string) string {
ret := _m.Called(path)
Expand All @@ -651,6 +667,7 @@ func (_m *Fooer) Baz(path string) func(string) string {
return r0
}
// Foo provides a mock function with given fields: f
func (_m *Fooer) Foo(f func(string) string) error {
ret := _m.Called(f)
Expand Down Expand Up @@ -691,6 +708,7 @@ func (_m *AsyncProducer) Input() chan<- bool {
return r0
}
// Output provides a mock function with given fields:
func (_m *AsyncProducer) Output() <-chan bool {
ret := _m.Called()
Expand All @@ -706,6 +724,7 @@ func (_m *AsyncProducer) Output() <-chan bool {
return r0
}
// Whatever provides a mock function with given fields:
func (_m *AsyncProducer) Whatever() chan bool {
ret := _m.Called()
Expand Down Expand Up @@ -789,6 +808,7 @@ func (_m *ConsulLock) Lock(_a0 <-chan struct{}) (<-chan struct{}, error) {
return r0, r1
}
// Unlock provides a mock function with given fields:
func (_m *ConsulLock) Unlock() error {
ret := _m.Called()
Expand Down Expand Up @@ -914,6 +934,7 @@ func (_m *Example) A() http.Flusher {
return r0
}
// B provides a mock function with given fields: _a0
func (_m *Example) B(_a0 string) fixtureshttp.MyStruct {
ret := _m.Called(_a0)
Expand Down Expand Up @@ -961,6 +982,7 @@ func (_m *ImportsSameAsPackage) A() test.B {
return r0
}
// B provides a mock function with given fields:
func (_m *ImportsSameAsPackage) B() fixtures.KeyManager {
ret := _m.Called()
Expand All @@ -976,6 +998,7 @@ func (_m *ImportsSameAsPackage) B() fixtures.KeyManager {
return r0
}
// C provides a mock function with given fields: _a0
func (_m *ImportsSameAsPackage) C(_a0 fixtures.C) {
_m.Called(_a0)
Expand Down

0 comments on commit 4df6d1b

Please sign in to comment.