Skip to content

Commit

Permalink
add assert positive/negative
Browse files Browse the repository at this point in the history
  • Loading branch information
rinchsan authored and boyan-soubachov committed Nov 3, 2020
1 parent 54d05a4 commit 1544508
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
18 changes: 18 additions & 0 deletions assert/assertion_compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,24 @@ func LessOrEqual(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...inter
return compareTwoValues(t, e1, e2, []CompareType{compareLess, compareEqual}, "\"%v\" is not less than or equal to \"%v\"", msgAndArgs)
}

// Positive asserts that the specified element is positive
//
// assert.Positive(t, 1)
// assert.Positive(t, 1.23)
func Positive(t TestingT, e interface{}, msgAndArgs ...interface{}) bool {
zero := reflect.Zero(reflect.TypeOf(e))
return compareTwoValues(t, e, zero.Interface(), []CompareType{compareGreater}, "\"%v\" is not positive", msgAndArgs)
}

// Negative asserts that the specified element is negative
//
// assert.Negative(t, -1)
// assert.Negative(t, -1.23)
func Negative(t TestingT, e interface{}, msgAndArgs ...interface{}) bool {
zero := reflect.Zero(reflect.TypeOf(e))
return compareTwoValues(t, e, zero.Interface(), []CompareType{compareLess}, "\"%v\" is not negative", msgAndArgs)
}

func compareTwoValues(t TestingT, e1 interface{}, e2 interface{}, allowedComparesResults []CompareType, failMessage string, msgAndArgs ...interface{}) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
Expand Down
76 changes: 76 additions & 0 deletions assert/assertion_compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,82 @@ func TestLessOrEqual(t *testing.T) {
}
}

func TestPositive(t *testing.T) {
mockT := new(testing.T)

if !Positive(mockT, 1) {
t.Error("Positive should return true")
}

if !Positive(mockT, 1.23) {
t.Error("Positive should return true")
}

if Positive(mockT, -1) {
t.Error("Positive should return false")
}

if Positive(mockT, -1.23) {
t.Error("Positive should return false")
}

// Check error report
for _, currCase := range []struct {
e interface{}
msg string
}{
{e: int(-1), msg: `"-1" is not positive`},
{e: int8(-1), msg: `"-1" is not positive`},
{e: int16(-1), msg: `"-1" is not positive`},
{e: int32(-1), msg: `"-1" is not positive`},
{e: int64(-1), msg: `"-1" is not positive`},
{e: float32(-1.23), msg: `"-1.23" is not positive`},
{e: float64(-1.23), msg: `"-1.23" is not positive`},
} {
out := &outputT{buf: bytes.NewBuffer(nil)}
False(t, Positive(out, currCase.e))
Contains(t, string(out.buf.Bytes()), currCase.msg)
}
}

func TestNegative(t *testing.T) {
mockT := new(testing.T)

if !Negative(mockT, -1) {
t.Error("Negative should return true")
}

if !Negative(mockT, -1.23) {
t.Error("Negative should return true")
}

if Negative(mockT, 1) {
t.Error("Negative should return false")
}

if Negative(mockT, 1.23) {
t.Error("Negative should return false")
}

// Check error report
for _, currCase := range []struct {
e interface{}
msg string
}{
{e: int(1), msg: `"1" is not negative`},
{e: int8(1), msg: `"1" is not negative`},
{e: int16(1), msg: `"1" is not negative`},
{e: int32(1), msg: `"1" is not negative`},
{e: int64(1), msg: `"1" is not negative`},
{e: float32(1.23), msg: `"1.23" is not negative`},
{e: float64(1.23), msg: `"1.23" is not negative`},
} {
out := &outputT{buf: bytes.NewBuffer(nil)}
False(t, Negative(out, currCase.e))
Contains(t, string(out.buf.Bytes()), currCase.msg)
}
}

func Test_compareTwoValuesDifferentValuesTypes(t *testing.T) {
mockT := new(testing.T)

Expand Down

0 comments on commit 1544508

Please sign in to comment.