From ee686e1a7e971f4d4d9c2f745e08d0c921eb1993 Mon Sep 17 00:00:00 2001 From: Yongxin Wang Date: Fri, 20 Nov 2020 10:05:27 +0800 Subject: [PATCH 1/2] fix the output source file in test output --- assert/assertion_compare.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/assert/assertion_compare.go b/assert/assertion_compare.go index 41649d267..aff1c4b80 100644 --- a/assert/assertion_compare.go +++ b/assert/assertion_compare.go @@ -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) } @@ -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) } @@ -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) } @@ -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) } @@ -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) } @@ -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) } From cf265c0e6b05c7dd864394a2c7333d91a9edeabc Mon Sep 17 00:00:00 2001 From: Yongxin Wang Date: Fri, 15 Jan 2021 09:45:35 +0800 Subject: [PATCH 2/2] add test to check that Helper is called --- assert/assertion_compare_test.go | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/assert/assertion_compare_test.go b/assert/assertion_compare_test.go index b4631a489..da5a33033 100644 --- a/assert/assertion_compare_test.go +++ b/assert/assertion_compare_test.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" "reflect" + "runtime" "testing" ) @@ -82,7 +83,8 @@ func TestCompare(t *testing.T) { } type outputT struct { - buf *bytes.Buffer + buf *bytes.Buffer + helpers map[string]struct{} } // Implements TestingT @@ -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) @@ -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") } } @@ -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") } } @@ -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") } } @@ -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") } } @@ -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") } } @@ -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") } }