mirror of https://github.com/stretchr/testify.git
Merge remote-tracking branch 'origin/master' into allow-error-string-from-custom-matcher
commit
30f794c7de
27
.travis.yml
27
.travis.yml
|
@ -2,15 +2,18 @@ language: go
|
|||
|
||||
sudo: false
|
||||
|
||||
go:
|
||||
- "1.8.x"
|
||||
- "1.9.x"
|
||||
- "1.10.x"
|
||||
- "1.11.x"
|
||||
- tip
|
||||
|
||||
script:
|
||||
- ./.travis.gogenerate.sh
|
||||
- ./.travis.gofmt.sh
|
||||
- ./.travis.govet.sh
|
||||
- go test -v -race $(go list ./... | grep -v vendor)
|
||||
matrix:
|
||||
include:
|
||||
- go: "1.8.x"
|
||||
- go: "1.9.x"
|
||||
- go: "1.10.x"
|
||||
- go: "1.11.x"
|
||||
env: GO111MODULE=off
|
||||
- go: "1.11.x"
|
||||
env: GO111MODULE=on
|
||||
- go: tip
|
||||
script:
|
||||
- ./.travis.gogenerate.sh
|
||||
- ./.travis.gofmt.sh
|
||||
- ./.travis.govet.sh
|
||||
- go test -v -race $(go list ./... | grep -v vendor)
|
||||
|
|
|
@ -287,8 +287,10 @@ To install Testify, use `go get`:
|
|||
This will then make the following packages available to you:
|
||||
|
||||
github.com/stretchr/testify/assert
|
||||
github.com/stretchr/testify/require
|
||||
github.com/stretchr/testify/mock
|
||||
github.com/stretchr/testify/http
|
||||
github.com/stretchr/testify/suite
|
||||
github.com/stretchr/testify/http (deprecated)
|
||||
|
||||
Import the `testify/assert` package into your code using this template:
|
||||
|
||||
|
|
|
@ -157,6 +157,31 @@ func FileExistsf(t TestingT, path string, msg string, args ...interface{}) bool
|
|||
return FileExists(t, path, append([]interface{}{msg}, args...)...)
|
||||
}
|
||||
|
||||
// Greaterf asserts that the first element is greater than the second
|
||||
//
|
||||
// assert.Greaterf(t, 2, 1, "error message %s", "formatted")
|
||||
// assert.Greaterf(t, float64(2, "error message %s", "formatted"), float64(1))
|
||||
// assert.Greaterf(t, "b", "a", "error message %s", "formatted")
|
||||
func Greaterf(t TestingT, e1 interface{}, e2 interface{}, msg string, args ...interface{}) bool {
|
||||
if h, ok := t.(tHelper); ok {
|
||||
h.Helper()
|
||||
}
|
||||
return Greater(t, e1, e2, append([]interface{}{msg}, args...)...)
|
||||
}
|
||||
|
||||
// GreaterOrEqualf asserts that the first element in greater or equal than the second
|
||||
//
|
||||
// assert.GreaterOrEqualf(t, 2, 1, "error message %s", "formatted")
|
||||
// assert.GreaterOrEqualf(t, 2, 2, "error message %s", "formatted")
|
||||
// assert.GreaterOrEqualf(t, "b", "a", "error message %s", "formatted")
|
||||
// assert.GreaterOrEqualf(t, "b", "b", "error message %s", "formatted")
|
||||
func GreaterOrEqualf(t TestingT, e1 interface{}, e2 interface{}, msg string, args ...interface{}) bool {
|
||||
if h, ok := t.(tHelper); ok {
|
||||
h.Helper()
|
||||
}
|
||||
return GreaterOrEqual(t, e1, e2, append([]interface{}{msg}, args...)...)
|
||||
}
|
||||
|
||||
// HTTPBodyContainsf asserts that a specified handler returns a
|
||||
// body that contains a string.
|
||||
//
|
||||
|
@ -300,6 +325,31 @@ func Lenf(t TestingT, object interface{}, length int, msg string, args ...interf
|
|||
return Len(t, object, length, append([]interface{}{msg}, args...)...)
|
||||
}
|
||||
|
||||
// Lessf asserts that the first element in less than the second
|
||||
//
|
||||
// assert.Lessf(t, 1, 2, "error message %s", "formatted")
|
||||
// assert.Lessf(t, float64(1, "error message %s", "formatted"), float64(2))
|
||||
// assert.Lessf(t, "a", "b", "error message %s", "formatted")
|
||||
func Lessf(t TestingT, e1 interface{}, e2 interface{}, msg string, args ...interface{}) bool {
|
||||
if h, ok := t.(tHelper); ok {
|
||||
h.Helper()
|
||||
}
|
||||
return Less(t, e1, e2, append([]interface{}{msg}, args...)...)
|
||||
}
|
||||
|
||||
// LessOrEqualf asserts that the first element in greater or equal than the second
|
||||
//
|
||||
// assert.LessOrEqualf(t, 1, 2, "error message %s", "formatted")
|
||||
// assert.LessOrEqualf(t, 2, 2, "error message %s", "formatted")
|
||||
// assert.LessOrEqualf(t, "a", "b", "error message %s", "formatted")
|
||||
// assert.LessOrEqualf(t, "b", "b", "error message %s", "formatted")
|
||||
func LessOrEqualf(t TestingT, e1 interface{}, e2 interface{}, msg string, args ...interface{}) bool {
|
||||
if h, ok := t.(tHelper); ok {
|
||||
h.Helper()
|
||||
}
|
||||
return LessOrEqual(t, e1, e2, append([]interface{}{msg}, args...)...)
|
||||
}
|
||||
|
||||
// Nilf asserts that the specified object is nil.
|
||||
//
|
||||
// assert.Nilf(t, err, "error message %s", "formatted")
|
||||
|
@ -444,6 +494,19 @@ func Regexpf(t TestingT, rx interface{}, str interface{}, msg string, args ...in
|
|||
return Regexp(t, rx, str, append([]interface{}{msg}, args...)...)
|
||||
}
|
||||
|
||||
// Samef asserts that two pointers reference the same object.
|
||||
//
|
||||
// assert.Samef(t, ptr1, ptr2, "error message %s", "formatted")
|
||||
//
|
||||
// Both arguments must be pointer variables. Pointer variable sameness is
|
||||
// determined based on the equality of both type and value.
|
||||
func Samef(t TestingT, expected interface{}, actual interface{}, msg string, args ...interface{}) bool {
|
||||
if h, ok := t.(tHelper); ok {
|
||||
h.Helper()
|
||||
}
|
||||
return Same(t, expected, actual, append([]interface{}{msg}, args...)...)
|
||||
}
|
||||
|
||||
// Subsetf asserts that the specified list(array, slice...) contains all
|
||||
// elements given in the specified subset(array, slice...).
|
||||
//
|
||||
|
|
|
@ -303,6 +303,56 @@ func (a *Assertions) FileExistsf(path string, msg string, args ...interface{}) b
|
|||
return FileExistsf(a.t, path, msg, args...)
|
||||
}
|
||||
|
||||
// Greater asserts that the first element is greater than the second
|
||||
//
|
||||
// a.Greater(2, 1)
|
||||
// a.Greater(float64(2), float64(1))
|
||||
// a.Greater("b", "a")
|
||||
func (a *Assertions) Greater(e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool {
|
||||
if h, ok := a.t.(tHelper); ok {
|
||||
h.Helper()
|
||||
}
|
||||
return Greater(a.t, e1, e2, msgAndArgs...)
|
||||
}
|
||||
|
||||
// GreaterOrEqual asserts that the first element in greater or equal than the second
|
||||
//
|
||||
// a.GreaterOrEqual(2, 1)
|
||||
// a.GreaterOrEqual(2, 2)
|
||||
// a.GreaterOrEqual("b", "a")
|
||||
// a.GreaterOrEqual("b", "b")
|
||||
func (a *Assertions) GreaterOrEqual(e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool {
|
||||
if h, ok := a.t.(tHelper); ok {
|
||||
h.Helper()
|
||||
}
|
||||
return GreaterOrEqual(a.t, e1, e2, msgAndArgs...)
|
||||
}
|
||||
|
||||
// GreaterOrEqualf asserts that the first element in greater or equal than the second
|
||||
//
|
||||
// a.GreaterOrEqualf(2, 1, "error message %s", "formatted")
|
||||
// a.GreaterOrEqualf(2, 2, "error message %s", "formatted")
|
||||
// a.GreaterOrEqualf("b", "a", "error message %s", "formatted")
|
||||
// a.GreaterOrEqualf("b", "b", "error message %s", "formatted")
|
||||
func (a *Assertions) GreaterOrEqualf(e1 interface{}, e2 interface{}, msg string, args ...interface{}) bool {
|
||||
if h, ok := a.t.(tHelper); ok {
|
||||
h.Helper()
|
||||
}
|
||||
return GreaterOrEqualf(a.t, e1, e2, msg, args...)
|
||||
}
|
||||
|
||||
// Greaterf asserts that the first element is greater than the second
|
||||
//
|
||||
// a.Greaterf(2, 1, "error message %s", "formatted")
|
||||
// a.Greaterf(float64(2, "error message %s", "formatted"), float64(1))
|
||||
// a.Greaterf("b", "a", "error message %s", "formatted")
|
||||
func (a *Assertions) Greaterf(e1 interface{}, e2 interface{}, msg string, args ...interface{}) bool {
|
||||
if h, ok := a.t.(tHelper); ok {
|
||||
h.Helper()
|
||||
}
|
||||
return Greaterf(a.t, e1, e2, msg, args...)
|
||||
}
|
||||
|
||||
// HTTPBodyContains asserts that a specified handler returns a
|
||||
// body that contains a string.
|
||||
//
|
||||
|
@ -589,6 +639,56 @@ func (a *Assertions) Lenf(object interface{}, length int, msg string, args ...in
|
|||
return Lenf(a.t, object, length, msg, args...)
|
||||
}
|
||||
|
||||
// Less asserts that the first element in less than the second
|
||||
//
|
||||
// a.Less(1, 2)
|
||||
// a.Less(float64(1), float64(2))
|
||||
// a.Less("a", "b")
|
||||
func (a *Assertions) Less(e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool {
|
||||
if h, ok := a.t.(tHelper); ok {
|
||||
h.Helper()
|
||||
}
|
||||
return Less(a.t, e1, e2, msgAndArgs...)
|
||||
}
|
||||
|
||||
// LessOrEqual asserts that the first element in greater or equal than the second
|
||||
//
|
||||
// a.LessOrEqual(1, 2)
|
||||
// a.LessOrEqual(2, 2)
|
||||
// a.LessOrEqual("a", "b")
|
||||
// a.LessOrEqual("b", "b")
|
||||
func (a *Assertions) LessOrEqual(e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool {
|
||||
if h, ok := a.t.(tHelper); ok {
|
||||
h.Helper()
|
||||
}
|
||||
return LessOrEqual(a.t, e1, e2, msgAndArgs...)
|
||||
}
|
||||
|
||||
// LessOrEqualf asserts that the first element in greater or equal than the second
|
||||
//
|
||||
// a.LessOrEqualf(1, 2, "error message %s", "formatted")
|
||||
// a.LessOrEqualf(2, 2, "error message %s", "formatted")
|
||||
// a.LessOrEqualf("a", "b", "error message %s", "formatted")
|
||||
// a.LessOrEqualf("b", "b", "error message %s", "formatted")
|
||||
func (a *Assertions) LessOrEqualf(e1 interface{}, e2 interface{}, msg string, args ...interface{}) bool {
|
||||
if h, ok := a.t.(tHelper); ok {
|
||||
h.Helper()
|
||||
}
|
||||
return LessOrEqualf(a.t, e1, e2, msg, args...)
|
||||
}
|
||||
|
||||
// Lessf asserts that the first element in less than the second
|
||||
//
|
||||
// a.Lessf(1, 2, "error message %s", "formatted")
|
||||
// a.Lessf(float64(1, "error message %s", "formatted"), float64(2))
|
||||
// a.Lessf("a", "b", "error message %s", "formatted")
|
||||
func (a *Assertions) Lessf(e1 interface{}, e2 interface{}, msg string, args ...interface{}) bool {
|
||||
if h, ok := a.t.(tHelper); ok {
|
||||
h.Helper()
|
||||
}
|
||||
return Lessf(a.t, e1, e2, msg, args...)
|
||||
}
|
||||
|
||||
// Nil asserts that the specified object is nil.
|
||||
//
|
||||
// a.Nil(err)
|
||||
|
@ -877,6 +977,32 @@ func (a *Assertions) Regexpf(rx interface{}, str interface{}, msg string, args .
|
|||
return Regexpf(a.t, rx, str, msg, args...)
|
||||
}
|
||||
|
||||
// Same asserts that two pointers reference the same object.
|
||||
//
|
||||
// a.Same(ptr1, ptr2)
|
||||
//
|
||||
// Both arguments must be pointer variables. Pointer variable sameness is
|
||||
// determined based on the equality of both type and value.
|
||||
func (a *Assertions) Same(expected interface{}, actual interface{}, msgAndArgs ...interface{}) bool {
|
||||
if h, ok := a.t.(tHelper); ok {
|
||||
h.Helper()
|
||||
}
|
||||
return Same(a.t, expected, actual, msgAndArgs...)
|
||||
}
|
||||
|
||||
// Samef asserts that two pointers reference the same object.
|
||||
//
|
||||
// a.Samef(ptr1, ptr2, "error message %s", "formatted")
|
||||
//
|
||||
// Both arguments must be pointer variables. Pointer variable sameness is
|
||||
// determined based on the equality of both type and value.
|
||||
func (a *Assertions) Samef(expected interface{}, actual interface{}, msg string, args ...interface{}) bool {
|
||||
if h, ok := a.t.(tHelper); ok {
|
||||
h.Helper()
|
||||
}
|
||||
return Samef(a.t, expected, actual, msg, args...)
|
||||
}
|
||||
|
||||
// Subset asserts that the specified list(array, slice...) contains all
|
||||
// elements given in the specified subset(array, slice...).
|
||||
//
|
||||
|
|
|
@ -0,0 +1,309 @@
|
|||
package assert
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
func compare(obj1, obj2 interface{}, kind reflect.Kind) (int, bool) {
|
||||
switch kind {
|
||||
case reflect.Int:
|
||||
{
|
||||
intobj1 := obj1.(int)
|
||||
intobj2 := obj2.(int)
|
||||
if intobj1 > intobj2 {
|
||||
return -1, true
|
||||
}
|
||||
if intobj1 == intobj2 {
|
||||
return 0, true
|
||||
}
|
||||
if intobj1 < intobj2 {
|
||||
return 1, true
|
||||
}
|
||||
}
|
||||
case reflect.Int8:
|
||||
{
|
||||
int8obj1 := obj1.(int8)
|
||||
int8obj2 := obj2.(int8)
|
||||
if int8obj1 > int8obj2 {
|
||||
return -1, true
|
||||
}
|
||||
if int8obj1 == int8obj2 {
|
||||
return 0, true
|
||||
}
|
||||
if int8obj1 < int8obj2 {
|
||||
return 1, true
|
||||
}
|
||||
}
|
||||
case reflect.Int16:
|
||||
{
|
||||
int16obj1 := obj1.(int16)
|
||||
int16obj2 := obj2.(int16)
|
||||
if int16obj1 > int16obj2 {
|
||||
return -1, true
|
||||
}
|
||||
if int16obj1 == int16obj2 {
|
||||
return 0, true
|
||||
}
|
||||
if int16obj1 < int16obj2 {
|
||||
return 1, true
|
||||
}
|
||||
}
|
||||
case reflect.Int32:
|
||||
{
|
||||
int32obj1 := obj1.(int32)
|
||||
int32obj2 := obj2.(int32)
|
||||
if int32obj1 > int32obj2 {
|
||||
return -1, true
|
||||
}
|
||||
if int32obj1 == int32obj2 {
|
||||
return 0, true
|
||||
}
|
||||
if int32obj1 < int32obj2 {
|
||||
return 1, true
|
||||
}
|
||||
}
|
||||
case reflect.Int64:
|
||||
{
|
||||
int64obj1 := obj1.(int64)
|
||||
int64obj2 := obj2.(int64)
|
||||
if int64obj1 > int64obj2 {
|
||||
return -1, true
|
||||
}
|
||||
if int64obj1 == int64obj2 {
|
||||
return 0, true
|
||||
}
|
||||
if int64obj1 < int64obj2 {
|
||||
return 1, true
|
||||
}
|
||||
}
|
||||
case reflect.Uint:
|
||||
{
|
||||
uintobj1 := obj1.(uint)
|
||||
uintobj2 := obj2.(uint)
|
||||
if uintobj1 > uintobj2 {
|
||||
return -1, true
|
||||
}
|
||||
if uintobj1 == uintobj2 {
|
||||
return 0, true
|
||||
}
|
||||
if uintobj1 < uintobj2 {
|
||||
return 1, true
|
||||
}
|
||||
}
|
||||
case reflect.Uint8:
|
||||
{
|
||||
uint8obj1 := obj1.(uint8)
|
||||
uint8obj2 := obj2.(uint8)
|
||||
if uint8obj1 > uint8obj2 {
|
||||
return -1, true
|
||||
}
|
||||
if uint8obj1 == uint8obj2 {
|
||||
return 0, true
|
||||
}
|
||||
if uint8obj1 < uint8obj2 {
|
||||
return 1, true
|
||||
}
|
||||
}
|
||||
case reflect.Uint16:
|
||||
{
|
||||
uint16obj1 := obj1.(uint16)
|
||||
uint16obj2 := obj2.(uint16)
|
||||
if uint16obj1 > uint16obj2 {
|
||||
return -1, true
|
||||
}
|
||||
if uint16obj1 == uint16obj2 {
|
||||
return 0, true
|
||||
}
|
||||
if uint16obj1 < uint16obj2 {
|
||||
return 1, true
|
||||
}
|
||||
}
|
||||
case reflect.Uint32:
|
||||
{
|
||||
uint32obj1 := obj1.(uint32)
|
||||
uint32obj2 := obj2.(uint32)
|
||||
if uint32obj1 > uint32obj2 {
|
||||
return -1, true
|
||||
}
|
||||
if uint32obj1 == uint32obj2 {
|
||||
return 0, true
|
||||
}
|
||||
if uint32obj1 < uint32obj2 {
|
||||
return 1, true
|
||||
}
|
||||
}
|
||||
case reflect.Uint64:
|
||||
{
|
||||
uint64obj1 := obj1.(uint64)
|
||||
uint64obj2 := obj2.(uint64)
|
||||
if uint64obj1 > uint64obj2 {
|
||||
return -1, true
|
||||
}
|
||||
if uint64obj1 == uint64obj2 {
|
||||
return 0, true
|
||||
}
|
||||
if uint64obj1 < uint64obj2 {
|
||||
return 1, true
|
||||
}
|
||||
}
|
||||
case reflect.Float32:
|
||||
{
|
||||
float32obj1 := obj1.(float32)
|
||||
float32obj2 := obj2.(float32)
|
||||
if float32obj1 > float32obj2 {
|
||||
return -1, true
|
||||
}
|
||||
if float32obj1 == float32obj2 {
|
||||
return 0, true
|
||||
}
|
||||
if float32obj1 < float32obj2 {
|
||||
return 1, true
|
||||
}
|
||||
}
|
||||
case reflect.Float64:
|
||||
{
|
||||
float64obj1 := obj1.(float64)
|
||||
float64obj2 := obj2.(float64)
|
||||
if float64obj1 > float64obj2 {
|
||||
return -1, true
|
||||
}
|
||||
if float64obj1 == float64obj2 {
|
||||
return 0, true
|
||||
}
|
||||
if float64obj1 < float64obj2 {
|
||||
return 1, true
|
||||
}
|
||||
}
|
||||
case reflect.String:
|
||||
{
|
||||
stringobj1 := obj1.(string)
|
||||
stringobj2 := obj2.(string)
|
||||
if stringobj1 > stringobj2 {
|
||||
return -1, true
|
||||
}
|
||||
if stringobj1 == stringobj2 {
|
||||
return 0, true
|
||||
}
|
||||
if stringobj1 < stringobj2 {
|
||||
return 1, true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0, false
|
||||
}
|
||||
|
||||
// Greater asserts that the first element is greater than the second
|
||||
//
|
||||
// assert.Greater(t, 2, 1)
|
||||
// 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()
|
||||
}
|
||||
|
||||
e1Kind := reflect.ValueOf(e1).Kind()
|
||||
e2Kind := reflect.ValueOf(e2).Kind()
|
||||
if e1Kind != e2Kind {
|
||||
return Fail(t, "Elements should be the same type", msgAndArgs...)
|
||||
}
|
||||
|
||||
res, isComparable := compare(e1, e2, e1Kind)
|
||||
if !isComparable {
|
||||
return Fail(t, fmt.Sprintf("Can not compare type \"%s\"", reflect.TypeOf(e1)), msgAndArgs...)
|
||||
}
|
||||
|
||||
if res != -1 {
|
||||
return Fail(t, fmt.Sprintf("\"%s\" is not greater than \"%s\"", e1, e2), msgAndArgs...)
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// GreaterOrEqual asserts that the first element in greater or equal than the second
|
||||
//
|
||||
// assert.GreaterOrEqual(t, 2, 1)
|
||||
// assert.GreaterOrEqual(t, 2, 2)
|
||||
// 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()
|
||||
}
|
||||
|
||||
e1Kind := reflect.ValueOf(e1).Kind()
|
||||
e2Kind := reflect.ValueOf(e2).Kind()
|
||||
if e1Kind != e2Kind {
|
||||
return Fail(t, "Elements should be the same type", msgAndArgs...)
|
||||
}
|
||||
|
||||
res, isComparable := compare(e1, e2, e1Kind)
|
||||
if !isComparable {
|
||||
return Fail(t, fmt.Sprintf("Can not compare type \"%s\"", reflect.TypeOf(e1)), msgAndArgs...)
|
||||
}
|
||||
|
||||
if res != -1 && res != 0 {
|
||||
return Fail(t, fmt.Sprintf("\"%s\" is not greater or equal than \"%s\"", e1, e2), msgAndArgs...)
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// Less asserts that the first element in less than the second
|
||||
//
|
||||
// assert.Less(t, 1, 2)
|
||||
// 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()
|
||||
}
|
||||
|
||||
e1Kind := reflect.ValueOf(e1).Kind()
|
||||
e2Kind := reflect.ValueOf(e2).Kind()
|
||||
if e1Kind != e2Kind {
|
||||
return Fail(t, "Elements should be the same type", msgAndArgs...)
|
||||
}
|
||||
|
||||
res, isComparable := compare(e1, e2, e1Kind)
|
||||
if !isComparable {
|
||||
return Fail(t, fmt.Sprintf("Can not compare type \"%s\"", reflect.TypeOf(e1)), msgAndArgs...)
|
||||
}
|
||||
|
||||
if res != 1 {
|
||||
return Fail(t, fmt.Sprintf("\"%s\" is not less than \"%s\"", e1, e2), msgAndArgs...)
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// LessOrEqual asserts that the first element in greater or equal than the second
|
||||
//
|
||||
// assert.LessOrEqual(t, 1, 2)
|
||||
// assert.LessOrEqual(t, 2, 2)
|
||||
// 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()
|
||||
}
|
||||
|
||||
e1Kind := reflect.ValueOf(e1).Kind()
|
||||
e2Kind := reflect.ValueOf(e2).Kind()
|
||||
if e1Kind != e2Kind {
|
||||
return Fail(t, "Elements should be the same type", msgAndArgs...)
|
||||
}
|
||||
|
||||
res, isComparable := compare(e1, e2, e1Kind)
|
||||
if !isComparable {
|
||||
return Fail(t, fmt.Sprintf("Can not compare type \"%s\"", reflect.TypeOf(e1)), msgAndArgs...)
|
||||
}
|
||||
|
||||
if res != 1 && res != 0 {
|
||||
return Fail(t, fmt.Sprintf("\"%s\" is not less or equal than \"%s\"", e1, e2), msgAndArgs...)
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
|
@ -0,0 +1,118 @@
|
|||
package assert
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCompare(t *testing.T) {
|
||||
for _, currCase := range []struct {
|
||||
less interface{}
|
||||
greater interface{}
|
||||
cType string
|
||||
}{
|
||||
{less: "a", greater: "b", cType: "string"},
|
||||
{less: int(1), greater: int(2), cType: "int"},
|
||||
{less: int8(1), greater: int8(2), cType: "int8"},
|
||||
{less: int16(1), greater: int16(2), cType: "int16"},
|
||||
{less: int32(1), greater: int32(2), cType: "int32"},
|
||||
{less: int64(1), greater: int64(2), cType: "int64"},
|
||||
{less: uint8(1), greater: uint8(2), cType: "uint8"},
|
||||
{less: uint16(1), greater: uint16(2), cType: "uint16"},
|
||||
{less: uint32(1), greater: uint32(2), cType: "uint32"},
|
||||
{less: uint64(1), greater: uint64(2), cType: "uint64"},
|
||||
{less: float32(1), greater: float32(2), cType: "float32"},
|
||||
{less: float64(1), greater: float64(2), cType: "float64"},
|
||||
} {
|
||||
resLess, isComparable := compare(currCase.less, currCase.greater, reflect.ValueOf(currCase.less).Kind())
|
||||
if !isComparable {
|
||||
t.Error("object should be comparable for type " + currCase.cType)
|
||||
}
|
||||
|
||||
if resLess != 1 {
|
||||
t.Errorf("object less should be less than greater for type " + currCase.cType)
|
||||
}
|
||||
|
||||
resGreater, isComparable := compare(currCase.greater, currCase.less, reflect.ValueOf(currCase.less).Kind())
|
||||
if !isComparable {
|
||||
t.Error("object are comparable for type " + currCase.cType)
|
||||
}
|
||||
|
||||
if resGreater != -1 {
|
||||
t.Errorf("object greater should be greater than less for type " + currCase.cType)
|
||||
}
|
||||
|
||||
resEqual, isComparable := compare(currCase.less, currCase.less, reflect.ValueOf(currCase.less).Kind())
|
||||
if !isComparable {
|
||||
t.Error("object are comparable for type " + currCase.cType)
|
||||
}
|
||||
|
||||
if resEqual != 0 {
|
||||
t.Errorf("objects should be equal for type " + currCase.cType)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestGreater(t *testing.T) {
|
||||
mockT := new(testing.T)
|
||||
|
||||
if !Greater(mockT, 2, 1) {
|
||||
t.Error("Greater should return true")
|
||||
}
|
||||
|
||||
if Greater(mockT, 1, 1) {
|
||||
t.Error("Greater should return false")
|
||||
}
|
||||
|
||||
if Greater(mockT, 1, 2) {
|
||||
t.Error("Greater should return false")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGreaterOrEqual(t *testing.T) {
|
||||
mockT := new(testing.T)
|
||||
|
||||
if !GreaterOrEqual(mockT, 2, 1) {
|
||||
t.Error("Greater should return true")
|
||||
}
|
||||
|
||||
if !GreaterOrEqual(mockT, 1, 1) {
|
||||
t.Error("Greater should return true")
|
||||
}
|
||||
|
||||
if GreaterOrEqual(mockT, 1, 2) {
|
||||
t.Error("Greater should return false")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLess(t *testing.T) {
|
||||
mockT := new(testing.T)
|
||||
|
||||
if !Less(mockT, 1, 2) {
|
||||
t.Error("Less should return true")
|
||||
}
|
||||
|
||||
if Less(mockT, 1, 1) {
|
||||
t.Error("Less should return false")
|
||||
}
|
||||
|
||||
if Less(mockT, 2, 1) {
|
||||
t.Error("Less should return false")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLessOrEqual(t *testing.T) {
|
||||
mockT := new(testing.T)
|
||||
|
||||
if !LessOrEqual(mockT, 1, 2) {
|
||||
t.Error("Greater should return true")
|
||||
}
|
||||
|
||||
if !LessOrEqual(mockT, 1, 1) {
|
||||
t.Error("Greater should return true")
|
||||
}
|
||||
|
||||
if LessOrEqual(mockT, 2, 1) {
|
||||
t.Error("Greater should return false")
|
||||
}
|
||||
}
|
|
@ -350,6 +350,37 @@ func Equal(t TestingT, expected, actual interface{}, msgAndArgs ...interface{})
|
|||
|
||||
}
|
||||
|
||||
// Same asserts that two pointers reference the same object.
|
||||
//
|
||||
// assert.Same(t, ptr1, ptr2)
|
||||
//
|
||||
// Both arguments must be pointer variables. Pointer variable sameness is
|
||||
// determined based on the equality of both type and value.
|
||||
func Same(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool {
|
||||
if h, ok := t.(tHelper); ok {
|
||||
h.Helper()
|
||||
}
|
||||
|
||||
expectedPtr, actualPtr := reflect.ValueOf(expected), reflect.ValueOf(actual)
|
||||
if expectedPtr.Kind() != reflect.Ptr || actualPtr.Kind() != reflect.Ptr {
|
||||
return Fail(t, "Invalid operation: both arguments must be pointers", msgAndArgs...)
|
||||
}
|
||||
|
||||
expectedType, actualType := reflect.TypeOf(expected), reflect.TypeOf(actual)
|
||||
if expectedType != actualType {
|
||||
return Fail(t, fmt.Sprintf("Pointer expected to be of type %v, but was %v",
|
||||
expectedType, actualType), msgAndArgs...)
|
||||
}
|
||||
|
||||
if expected != actual {
|
||||
return Fail(t, fmt.Sprintf("Not same: \n"+
|
||||
"expected: %p %#v\n"+
|
||||
"actual : %p %#v", expected, expected, actual, actual), msgAndArgs...)
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// formatUnequalValues takes two values of arbitrary types and returns string
|
||||
// representations appropriate to be presented to the user.
|
||||
//
|
||||
|
@ -419,6 +450,17 @@ func NotNil(t TestingT, object interface{}, msgAndArgs ...interface{}) bool {
|
|||
return Fail(t, "Expected value not to be nil.", msgAndArgs...)
|
||||
}
|
||||
|
||||
// containsKind checks if a specified kind in the slice of kinds.
|
||||
func containsKind(kinds []reflect.Kind, kind reflect.Kind) bool {
|
||||
for i := 0; i < len(kinds); i++ {
|
||||
if kind == kinds[i] {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// isNil checks if a specified object is nil or not, without Failing.
|
||||
func isNil(object interface{}) bool {
|
||||
if object == nil {
|
||||
|
@ -427,7 +469,14 @@ func isNil(object interface{}) bool {
|
|||
|
||||
value := reflect.ValueOf(object)
|
||||
kind := value.Kind()
|
||||
if kind >= reflect.Chan && kind <= reflect.Slice && value.IsNil() {
|
||||
isNilableKind := containsKind(
|
||||
[]reflect.Kind{
|
||||
reflect.Chan, reflect.Func,
|
||||
reflect.Interface, reflect.Map,
|
||||
reflect.Ptr, reflect.Slice},
|
||||
kind)
|
||||
|
||||
if isNilableKind && value.IsNil() {
|
||||
return true
|
||||
}
|
||||
|
||||
|
@ -611,7 +660,7 @@ func NotEqual(t TestingT, expected, actual interface{}, msgAndArgs ...interface{
|
|||
func includeElement(list interface{}, element interface{}) (ok, found bool) {
|
||||
|
||||
listValue := reflect.ValueOf(list)
|
||||
elementValue := reflect.ValueOf(element)
|
||||
listKind := reflect.TypeOf(list).Kind()
|
||||
defer func() {
|
||||
if e := recover(); e != nil {
|
||||
ok = false
|
||||
|
@ -619,11 +668,12 @@ func includeElement(list interface{}, element interface{}) (ok, found bool) {
|
|||
}
|
||||
}()
|
||||
|
||||
if reflect.TypeOf(list).Kind() == reflect.String {
|
||||
if listKind == reflect.String {
|
||||
elementValue := reflect.ValueOf(element)
|
||||
return true, strings.Contains(listValue.String(), elementValue.String())
|
||||
}
|
||||
|
||||
if reflect.TypeOf(list).Kind() == reflect.Map {
|
||||
if listKind == reflect.Map {
|
||||
mapKeys := listValue.MapKeys()
|
||||
for i := 0; i < len(mapKeys); i++ {
|
||||
if ObjectsAreEqual(mapKeys[i].Interface(), element) {
|
||||
|
|
|
@ -217,6 +217,29 @@ func TestEqual(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestSame(t *testing.T) {
|
||||
|
||||
mockT := new(testing.T)
|
||||
|
||||
ptr := func(i int) *int {
|
||||
return &i
|
||||
}
|
||||
|
||||
if Same(mockT, ptr(1), ptr(1)) {
|
||||
t.Error("Same should return false")
|
||||
}
|
||||
if Same(mockT, 1, 1) {
|
||||
t.Error("Same should return false")
|
||||
}
|
||||
p := ptr(2)
|
||||
if Same(mockT, p, *p) {
|
||||
t.Error("Same should return false")
|
||||
}
|
||||
if !Same(mockT, p, p) {
|
||||
t.Error("Same should return true")
|
||||
}
|
||||
}
|
||||
|
||||
// bufferT implements TestingT. Its implementation of Errorf writes the output that would be produced by
|
||||
// testing.T.Errorf to an internal bytes.Buffer.
|
||||
type bufferT struct {
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
module github.com/stretchr/testify
|
||||
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.0
|
||||
github.com/pmezard/go-difflib v1.0.0
|
||||
github.com/stretchr/objx v0.1.0
|
||||
)
|
|
@ -0,0 +1,6 @@
|
|||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
|
@ -176,6 +176,7 @@ func (c *Call) Maybe() *Call {
|
|||
// Mock.
|
||||
// On("MyMethod", 1).Return(nil).
|
||||
// On("MyOtherMethod", 'a', 'b', 'c').Return(errors.New("Some Error"))
|
||||
//go:noinline
|
||||
func (c *Call) On(methodName string, arguments ...interface{}) *Call {
|
||||
return c.Parent.On(methodName, arguments...)
|
||||
}
|
||||
|
|
|
@ -32,6 +32,7 @@ func (i *TestExampleImplementation) TheExampleMethod(a, b, c int) (int, error) {
|
|||
return args.Int(0), errors.New("Whoops")
|
||||
}
|
||||
|
||||
//go:noinline
|
||||
func (i *TestExampleImplementation) TheExampleMethod2(yesorno bool) {
|
||||
i.Called(yesorno)
|
||||
}
|
||||
|
@ -1517,6 +1518,7 @@ func unexpectedCallRegex(method, calledArg, expectedArg, diff string) string {
|
|||
rMethod, calledArg, rMethod, expectedArg, diff)
|
||||
}
|
||||
|
||||
//go:noinline
|
||||
func ConcurrencyTestMethod(m *Mock) {
|
||||
m.Called()
|
||||
}
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -1,6 +1,6 @@
|
|||
{{.Comment}}
|
||||
func {{.DocInfo.Name}}(t TestingT, {{.Params}}) {
|
||||
if assert.{{.DocInfo.Name}}(t, {{.ForwardedParams}}) { return }
|
||||
if h, ok := t.(tHelper); ok { h.Helper() }
|
||||
if assert.{{.DocInfo.Name}}(t, {{.ForwardedParams}}) { return }
|
||||
t.FailNow()
|
||||
}
|
||||
|
|
|
@ -304,6 +304,56 @@ func (a *Assertions) FileExistsf(path string, msg string, args ...interface{}) {
|
|||
FileExistsf(a.t, path, msg, args...)
|
||||
}
|
||||
|
||||
// Greater asserts that the first element is greater than the second
|
||||
//
|
||||
// a.Greater(2, 1)
|
||||
// a.Greater(float64(2), float64(1))
|
||||
// a.Greater("b", "a")
|
||||
func (a *Assertions) Greater(e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) {
|
||||
if h, ok := a.t.(tHelper); ok {
|
||||
h.Helper()
|
||||
}
|
||||
Greater(a.t, e1, e2, msgAndArgs...)
|
||||
}
|
||||
|
||||
// GreaterOrEqual asserts that the first element in greater or equal than the second
|
||||
//
|
||||
// a.GreaterOrEqual(2, 1)
|
||||
// a.GreaterOrEqual(2, 2)
|
||||
// a.GreaterOrEqual("b", "a")
|
||||
// a.GreaterOrEqual("b", "b")
|
||||
func (a *Assertions) GreaterOrEqual(e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) {
|
||||
if h, ok := a.t.(tHelper); ok {
|
||||
h.Helper()
|
||||
}
|
||||
GreaterOrEqual(a.t, e1, e2, msgAndArgs...)
|
||||
}
|
||||
|
||||
// GreaterOrEqualf asserts that the first element in greater or equal than the second
|
||||
//
|
||||
// a.GreaterOrEqualf(2, 1, "error message %s", "formatted")
|
||||
// a.GreaterOrEqualf(2, 2, "error message %s", "formatted")
|
||||
// a.GreaterOrEqualf("b", "a", "error message %s", "formatted")
|
||||
// a.GreaterOrEqualf("b", "b", "error message %s", "formatted")
|
||||
func (a *Assertions) GreaterOrEqualf(e1 interface{}, e2 interface{}, msg string, args ...interface{}) {
|
||||
if h, ok := a.t.(tHelper); ok {
|
||||
h.Helper()
|
||||
}
|
||||
GreaterOrEqualf(a.t, e1, e2, msg, args...)
|
||||
}
|
||||
|
||||
// Greaterf asserts that the first element is greater than the second
|
||||
//
|
||||
// a.Greaterf(2, 1, "error message %s", "formatted")
|
||||
// a.Greaterf(float64(2, "error message %s", "formatted"), float64(1))
|
||||
// a.Greaterf("b", "a", "error message %s", "formatted")
|
||||
func (a *Assertions) Greaterf(e1 interface{}, e2 interface{}, msg string, args ...interface{}) {
|
||||
if h, ok := a.t.(tHelper); ok {
|
||||
h.Helper()
|
||||
}
|
||||
Greaterf(a.t, e1, e2, msg, args...)
|
||||
}
|
||||
|
||||
// HTTPBodyContains asserts that a specified handler returns a
|
||||
// body that contains a string.
|
||||
//
|
||||
|
@ -590,6 +640,56 @@ func (a *Assertions) Lenf(object interface{}, length int, msg string, args ...in
|
|||
Lenf(a.t, object, length, msg, args...)
|
||||
}
|
||||
|
||||
// Less asserts that the first element in less than the second
|
||||
//
|
||||
// a.Less(1, 2)
|
||||
// a.Less(float64(1), float64(2))
|
||||
// a.Less("a", "b")
|
||||
func (a *Assertions) Less(e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) {
|
||||
if h, ok := a.t.(tHelper); ok {
|
||||
h.Helper()
|
||||
}
|
||||
Less(a.t, e1, e2, msgAndArgs...)
|
||||
}
|
||||
|
||||
// LessOrEqual asserts that the first element in greater or equal than the second
|
||||
//
|
||||
// a.LessOrEqual(1, 2)
|
||||
// a.LessOrEqual(2, 2)
|
||||
// a.LessOrEqual("a", "b")
|
||||
// a.LessOrEqual("b", "b")
|
||||
func (a *Assertions) LessOrEqual(e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) {
|
||||
if h, ok := a.t.(tHelper); ok {
|
||||
h.Helper()
|
||||
}
|
||||
LessOrEqual(a.t, e1, e2, msgAndArgs...)
|
||||
}
|
||||
|
||||
// LessOrEqualf asserts that the first element in greater or equal than the second
|
||||
//
|
||||
// a.LessOrEqualf(1, 2, "error message %s", "formatted")
|
||||
// a.LessOrEqualf(2, 2, "error message %s", "formatted")
|
||||
// a.LessOrEqualf("a", "b", "error message %s", "formatted")
|
||||
// a.LessOrEqualf("b", "b", "error message %s", "formatted")
|
||||
func (a *Assertions) LessOrEqualf(e1 interface{}, e2 interface{}, msg string, args ...interface{}) {
|
||||
if h, ok := a.t.(tHelper); ok {
|
||||
h.Helper()
|
||||
}
|
||||
LessOrEqualf(a.t, e1, e2, msg, args...)
|
||||
}
|
||||
|
||||
// Lessf asserts that the first element in less than the second
|
||||
//
|
||||
// a.Lessf(1, 2, "error message %s", "formatted")
|
||||
// a.Lessf(float64(1, "error message %s", "formatted"), float64(2))
|
||||
// a.Lessf("a", "b", "error message %s", "formatted")
|
||||
func (a *Assertions) Lessf(e1 interface{}, e2 interface{}, msg string, args ...interface{}) {
|
||||
if h, ok := a.t.(tHelper); ok {
|
||||
h.Helper()
|
||||
}
|
||||
Lessf(a.t, e1, e2, msg, args...)
|
||||
}
|
||||
|
||||
// Nil asserts that the specified object is nil.
|
||||
//
|
||||
// a.Nil(err)
|
||||
|
@ -878,6 +978,32 @@ func (a *Assertions) Regexpf(rx interface{}, str interface{}, msg string, args .
|
|||
Regexpf(a.t, rx, str, msg, args...)
|
||||
}
|
||||
|
||||
// Same asserts that two pointers reference the same object.
|
||||
//
|
||||
// a.Same(ptr1, ptr2)
|
||||
//
|
||||
// Both arguments must be pointer variables. Pointer variable sameness is
|
||||
// determined based on the equality of both type and value.
|
||||
func (a *Assertions) Same(expected interface{}, actual interface{}, msgAndArgs ...interface{}) {
|
||||
if h, ok := a.t.(tHelper); ok {
|
||||
h.Helper()
|
||||
}
|
||||
Same(a.t, expected, actual, msgAndArgs...)
|
||||
}
|
||||
|
||||
// Samef asserts that two pointers reference the same object.
|
||||
//
|
||||
// a.Samef(ptr1, ptr2, "error message %s", "formatted")
|
||||
//
|
||||
// Both arguments must be pointer variables. Pointer variable sameness is
|
||||
// determined based on the equality of both type and value.
|
||||
func (a *Assertions) Samef(expected interface{}, actual interface{}, msg string, args ...interface{}) {
|
||||
if h, ok := a.t.(tHelper); ok {
|
||||
h.Helper()
|
||||
}
|
||||
Samef(a.t, expected, actual, msg, args...)
|
||||
}
|
||||
|
||||
// Subset asserts that the specified list(array, slice...) contains all
|
||||
// elements given in the specified subset(array, slice...).
|
||||
//
|
||||
|
|
|
@ -6,6 +6,7 @@ import (
|
|||
"os"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"runtime/debug"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
@ -58,26 +59,32 @@ func (suite *Suite) Assert() *assert.Assertions {
|
|||
func failOnPanic(t *testing.T) {
|
||||
r := recover()
|
||||
if r != nil {
|
||||
t.Errorf("test panicked: %v", r)
|
||||
t.Errorf("test panicked: %v\n%s", r, debug.Stack())
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
|
||||
// Run provides suite functionality around golang subtests. It should be
|
||||
// called in place of t.Run(name, func(t *testing.T)) in test suite code.
|
||||
// The passed-in func will be executed as a subtest with a fresh instance of t.
|
||||
// Provides compatibility with go test pkg -run TestSuite/TestName/SubTestName.
|
||||
func (suite *Suite) Run(name string, subtest func()) bool {
|
||||
oldT := suite.T()
|
||||
defer suite.SetT(oldT)
|
||||
return oldT.Run(name, func(t *testing.T) {
|
||||
suite.SetT(t)
|
||||
subtest()
|
||||
})
|
||||
}
|
||||
|
||||
// Run takes a testing suite and runs all of the tests attached
|
||||
// to it.
|
||||
func Run(t *testing.T, suite TestingSuite) {
|
||||
suite.SetT(t)
|
||||
defer failOnPanic(t)
|
||||
|
||||
if setupAllSuite, ok := suite.(SetupAllSuite); ok {
|
||||
setupAllSuite.SetupSuite()
|
||||
}
|
||||
defer func() {
|
||||
if tearDownAllSuite, ok := suite.(TearDownAllSuite); ok {
|
||||
tearDownAllSuite.TearDownSuite()
|
||||
}
|
||||
}()
|
||||
|
||||
suiteSetupDone := false
|
||||
|
||||
methodFinder := reflect.TypeOf(suite)
|
||||
tests := []testing.InternalTest{}
|
||||
for index := 0; index < methodFinder.NumMethod(); index++ {
|
||||
|
@ -87,34 +94,46 @@ func Run(t *testing.T, suite TestingSuite) {
|
|||
fmt.Fprintf(os.Stderr, "testify: invalid regexp for -m: %s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if ok {
|
||||
test := testing.InternalTest{
|
||||
Name: method.Name,
|
||||
F: func(t *testing.T) {
|
||||
parentT := suite.T()
|
||||
suite.SetT(t)
|
||||
defer failOnPanic(t)
|
||||
|
||||
if setupTestSuite, ok := suite.(SetupTestSuite); ok {
|
||||
setupTestSuite.SetupTest()
|
||||
}
|
||||
if beforeTestSuite, ok := suite.(BeforeTest); ok {
|
||||
beforeTestSuite.BeforeTest(methodFinder.Elem().Name(), method.Name)
|
||||
}
|
||||
defer func() {
|
||||
if afterTestSuite, ok := suite.(AfterTest); ok {
|
||||
afterTestSuite.AfterTest(methodFinder.Elem().Name(), method.Name)
|
||||
}
|
||||
if tearDownTestSuite, ok := suite.(TearDownTestSuite); ok {
|
||||
tearDownTestSuite.TearDownTest()
|
||||
}
|
||||
suite.SetT(parentT)
|
||||
}()
|
||||
method.Func.Call([]reflect.Value{reflect.ValueOf(suite)})
|
||||
},
|
||||
}
|
||||
tests = append(tests, test)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if !suiteSetupDone {
|
||||
if setupAllSuite, ok := suite.(SetupAllSuite); ok {
|
||||
setupAllSuite.SetupSuite()
|
||||
}
|
||||
defer func() {
|
||||
if tearDownAllSuite, ok := suite.(TearDownAllSuite); ok {
|
||||
tearDownAllSuite.TearDownSuite()
|
||||
}
|
||||
}()
|
||||
suiteSetupDone = true
|
||||
}
|
||||
test := testing.InternalTest{
|
||||
Name: method.Name,
|
||||
F: func(t *testing.T) {
|
||||
parentT := suite.T()
|
||||
suite.SetT(t)
|
||||
defer failOnPanic(t)
|
||||
|
||||
if setupTestSuite, ok := suite.(SetupTestSuite); ok {
|
||||
setupTestSuite.SetupTest()
|
||||
}
|
||||
if beforeTestSuite, ok := suite.(BeforeTest); ok {
|
||||
beforeTestSuite.BeforeTest(methodFinder.Elem().Name(), method.Name)
|
||||
}
|
||||
defer func() {
|
||||
if afterTestSuite, ok := suite.(AfterTest); ok {
|
||||
afterTestSuite.AfterTest(methodFinder.Elem().Name(), method.Name)
|
||||
}
|
||||
if tearDownTestSuite, ok := suite.(TearDownTestSuite); ok {
|
||||
tearDownTestSuite.TearDownTest()
|
||||
}
|
||||
suite.SetT(parentT)
|
||||
}()
|
||||
method.Func.Call([]reflect.Value{reflect.ValueOf(suite)})
|
||||
},
|
||||
}
|
||||
tests = append(tests, test)
|
||||
}
|
||||
runTests(t, tests)
|
||||
}
|
||||
|
|
|
@ -152,6 +152,7 @@ type SuiteTester struct {
|
|||
TearDownTestRunCount int
|
||||
TestOneRunCount int
|
||||
TestTwoRunCount int
|
||||
TestSubtestRunCount int
|
||||
NonTestMethodRunCount int
|
||||
|
||||
SuiteNameBefore []string
|
||||
|
@ -164,15 +165,6 @@ type SuiteTester struct {
|
|||
TimeAfter []time.Time
|
||||
}
|
||||
|
||||
type SuiteSkipTester struct {
|
||||
// Include our basic suite logic.
|
||||
Suite
|
||||
|
||||
// Keep counts of how many times each method is run.
|
||||
SetupSuiteRunCount int
|
||||
TearDownSuiteRunCount int
|
||||
}
|
||||
|
||||
// The SetupSuite method will be run by testify once, at the very
|
||||
// start of the testing suite, before any tests are run.
|
||||
func (suite *SuiteTester) SetupSuite() {
|
||||
|
@ -191,21 +183,12 @@ func (suite *SuiteTester) AfterTest(suiteName, testName string) {
|
|||
suite.TimeAfter = append(suite.TimeAfter, time.Now())
|
||||
}
|
||||
|
||||
func (suite *SuiteSkipTester) SetupSuite() {
|
||||
suite.SetupSuiteRunCount++
|
||||
suite.T().Skip()
|
||||
}
|
||||
|
||||
// The TearDownSuite method will be run by testify once, at the very
|
||||
// end of the testing suite, after all tests have been run.
|
||||
func (suite *SuiteTester) TearDownSuite() {
|
||||
suite.TearDownSuiteRunCount++
|
||||
}
|
||||
|
||||
func (suite *SuiteSkipTester) TearDownSuite() {
|
||||
suite.TearDownSuiteRunCount++
|
||||
}
|
||||
|
||||
// The SetupTest method will be run before every test in the suite.
|
||||
func (suite *SuiteTester) SetupTest() {
|
||||
suite.SetupTestRunCount++
|
||||
|
@ -246,6 +229,51 @@ func (suite *SuiteTester) NonTestMethod() {
|
|||
suite.NonTestMethodRunCount++
|
||||
}
|
||||
|
||||
func (suite *SuiteTester) TestSubtest() {
|
||||
suite.TestSubtestRunCount++
|
||||
|
||||
for _, t := range []struct {
|
||||
testName string
|
||||
}{
|
||||
{"first"},
|
||||
{"second"},
|
||||
} {
|
||||
suiteT := suite.T()
|
||||
suite.Run(t.testName, func() {
|
||||
// We should get a different *testing.T for subtests, so that
|
||||
// go test recognizes them as proper subtests for output formatting
|
||||
// and running individual subtests
|
||||
subTestT := suite.T()
|
||||
suite.NotEqual(subTestT, suiteT)
|
||||
})
|
||||
suite.Equal(suiteT, suite.T())
|
||||
}
|
||||
}
|
||||
|
||||
type SuiteSkipTester struct {
|
||||
// Include our basic suite logic.
|
||||
Suite
|
||||
|
||||
// Keep counts of how many times each method is run.
|
||||
SetupSuiteRunCount int
|
||||
TearDownSuiteRunCount int
|
||||
}
|
||||
|
||||
func (suite *SuiteSkipTester) SetupSuite() {
|
||||
suite.SetupSuiteRunCount++
|
||||
suite.T().Skip()
|
||||
}
|
||||
|
||||
func (suite *SuiteSkipTester) TestNothing() {
|
||||
// SetupSuite is only called when at least one test satisfies
|
||||
// test filter. For this suite to be set up (and then tore down)
|
||||
// it is necessary to add at least one test method.
|
||||
}
|
||||
|
||||
func (suite *SuiteSkipTester) TearDownSuite() {
|
||||
suite.TearDownSuiteRunCount++
|
||||
}
|
||||
|
||||
// TestRunSuite will be run by the 'go test' command, so within it, we
|
||||
// can run our suite using the Run(*testing.T, TestingSuite) function.
|
||||
func TestRunSuite(t *testing.T) {
|
||||
|
@ -261,18 +289,20 @@ func TestRunSuite(t *testing.T) {
|
|||
assert.Equal(t, suiteTester.SetupSuiteRunCount, 1)
|
||||
assert.Equal(t, suiteTester.TearDownSuiteRunCount, 1)
|
||||
|
||||
assert.Equal(t, len(suiteTester.SuiteNameAfter), 3)
|
||||
assert.Equal(t, len(suiteTester.SuiteNameBefore), 3)
|
||||
assert.Equal(t, len(suiteTester.TestNameAfter), 3)
|
||||
assert.Equal(t, len(suiteTester.TestNameBefore), 3)
|
||||
assert.Equal(t, len(suiteTester.SuiteNameAfter), 4)
|
||||
assert.Equal(t, len(suiteTester.SuiteNameBefore), 4)
|
||||
assert.Equal(t, len(suiteTester.TestNameAfter), 4)
|
||||
assert.Equal(t, len(suiteTester.TestNameBefore), 4)
|
||||
|
||||
assert.Contains(t, suiteTester.TestNameAfter, "TestOne")
|
||||
assert.Contains(t, suiteTester.TestNameAfter, "TestTwo")
|
||||
assert.Contains(t, suiteTester.TestNameAfter, "TestSkip")
|
||||
assert.Contains(t, suiteTester.TestNameAfter, "TestSubtest")
|
||||
|
||||
assert.Contains(t, suiteTester.TestNameBefore, "TestOne")
|
||||
assert.Contains(t, suiteTester.TestNameBefore, "TestTwo")
|
||||
assert.Contains(t, suiteTester.TestNameBefore, "TestSkip")
|
||||
assert.Contains(t, suiteTester.TestNameBefore, "TestSubtest")
|
||||
|
||||
for _, suiteName := range suiteTester.SuiteNameAfter {
|
||||
assert.Equal(t, "SuiteTester", suiteName)
|
||||
|
@ -290,15 +320,16 @@ func TestRunSuite(t *testing.T) {
|
|||
assert.False(t, when.IsZero())
|
||||
}
|
||||
|
||||
// There are three test methods (TestOne, TestTwo, and TestSkip), so
|
||||
// There are four test methods (TestOne, TestTwo, TestSkip, and TestSubtest), so
|
||||
// the SetupTest and TearDownTest methods (which should be run once for
|
||||
// each test) should have been run three times.
|
||||
assert.Equal(t, suiteTester.SetupTestRunCount, 3)
|
||||
assert.Equal(t, suiteTester.TearDownTestRunCount, 3)
|
||||
// each test) should have been run four times.
|
||||
assert.Equal(t, suiteTester.SetupTestRunCount, 4)
|
||||
assert.Equal(t, suiteTester.TearDownTestRunCount, 4)
|
||||
|
||||
// Each test should have been run once.
|
||||
assert.Equal(t, suiteTester.TestOneRunCount, 1)
|
||||
assert.Equal(t, suiteTester.TestTwoRunCount, 1)
|
||||
assert.Equal(t, suiteTester.TestSubtestRunCount, 1)
|
||||
|
||||
// Methods that don't match the test method identifier shouldn't
|
||||
// have been run at all.
|
||||
|
@ -315,6 +346,33 @@ func TestRunSuite(t *testing.T) {
|
|||
|
||||
}
|
||||
|
||||
// This suite has no Test... methods. It's setup and teardown must be skipped.
|
||||
type SuiteSetupSkipTester struct {
|
||||
Suite
|
||||
|
||||
setUp bool
|
||||
toreDown bool
|
||||
}
|
||||
|
||||
func (s *SuiteSetupSkipTester) SetupSuite() {
|
||||
s.setUp = true
|
||||
}
|
||||
|
||||
func (s *SuiteSetupSkipTester) NonTestMethod() {
|
||||
|
||||
}
|
||||
|
||||
func (s *SuiteSetupSkipTester) TearDownSuite() {
|
||||
s.toreDown = true
|
||||
}
|
||||
|
||||
func TestSkippingSuiteSetup(t *testing.T) {
|
||||
suiteTester := new(SuiteSetupSkipTester)
|
||||
Run(t, suiteTester)
|
||||
assert.False(t, suiteTester.setUp)
|
||||
assert.False(t, suiteTester.toreDown)
|
||||
}
|
||||
|
||||
func TestSuiteGetters(t *testing.T) {
|
||||
suite := new(SuiteTester)
|
||||
suite.SetT(t)
|
||||
|
|
Loading…
Reference in New Issue