Skip to content

Commit

Permalink
assert: reverse return values of internal getLen()
Browse files Browse the repository at this point in the history
Reverse order of return values of internal function getLen() to be more
consistent with stdlib (ex: os.LookupEnv).

Old: func (any) (ok bool, length int)
New: func (any) (length int, ok bool)
  • Loading branch information
dolmen committed Jul 30, 2023
1 parent 06dba72 commit 4ecbb63
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
10 changes: 5 additions & 5 deletions assert/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -731,14 +731,14 @@ func NotEmpty(t TestingT, object interface{}, msgAndArgs ...interface{}) bool {

}

// getLen try to get length of object.
// return (false, 0) if impossible.
func getLen(x interface{}) (ok bool, length int) {
// getLen tries to get the length of an object.
// It returns (0, false) if impossible.
func getLen(x interface{}) (length int, ok bool) {
v := reflect.ValueOf(x)
defer func() {
ok = recover() == nil
}()
return true, v.Len()
return v.Len(), true
}

// Len asserts that the specified object has specific length.
Expand All @@ -749,7 +749,7 @@ func Len(t TestingT, object interface{}, length int, msgAndArgs ...interface{})
if h, ok := t.(tHelper); ok {
h.Helper()
}
ok, l := getLen(object)
l, ok := getLen(object)
if !ok {
return Fail(t, fmt.Sprintf("\"%s\" could not be applied builtin len()", object), msgAndArgs...)
}
Expand Down
4 changes: 2 additions & 2 deletions assert/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1582,7 +1582,7 @@ func Test_getLen(t *testing.T) {
struct{}{},
}
for _, v := range falseCases {
ok, l := getLen(v)
l, ok := getLen(v)
False(t, ok, "Expected getLen fail to get length of %#v", v)
Equal(t, 0, l, "getLen should return 0 for %#v", v)
}
Expand Down Expand Up @@ -1611,7 +1611,7 @@ func Test_getLen(t *testing.T) {
}

for _, c := range trueCases {
ok, l := getLen(c.v)
l, ok := getLen(c.v)
True(t, ok, "Expected getLen success to get length of %#v", c.v)
Equal(t, c.l, l)
}
Expand Down

0 comments on commit 4ecbb63

Please sign in to comment.