Skip to content

Commit 7c6d829

Browse files
Merge branch 'master' into ObjectsExportedFieldsAreEqual_refactor
2 parents fc8b99a + 882382d commit 7c6d829

File tree

4 files changed

+42
-28
lines changed

4 files changed

+42
-28
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ func TestSomething(t *testing.T) {
159159
// create an instance of our test object
160160
testObj := new(MyMockedObject)
161161

162-
// setup expectations
162+
// set up expectations
163163
testObj.On("DoSomething", 123).Return(true, nil)
164164

165165
// call the code we are testing
@@ -181,7 +181,7 @@ func TestSomethingWithPlaceholder(t *testing.T) {
181181
// create an instance of our test object
182182
testObj := new(MyMockedObject)
183183

184-
// setup expectations with a placeholder in the argument list
184+
// set up expectations with a placeholder in the argument list
185185
testObj.On("DoSomething", mock.Anything).Return(true, nil)
186186

187187
// call the code we are testing
@@ -200,7 +200,7 @@ func TestSomethingElse2(t *testing.T) {
200200
// create an instance of our test object
201201
testObj := new(MyMockedObject)
202202

203-
// setup expectations with a placeholder in the argument list
203+
// set up expectations with a placeholder in the argument list
204204
mockCall := testObj.On("DoSomething", mock.Anything).Return(true, nil)
205205

206206
// call the code we are testing

assert/assertions.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -725,16 +725,14 @@ func NotEmpty(t TestingT, object interface{}, msgAndArgs ...interface{}) bool {
725725

726726
}
727727

728-
// getLen try to get length of object.
729-
// return (false, 0) if impossible.
730-
func getLen(x interface{}) (ok bool, length int) {
728+
// getLen tries to get the length of an object.
729+
// It returns (0, false) if impossible.
730+
func getLen(x interface{}) (length int, ok bool) {
731731
v := reflect.ValueOf(x)
732732
defer func() {
733-
if e := recover(); e != nil {
734-
ok = false
735-
}
733+
ok = recover() == nil
736734
}()
737-
return true, v.Len()
735+
return v.Len(), true
738736
}
739737

740738
// Len asserts that the specified object has specific length.
@@ -745,7 +743,7 @@ func Len(t TestingT, object interface{}, length int, msgAndArgs ...interface{})
745743
if h, ok := t.(tHelper); ok {
746744
h.Helper()
747745
}
748-
ok, l := getLen(object)
746+
l, ok := getLen(object)
749747
if !ok {
750748
return Fail(t, fmt.Sprintf("\"%s\" could not be applied builtin len()", object), msgAndArgs...)
751749
}

assert/assertions_test.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1585,7 +1585,7 @@ func Test_getLen(t *testing.T) {
15851585
struct{}{},
15861586
}
15871587
for _, v := range falseCases {
1588-
ok, l := getLen(v)
1588+
l, ok := getLen(v)
15891589
False(t, ok, "Expected getLen fail to get length of %#v", v)
15901590
Equal(t, 0, l, "getLen should return 0 for %#v", v)
15911591
}
@@ -1614,7 +1614,7 @@ func Test_getLen(t *testing.T) {
16141614
}
16151615

16161616
for _, c := range trueCases {
1617-
ok, l := getLen(c.v)
1617+
l, ok := getLen(c.v)
16181618
True(t, ok, "Expected getLen success to get length of %#v", c.v)
16191619
Equal(t, c.l, l)
16201620
}
@@ -2797,14 +2797,20 @@ func TestNeverFalse(t *testing.T) {
27972797
True(t, Never(t, condition, 100*time.Millisecond, 20*time.Millisecond))
27982798
}
27992799

2800+
// TestNeverTrue checks Never with a condition that returns true on second call.
28002801
func TestNeverTrue(t *testing.T) {
28012802
mockT := new(testing.T)
2802-
state := 0
2803+
2804+
// A list of values returned by condition.
2805+
// Channel protects against concurrent access.
2806+
returns := make(chan bool, 2)
2807+
returns <- false
2808+
returns <- true
2809+
defer close(returns)
2810+
2811+
// Will return true on second call.
28032812
condition := func() bool {
2804-
defer func() {
2805-
state = state + 1
2806-
}()
2807-
return state == 2
2813+
return <-returns
28082814
}
28092815

28102816
False(t, Never(mockT, condition, 100*time.Millisecond, 20*time.Millisecond))

mock/mock.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ import (
1818
"github.com/stretchr/testify/assert"
1919
)
2020

21+
// regex for GCCGO functions
22+
var gccgoRE = regexp.MustCompile(`\.pN\d+_`)
23+
2124
// TestingT is an interface wrapper around *testing.T
2225
type TestingT interface {
2326
Logf(format string, args ...interface{})
@@ -455,9 +458,8 @@ func (m *Mock) Called(arguments ...interface{}) Arguments {
455458
// For Ex: github_com_docker_libkv_store_mock.WatchTree.pN39_github_com_docker_libkv_store_mock.Mock
456459
// uses interface information unlike golang github.com/docker/libkv/store/mock.(*Mock).WatchTree
457460
// With GCCGO we need to remove interface information starting from pN<dd>.
458-
re := regexp.MustCompile("\\.pN\\d+_")
459-
if re.MatchString(functionPath) {
460-
functionPath = re.Split(functionPath, -1)[0]
461+
if gccgoRE.MatchString(functionPath) {
462+
functionPath = gccgoRE.Split(functionPath, -1)[0]
461463
}
462464
parts := strings.Split(functionPath, ".")
463465
functionName := parts[len(parts)-1]
@@ -758,18 +760,26 @@ const (
758760
Anything = "mock.Anything"
759761
)
760762

761-
// AnythingOfTypeArgument is a string that contains the type of an argument
763+
// AnythingOfTypeArgument contains the type of an argument
764+
// for use when type checking. Used in Diff and Assert.
765+
//
766+
// Deprecated: this is an implementation detail that must not be used. Use [AnythingOfType] instead.
767+
type AnythingOfTypeArgument = anythingOfTypeArgument
768+
769+
// anythingOfTypeArgument is a string that contains the type of an argument
762770
// for use when type checking. Used in Diff and Assert.
763-
type AnythingOfTypeArgument string
771+
type anythingOfTypeArgument string
764772

765-
// AnythingOfType returns an AnythingOfTypeArgument object containing the
766-
// name of the type to check for. Used in Diff and Assert.
773+
// AnythingOfType returns a special value containing the
774+
// name of the type to check for. The type name will be matched against the type name returned by [reflect.Type.String].
775+
//
776+
// Used in Diff and Assert.
767777
//
768778
// For example:
769779
//
770780
// Assert(t, AnythingOfType("string"), AnythingOfType("int"))
771781
func AnythingOfType(t string) AnythingOfTypeArgument {
772-
return AnythingOfTypeArgument(t)
782+
return anythingOfTypeArgument(t)
773783
}
774784

775785
// IsTypeArgument is a struct that contains the type of an argument
@@ -950,9 +960,9 @@ func (args Arguments) Diff(objects []interface{}) (string, int) {
950960
differences++
951961
output = fmt.Sprintf("%s\t%d: FAIL: %s not matched by %s\n", output, i, actualFmt, matcher)
952962
}
953-
} else if reflect.TypeOf(expected) == reflect.TypeOf((*AnythingOfTypeArgument)(nil)).Elem() {
963+
} else if reflect.TypeOf(expected) == reflect.TypeOf((*anythingOfTypeArgument)(nil)).Elem() {
954964
// type checking
955-
if reflect.TypeOf(actual).Name() != string(expected.(AnythingOfTypeArgument)) && reflect.TypeOf(actual).String() != string(expected.(AnythingOfTypeArgument)) {
965+
if reflect.TypeOf(actual).Name() != string(expected.(anythingOfTypeArgument)) && reflect.TypeOf(actual).String() != string(expected.(anythingOfTypeArgument)) {
956966
// not match
957967
differences++
958968
output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, expected, reflect.TypeOf(actual).Name(), actualFmt)

0 commit comments

Comments
 (0)