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 {
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)
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
// 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)
importer := imports.New(*outputPkg)
funcs := make([]Func, 0)
var funcs []testFunc
// Go through all the top level functions
for _, fdocs := range docs.Funcs {
// Find the function
@ -151,7 +151,7 @@ func analyzeCode(scope *types.Scope, docs *doc.Package) (imports.Importer, []Fun
continue
}
funcs = append(funcs, Func{*outputPkg, fdocs, fn})
funcs = append(funcs, testFunc{*outputPkg, fdocs, fn})
importer.AddImportsFrom(sig.Params())
}
return importer, funcs, nil
@ -199,20 +199,20 @@ func parsePackageSource(pkg string) (*types.Scope, *doc.Package, error) {
return scope, docs, nil
}
type Func struct {
type testFunc struct {
CurrentPkg string
DocInfo *doc.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 {
return ""
}
return p.Name()
}
func (f *Func) Params() string {
func (f *testFunc) Params() string {
sig := f.TypeInfo.Type().(*types.Signature)
params := sig.Params()
p := ""
@ -235,7 +235,7 @@ func (f *Func) Params() string {
return p
}
func (f *Func) ForwardedParams() string {
func (f *testFunc) ForwardedParams() string {
sig := f.TypeInfo.Type().(*types.Signature)
params := sig.Params()
p := ""
@ -258,11 +258,11 @@ func (f *Func) ForwardedParams() string {
return p
}
func (f *Func) Comment() string {
func (f *testFunc) Comment() string {
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)
replace := fmt.Sprintf("%s.%s(", receiver, f.DocInfo.Name)
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
//

View File

@ -65,64 +65,67 @@ func newCall(parent *Mock, methodName string, methodArguments ...interface{}) *C
}
}
func (self *Call) lock() {
self.Parent.mutex.Lock()
func (c *Call) lock() {
c.Parent.mutex.Lock()
}
func (self *Call) unlock() {
self.Parent.mutex.Unlock()
func (c *Call) unlock() {
c.Parent.mutex.Unlock()
}
func (self *Call) Return(returnArguments ...interface{}) *Call {
self.lock()
defer self.unlock()
// Return specifies the return arguments for the expectation.
//
// 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.
//
// Mock.On("MyMethod", arg1, arg2).Return(returnArg1, returnArg2).Once()
func (self *Call) Once() *Call {
return self.Times(1)
func (c *Call) Once() *Call {
return c.Times(1)
}
// Twice indicates that that the mock should only return the value twice.
//
// Mock.On("MyMethod", arg1, arg2).Return(returnArg1, returnArg2).Twice()
func (self *Call) Twice() *Call {
return self.Times(2)
func (c *Call) Twice() *Call {
return c.Times(2)
}
// Times indicates that that the mock should only return the indicated number
// of times.
//
// Mock.On("MyMethod", arg1, arg2).Return(returnArg1, returnArg2).Times(5)
func (self *Call) Times(i int) *Call {
self.lock()
defer self.unlock()
self.Repeatability = i
return self
func (c *Call) Times(i int) *Call {
c.lock()
defer c.unlock()
c.Repeatability = i
return c
}
// WaitUntil sets the channel that will block the mock's return until its closed
// or a message is received.
//
// Mock.On("MyMethod", arg1, arg2).WaitUntil(time.After(time.Second))
func (self *Call) WaitUntil(w <-chan time.Time) *Call {
self.lock()
defer self.unlock()
self.WaitFor = w
return self
func (c *Call) WaitUntil(w <-chan time.Time) *Call {
c.lock()
defer c.unlock()
c.WaitFor = w
return c
}
// After sets how long to block until the call returns
//
// Mock.On("MyMethod", arg1, arg2).After(time.Second)
func (self *Call) After(d time.Duration) *Call {
return self.WaitUntil(time.After(d))
func (c *Call) After(d time.Duration) *Call {
return c.WaitUntil(time.After(d))
}
// 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["foo"] = "bar"
// })
func (self *Call) Run(fn func(Arguments)) *Call {
self.lock()
defer self.unlock()
self.RunFn = fn
return self
func (c *Call) Run(fn func(Arguments)) *Call {
c.lock()
defer c.unlock()
c.RunFn = fn
return c
}
// On chains a new expectation description onto the mocked interface. This
@ -146,8 +149,8 @@ func (self *Call) Run(fn func(Arguments)) *Call {
// Mock.
// On("MyMethod", 1).Return(nil).
// On("MyOtherMethod", 'a', 'b', 'c').Return(errors.New("Some Error"))
func (self *Call) On(methodName string, arguments ...interface{}) *Call {
return self.Parent.On(methodName, arguments...)
func (c *Call) On(methodName string, arguments ...interface{}) *Call {
return c.Parent.On(methodName, arguments...)
}
// Mock is the workhorse used to track activity on another object.
@ -187,17 +190,17 @@ func (m *Mock) TestData() objx.Map {
// being called.
//
// 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 {
if v := reflect.ValueOf(arg); v.Kind() == reflect.Func {
panic(fmt.Sprintf("cannot use Func in expectations. Use mock.AnythingOfType(\"%T\")", arg))
}
}
self.mutex.Lock()
defer self.mutex.Unlock()
c := newCall(self, methodName, arguments...)
self.ExpectedCalls = append(self.ExpectedCalls, c)
m.mutex.Lock()
defer m.mutex.Unlock()
c := newCall(m, methodName, arguments...)
m.ExpectedCalls = append(m.ExpectedCalls, 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) {
diffCount := 0
var closestCall *Call = nil
var closestCall *Call
for _, call := range m.expectedCalls() {
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 {
var argValsString string = ""
var argValsString string
if includeArgumentValues {
var argVals []string
for argIndex, arg := range arguments {
@ -304,7 +307,7 @@ func (m *Mock) Called(arguments ...interface{}) Arguments {
call.Repeatability = -1
case call.Repeatability > 1:
call.Repeatability -= 1
call.Repeatability--
}
m.mutex.Unlock()
}
@ -335,7 +338,7 @@ func (m *Mock) Called(arguments ...interface{}) Arguments {
//
// Calls may have occurred in any order.
func AssertExpectationsForObjects(t TestingT, testObjects ...interface{}) bool {
var success bool = true
var success = true
for _, obj := range testObjects {
mockObj := obj.(Mock)
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
// in fact called as expected. Calls may have occurred in any order.
func (m *Mock) AssertExpectations(t TestingT) bool {
var somethingMissing bool = false
var failedExpectations int = 0
var somethingMissing bool
var failedExpectations int
// iterate through each expectation
expectedCalls := m.expectedCalls()
@ -377,7 +380,7 @@ func (m *Mock) AssertExpectations(t TestingT) bool {
// AssertNumberOfCalls asserts that the method was called expectedCalls times.
func (m *Mock) AssertNumberOfCalls(t TestingT, methodName string, expectedCalls int) bool {
var actualCalls int = 0
var actualCalls int
for _, call := range m.calls() {
if call.Method == methodName {
actualCalls++
@ -441,8 +444,8 @@ func (m *Mock) calls() []Call {
type Arguments []interface{}
const (
// The "any" argument. Used in Diff and Assert when
// the argument being tested shouldn't be taken into consideration.
// Anything is used in Diff and Assert when the argument being tested
// shouldn't be taken into consideration.
Anything string = "mock.Anything"
)
@ -472,9 +475,8 @@ func (f argumentMatcher) Matches(argument interface{}) bool {
if reflect.TypeOf(argument).AssignableTo(expectType) {
result := f.fn.Call([]reflect.Value{reflect.ValueOf(argument)})
return result[0].Bool()
} else {
return false
}
return false
}
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.
func (args Arguments) Diff(objects []interface{}) (string, int) {
var output string = "\n"
var output = "\n"
var differences int
var maxArgCount int = len(args)
var maxArgCount = len(args)
if len(objects) > maxArgCount {
maxArgCount = len(objects)
}
@ -630,7 +632,7 @@ func (args Arguments) String(indexOrNil ...int) string {
return strings.Join(argsStr, ",")
} else if len(indexOrNil) == 1 {
// Index has been specified - get the argument at that index
var index int = indexOrNil[0]
var index = indexOrNil[0]
var s string
var ok bool
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) {
var mockedService *TestExampleImplementation = new(TestExampleImplementation)
var mockedService = new(TestExampleImplementation)
if assert.NotNil(t, mockedService.TestData()) {
@ -80,7 +80,7 @@ func Test_Mock_TestData(t *testing.T) {
func Test_Mock_On(t *testing.T) {
// make a test impl object
var mockedService *TestExampleImplementation = new(TestExampleImplementation)
var mockedService = new(TestExampleImplementation)
c := mockedService.On("TheExampleMethod")
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) {
// make a test impl object
var mockedService *TestExampleImplementation = new(TestExampleImplementation)
var mockedService = new(TestExampleImplementation)
mockedService.
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) {
// make a test impl object
var mockedService *TestExampleImplementation = new(TestExampleImplementation)
var mockedService = new(TestExampleImplementation)
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) {
// make a test impl object
var mockedService *TestExampleImplementation = new(TestExampleImplementation)
var mockedService = new(TestExampleImplementation)
c := mockedService.
On("TheExampleMethodFunc", AnythingOfType("func(string) error")).
@ -179,10 +179,10 @@ func Test_Mock_On_WithPtrArgMatcher(t *testing.T) {
mockedService.On("TheExampleMethod3",
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.EqualError(t, mockedService.TheExampleMethod3(&ExampleType{false}), "error!")
assert.EqualError(t, mockedService.TheExampleMethod3(&ExampleType{false}), "error")
}
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) {
// make a test impl object
var mockedService *TestExampleImplementation = new(TestExampleImplementation)
var mockedService = new(TestExampleImplementation)
c := mockedService.
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) {
// make a test impl object
var mockedService *TestExampleImplementation = new(TestExampleImplementation)
var mockedService = new(TestExampleImplementation)
c := mockedService.On("TheExampleMethodVariadicInterface", []interface{}{1, 2, 3}).
Return(nil)
@ -250,7 +250,7 @@ func Test_Mock_On_WithVariadicFuncWithInterface(t *testing.T) {
func Test_Mock_On_WithVariadicFuncWithEmptyInterfaceArray(t *testing.T) {
// make a test impl object
var mockedService *TestExampleImplementation = new(TestExampleImplementation)
var mockedService = new(TestExampleImplementation)
var expected []interface{}
c := mockedService.
@ -272,7 +272,7 @@ func Test_Mock_On_WithVariadicFuncWithEmptyInterfaceArray(t *testing.T) {
func Test_Mock_On_WithFuncPanics(t *testing.T) {
// make a test impl object
var mockedService *TestExampleImplementation = new(TestExampleImplementation)
var mockedService = new(TestExampleImplementation)
assert.Panics(t, func() {
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) {
// make a test impl object
var mockedService *TestExampleImplementation = new(TestExampleImplementation)
var mockedService = new(TestExampleImplementation)
c := mockedService.
On("TheExampleMethodFuncType", AnythingOfType("mock.ExampleFuncType")).
@ -301,7 +301,7 @@ func Test_Mock_On_WithFuncTypeArg(t *testing.T) {
func Test_Mock_Return(t *testing.T) {
// make a test impl object
var mockedService *TestExampleImplementation = new(TestExampleImplementation)
var mockedService = new(TestExampleImplementation)
c := mockedService.
On("TheExampleMethod", "A", "B", true).
@ -325,7 +325,7 @@ func Test_Mock_Return(t *testing.T) {
func Test_Mock_Return_WaitUntil(t *testing.T) {
// make a test impl object
var mockedService *TestExampleImplementation = new(TestExampleImplementation)
var mockedService = new(TestExampleImplementation)
ch := time.After(time.Second)
c := mockedService.Mock.
@ -352,7 +352,7 @@ func Test_Mock_Return_WaitUntil(t *testing.T) {
func Test_Mock_Return_After(t *testing.T) {
// make a test impl object
var mockedService *TestExampleImplementation = new(TestExampleImplementation)
var mockedService = new(TestExampleImplementation)
c := mockedService.Mock.
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) {
// make a test impl object
var mockedService *TestExampleImplementation = new(TestExampleImplementation)
var mockedService = new(TestExampleImplementation)
fn := func(args Arguments) {
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) {
// make a test impl object
var mockedService *TestExampleImplementation = new(TestExampleImplementation)
var mockedService = new(TestExampleImplementation)
f := func(args Arguments) {
arg := args.Get(0).(*ExampleType)
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) {
// make a test impl object
var mockedService *TestExampleImplementation = new(TestExampleImplementation)
var mockedService = new(TestExampleImplementation)
c := mockedService.On("TheExampleMethod", "A", "B", 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) {
// make a test impl object
var mockedService *TestExampleImplementation = new(TestExampleImplementation)
var mockedService = new(TestExampleImplementation)
c := mockedService.
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) {
// make a test impl object
var mockedService *TestExampleImplementation = new(TestExampleImplementation)
var mockedService = new(TestExampleImplementation)
c := mockedService.
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) {
// make a test impl object
var mockedService *TestExampleImplementation = new(TestExampleImplementation)
var mockedService = new(TestExampleImplementation)
c := mockedService.
On("TheExampleMethod", "A", "B", true).
@ -586,7 +586,7 @@ func Test_callString(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)
@ -613,7 +613,7 @@ func asyncCall(m *Mock, ch chan Arguments) {
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)
@ -646,7 +646,7 @@ func Test_Mock_Called_blocks(t *testing.T) {
func Test_Mock_Called_For_Bounded_Repeatability(t *testing.T) {
var mockedService *TestExampleImplementation = new(TestExampleImplementation)
var mockedService = new(TestExampleImplementation)
mockedService.
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) {
var mockedService *TestExampleImplementation = new(TestExampleImplementation)
var mockedService = new(TestExampleImplementation)
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) {
var mockedService *TestExampleImplementation = new(TestExampleImplementation)
var mockedService = new(TestExampleImplementation)
// make sure it panics if no expectation was made
assert.Panics(t, func() {
@ -714,9 +714,9 @@ func Test_Mock_Called_Unexpected(t *testing.T) {
func Test_AssertExpectationsForObjects_Helper(t *testing.T) {
var mockedService1 *TestExampleImplementation = new(TestExampleImplementation)
var mockedService2 *TestExampleImplementation = new(TestExampleImplementation)
var mockedService3 *TestExampleImplementation = new(TestExampleImplementation)
var mockedService1 = new(TestExampleImplementation)
var mockedService2 = new(TestExampleImplementation)
var mockedService3 = new(TestExampleImplementation)
mockedService1.On("Test_AssertExpectationsForObjects_Helper", 1).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) {
var mockedService1 *TestExampleImplementation = new(TestExampleImplementation)
var mockedService2 *TestExampleImplementation = new(TestExampleImplementation)
var mockedService3 *TestExampleImplementation = new(TestExampleImplementation)
var mockedService1 = new(TestExampleImplementation)
var mockedService2 = new(TestExampleImplementation)
var mockedService3 = new(TestExampleImplementation)
mockedService1.On("Test_AssertExpectationsForObjects_Helper_Failed", 1).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) {
var mockedService *TestExampleImplementation = new(TestExampleImplementation)
var mockedService = new(TestExampleImplementation)
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) {
var mockedService *TestExampleImplementation = new(TestExampleImplementation)
var mockedService = new(TestExampleImplementation)
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) {
var mockedService *TestExampleImplementation = new(TestExampleImplementation)
var mockedService = new(TestExampleImplementation)
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) {
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", 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) {
var mockedService *TestExampleImplementation = new(TestExampleImplementation)
var mockedService = new(TestExampleImplementation)
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) {
var mockedService *TestExampleImplementation = new(TestExampleImplementation)
var mockedService = new(TestExampleImplementation)
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) {
var mockedService *TestExampleImplementation = new(TestExampleImplementation)
var mockedService = new(TestExampleImplementation)
mockedService.
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) {
var mockedService *TestExampleImplementation = new(TestExampleImplementation)
var mockedService = new(TestExampleImplementation)
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) {
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", 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) {
var mockedService *TestExampleImplementation = new(TestExampleImplementation)
var mockedService = new(TestExampleImplementation)
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) {
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, 123, args.Get(1).(int))
@ -920,7 +920,7 @@ func Test_Arguments_Get(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.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) {
var args Arguments = []interface{}{"Hello World", 123, true}
var args = Arguments([]interface{}{"Hello World", 123, true})
var diff string
var count int
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) {
var args Arguments = []interface{}{"string", 123, true}
var args = Arguments([]interface{}{"string", 123, true})
var diff string
var count int
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) {
var args Arguments = []interface{}{"string", 123, true}
var args = Arguments([]interface{}{"string", 123, true})
var count int
_, 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) {
var args Arguments = []interface{}{"string", Anything, true}
var args = Arguments([]interface{}{"string", Anything, true})
var count int
_, 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) {
var args Arguments = []interface{}{"string", AnythingOfType("int"), true}
var args = Arguments([]interface{}{"string", AnythingOfType("int"), true})
var count int
_, 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) {
var args Arguments = []interface{}{"string", AnythingOfType("string"), true}
var args = Arguments([]interface{}{"string", AnythingOfType("string"), true})
var count int
var diff string
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 {
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})
assert.Equal(t, 1, count)
@ -1018,7 +1018,7 @@ func Test_Arguments_Diff_WithArgMatcher(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))
@ -1026,43 +1026,43 @@ func Test_Arguments_Assert(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())
}
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))
}
func Test_Arguments_Error(t *testing.T) {
var err error = errors.New("An Error")
var args Arguments = []interface{}{"string", 123, true, err}
var err = errors.New("An Error")
var args = Arguments([]interface{}{"string", 123, true, err})
assert.Equal(t, err, args.Error(3))
}
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))
}
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))
}
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))
}

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
//

View File

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

View File

@ -1,5 +1,6 @@
package require
// TestingT is an interface wrapper around *testing.T
type TestingT interface {
Errorf(format string, args ...interface{})
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
// piece of this package is that you can create setup/teardown methods
// on your testing suites, which will run before/after the whole suite