2020-03-09 08:03:59 +00:00
|
|
|
// Copyright 2020 The Xorm Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2023-10-28 10:59:32 +00:00
|
|
|
type (
|
|
|
|
MyInt int
|
|
|
|
ZeroStruct struct{}
|
|
|
|
)
|
2020-03-09 08:03:59 +00:00
|
|
|
|
|
|
|
func TestZero(t *testing.T) {
|
2023-10-28 10:59:32 +00:00
|
|
|
zeroValues := []any{
|
2020-03-09 08:03:59 +00:00
|
|
|
int8(0),
|
|
|
|
int16(0),
|
|
|
|
int(0),
|
|
|
|
int32(0),
|
|
|
|
int64(0),
|
|
|
|
uint8(0),
|
|
|
|
uint16(0),
|
|
|
|
uint(0),
|
|
|
|
uint32(0),
|
|
|
|
uint64(0),
|
|
|
|
MyInt(0),
|
|
|
|
reflect.ValueOf(0),
|
|
|
|
nil,
|
|
|
|
time.Time{},
|
|
|
|
&time.Time{},
|
|
|
|
nilTime,
|
|
|
|
ZeroStruct{},
|
|
|
|
&ZeroStruct{},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range zeroValues {
|
|
|
|
t.Run(fmt.Sprintf("%#v", v), func(t *testing.T) {
|
|
|
|
assert.True(t, IsZero(v))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsValueZero(t *testing.T) {
|
2023-10-28 10:59:32 +00:00
|
|
|
zeroReflectValues := []reflect.Value{
|
2020-03-09 08:03:59 +00:00
|
|
|
reflect.ValueOf(int8(0)),
|
|
|
|
reflect.ValueOf(int16(0)),
|
|
|
|
reflect.ValueOf(int(0)),
|
|
|
|
reflect.ValueOf(int32(0)),
|
|
|
|
reflect.ValueOf(int64(0)),
|
|
|
|
reflect.ValueOf(uint8(0)),
|
|
|
|
reflect.ValueOf(uint16(0)),
|
|
|
|
reflect.ValueOf(uint(0)),
|
|
|
|
reflect.ValueOf(uint32(0)),
|
|
|
|
reflect.ValueOf(uint64(0)),
|
|
|
|
reflect.ValueOf(MyInt(0)),
|
|
|
|
reflect.ValueOf(time.Time{}),
|
|
|
|
reflect.ValueOf(&time.Time{}),
|
|
|
|
reflect.ValueOf(nilTime),
|
|
|
|
reflect.ValueOf(ZeroStruct{}),
|
|
|
|
reflect.ValueOf(&ZeroStruct{}),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range zeroReflectValues {
|
|
|
|
t.Run(fmt.Sprintf("%#v", v), func(t *testing.T) {
|
|
|
|
assert.True(t, IsValueZero(v))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|