Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions assert/assertion_compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,9 @@ func compare(obj1, obj2 interface{}, kind reflect.Kind) (CompareType, bool) {
// assert.Greater(t, float64(2), float64(1))
// assert.Greater(t, "b", "a")
func Greater(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
}
return compareTwoValues(t, e1, e2, []CompareType{compareGreater}, "\"%v\" is not greater than \"%v\"", msgAndArgs)
}

Expand All @@ -320,6 +323,9 @@ func Greater(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface
// assert.GreaterOrEqual(t, "b", "a")
// assert.GreaterOrEqual(t, "b", "b")
func GreaterOrEqual(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
}
return compareTwoValues(t, e1, e2, []CompareType{compareGreater, compareEqual}, "\"%v\" is not greater than or equal to \"%v\"", msgAndArgs)
}

Expand All @@ -329,6 +335,9 @@ func GreaterOrEqual(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...in
// assert.Less(t, float64(1), float64(2))
// assert.Less(t, "a", "b")
func Less(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
}
return compareTwoValues(t, e1, e2, []CompareType{compareLess}, "\"%v\" is not less than \"%v\"", msgAndArgs)
}

Expand All @@ -339,6 +348,9 @@ func Less(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{})
// assert.LessOrEqual(t, "a", "b")
// assert.LessOrEqual(t, "b", "b")
func LessOrEqual(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
}
return compareTwoValues(t, e1, e2, []CompareType{compareLess, compareEqual}, "\"%v\" is not less than or equal to \"%v\"", msgAndArgs)
}

Expand All @@ -347,6 +359,9 @@ func LessOrEqual(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...inter
// assert.Positive(t, 1)
// assert.Positive(t, 1.23)
func Positive(t TestingT, e interface{}, msgAndArgs ...interface{}) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
}
zero := reflect.Zero(reflect.TypeOf(e))
return compareTwoValues(t, e, zero.Interface(), []CompareType{compareGreater}, "\"%v\" is not positive", msgAndArgs)
}
Expand All @@ -356,6 +371,9 @@ func Positive(t TestingT, e interface{}, msgAndArgs ...interface{}) bool {
// assert.Negative(t, -1)
// assert.Negative(t, -1.23)
func Negative(t TestingT, e interface{}, msgAndArgs ...interface{}) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
}
zero := reflect.Zero(reflect.TypeOf(e))
return compareTwoValues(t, e, zero.Interface(), []CompareType{compareLess}, "\"%v\" is not negative", msgAndArgs)
}
Expand Down
31 changes: 30 additions & 1 deletion assert/assertion_compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"reflect"
"runtime"
"testing"
)

Expand Down Expand Up @@ -82,7 +83,8 @@ func TestCompare(t *testing.T) {
}

type outputT struct {
buf *bytes.Buffer
buf *bytes.Buffer
helpers map[string]struct{}
}

// Implements TestingT
Expand All @@ -91,6 +93,27 @@ func (t *outputT) Errorf(format string, args ...interface{}) {
t.buf.WriteString(s)
}

func (t *outputT) Helper() {
if t.helpers == nil {
t.helpers = make(map[string]struct{})
}
t.helpers[callerName(1)] = struct{}{}
}

// callerName gives the function name (qualified with a package path)
// for the caller after skip frames (where 0 means the current function).
func callerName(skip int) string {
// Make room for the skip PC.
var pc [1]uintptr
n := runtime.Callers(skip+2, pc[:]) // skip + runtime.Callers + callerName
if n == 0 {
panic("testing: zero callers found")
}
frames := runtime.CallersFrames(pc[:n])
frame, _ := frames.Next()
return frame.Function
}

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

Expand Down Expand Up @@ -128,6 +151,7 @@ func TestGreater(t *testing.T) {
out := &outputT{buf: bytes.NewBuffer(nil)}
False(t, Greater(out, currCase.less, currCase.greater))
Contains(t, string(out.buf.Bytes()), currCase.msg)
Contains(t, out.helpers, "github.com/stretchr/testify/assert.Greater")
}
}

Expand Down Expand Up @@ -168,6 +192,7 @@ func TestGreaterOrEqual(t *testing.T) {
out := &outputT{buf: bytes.NewBuffer(nil)}
False(t, GreaterOrEqual(out, currCase.less, currCase.greater))
Contains(t, string(out.buf.Bytes()), currCase.msg)
Contains(t, out.helpers, "github.com/stretchr/testify/assert.GreaterOrEqual")
}
}

Expand Down Expand Up @@ -208,6 +233,7 @@ func TestLess(t *testing.T) {
out := &outputT{buf: bytes.NewBuffer(nil)}
False(t, Less(out, currCase.greater, currCase.less))
Contains(t, string(out.buf.Bytes()), currCase.msg)
Contains(t, out.helpers, "github.com/stretchr/testify/assert.Less")
}
}

Expand Down Expand Up @@ -248,6 +274,7 @@ func TestLessOrEqual(t *testing.T) {
out := &outputT{buf: bytes.NewBuffer(nil)}
False(t, LessOrEqual(out, currCase.greater, currCase.less))
Contains(t, string(out.buf.Bytes()), currCase.msg)
Contains(t, out.helpers, "github.com/stretchr/testify/assert.LessOrEqual")
}
}

Expand Down Expand Up @@ -286,6 +313,7 @@ func TestPositive(t *testing.T) {
out := &outputT{buf: bytes.NewBuffer(nil)}
False(t, Positive(out, currCase.e))
Contains(t, string(out.buf.Bytes()), currCase.msg)
Contains(t, out.helpers, "github.com/stretchr/testify/assert.Positive")
}
}

Expand Down Expand Up @@ -324,6 +352,7 @@ func TestNegative(t *testing.T) {
out := &outputT{buf: bytes.NewBuffer(nil)}
False(t, Negative(out, currCase.e))
Contains(t, string(out.buf.Bytes()), currCase.msg)
Contains(t, out.helpers, "github.com/stretchr/testify/assert.Negative")
}
}

Expand Down