mirror of
https://gitea.com/Sirherobrine23/tea.git
synced 2024-08-22 02:07:24 +00:00
update go min version Update Vendors: * code.gitea.io/gitea-vet v0.2.0 -> v0.2.1 * code.gitea.io/sdk/gitea v0.13.0 -> v0.13.1 * github.com/AlecAivazis/survey v2.1.1 -> v2.2.2 * github.com/adrg/xdg v0.2.1 -> v0.2.2 * github.com/araddon/dateparse d820a6159ab1 -> 8aadafed4dc4 * github.com/go-git/go-git v5.1.0 -> v5.2.0 * github.com/muesli/termenv v0.7.2 -> v0.7.4 * github.com/stretchr/testify v1.5.1 -> v1.6.1 * github.com/urfave/cli v2.2.0 -> v2.3.0 Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/tea/pulls/250 Reviewed-by: Andrew Thornton <art27@cantab.net> Reviewed-by: mrsdizzie <info@mrsdizzie.com> Co-Authored-By: 6543 <6543@noreply.gitea.io> Co-Committed-By: 6543 <6543@noreply.gitea.io>
275 lines
6.4 KiB
Go
275 lines
6.4 KiB
Go
package assert
|
|
|
|
import (
|
|
"fmt"
|
|
"reflect"
|
|
)
|
|
|
|
type CompareType int
|
|
|
|
const (
|
|
compareLess CompareType = iota - 1
|
|
compareEqual
|
|
compareGreater
|
|
)
|
|
|
|
func compare(obj1, obj2 interface{}, kind reflect.Kind) (CompareType, bool) {
|
|
switch kind {
|
|
case reflect.Int:
|
|
{
|
|
intobj1 := obj1.(int)
|
|
intobj2 := obj2.(int)
|
|
if intobj1 > intobj2 {
|
|
return compareGreater, true
|
|
}
|
|
if intobj1 == intobj2 {
|
|
return compareEqual, true
|
|
}
|
|
if intobj1 < intobj2 {
|
|
return compareLess, true
|
|
}
|
|
}
|
|
case reflect.Int8:
|
|
{
|
|
int8obj1 := obj1.(int8)
|
|
int8obj2 := obj2.(int8)
|
|
if int8obj1 > int8obj2 {
|
|
return compareGreater, true
|
|
}
|
|
if int8obj1 == int8obj2 {
|
|
return compareEqual, true
|
|
}
|
|
if int8obj1 < int8obj2 {
|
|
return compareLess, true
|
|
}
|
|
}
|
|
case reflect.Int16:
|
|
{
|
|
int16obj1 := obj1.(int16)
|
|
int16obj2 := obj2.(int16)
|
|
if int16obj1 > int16obj2 {
|
|
return compareGreater, true
|
|
}
|
|
if int16obj1 == int16obj2 {
|
|
return compareEqual, true
|
|
}
|
|
if int16obj1 < int16obj2 {
|
|
return compareLess, true
|
|
}
|
|
}
|
|
case reflect.Int32:
|
|
{
|
|
int32obj1 := obj1.(int32)
|
|
int32obj2 := obj2.(int32)
|
|
if int32obj1 > int32obj2 {
|
|
return compareGreater, true
|
|
}
|
|
if int32obj1 == int32obj2 {
|
|
return compareEqual, true
|
|
}
|
|
if int32obj1 < int32obj2 {
|
|
return compareLess, true
|
|
}
|
|
}
|
|
case reflect.Int64:
|
|
{
|
|
int64obj1 := obj1.(int64)
|
|
int64obj2 := obj2.(int64)
|
|
if int64obj1 > int64obj2 {
|
|
return compareGreater, true
|
|
}
|
|
if int64obj1 == int64obj2 {
|
|
return compareEqual, true
|
|
}
|
|
if int64obj1 < int64obj2 {
|
|
return compareLess, true
|
|
}
|
|
}
|
|
case reflect.Uint:
|
|
{
|
|
uintobj1 := obj1.(uint)
|
|
uintobj2 := obj2.(uint)
|
|
if uintobj1 > uintobj2 {
|
|
return compareGreater, true
|
|
}
|
|
if uintobj1 == uintobj2 {
|
|
return compareEqual, true
|
|
}
|
|
if uintobj1 < uintobj2 {
|
|
return compareLess, true
|
|
}
|
|
}
|
|
case reflect.Uint8:
|
|
{
|
|
uint8obj1 := obj1.(uint8)
|
|
uint8obj2 := obj2.(uint8)
|
|
if uint8obj1 > uint8obj2 {
|
|
return compareGreater, true
|
|
}
|
|
if uint8obj1 == uint8obj2 {
|
|
return compareEqual, true
|
|
}
|
|
if uint8obj1 < uint8obj2 {
|
|
return compareLess, true
|
|
}
|
|
}
|
|
case reflect.Uint16:
|
|
{
|
|
uint16obj1 := obj1.(uint16)
|
|
uint16obj2 := obj2.(uint16)
|
|
if uint16obj1 > uint16obj2 {
|
|
return compareGreater, true
|
|
}
|
|
if uint16obj1 == uint16obj2 {
|
|
return compareEqual, true
|
|
}
|
|
if uint16obj1 < uint16obj2 {
|
|
return compareLess, true
|
|
}
|
|
}
|
|
case reflect.Uint32:
|
|
{
|
|
uint32obj1 := obj1.(uint32)
|
|
uint32obj2 := obj2.(uint32)
|
|
if uint32obj1 > uint32obj2 {
|
|
return compareGreater, true
|
|
}
|
|
if uint32obj1 == uint32obj2 {
|
|
return compareEqual, true
|
|
}
|
|
if uint32obj1 < uint32obj2 {
|
|
return compareLess, true
|
|
}
|
|
}
|
|
case reflect.Uint64:
|
|
{
|
|
uint64obj1 := obj1.(uint64)
|
|
uint64obj2 := obj2.(uint64)
|
|
if uint64obj1 > uint64obj2 {
|
|
return compareGreater, true
|
|
}
|
|
if uint64obj1 == uint64obj2 {
|
|
return compareEqual, true
|
|
}
|
|
if uint64obj1 < uint64obj2 {
|
|
return compareLess, true
|
|
}
|
|
}
|
|
case reflect.Float32:
|
|
{
|
|
float32obj1 := obj1.(float32)
|
|
float32obj2 := obj2.(float32)
|
|
if float32obj1 > float32obj2 {
|
|
return compareGreater, true
|
|
}
|
|
if float32obj1 == float32obj2 {
|
|
return compareEqual, true
|
|
}
|
|
if float32obj1 < float32obj2 {
|
|
return compareLess, true
|
|
}
|
|
}
|
|
case reflect.Float64:
|
|
{
|
|
float64obj1 := obj1.(float64)
|
|
float64obj2 := obj2.(float64)
|
|
if float64obj1 > float64obj2 {
|
|
return compareGreater, true
|
|
}
|
|
if float64obj1 == float64obj2 {
|
|
return compareEqual, true
|
|
}
|
|
if float64obj1 < float64obj2 {
|
|
return compareLess, true
|
|
}
|
|
}
|
|
case reflect.String:
|
|
{
|
|
stringobj1 := obj1.(string)
|
|
stringobj2 := obj2.(string)
|
|
if stringobj1 > stringobj2 {
|
|
return compareGreater, true
|
|
}
|
|
if stringobj1 == stringobj2 {
|
|
return compareEqual, true
|
|
}
|
|
if stringobj1 < stringobj2 {
|
|
return compareLess, true
|
|
}
|
|
}
|
|
}
|
|
|
|
return compareEqual, 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 {
|
|
return compareTwoValues(t, e1, e2, []CompareType{compareGreater}, "\"%v\" is not greater than \"%v\"", msgAndArgs)
|
|
}
|
|
|
|
// GreaterOrEqual asserts that the first element is greater than or equal to 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 {
|
|
return compareTwoValues(t, e1, e2, []CompareType{compareGreater, compareEqual}, "\"%v\" is not greater than or equal to \"%v\"", msgAndArgs)
|
|
}
|
|
|
|
// Less asserts that the first element is 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 {
|
|
return compareTwoValues(t, e1, e2, []CompareType{compareLess}, "\"%v\" is not less than \"%v\"", msgAndArgs)
|
|
}
|
|
|
|
// LessOrEqual asserts that the first element is less than or equal to 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 {
|
|
return compareTwoValues(t, e1, e2, []CompareType{compareLess, compareEqual}, "\"%v\" is not less than or equal to \"%v\"", msgAndArgs)
|
|
}
|
|
|
|
func compareTwoValues(t TestingT, e1 interface{}, e2 interface{}, allowedComparesResults []CompareType, failMessage string, 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...)
|
|
}
|
|
|
|
compareResult, isComparable := compare(e1, e2, e1Kind)
|
|
if !isComparable {
|
|
return Fail(t, fmt.Sprintf("Can not compare type \"%s\"", reflect.TypeOf(e1)), msgAndArgs...)
|
|
}
|
|
|
|
if !containsValue(allowedComparesResults, compareResult) {
|
|
return Fail(t, fmt.Sprintf(failMessage, e1, e2), msgAndArgs...)
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
func containsValue(values []CompareType, value CompareType) bool {
|
|
for _, v := range values {
|
|
if v == value {
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|