mirror of https://github.com/stretchr/testify.git
Added Go 1.18.1 as a build/supported version (#1182)
* Added Go 1.18.1 as a build/supported version Removed Go 1.15.13 as a build version as it's no longer supported * Fix mutex passed by value for the Mock struct * Add mutex initialisation for Mock Co-authored-by: Boyan Soubachov <bsoubachov@atlassian.com>pull/1163/head
parent
e2b56b3a38
commit
3c33e07c4c
|
@ -6,7 +6,7 @@ jobs:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
go_version: ["1.17.8", "1.16.15", "1.15.15"]
|
go_version: ["1.18.1", "1.17.6", "1.16.5"]
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v2
|
||||||
- name: Setup Go
|
- name: Setup Go
|
||||||
|
|
24
mock/mock.go
24
mock/mock.go
|
@ -218,7 +218,7 @@ type Mock struct {
|
||||||
// this data completely allowing you to do whatever you like with it.
|
// this data completely allowing you to do whatever you like with it.
|
||||||
testData objx.Map
|
testData objx.Map
|
||||||
|
|
||||||
mutex sync.Mutex
|
mutex *sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
// String provides a %v format string for Mock.
|
// String provides a %v format string for Mock.
|
||||||
|
@ -232,7 +232,6 @@ func (m *Mock) String() string {
|
||||||
// TestData holds any data that might be useful for testing. Testify ignores
|
// TestData holds any data that might be useful for testing. Testify ignores
|
||||||
// this data completely allowing you to do whatever you like with it.
|
// this data completely allowing you to do whatever you like with it.
|
||||||
func (m *Mock) TestData() objx.Map {
|
func (m *Mock) TestData() objx.Map {
|
||||||
|
|
||||||
if m.testData == nil {
|
if m.testData == nil {
|
||||||
m.testData = make(objx.Map)
|
m.testData = make(objx.Map)
|
||||||
}
|
}
|
||||||
|
@ -246,6 +245,10 @@ func (m *Mock) TestData() objx.Map {
|
||||||
|
|
||||||
// Test sets the test struct variable of the mock object
|
// Test sets the test struct variable of the mock object
|
||||||
func (m *Mock) Test(t TestingT) {
|
func (m *Mock) Test(t TestingT) {
|
||||||
|
if m.mutex == nil {
|
||||||
|
m.mutex = &sync.Mutex{}
|
||||||
|
}
|
||||||
|
|
||||||
m.mutex.Lock()
|
m.mutex.Lock()
|
||||||
defer m.mutex.Unlock()
|
defer m.mutex.Unlock()
|
||||||
m.test = t
|
m.test = t
|
||||||
|
@ -276,6 +279,9 @@ func (m *Mock) On(methodName string, arguments ...interface{}) *Call {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Since we start mocks with the .On() function, m.mutex should be reset
|
||||||
|
m.mutex = &sync.Mutex{}
|
||||||
|
|
||||||
m.mutex.Lock()
|
m.mutex.Lock()
|
||||||
defer m.mutex.Unlock()
|
defer m.mutex.Unlock()
|
||||||
c := newCall(m, methodName, assert.CallerInfo(), arguments...)
|
c := newCall(m, methodName, assert.CallerInfo(), arguments...)
|
||||||
|
@ -354,7 +360,6 @@ func (m *Mock) findClosestCall(method string, arguments ...interface{}) (*Call,
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
|
@ -783,10 +788,10 @@ func (args Arguments) Is(objects ...interface{}) bool {
|
||||||
func (args Arguments) Diff(objects []interface{}) (string, int) {
|
func (args Arguments) Diff(objects []interface{}) (string, int) {
|
||||||
// TODO: could return string as error and nil for No difference
|
// TODO: could return string as error and nil for No difference
|
||||||
|
|
||||||
var output = "\n"
|
output := "\n"
|
||||||
var differences int
|
var differences int
|
||||||
|
|
||||||
var maxArgCount = len(args)
|
maxArgCount := len(args)
|
||||||
if len(objects) > maxArgCount {
|
if len(objects) > maxArgCount {
|
||||||
maxArgCount = len(objects)
|
maxArgCount = len(objects)
|
||||||
}
|
}
|
||||||
|
@ -819,14 +824,12 @@ func (args Arguments) Diff(objects []interface{}) (string, int) {
|
||||||
output = fmt.Sprintf("%s\t%d: FAIL: %s not matched by %s\n", output, i, actualFmt, matcher)
|
output = fmt.Sprintf("%s\t%d: FAIL: %s not matched by %s\n", output, i, actualFmt, matcher)
|
||||||
}
|
}
|
||||||
} else if reflect.TypeOf(expected) == reflect.TypeOf((*AnythingOfTypeArgument)(nil)).Elem() {
|
} else if reflect.TypeOf(expected) == reflect.TypeOf((*AnythingOfTypeArgument)(nil)).Elem() {
|
||||||
|
|
||||||
// type checking
|
// type checking
|
||||||
if reflect.TypeOf(actual).Name() != string(expected.(AnythingOfTypeArgument)) && reflect.TypeOf(actual).String() != string(expected.(AnythingOfTypeArgument)) {
|
if reflect.TypeOf(actual).Name() != string(expected.(AnythingOfTypeArgument)) && reflect.TypeOf(actual).String() != string(expected.(AnythingOfTypeArgument)) {
|
||||||
// not match
|
// not match
|
||||||
differences++
|
differences++
|
||||||
output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, expected, reflect.TypeOf(actual).Name(), actualFmt)
|
output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, expected, reflect.TypeOf(actual).Name(), actualFmt)
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if reflect.TypeOf(expected) == reflect.TypeOf((*IsTypeArgument)(nil)) {
|
} else if reflect.TypeOf(expected) == reflect.TypeOf((*IsTypeArgument)(nil)) {
|
||||||
t := expected.(*IsTypeArgument).t
|
t := expected.(*IsTypeArgument).t
|
||||||
if reflect.TypeOf(t) != reflect.TypeOf(actual) {
|
if reflect.TypeOf(t) != reflect.TypeOf(actual) {
|
||||||
|
@ -834,7 +837,6 @@ func (args Arguments) Diff(objects []interface{}) (string, int) {
|
||||||
output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, reflect.TypeOf(t).Name(), reflect.TypeOf(actual).Name(), actualFmt)
|
output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, reflect.TypeOf(t).Name(), reflect.TypeOf(actual).Name(), actualFmt)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
// normal checking
|
// normal checking
|
||||||
|
|
||||||
if assert.ObjectsAreEqual(expected, Anything) || assert.ObjectsAreEqual(actual, Anything) || assert.ObjectsAreEqual(actual, expected) {
|
if assert.ObjectsAreEqual(expected, Anything) || assert.ObjectsAreEqual(actual, Anything) || assert.ObjectsAreEqual(actual, expected) {
|
||||||
|
@ -854,7 +856,6 @@ func (args Arguments) Diff(objects []interface{}) (string, int) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return output, differences
|
return output, differences
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Assert compares the arguments with the specified objects and fails if
|
// Assert compares the arguments with the specified objects and fails if
|
||||||
|
@ -876,7 +877,6 @@ func (args Arguments) Assert(t TestingT, objects ...interface{}) bool {
|
||||||
t.Errorf("%sArguments do not match.", assert.CallerInfo())
|
t.Errorf("%sArguments do not match.", assert.CallerInfo())
|
||||||
|
|
||||||
return false
|
return false
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// String gets the argument at the specified index. Panics if there is no argument, or
|
// String gets the argument at the specified index. Panics if there is no argument, or
|
||||||
|
@ -885,7 +885,6 @@ func (args Arguments) Assert(t TestingT, objects ...interface{}) bool {
|
||||||
// If no index is provided, String() returns a complete string representation
|
// If no index is provided, String() returns a complete string representation
|
||||||
// of the arguments.
|
// of the arguments.
|
||||||
func (args Arguments) String(indexOrNil ...int) string {
|
func (args Arguments) String(indexOrNil ...int) string {
|
||||||
|
|
||||||
if len(indexOrNil) == 0 {
|
if len(indexOrNil) == 0 {
|
||||||
// normal String() method - return a string representation of the args
|
// normal String() method - return a string representation of the args
|
||||||
var argsStr []string
|
var argsStr []string
|
||||||
|
@ -895,7 +894,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 = indexOrNil[0]
|
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 {
|
||||||
|
@ -905,7 +904,6 @@ func (args Arguments) String(indexOrNil ...int) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
panic(fmt.Sprintf("assert: arguments: Wrong number of arguments passed to String. Must be 0 or 1, not %d", len(indexOrNil)))
|
panic(fmt.Sprintf("assert: arguments: Wrong number of arguments passed to String. Must be 0 or 1, not %d", len(indexOrNil)))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Int gets the argument at the specified index. Panics if there is no argument, or
|
// Int gets the argument at the specified index. Panics if there is no argument, or
|
||||||
|
|
Loading…
Reference in New Issue