180 lines
4.5 KiB
Go
180 lines
4.5 KiB
Go
|
//
|
||
|
// test.go
|
||
|
// Copyright (C) 2022 tiglog <me@tiglog.com>
|
||
|
//
|
||
|
// Distributed under terms of the MIT license.
|
||
|
//
|
||
|
|
||
|
package gtest
|
||
|
|
||
|
import (
|
||
|
"reflect"
|
||
|
"strings"
|
||
|
"testing"
|
||
|
)
|
||
|
|
||
|
func Equal(t *testing.T, expected, val interface{}) {
|
||
|
if val != expected {
|
||
|
t.Errorf("Expected [%v] (type %v), but got [%v] (type %v)", expected, reflect.TypeOf(expected), val, reflect.TypeOf(val))
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func NotEqual(t *testing.T, expected, val interface{}) {
|
||
|
if val == expected {
|
||
|
t.Errorf("Expected not [%v] (type %v), but got [%v] (type %v)", expected, reflect.TypeOf(expected), val, reflect.TypeOf(val))
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func True(t *testing.T, val interface{}) {
|
||
|
if val != true {
|
||
|
t.Errorf("Expected true, but got [%v] (type %v)", val, reflect.TypeOf(val))
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func False(t *testing.T, val interface{}) {
|
||
|
if val != false {
|
||
|
t.Errorf("Expected false, but got [%v] (type %v)", val, reflect.TypeOf(val))
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func MsgNil(t *testing.T, val interface{}) {
|
||
|
t.Errorf("Expected nil, but got [%v] (type %v)", val, reflect.TypeOf(val))
|
||
|
}
|
||
|
|
||
|
func MsgNotNil(t *testing.T, val interface{}) {
|
||
|
t.Errorf("Expected not nil, but got [%v] (type %v)", val, reflect.TypeOf(val))
|
||
|
}
|
||
|
|
||
|
func MsgExpect(t *testing.T, expect string, val string) {
|
||
|
t.Errorf("Expected %s, but got [%v] (type %v)", expect, val, reflect.TypeOf(val))
|
||
|
}
|
||
|
|
||
|
// 有些情况下不能使用
|
||
|
func Nil(t *testing.T, val interface{}) {
|
||
|
if val != nil {
|
||
|
t.Errorf("Expected nil, but got [%v] (type %v)", val, reflect.TypeOf(val))
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// 有些情况下不能使用
|
||
|
func NotNil(t *testing.T, val interface{}) {
|
||
|
if val == nil {
|
||
|
t.Errorf("Expected not nil, but got [%v] (type %v)", val, reflect.TypeOf(val))
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func IsString(t *testing.T, val interface{}) {
|
||
|
_, ok := val.(string)
|
||
|
if !ok {
|
||
|
t.Errorf("Expected a string, but got [%v] (type %v)", val, reflect.TypeOf(val))
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func IsBool(t *testing.T, val interface{}) {
|
||
|
_, ok := val.(bool)
|
||
|
if !ok {
|
||
|
t.Errorf("Expected a bool, but got [%v] (type %v)", val, reflect.TypeOf(val))
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// val 要大于 base
|
||
|
func Greater(t *testing.T, base int64, val interface{}) {
|
||
|
v := reflect.ValueOf(val)
|
||
|
switch v.Kind() {
|
||
|
case reflect.Int, reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8:
|
||
|
if v.Int() <= base {
|
||
|
t.Errorf("Expected greater than %d, but got %v", base, v)
|
||
|
}
|
||
|
return
|
||
|
case reflect.Chan, reflect.Map, reflect.Slice:
|
||
|
if int64(v.Len()) <= base {
|
||
|
t.Errorf("Expected greater than %d, but got %d (%v)", base, v.Len(), v)
|
||
|
}
|
||
|
return
|
||
|
}
|
||
|
t.Errorf("Expected a num or countable val, but got [%v] (type %v)", val, reflect.TypeOf(val))
|
||
|
}
|
||
|
|
||
|
// val 要小于 base
|
||
|
func Less(t *testing.T, base int64, val interface{}) {
|
||
|
v := reflect.ValueOf(val)
|
||
|
switch v.Kind() {
|
||
|
case reflect.Int, reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8:
|
||
|
if v.Int() >= base {
|
||
|
t.Errorf("Expected less than %d, but got %v", base, v)
|
||
|
}
|
||
|
return
|
||
|
case reflect.Chan, reflect.Map, reflect.Slice:
|
||
|
if int64(v.Len()) >= base {
|
||
|
t.Errorf("Expected less than %d, but got %d (%v)", base, v.Len(), v)
|
||
|
}
|
||
|
return
|
||
|
}
|
||
|
t.Errorf("Expected a num or countable val, but got [%v] (type %v)", val, reflect.TypeOf(val))
|
||
|
}
|
||
|
|
||
|
func StartWith(t *testing.T, e string, val string) {
|
||
|
if !strings.HasPrefix(val, e) {
|
||
|
t.Errorf("Expected a string has prefix %s, but got %s", e, val)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func EndWith(t *testing.T, e string, val string) {
|
||
|
if !strings.HasSuffix(val, e) {
|
||
|
t.Errorf("Expected a string has suffix %s, but got %s", e, val)
|
||
|
}
|
||
|
}
|
||
|
func ContainWith(t *testing.T, e string, val string) {
|
||
|
if !strings.Contains(val, e) {
|
||
|
t.Errorf("Expected a string contain %s, but got %s", e, val)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func Error(t *testing.T, err error) {
|
||
|
if err == nil {
|
||
|
t.Errorf("Expected an error, but got nil")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// 不希望是一个 error
|
||
|
func NoError(t *testing.T, err error) {
|
||
|
if err != nil {
|
||
|
t.Errorf("Received unexpected error:\n%+v", err)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func Empty(t *testing.T, val interface{}) {
|
||
|
if !isEmpty(val) {
|
||
|
t.Errorf("Should be empty, but got [%v] (type %v)", val, reflect.TypeOf(val))
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func NotEmpty(t *testing.T, val interface{}) {
|
||
|
if isEmpty(val) {
|
||
|
t.Errorf("should be not emtpy, but got [%v] (type %v)", val, reflect.TypeOf(val))
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func isEmpty(object interface{}) bool {
|
||
|
|
||
|
if object == nil {
|
||
|
return true
|
||
|
}
|
||
|
|
||
|
objValue := reflect.ValueOf(object)
|
||
|
|
||
|
switch objValue.Kind() {
|
||
|
case reflect.Chan, reflect.Map, reflect.Slice:
|
||
|
return objValue.Len() == 0
|
||
|
case reflect.Ptr:
|
||
|
if objValue.IsNil() {
|
||
|
return true
|
||
|
}
|
||
|
deref := objValue.Elem().Interface()
|
||
|
return isEmpty(deref)
|
||
|
default:
|
||
|
zero := reflect.Zero(objValue.Type())
|
||
|
return reflect.DeepEqual(object, zero.Interface())
|
||
|
}
|
||
|
}
|