Skip to content

Commit 1544508

Browse files
rinchsanboyan-soubachov
authored andcommitted
add assert positive/negative
1 parent 54d05a4 commit 1544508

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

assert/assertion_compare.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,24 @@ func LessOrEqual(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...inter
342342
return compareTwoValues(t, e1, e2, []CompareType{compareLess, compareEqual}, "\"%v\" is not less than or equal to \"%v\"", msgAndArgs)
343343
}
344344

345+
// Positive asserts that the specified element is positive
346+
//
347+
// assert.Positive(t, 1)
348+
// assert.Positive(t, 1.23)
349+
func Positive(t TestingT, e interface{}, msgAndArgs ...interface{}) bool {
350+
zero := reflect.Zero(reflect.TypeOf(e))
351+
return compareTwoValues(t, e, zero.Interface(), []CompareType{compareGreater}, "\"%v\" is not positive", msgAndArgs)
352+
}
353+
354+
// Negative asserts that the specified element is negative
355+
//
356+
// assert.Negative(t, -1)
357+
// assert.Negative(t, -1.23)
358+
func Negative(t TestingT, e interface{}, msgAndArgs ...interface{}) bool {
359+
zero := reflect.Zero(reflect.TypeOf(e))
360+
return compareTwoValues(t, e, zero.Interface(), []CompareType{compareLess}, "\"%v\" is not negative", msgAndArgs)
361+
}
362+
345363
func compareTwoValues(t TestingT, e1 interface{}, e2 interface{}, allowedComparesResults []CompareType, failMessage string, msgAndArgs ...interface{}) bool {
346364
if h, ok := t.(tHelper); ok {
347365
h.Helper()

assert/assertion_compare_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,82 @@ func TestLessOrEqual(t *testing.T) {
251251
}
252252
}
253253

254+
func TestPositive(t *testing.T) {
255+
mockT := new(testing.T)
256+
257+
if !Positive(mockT, 1) {
258+
t.Error("Positive should return true")
259+
}
260+
261+
if !Positive(mockT, 1.23) {
262+
t.Error("Positive should return true")
263+
}
264+
265+
if Positive(mockT, -1) {
266+
t.Error("Positive should return false")
267+
}
268+
269+
if Positive(mockT, -1.23) {
270+
t.Error("Positive should return false")
271+
}
272+
273+
// Check error report
274+
for _, currCase := range []struct {
275+
e interface{}
276+
msg string
277+
}{
278+
{e: int(-1), msg: `"-1" is not positive`},
279+
{e: int8(-1), msg: `"-1" is not positive`},
280+
{e: int16(-1), msg: `"-1" is not positive`},
281+
{e: int32(-1), msg: `"-1" is not positive`},
282+
{e: int64(-1), msg: `"-1" is not positive`},
283+
{e: float32(-1.23), msg: `"-1.23" is not positive`},
284+
{e: float64(-1.23), msg: `"-1.23" is not positive`},
285+
} {
286+
out := &outputT{buf: bytes.NewBuffer(nil)}
287+
False(t, Positive(out, currCase.e))
288+
Contains(t, string(out.buf.Bytes()), currCase.msg)
289+
}
290+
}
291+
292+
func TestNegative(t *testing.T) {
293+
mockT := new(testing.T)
294+
295+
if !Negative(mockT, -1) {
296+
t.Error("Negative should return true")
297+
}
298+
299+
if !Negative(mockT, -1.23) {
300+
t.Error("Negative should return true")
301+
}
302+
303+
if Negative(mockT, 1) {
304+
t.Error("Negative should return false")
305+
}
306+
307+
if Negative(mockT, 1.23) {
308+
t.Error("Negative should return false")
309+
}
310+
311+
// Check error report
312+
for _, currCase := range []struct {
313+
e interface{}
314+
msg string
315+
}{
316+
{e: int(1), msg: `"1" is not negative`},
317+
{e: int8(1), msg: `"1" is not negative`},
318+
{e: int16(1), msg: `"1" is not negative`},
319+
{e: int32(1), msg: `"1" is not negative`},
320+
{e: int64(1), msg: `"1" is not negative`},
321+
{e: float32(1.23), msg: `"1.23" is not negative`},
322+
{e: float64(1.23), msg: `"1.23" is not negative`},
323+
} {
324+
out := &outputT{buf: bytes.NewBuffer(nil)}
325+
False(t, Negative(out, currCase.e))
326+
Contains(t, string(out.buf.Bytes()), currCase.msg)
327+
}
328+
}
329+
254330
func Test_compareTwoValuesDifferentValuesTypes(t *testing.T) {
255331
mockT := new(testing.T)
256332

0 commit comments

Comments
 (0)