Skip to content

Commit f03d037

Browse files
Merge pull request #1787 from brackendawson/1419-fail-assertions
assert: IsIncreasing et al can return false w/out failing
2 parents b5a0821 + 65e0b94 commit f03d037

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed

assert/assertion_order.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
func isOrdered(t TestingT, object interface{}, allowedComparesResults []compareResult, failMessage string, msgAndArgs ...interface{}) bool {
1010
objKind := reflect.TypeOf(object).Kind()
1111
if objKind != reflect.Slice && objKind != reflect.Array {
12-
return false
12+
return Fail(t, fmt.Sprintf("object %T is not an ordered collection", object), msgAndArgs...)
1313
}
1414

1515
objValue := reflect.ValueOf(object)
@@ -50,6 +50,9 @@ func isOrdered(t TestingT, object interface{}, allowedComparesResults []compareR
5050
// assert.IsIncreasing(t, []float{1, 2})
5151
// assert.IsIncreasing(t, []string{"a", "b"})
5252
func IsIncreasing(t TestingT, object interface{}, msgAndArgs ...interface{}) bool {
53+
if h, ok := t.(tHelper); ok {
54+
h.Helper()
55+
}
5356
return isOrdered(t, object, []compareResult{compareLess}, "\"%v\" is not less than \"%v\"", msgAndArgs...)
5457
}
5558

@@ -59,6 +62,9 @@ func IsIncreasing(t TestingT, object interface{}, msgAndArgs ...interface{}) boo
5962
// assert.IsNonIncreasing(t, []float{2, 1})
6063
// assert.IsNonIncreasing(t, []string{"b", "a"})
6164
func IsNonIncreasing(t TestingT, object interface{}, msgAndArgs ...interface{}) bool {
65+
if h, ok := t.(tHelper); ok {
66+
h.Helper()
67+
}
6268
return isOrdered(t, object, []compareResult{compareEqual, compareGreater}, "\"%v\" is not greater than or equal to \"%v\"", msgAndArgs...)
6369
}
6470

@@ -68,6 +74,9 @@ func IsNonIncreasing(t TestingT, object interface{}, msgAndArgs ...interface{})
6874
// assert.IsDecreasing(t, []float{2, 1})
6975
// assert.IsDecreasing(t, []string{"b", "a"})
7076
func IsDecreasing(t TestingT, object interface{}, msgAndArgs ...interface{}) bool {
77+
if h, ok := t.(tHelper); ok {
78+
h.Helper()
79+
}
7180
return isOrdered(t, object, []compareResult{compareGreater}, "\"%v\" is not greater than \"%v\"", msgAndArgs...)
7281
}
7382

@@ -77,5 +86,8 @@ func IsDecreasing(t TestingT, object interface{}, msgAndArgs ...interface{}) boo
7786
// assert.IsNonDecreasing(t, []float{1, 2})
7887
// assert.IsNonDecreasing(t, []string{"a", "b"})
7988
func IsNonDecreasing(t TestingT, object interface{}, msgAndArgs ...interface{}) bool {
89+
if h, ok := t.(tHelper); ok {
90+
h.Helper()
91+
}
8092
return isOrdered(t, object, []compareResult{compareLess, compareEqual}, "\"%v\" is not less than or equal to \"%v\"", msgAndArgs...)
8193
}

assert/assertion_order_test.go

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package assert
22

33
import (
44
"bytes"
5+
"fmt"
56
"testing"
67
)
78

@@ -45,10 +46,13 @@ func TestIsIncreasing(t *testing.T) {
4546
{collection: []uint64{2, 1}, msg: `"2" is not less than "1"`},
4647
{collection: []float32{2.34, 1.23}, msg: `"2.34" is not less than "1.23"`},
4748
{collection: []float64{2.34, 1.23}, msg: `"2.34" is not less than "1.23"`},
49+
{collection: struct{}{}, msg: `object struct {} is not an ordered collection`},
4850
} {
49-
out := &outputT{buf: bytes.NewBuffer(nil)}
50-
False(t, IsIncreasing(out, currCase.collection))
51-
Contains(t, out.buf.String(), currCase.msg)
51+
t.Run(fmt.Sprintf("%#v", currCase.collection), func(t *testing.T) {
52+
out := &outputT{buf: bytes.NewBuffer(nil)}
53+
False(t, IsIncreasing(out, currCase.collection))
54+
Contains(t, out.buf.String(), currCase.msg)
55+
})
5256
}
5357
}
5458

@@ -92,10 +96,13 @@ func TestIsNonIncreasing(t *testing.T) {
9296
{collection: []uint64{1, 2}, msg: `"1" is not greater than or equal to "2"`},
9397
{collection: []float32{1.23, 2.34}, msg: `"1.23" is not greater than or equal to "2.34"`},
9498
{collection: []float64{1.23, 2.34}, msg: `"1.23" is not greater than or equal to "2.34"`},
99+
{collection: struct{}{}, msg: `object struct {} is not an ordered collection`},
95100
} {
96-
out := &outputT{buf: bytes.NewBuffer(nil)}
97-
False(t, IsNonIncreasing(out, currCase.collection))
98-
Contains(t, out.buf.String(), currCase.msg)
101+
t.Run(fmt.Sprintf("%#v", currCase.collection), func(t *testing.T) {
102+
out := &outputT{buf: bytes.NewBuffer(nil)}
103+
False(t, IsNonIncreasing(out, currCase.collection))
104+
Contains(t, out.buf.String(), currCase.msg)
105+
})
99106
}
100107
}
101108

@@ -139,10 +146,13 @@ func TestIsDecreasing(t *testing.T) {
139146
{collection: []uint64{1, 2}, msg: `"1" is not greater than "2"`},
140147
{collection: []float32{1.23, 2.34}, msg: `"1.23" is not greater than "2.34"`},
141148
{collection: []float64{1.23, 2.34}, msg: `"1.23" is not greater than "2.34"`},
149+
{collection: struct{}{}, msg: `object struct {} is not an ordered collection`},
142150
} {
143-
out := &outputT{buf: bytes.NewBuffer(nil)}
144-
False(t, IsDecreasing(out, currCase.collection))
145-
Contains(t, out.buf.String(), currCase.msg)
151+
t.Run(fmt.Sprintf("%#v", currCase.collection), func(t *testing.T) {
152+
out := &outputT{buf: bytes.NewBuffer(nil)}
153+
False(t, IsDecreasing(out, currCase.collection))
154+
Contains(t, out.buf.String(), currCase.msg)
155+
})
146156
}
147157
}
148158

@@ -186,10 +196,13 @@ func TestIsNonDecreasing(t *testing.T) {
186196
{collection: []uint64{2, 1}, msg: `"2" is not less than or equal to "1"`},
187197
{collection: []float32{2.34, 1.23}, msg: `"2.34" is not less than or equal to "1.23"`},
188198
{collection: []float64{2.34, 1.23}, msg: `"2.34" is not less than or equal to "1.23"`},
199+
{collection: struct{}{}, msg: `object struct {} is not an ordered collection`},
189200
} {
190-
out := &outputT{buf: bytes.NewBuffer(nil)}
191-
False(t, IsNonDecreasing(out, currCase.collection))
192-
Contains(t, out.buf.String(), currCase.msg)
201+
t.Run(fmt.Sprintf("%#v", currCase.collection), func(t *testing.T) {
202+
out := &outputT{buf: bytes.NewBuffer(nil)}
203+
False(t, IsNonDecreasing(out, currCase.collection))
204+
Contains(t, out.buf.String(), currCase.msg)
205+
})
193206
}
194207
}
195208

0 commit comments

Comments
 (0)