Fix most golint warnings

pull/266/head
Ernesto Jiménez 2016-01-09 19:30:22 +01:00
parent 9b5e169170
commit 1661650f98
8 changed files with 132 additions and 124 deletions

View File

@ -46,11 +46,11 @@ func main() {
} }
if err := generateCode(importer, funcs); err != nil { if err := generateCode(importer, funcs); err != nil {
log.Fatal() log.Fatal(err)
} }
} }
func generateCode(importer imports.Importer, funcs []Func) error { func generateCode(importer imports.Importer, funcs []testFunc) error {
buff := bytes.NewBuffer(nil) buff := bytes.NewBuffer(nil)
tmplHead, tmplFunc, err := parseTemplates() tmplHead, tmplFunc, err := parseTemplates()
@ -119,11 +119,11 @@ func outputFile() (*os.File, error) {
// analyzeCode takes the types scope and the docs and returns the import // analyzeCode takes the types scope and the docs and returns the import
// information and information about all the assertion functions. // information and information about all the assertion functions.
func analyzeCode(scope *types.Scope, docs *doc.Package) (imports.Importer, []Func, error) { func analyzeCode(scope *types.Scope, docs *doc.Package) (imports.Importer, []testFunc, error) {
testingT := scope.Lookup("TestingT").Type().Underlying().(*types.Interface) testingT := scope.Lookup("TestingT").Type().Underlying().(*types.Interface)
importer := imports.New(*outputPkg) importer := imports.New(*outputPkg)
funcs := make([]Func, 0) var funcs []testFunc
// Go through all the top level functions // Go through all the top level functions
for _, fdocs := range docs.Funcs { for _, fdocs := range docs.Funcs {
// Find the function // Find the function
@ -151,7 +151,7 @@ func analyzeCode(scope *types.Scope, docs *doc.Package) (imports.Importer, []Fun
continue continue
} }
funcs = append(funcs, Func{*outputPkg, fdocs, fn}) funcs = append(funcs, testFunc{*outputPkg, fdocs, fn})
importer.AddImportsFrom(sig.Params()) importer.AddImportsFrom(sig.Params())
} }
return importer, funcs, nil return importer, funcs, nil
@ -199,20 +199,20 @@ func parsePackageSource(pkg string) (*types.Scope, *doc.Package, error) {
return scope, docs, nil return scope, docs, nil
} }
type Func struct { type testFunc struct {
CurrentPkg string CurrentPkg string
DocInfo *doc.Func DocInfo *doc.Func
TypeInfo *types.Func TypeInfo *types.Func
} }
func (f *Func) Qualifier(p *types.Package) string { func (f *testFunc) Qualifier(p *types.Package) string {
if p == nil || p.Name() == f.CurrentPkg { if p == nil || p.Name() == f.CurrentPkg {
return "" return ""
} }
return p.Name() return p.Name()
} }
func (f *Func) Params() string { func (f *testFunc) Params() string {
sig := f.TypeInfo.Type().(*types.Signature) sig := f.TypeInfo.Type().(*types.Signature)
params := sig.Params() params := sig.Params()
p := "" p := ""
@ -235,7 +235,7 @@ func (f *Func) Params() string {
return p return p
} }
func (f *Func) ForwardedParams() string { func (f *testFunc) ForwardedParams() string {
sig := f.TypeInfo.Type().(*types.Signature) sig := f.TypeInfo.Type().(*types.Signature)
params := sig.Params() params := sig.Params()
p := "" p := ""
@ -258,11 +258,11 @@ func (f *Func) ForwardedParams() string {
return p return p
} }
func (f *Func) Comment() string { func (f *testFunc) Comment() string {
return "// " + strings.Replace(strings.TrimSpace(f.DocInfo.Doc), "\n", "\n// ", -1) return "// " + strings.Replace(strings.TrimSpace(f.DocInfo.Doc), "\n", "\n// ", -1)
} }
func (f *Func) CommentWithoutT(receiver string) string { func (f *testFunc) CommentWithoutT(receiver string) string {
search := fmt.Sprintf("assert.%s(t, ", f.DocInfo.Name) search := fmt.Sprintf("assert.%s(t, ", f.DocInfo.Name)
replace := fmt.Sprintf("%s.%s(", receiver, f.DocInfo.Name) replace := fmt.Sprintf("%s.%s(", receiver, f.DocInfo.Name)
return strings.Replace(f.Comment(), search, replace, -1) return strings.Replace(f.Comment(), search, replace, -1)

View File

@ -1,4 +1,5 @@
// Provides a system by which it is possible to mock your objects and verify calls are happening as expected. // Package mock provides a system by which it is possible to mock your objects
// and verify calls are happening as expected.
// //
// Example Usage // Example Usage
// //

View File

@ -65,64 +65,67 @@ func newCall(parent *Mock, methodName string, methodArguments ...interface{}) *C
} }
} }
func (self *Call) lock() { func (c *Call) lock() {
self.Parent.mutex.Lock() c.Parent.mutex.Lock()
} }
func (self *Call) unlock() { func (c *Call) unlock() {
self.Parent.mutex.Unlock() c.Parent.mutex.Unlock()
} }
func (self *Call) Return(returnArguments ...interface{}) *Call { // Return specifies the return arguments for the expectation.
self.lock() //
defer self.unlock() // Mock.On("DoSomething").Return(errors.New("failed"))
func (c *Call) Return(returnArguments ...interface{}) *Call {
c.lock()
defer c.unlock()
self.ReturnArguments = returnArguments c.ReturnArguments = returnArguments
return self return c
} }
// Once indicates that that the mock should only return the value once. // Once indicates that that the mock should only return the value once.
// //
// Mock.On("MyMethod", arg1, arg2).Return(returnArg1, returnArg2).Once() // Mock.On("MyMethod", arg1, arg2).Return(returnArg1, returnArg2).Once()
func (self *Call) Once() *Call { func (c *Call) Once() *Call {
return self.Times(1) return c.Times(1)
} }
// Twice indicates that that the mock should only return the value twice. // Twice indicates that that the mock should only return the value twice.
// //
// Mock.On("MyMethod", arg1, arg2).Return(returnArg1, returnArg2).Twice() // Mock.On("MyMethod", arg1, arg2).Return(returnArg1, returnArg2).Twice()
func (self *Call) Twice() *Call { func (c *Call) Twice() *Call {
return self.Times(2) return c.Times(2)
} }
// Times indicates that that the mock should only return the indicated number // Times indicates that that the mock should only return the indicated number
// of times. // of times.
// //
// Mock.On("MyMethod", arg1, arg2).Return(returnArg1, returnArg2).Times(5) // Mock.On("MyMethod", arg1, arg2).Return(returnArg1, returnArg2).Times(5)
func (self *Call) Times(i int) *Call { func (c *Call) Times(i int) *Call {
self.lock() c.lock()
defer self.unlock() defer c.unlock()
self.Repeatability = i c.Repeatability = i
return self return c
} }
// WaitUntil sets the channel that will block the mock's return until its closed // WaitUntil sets the channel that will block the mock's return until its closed
// or a message is received. // or a message is received.
// //
// Mock.On("MyMethod", arg1, arg2).WaitUntil(time.After(time.Second)) // Mock.On("MyMethod", arg1, arg2).WaitUntil(time.After(time.Second))
func (self *Call) WaitUntil(w <-chan time.Time) *Call { func (c *Call) WaitUntil(w <-chan time.Time) *Call {
self.lock() c.lock()
defer self.unlock() defer c.unlock()
self.WaitFor = w c.WaitFor = w
return self return c
} }
// After sets how long to block until the call returns // After sets how long to block until the call returns
// //
// Mock.On("MyMethod", arg1, arg2).After(time.Second) // Mock.On("MyMethod", arg1, arg2).After(time.Second)
func (self *Call) After(d time.Duration) *Call { func (c *Call) After(d time.Duration) *Call {
return self.WaitUntil(time.After(d)) return c.WaitUntil(time.After(d))
} }
// Run sets a handler to be called before returning. It can be used when // Run sets a handler to be called before returning. It can be used when
@ -133,11 +136,11 @@ func (self *Call) After(d time.Duration) *Call {
// arg := args.Get(0).(*map[string]interface{}) // arg := args.Get(0).(*map[string]interface{})
// arg["foo"] = "bar" // arg["foo"] = "bar"
// }) // })
func (self *Call) Run(fn func(Arguments)) *Call { func (c *Call) Run(fn func(Arguments)) *Call {
self.lock() c.lock()
defer self.unlock() defer c.unlock()
self.RunFn = fn c.RunFn = fn
return self return c
} }
// On chains a new expectation description onto the mocked interface. This // On chains a new expectation description onto the mocked interface. This
@ -146,8 +149,8 @@ func (self *Call) Run(fn func(Arguments)) *Call {
// Mock. // Mock.
// On("MyMethod", 1).Return(nil). // On("MyMethod", 1).Return(nil).
// On("MyOtherMethod", 'a', 'b', 'c').Return(errors.New("Some Error")) // On("MyOtherMethod", 'a', 'b', 'c').Return(errors.New("Some Error"))
func (self *Call) On(methodName string, arguments ...interface{}) *Call { func (c *Call) On(methodName string, arguments ...interface{}) *Call {
return self.Parent.On(methodName, arguments...) return c.Parent.On(methodName, arguments...)
} }
// Mock is the workhorse used to track activity on another object. // Mock is the workhorse used to track activity on another object.
@ -187,17 +190,17 @@ func (m *Mock) TestData() objx.Map {
// being called. // being called.
// //
// Mock.On("MyMethod", arg1, arg2) // Mock.On("MyMethod", arg1, arg2)
func (self *Mock) On(methodName string, arguments ...interface{}) *Call { func (m *Mock) On(methodName string, arguments ...interface{}) *Call {
for _, arg := range arguments { for _, arg := range arguments {
if v := reflect.ValueOf(arg); v.Kind() == reflect.Func { if v := reflect.ValueOf(arg); v.Kind() == reflect.Func {
panic(fmt.Sprintf("cannot use Func in expectations. Use mock.AnythingOfType(\"%T\")", arg)) panic(fmt.Sprintf("cannot use Func in expectations. Use mock.AnythingOfType(\"%T\")", arg))
} }
} }
self.mutex.Lock() m.mutex.Lock()
defer self.mutex.Unlock() defer m.mutex.Unlock()
c := newCall(self, methodName, arguments...) c := newCall(m, methodName, arguments...)
self.ExpectedCalls = append(self.ExpectedCalls, c) m.ExpectedCalls = append(m.ExpectedCalls, c)
return c return c
} }
@ -223,7 +226,7 @@ func (m *Mock) findExpectedCall(method string, arguments ...interface{}) (int, *
func (m *Mock) findClosestCall(method string, arguments ...interface{}) (bool, *Call) { func (m *Mock) findClosestCall(method string, arguments ...interface{}) (bool, *Call) {
diffCount := 0 diffCount := 0
var closestCall *Call = nil var closestCall *Call
for _, call := range m.expectedCalls() { for _, call := range m.expectedCalls() {
if call.Method == method { if call.Method == method {
@ -246,7 +249,7 @@ func (m *Mock) findClosestCall(method string, arguments ...interface{}) (bool, *
func callString(method string, arguments Arguments, includeArgumentValues bool) string { func callString(method string, arguments Arguments, includeArgumentValues bool) string {
var argValsString string = "" var argValsString string
if includeArgumentValues { if includeArgumentValues {
var argVals []string var argVals []string
for argIndex, arg := range arguments { for argIndex, arg := range arguments {
@ -304,7 +307,7 @@ func (m *Mock) Called(arguments ...interface{}) Arguments {
call.Repeatability = -1 call.Repeatability = -1
case call.Repeatability > 1: case call.Repeatability > 1:
call.Repeatability -= 1 call.Repeatability--
} }
m.mutex.Unlock() m.mutex.Unlock()
} }
@ -335,7 +338,7 @@ func (m *Mock) Called(arguments ...interface{}) Arguments {
// //
// Calls may have occurred in any order. // Calls may have occurred in any order.
func AssertExpectationsForObjects(t TestingT, testObjects ...interface{}) bool { func AssertExpectationsForObjects(t TestingT, testObjects ...interface{}) bool {
var success bool = true var success = true
for _, obj := range testObjects { for _, obj := range testObjects {
mockObj := obj.(Mock) mockObj := obj.(Mock)
success = success && mockObj.AssertExpectations(t) success = success && mockObj.AssertExpectations(t)
@ -346,8 +349,8 @@ func AssertExpectationsForObjects(t TestingT, testObjects ...interface{}) bool {
// AssertExpectations asserts that everything specified with On and Return was // AssertExpectations asserts that everything specified with On and Return was
// in fact called as expected. Calls may have occurred in any order. // in fact called as expected. Calls may have occurred in any order.
func (m *Mock) AssertExpectations(t TestingT) bool { func (m *Mock) AssertExpectations(t TestingT) bool {
var somethingMissing bool = false var somethingMissing bool
var failedExpectations int = 0 var failedExpectations int
// iterate through each expectation // iterate through each expectation
expectedCalls := m.expectedCalls() expectedCalls := m.expectedCalls()
@ -377,7 +380,7 @@ func (m *Mock) AssertExpectations(t TestingT) bool {
// AssertNumberOfCalls asserts that the method was called expectedCalls times. // AssertNumberOfCalls asserts that the method was called expectedCalls times.
func (m *Mock) AssertNumberOfCalls(t TestingT, methodName string, expectedCalls int) bool { func (m *Mock) AssertNumberOfCalls(t TestingT, methodName string, expectedCalls int) bool {
var actualCalls int = 0 var actualCalls int
for _, call := range m.calls() { for _, call := range m.calls() {
if call.Method == methodName { if call.Method == methodName {
actualCalls++ actualCalls++
@ -441,8 +444,8 @@ func (m *Mock) calls() []Call {
type Arguments []interface{} type Arguments []interface{}
const ( const (
// The "any" argument. Used in Diff and Assert when // Anything is used in Diff and Assert when the argument being tested
// the argument being tested shouldn't be taken into consideration. // shouldn't be taken into consideration.
Anything string = "mock.Anything" Anything string = "mock.Anything"
) )
@ -472,9 +475,8 @@ func (f argumentMatcher) Matches(argument interface{}) bool {
if reflect.TypeOf(argument).AssignableTo(expectType) { if reflect.TypeOf(argument).AssignableTo(expectType) {
result := f.fn.Call([]reflect.Value{reflect.ValueOf(argument)}) result := f.fn.Call([]reflect.Value{reflect.ValueOf(argument)})
return result[0].Bool() return result[0].Bool()
} else {
return false
} }
return false
} }
func (f argumentMatcher) String() string { func (f argumentMatcher) String() string {
@ -532,10 +534,10 @@ func (args Arguments) Is(objects ...interface{}) bool {
// Returns the diff string and number of differences found. // Returns the diff string and number of differences found.
func (args Arguments) Diff(objects []interface{}) (string, int) { func (args Arguments) Diff(objects []interface{}) (string, int) {
var output string = "\n" var output = "\n"
var differences int var differences int
var maxArgCount int = len(args) var maxArgCount = len(args)
if len(objects) > maxArgCount { if len(objects) > maxArgCount {
maxArgCount = len(objects) maxArgCount = len(objects)
} }
@ -630,7 +632,7 @@ func (args Arguments) String(indexOrNil ...int) string {
return strings.Join(argsStr, ",") return strings.Join(argsStr, ",")
} else if len(indexOrNil) == 1 { } else if len(indexOrNil) == 1 {
// Index has been specified - get the argument at that index // Index has been specified - get the argument at that index
var index int = indexOrNil[0] var index = indexOrNil[0]
var s string var s string
var ok bool var ok bool
if s, ok = args.Get(index).(string); !ok { if s, ok = args.Get(index).(string); !ok {

View File

@ -68,7 +68,7 @@ func (i *TestExampleImplementation) TheExampleMethodFuncType(fn ExampleFuncType)
func Test_Mock_TestData(t *testing.T) { func Test_Mock_TestData(t *testing.T) {
var mockedService *TestExampleImplementation = new(TestExampleImplementation) var mockedService = new(TestExampleImplementation)
if assert.NotNil(t, mockedService.TestData()) { if assert.NotNil(t, mockedService.TestData()) {
@ -80,7 +80,7 @@ func Test_Mock_TestData(t *testing.T) {
func Test_Mock_On(t *testing.T) { func Test_Mock_On(t *testing.T) {
// make a test impl object // make a test impl object
var mockedService *TestExampleImplementation = new(TestExampleImplementation) var mockedService = new(TestExampleImplementation)
c := mockedService.On("TheExampleMethod") c := mockedService.On("TheExampleMethod")
assert.Equal(t, []*Call{c}, mockedService.ExpectedCalls) assert.Equal(t, []*Call{c}, mockedService.ExpectedCalls)
@ -89,7 +89,7 @@ func Test_Mock_On(t *testing.T) {
func Test_Mock_Chained_On(t *testing.T) { func Test_Mock_Chained_On(t *testing.T) {
// make a test impl object // make a test impl object
var mockedService *TestExampleImplementation = new(TestExampleImplementation) var mockedService = new(TestExampleImplementation)
mockedService. mockedService.
On("TheExampleMethod", 1, 2, 3). On("TheExampleMethod", 1, 2, 3).
@ -117,7 +117,7 @@ func Test_Mock_Chained_On(t *testing.T) {
func Test_Mock_On_WithArgs(t *testing.T) { func Test_Mock_On_WithArgs(t *testing.T) {
// make a test impl object // make a test impl object
var mockedService *TestExampleImplementation = new(TestExampleImplementation) var mockedService = new(TestExampleImplementation)
c := mockedService.On("TheExampleMethod", 1, 2, 3, 4) c := mockedService.On("TheExampleMethod", 1, 2, 3, 4)
@ -129,7 +129,7 @@ func Test_Mock_On_WithArgs(t *testing.T) {
func Test_Mock_On_WithFuncArg(t *testing.T) { func Test_Mock_On_WithFuncArg(t *testing.T) {
// make a test impl object // make a test impl object
var mockedService *TestExampleImplementation = new(TestExampleImplementation) var mockedService = new(TestExampleImplementation)
c := mockedService. c := mockedService.
On("TheExampleMethodFunc", AnythingOfType("func(string) error")). On("TheExampleMethodFunc", AnythingOfType("func(string) error")).
@ -179,10 +179,10 @@ func Test_Mock_On_WithPtrArgMatcher(t *testing.T) {
mockedService.On("TheExampleMethod3", mockedService.On("TheExampleMethod3",
MatchedBy(func(a *ExampleType) bool { return a.ran == false }), MatchedBy(func(a *ExampleType) bool { return a.ran == false }),
).Return(errors.New("error!")) ).Return(errors.New("error"))
assert.Equal(t, mockedService.TheExampleMethod3(&ExampleType{true}), nil) assert.Equal(t, mockedService.TheExampleMethod3(&ExampleType{true}), nil)
assert.EqualError(t, mockedService.TheExampleMethod3(&ExampleType{false}), "error!") assert.EqualError(t, mockedService.TheExampleMethod3(&ExampleType{false}), "error")
} }
func Test_Mock_On_WithFuncArgMatcher(t *testing.T) { func Test_Mock_On_WithFuncArgMatcher(t *testing.T) {
@ -207,7 +207,7 @@ func Test_Mock_On_WithFuncArgMatcher(t *testing.T) {
func Test_Mock_On_WithVariadicFunc(t *testing.T) { func Test_Mock_On_WithVariadicFunc(t *testing.T) {
// make a test impl object // make a test impl object
var mockedService *TestExampleImplementation = new(TestExampleImplementation) var mockedService = new(TestExampleImplementation)
c := mockedService. c := mockedService.
On("TheExampleMethodVariadic", []int{1, 2, 3}). On("TheExampleMethodVariadic", []int{1, 2, 3}).
@ -229,7 +229,7 @@ func Test_Mock_On_WithVariadicFunc(t *testing.T) {
func Test_Mock_On_WithVariadicFuncWithInterface(t *testing.T) { func Test_Mock_On_WithVariadicFuncWithInterface(t *testing.T) {
// make a test impl object // make a test impl object
var mockedService *TestExampleImplementation = new(TestExampleImplementation) var mockedService = new(TestExampleImplementation)
c := mockedService.On("TheExampleMethodVariadicInterface", []interface{}{1, 2, 3}). c := mockedService.On("TheExampleMethodVariadicInterface", []interface{}{1, 2, 3}).
Return(nil) Return(nil)
@ -250,7 +250,7 @@ func Test_Mock_On_WithVariadicFuncWithInterface(t *testing.T) {
func Test_Mock_On_WithVariadicFuncWithEmptyInterfaceArray(t *testing.T) { func Test_Mock_On_WithVariadicFuncWithEmptyInterfaceArray(t *testing.T) {
// make a test impl object // make a test impl object
var mockedService *TestExampleImplementation = new(TestExampleImplementation) var mockedService = new(TestExampleImplementation)
var expected []interface{} var expected []interface{}
c := mockedService. c := mockedService.
@ -272,7 +272,7 @@ func Test_Mock_On_WithVariadicFuncWithEmptyInterfaceArray(t *testing.T) {
func Test_Mock_On_WithFuncPanics(t *testing.T) { func Test_Mock_On_WithFuncPanics(t *testing.T) {
// make a test impl object // make a test impl object
var mockedService *TestExampleImplementation = new(TestExampleImplementation) var mockedService = new(TestExampleImplementation)
assert.Panics(t, func() { assert.Panics(t, func() {
mockedService.On("TheExampleMethodFunc", func(string) error { return nil }) mockedService.On("TheExampleMethodFunc", func(string) error { return nil })
@ -282,7 +282,7 @@ func Test_Mock_On_WithFuncPanics(t *testing.T) {
func Test_Mock_On_WithFuncTypeArg(t *testing.T) { func Test_Mock_On_WithFuncTypeArg(t *testing.T) {
// make a test impl object // make a test impl object
var mockedService *TestExampleImplementation = new(TestExampleImplementation) var mockedService = new(TestExampleImplementation)
c := mockedService. c := mockedService.
On("TheExampleMethodFuncType", AnythingOfType("mock.ExampleFuncType")). On("TheExampleMethodFuncType", AnythingOfType("mock.ExampleFuncType")).
@ -301,7 +301,7 @@ func Test_Mock_On_WithFuncTypeArg(t *testing.T) {
func Test_Mock_Return(t *testing.T) { func Test_Mock_Return(t *testing.T) {
// make a test impl object // make a test impl object
var mockedService *TestExampleImplementation = new(TestExampleImplementation) var mockedService = new(TestExampleImplementation)
c := mockedService. c := mockedService.
On("TheExampleMethod", "A", "B", true). On("TheExampleMethod", "A", "B", true).
@ -325,7 +325,7 @@ func Test_Mock_Return(t *testing.T) {
func Test_Mock_Return_WaitUntil(t *testing.T) { func Test_Mock_Return_WaitUntil(t *testing.T) {
// make a test impl object // make a test impl object
var mockedService *TestExampleImplementation = new(TestExampleImplementation) var mockedService = new(TestExampleImplementation)
ch := time.After(time.Second) ch := time.After(time.Second)
c := mockedService.Mock. c := mockedService.Mock.
@ -352,7 +352,7 @@ func Test_Mock_Return_WaitUntil(t *testing.T) {
func Test_Mock_Return_After(t *testing.T) { func Test_Mock_Return_After(t *testing.T) {
// make a test impl object // make a test impl object
var mockedService *TestExampleImplementation = new(TestExampleImplementation) var mockedService = new(TestExampleImplementation)
c := mockedService.Mock. c := mockedService.Mock.
On("TheExampleMethod", "A", "B", true). On("TheExampleMethod", "A", "B", true).
@ -378,7 +378,7 @@ func Test_Mock_Return_After(t *testing.T) {
func Test_Mock_Return_Run(t *testing.T) { func Test_Mock_Return_Run(t *testing.T) {
// make a test impl object // make a test impl object
var mockedService *TestExampleImplementation = new(TestExampleImplementation) var mockedService = new(TestExampleImplementation)
fn := func(args Arguments) { fn := func(args Arguments) {
arg := args.Get(0).(*ExampleType) arg := args.Get(0).(*ExampleType)
@ -409,7 +409,7 @@ func Test_Mock_Return_Run(t *testing.T) {
func Test_Mock_Return_Run_Out_Of_Order(t *testing.T) { func Test_Mock_Return_Run_Out_Of_Order(t *testing.T) {
// make a test impl object // make a test impl object
var mockedService *TestExampleImplementation = new(TestExampleImplementation) var mockedService = new(TestExampleImplementation)
f := func(args Arguments) { f := func(args Arguments) {
arg := args.Get(0).(*ExampleType) arg := args.Get(0).(*ExampleType)
arg.ran = true arg.ran = true
@ -435,7 +435,7 @@ func Test_Mock_Return_Run_Out_Of_Order(t *testing.T) {
func Test_Mock_Return_Once(t *testing.T) { func Test_Mock_Return_Once(t *testing.T) {
// make a test impl object // make a test impl object
var mockedService *TestExampleImplementation = new(TestExampleImplementation) var mockedService = new(TestExampleImplementation)
c := mockedService.On("TheExampleMethod", "A", "B", true). c := mockedService.On("TheExampleMethod", "A", "B", true).
Return(1, "two", true). Return(1, "two", true).
@ -459,7 +459,7 @@ func Test_Mock_Return_Once(t *testing.T) {
func Test_Mock_Return_Twice(t *testing.T) { func Test_Mock_Return_Twice(t *testing.T) {
// make a test impl object // make a test impl object
var mockedService *TestExampleImplementation = new(TestExampleImplementation) var mockedService = new(TestExampleImplementation)
c := mockedService. c := mockedService.
On("TheExampleMethod", "A", "B", true). On("TheExampleMethod", "A", "B", true).
@ -484,7 +484,7 @@ func Test_Mock_Return_Twice(t *testing.T) {
func Test_Mock_Return_Times(t *testing.T) { func Test_Mock_Return_Times(t *testing.T) {
// make a test impl object // make a test impl object
var mockedService *TestExampleImplementation = new(TestExampleImplementation) var mockedService = new(TestExampleImplementation)
c := mockedService. c := mockedService.
On("TheExampleMethod", "A", "B", true). On("TheExampleMethod", "A", "B", true).
@ -509,7 +509,7 @@ func Test_Mock_Return_Times(t *testing.T) {
func Test_Mock_Return_Nothing(t *testing.T) { func Test_Mock_Return_Nothing(t *testing.T) {
// make a test impl object // make a test impl object
var mockedService *TestExampleImplementation = new(TestExampleImplementation) var mockedService = new(TestExampleImplementation)
c := mockedService. c := mockedService.
On("TheExampleMethod", "A", "B", true). On("TheExampleMethod", "A", "B", true).
@ -586,7 +586,7 @@ func Test_callString(t *testing.T) {
func Test_Mock_Called(t *testing.T) { func Test_Mock_Called(t *testing.T) {
var mockedService *TestExampleImplementation = new(TestExampleImplementation) var mockedService = new(TestExampleImplementation)
mockedService.On("Test_Mock_Called", 1, 2, 3).Return(5, "6", true) mockedService.On("Test_Mock_Called", 1, 2, 3).Return(5, "6", true)
@ -613,7 +613,7 @@ func asyncCall(m *Mock, ch chan Arguments) {
func Test_Mock_Called_blocks(t *testing.T) { func Test_Mock_Called_blocks(t *testing.T) {
var mockedService *TestExampleImplementation = new(TestExampleImplementation) var mockedService = new(TestExampleImplementation)
mockedService.Mock.On("asyncCall", 1, 2, 3).Return(5, "6", true).After(2 * time.Millisecond) mockedService.Mock.On("asyncCall", 1, 2, 3).Return(5, "6", true).After(2 * time.Millisecond)
@ -646,7 +646,7 @@ func Test_Mock_Called_blocks(t *testing.T) {
func Test_Mock_Called_For_Bounded_Repeatability(t *testing.T) { func Test_Mock_Called_For_Bounded_Repeatability(t *testing.T) {
var mockedService *TestExampleImplementation = new(TestExampleImplementation) var mockedService = new(TestExampleImplementation)
mockedService. mockedService.
On("Test_Mock_Called_For_Bounded_Repeatability", 1, 2, 3). On("Test_Mock_Called_For_Bounded_Repeatability", 1, 2, 3).
@ -687,7 +687,7 @@ func Test_Mock_Called_For_Bounded_Repeatability(t *testing.T) {
func Test_Mock_Called_For_SetTime_Expectation(t *testing.T) { func Test_Mock_Called_For_SetTime_Expectation(t *testing.T) {
var mockedService *TestExampleImplementation = new(TestExampleImplementation) var mockedService = new(TestExampleImplementation)
mockedService.On("TheExampleMethod", 1, 2, 3).Return(5, "6", true).Times(4) mockedService.On("TheExampleMethod", 1, 2, 3).Return(5, "6", true).Times(4)
@ -703,7 +703,7 @@ func Test_Mock_Called_For_SetTime_Expectation(t *testing.T) {
func Test_Mock_Called_Unexpected(t *testing.T) { func Test_Mock_Called_Unexpected(t *testing.T) {
var mockedService *TestExampleImplementation = new(TestExampleImplementation) var mockedService = new(TestExampleImplementation)
// make sure it panics if no expectation was made // make sure it panics if no expectation was made
assert.Panics(t, func() { assert.Panics(t, func() {
@ -714,9 +714,9 @@ func Test_Mock_Called_Unexpected(t *testing.T) {
func Test_AssertExpectationsForObjects_Helper(t *testing.T) { func Test_AssertExpectationsForObjects_Helper(t *testing.T) {
var mockedService1 *TestExampleImplementation = new(TestExampleImplementation) var mockedService1 = new(TestExampleImplementation)
var mockedService2 *TestExampleImplementation = new(TestExampleImplementation) var mockedService2 = new(TestExampleImplementation)
var mockedService3 *TestExampleImplementation = new(TestExampleImplementation) var mockedService3 = new(TestExampleImplementation)
mockedService1.On("Test_AssertExpectationsForObjects_Helper", 1).Return() mockedService1.On("Test_AssertExpectationsForObjects_Helper", 1).Return()
mockedService2.On("Test_AssertExpectationsForObjects_Helper", 2).Return() mockedService2.On("Test_AssertExpectationsForObjects_Helper", 2).Return()
@ -732,9 +732,9 @@ func Test_AssertExpectationsForObjects_Helper(t *testing.T) {
func Test_AssertExpectationsForObjects_Helper_Failed(t *testing.T) { func Test_AssertExpectationsForObjects_Helper_Failed(t *testing.T) {
var mockedService1 *TestExampleImplementation = new(TestExampleImplementation) var mockedService1 = new(TestExampleImplementation)
var mockedService2 *TestExampleImplementation = new(TestExampleImplementation) var mockedService2 = new(TestExampleImplementation)
var mockedService3 *TestExampleImplementation = new(TestExampleImplementation) var mockedService3 = new(TestExampleImplementation)
mockedService1.On("Test_AssertExpectationsForObjects_Helper_Failed", 1).Return() mockedService1.On("Test_AssertExpectationsForObjects_Helper_Failed", 1).Return()
mockedService2.On("Test_AssertExpectationsForObjects_Helper_Failed", 2).Return() mockedService2.On("Test_AssertExpectationsForObjects_Helper_Failed", 2).Return()
@ -750,7 +750,7 @@ func Test_AssertExpectationsForObjects_Helper_Failed(t *testing.T) {
func Test_Mock_AssertExpectations(t *testing.T) { func Test_Mock_AssertExpectations(t *testing.T) {
var mockedService *TestExampleImplementation = new(TestExampleImplementation) var mockedService = new(TestExampleImplementation)
mockedService.On("Test_Mock_AssertExpectations", 1, 2, 3).Return(5, 6, 7) mockedService.On("Test_Mock_AssertExpectations", 1, 2, 3).Return(5, 6, 7)
@ -767,7 +767,7 @@ func Test_Mock_AssertExpectations(t *testing.T) {
func Test_Mock_AssertExpectationsCustomType(t *testing.T) { func Test_Mock_AssertExpectationsCustomType(t *testing.T) {
var mockedService *TestExampleImplementation = new(TestExampleImplementation) var mockedService = new(TestExampleImplementation)
mockedService.On("TheExampleMethod3", AnythingOfType("*mock.ExampleType")).Return(nil).Once() mockedService.On("TheExampleMethod3", AnythingOfType("*mock.ExampleType")).Return(nil).Once()
@ -784,7 +784,7 @@ func Test_Mock_AssertExpectationsCustomType(t *testing.T) {
func Test_Mock_AssertExpectations_With_Repeatability(t *testing.T) { func Test_Mock_AssertExpectations_With_Repeatability(t *testing.T) {
var mockedService *TestExampleImplementation = new(TestExampleImplementation) var mockedService = new(TestExampleImplementation)
mockedService.On("Test_Mock_AssertExpectations_With_Repeatability", 1, 2, 3).Return(5, 6, 7).Twice() mockedService.On("Test_Mock_AssertExpectations_With_Repeatability", 1, 2, 3).Return(5, 6, 7).Twice()
@ -805,7 +805,7 @@ func Test_Mock_AssertExpectations_With_Repeatability(t *testing.T) {
func Test_Mock_TwoCallsWithDifferentArguments(t *testing.T) { func Test_Mock_TwoCallsWithDifferentArguments(t *testing.T) {
var mockedService *TestExampleImplementation = new(TestExampleImplementation) var mockedService = new(TestExampleImplementation)
mockedService.On("Test_Mock_TwoCallsWithDifferentArguments", 1, 2, 3).Return(5, 6, 7) mockedService.On("Test_Mock_TwoCallsWithDifferentArguments", 1, 2, 3).Return(5, 6, 7)
mockedService.On("Test_Mock_TwoCallsWithDifferentArguments", 4, 5, 6).Return(5, 6, 7) mockedService.On("Test_Mock_TwoCallsWithDifferentArguments", 4, 5, 6).Return(5, 6, 7)
@ -824,7 +824,7 @@ func Test_Mock_TwoCallsWithDifferentArguments(t *testing.T) {
func Test_Mock_AssertNumberOfCalls(t *testing.T) { func Test_Mock_AssertNumberOfCalls(t *testing.T) {
var mockedService *TestExampleImplementation = new(TestExampleImplementation) var mockedService = new(TestExampleImplementation)
mockedService.On("Test_Mock_AssertNumberOfCalls", 1, 2, 3).Return(5, 6, 7) mockedService.On("Test_Mock_AssertNumberOfCalls", 1, 2, 3).Return(5, 6, 7)
@ -838,7 +838,7 @@ func Test_Mock_AssertNumberOfCalls(t *testing.T) {
func Test_Mock_AssertCalled(t *testing.T) { func Test_Mock_AssertCalled(t *testing.T) {
var mockedService *TestExampleImplementation = new(TestExampleImplementation) var mockedService = new(TestExampleImplementation)
mockedService.On("Test_Mock_AssertCalled", 1, 2, 3).Return(5, 6, 7) mockedService.On("Test_Mock_AssertCalled", 1, 2, 3).Return(5, 6, 7)
@ -850,7 +850,7 @@ func Test_Mock_AssertCalled(t *testing.T) {
func Test_Mock_AssertCalled_WithAnythingOfTypeArgument(t *testing.T) { func Test_Mock_AssertCalled_WithAnythingOfTypeArgument(t *testing.T) {
var mockedService *TestExampleImplementation = new(TestExampleImplementation) var mockedService = new(TestExampleImplementation)
mockedService. mockedService.
On("Test_Mock_AssertCalled_WithAnythingOfTypeArgument", Anything, Anything, Anything). On("Test_Mock_AssertCalled_WithAnythingOfTypeArgument", Anything, Anything, Anything).
@ -864,7 +864,7 @@ func Test_Mock_AssertCalled_WithAnythingOfTypeArgument(t *testing.T) {
func Test_Mock_AssertCalled_WithArguments(t *testing.T) { func Test_Mock_AssertCalled_WithArguments(t *testing.T) {
var mockedService *TestExampleImplementation = new(TestExampleImplementation) var mockedService = new(TestExampleImplementation)
mockedService.On("Test_Mock_AssertCalled_WithArguments", 1, 2, 3).Return(5, 6, 7) mockedService.On("Test_Mock_AssertCalled_WithArguments", 1, 2, 3).Return(5, 6, 7)
@ -878,7 +878,7 @@ func Test_Mock_AssertCalled_WithArguments(t *testing.T) {
func Test_Mock_AssertCalled_WithArguments_With_Repeatability(t *testing.T) { func Test_Mock_AssertCalled_WithArguments_With_Repeatability(t *testing.T) {
var mockedService *TestExampleImplementation = new(TestExampleImplementation) var mockedService = new(TestExampleImplementation)
mockedService.On("Test_Mock_AssertCalled_WithArguments_With_Repeatability", 1, 2, 3).Return(5, 6, 7).Once() mockedService.On("Test_Mock_AssertCalled_WithArguments_With_Repeatability", 1, 2, 3).Return(5, 6, 7).Once()
mockedService.On("Test_Mock_AssertCalled_WithArguments_With_Repeatability", 2, 3, 4).Return(5, 6, 7).Once() mockedService.On("Test_Mock_AssertCalled_WithArguments_With_Repeatability", 2, 3, 4).Return(5, 6, 7).Once()
@ -895,7 +895,7 @@ func Test_Mock_AssertCalled_WithArguments_With_Repeatability(t *testing.T) {
func Test_Mock_AssertNotCalled(t *testing.T) { func Test_Mock_AssertNotCalled(t *testing.T) {
var mockedService *TestExampleImplementation = new(TestExampleImplementation) var mockedService = new(TestExampleImplementation)
mockedService.On("Test_Mock_AssertNotCalled", 1, 2, 3).Return(5, 6, 7) mockedService.On("Test_Mock_AssertNotCalled", 1, 2, 3).Return(5, 6, 7)
@ -910,7 +910,7 @@ func Test_Mock_AssertNotCalled(t *testing.T) {
*/ */
func Test_Arguments_Get(t *testing.T) { func Test_Arguments_Get(t *testing.T) {
var args Arguments = []interface{}{"string", 123, true} var args = Arguments([]interface{}{"string", 123, true})
assert.Equal(t, "string", args.Get(0).(string)) assert.Equal(t, "string", args.Get(0).(string))
assert.Equal(t, 123, args.Get(1).(int)) assert.Equal(t, 123, args.Get(1).(int))
@ -920,7 +920,7 @@ func Test_Arguments_Get(t *testing.T) {
func Test_Arguments_Is(t *testing.T) { func Test_Arguments_Is(t *testing.T) {
var args Arguments = []interface{}{"string", 123, true} var args = Arguments([]interface{}{"string", 123, true})
assert.True(t, args.Is("string", 123, true)) assert.True(t, args.Is("string", 123, true))
assert.False(t, args.Is("wrong", 456, false)) assert.False(t, args.Is("wrong", 456, false))
@ -929,7 +929,7 @@ func Test_Arguments_Is(t *testing.T) {
func Test_Arguments_Diff(t *testing.T) { func Test_Arguments_Diff(t *testing.T) {
var args Arguments = []interface{}{"Hello World", 123, true} var args = Arguments([]interface{}{"Hello World", 123, true})
var diff string var diff string
var count int var count int
diff, count = args.Diff([]interface{}{"Hello World", 456, "false"}) diff, count = args.Diff([]interface{}{"Hello World", 456, "false"})
@ -942,7 +942,7 @@ func Test_Arguments_Diff(t *testing.T) {
func Test_Arguments_Diff_DifferentNumberOfArgs(t *testing.T) { func Test_Arguments_Diff_DifferentNumberOfArgs(t *testing.T) {
var args Arguments = []interface{}{"string", 123, true} var args = Arguments([]interface{}{"string", 123, true})
var diff string var diff string
var count int var count int
diff, count = args.Diff([]interface{}{"string", 456, "false", "extra"}) diff, count = args.Diff([]interface{}{"string", 456, "false", "extra"})
@ -954,7 +954,7 @@ func Test_Arguments_Diff_DifferentNumberOfArgs(t *testing.T) {
func Test_Arguments_Diff_WithAnythingArgument(t *testing.T) { func Test_Arguments_Diff_WithAnythingArgument(t *testing.T) {
var args Arguments = []interface{}{"string", 123, true} var args = Arguments([]interface{}{"string", 123, true})
var count int var count int
_, count = args.Diff([]interface{}{"string", Anything, true}) _, count = args.Diff([]interface{}{"string", Anything, true})
@ -964,7 +964,7 @@ func Test_Arguments_Diff_WithAnythingArgument(t *testing.T) {
func Test_Arguments_Diff_WithAnythingArgument_InActualToo(t *testing.T) { func Test_Arguments_Diff_WithAnythingArgument_InActualToo(t *testing.T) {
var args Arguments = []interface{}{"string", Anything, true} var args = Arguments([]interface{}{"string", Anything, true})
var count int var count int
_, count = args.Diff([]interface{}{"string", 123, true}) _, count = args.Diff([]interface{}{"string", 123, true})
@ -974,7 +974,7 @@ func Test_Arguments_Diff_WithAnythingArgument_InActualToo(t *testing.T) {
func Test_Arguments_Diff_WithAnythingOfTypeArgument(t *testing.T) { func Test_Arguments_Diff_WithAnythingOfTypeArgument(t *testing.T) {
var args Arguments = []interface{}{"string", AnythingOfType("int"), true} var args = Arguments([]interface{}{"string", AnythingOfType("int"), true})
var count int var count int
_, count = args.Diff([]interface{}{"string", 123, true}) _, count = args.Diff([]interface{}{"string", 123, true})
@ -984,7 +984,7 @@ func Test_Arguments_Diff_WithAnythingOfTypeArgument(t *testing.T) {
func Test_Arguments_Diff_WithAnythingOfTypeArgument_Failing(t *testing.T) { func Test_Arguments_Diff_WithAnythingOfTypeArgument_Failing(t *testing.T) {
var args Arguments = []interface{}{"string", AnythingOfType("string"), true} var args = Arguments([]interface{}{"string", AnythingOfType("string"), true})
var count int var count int
var diff string var diff string
diff, count = args.Diff([]interface{}{"string", 123, true}) diff, count = args.Diff([]interface{}{"string", 123, true})
@ -998,7 +998,7 @@ func Test_Arguments_Diff_WithArgMatcher(t *testing.T) {
matchFn := func(a int) bool { matchFn := func(a int) bool {
return a == 123 return a == 123
} }
var args Arguments = []interface{}{"string", MatchedBy(matchFn), true} var args = Arguments([]interface{}{"string", MatchedBy(matchFn), true})
diff, count := args.Diff([]interface{}{"string", 124, true}) diff, count := args.Diff([]interface{}{"string", 124, true})
assert.Equal(t, 1, count) assert.Equal(t, 1, count)
@ -1018,7 +1018,7 @@ func Test_Arguments_Diff_WithArgMatcher(t *testing.T) {
func Test_Arguments_Assert(t *testing.T) { func Test_Arguments_Assert(t *testing.T) {
var args Arguments = []interface{}{"string", 123, true} var args = Arguments([]interface{}{"string", 123, true})
assert.True(t, args.Assert(t, "string", 123, true)) assert.True(t, args.Assert(t, "string", 123, true))
@ -1026,43 +1026,43 @@ func Test_Arguments_Assert(t *testing.T) {
func Test_Arguments_String_Representation(t *testing.T) { func Test_Arguments_String_Representation(t *testing.T) {
var args Arguments = []interface{}{"string", 123, true} var args = Arguments([]interface{}{"string", 123, true})
assert.Equal(t, `string,int,bool`, args.String()) assert.Equal(t, `string,int,bool`, args.String())
} }
func Test_Arguments_String(t *testing.T) { func Test_Arguments_String(t *testing.T) {
var args Arguments = []interface{}{"string", 123, true} var args = Arguments([]interface{}{"string", 123, true})
assert.Equal(t, "string", args.String(0)) assert.Equal(t, "string", args.String(0))
} }
func Test_Arguments_Error(t *testing.T) { func Test_Arguments_Error(t *testing.T) {
var err error = errors.New("An Error") var err = errors.New("An Error")
var args Arguments = []interface{}{"string", 123, true, err} var args = Arguments([]interface{}{"string", 123, true, err})
assert.Equal(t, err, args.Error(3)) assert.Equal(t, err, args.Error(3))
} }
func Test_Arguments_Error_Nil(t *testing.T) { func Test_Arguments_Error_Nil(t *testing.T) {
var args Arguments = []interface{}{"string", 123, true, nil} var args = Arguments([]interface{}{"string", 123, true, nil})
assert.Equal(t, nil, args.Error(3)) assert.Equal(t, nil, args.Error(3))
} }
func Test_Arguments_Int(t *testing.T) { func Test_Arguments_Int(t *testing.T) {
var args Arguments = []interface{}{"string", 123, true} var args = Arguments([]interface{}{"string", 123, true})
assert.Equal(t, 123, args.Int(1)) assert.Equal(t, 123, args.Int(1))
} }
func Test_Arguments_Bool(t *testing.T) { func Test_Arguments_Bool(t *testing.T) {
var args Arguments = []interface{}{"string", 123, true} var args = Arguments([]interface{}{"string", 123, true})
assert.Equal(t, true, args.Bool(2)) assert.Equal(t, true, args.Bool(2))
} }

View File

@ -1,4 +1,5 @@
// Alternative testing tools which stop test execution if test failed. // Package require implements the same assertions as the `assert` package but
// stops test execution when a test fails.
// //
// Example Usage // Example Usage
// //

View File

@ -1,9 +1,12 @@
package require package require
// Assertions provides assertion methods around the
// TestingT interface.
type Assertions struct { type Assertions struct {
t TestingT t TestingT
} }
// New makes a new Assertions object for the specified TestingT.
func New(t TestingT) *Assertions { func New(t TestingT) *Assertions {
return &Assertions{ return &Assertions{
t: t, t: t,

View File

@ -1,5 +1,6 @@
package require package require
// TestingT is an interface wrapper around *testing.T
type TestingT interface { type TestingT interface {
Errorf(format string, args ...interface{}) Errorf(format string, args ...interface{})
FailNow() FailNow()

View File

@ -1,4 +1,4 @@
// The suite package contains logic for creating testing suite structs // Package suite contains logic for creating testing suite structs
// and running the methods on those structs as tests. The most useful // and running the methods on those structs as tests. The most useful
// piece of this package is that you can create setup/teardown methods // piece of this package is that you can create setup/teardown methods
// on your testing suites, which will run before/after the whole suite // on your testing suites, which will run before/after the whole suite