add test to check that Helper is called

pull/1041/head
Yongxin Wang 2021-01-15 09:45:35 +08:00 committed by Boyan Soubachov
parent 6f81fdf1db
commit bf646ea5b3
1 changed files with 30 additions and 1 deletions

View File

@ -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")
}
}