189 lines
		
	
	
		
			6.7 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			189 lines
		
	
	
		
			6.7 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
// Package consts ...
 | 
						|
//
 | 
						|
// Description : consts ...
 | 
						|
//
 | 
						|
// Author : go_developer@163.com<白茶清欢>
 | 
						|
//
 | 
						|
// Date : 2025-04-20 15:06
 | 
						|
package consts
 | 
						|
 | 
						|
import (
 | 
						|
	"encoding/json"
 | 
						|
	. "github.com/smartystreets/goconvey/convey"
 | 
						|
	"testing"
 | 
						|
)
 | 
						|
 | 
						|
func TestDataType_String(t *testing.T) {
 | 
						|
	Convey("data type 字符串值", t, func() {
 | 
						|
		byteData, err := json.Marshal(DataTypeString)
 | 
						|
		So(err, ShouldBeNil)
 | 
						|
		So(DataTypeString.String(), ShouldEqual, "string")
 | 
						|
		So(string(byteData), ShouldEqual, `"string"`)
 | 
						|
	})
 | 
						|
	Convey("data type MarshalJSON", t, func() {
 | 
						|
		str, err := DataTypeString.MarshalJSON()
 | 
						|
		So(err, ShouldBeNil)
 | 
						|
		So(string(str), ShouldEqual, `"string"`)
 | 
						|
		dataList := []DataType{DataTypeString}
 | 
						|
		jsonData, err := json.Marshal(dataList)
 | 
						|
		So(err, ShouldBeNil)
 | 
						|
		So(string(jsonData), ShouldEqual, `["string"]`)
 | 
						|
	})
 | 
						|
}
 | 
						|
 | 
						|
func TestDataType_IsValid(t *testing.T) {
 | 
						|
	Convey("data type 非法验证", t, func() {
 | 
						|
		So(DataType("Invalid").IsValid(), ShouldBeFalse)
 | 
						|
	})
 | 
						|
	Convey("data type 合法验证", t, func() {
 | 
						|
		So(DataType("string").IsValid(), ShouldBeTrue)
 | 
						|
	})
 | 
						|
}
 | 
						|
 | 
						|
func TestGetDataTypeDefaultValue(t *testing.T) {
 | 
						|
	Convey("int 默认值", t, func() {
 | 
						|
		So(GetDataTypeDefaultValue(DataTypeInt), ShouldEqual, 0)
 | 
						|
	})
 | 
						|
	Convey("int8 默认值", t, func() {
 | 
						|
		So(GetDataTypeDefaultValue(DataTypeInt8), ShouldEqual, int8(0))
 | 
						|
	})
 | 
						|
	Convey("int16 默认值", t, func() {
 | 
						|
		So(GetDataTypeDefaultValue(DataTypeInt16), ShouldEqual, int16(0))
 | 
						|
	})
 | 
						|
	Convey("int32 默认值", t, func() {
 | 
						|
		So(GetDataTypeDefaultValue(DataTypeInt32), ShouldEqual, int32(0))
 | 
						|
	})
 | 
						|
	Convey("int64 默认值", t, func() {
 | 
						|
		So(GetDataTypeDefaultValue(DataTypeInt64), ShouldEqual, int64(0))
 | 
						|
	})
 | 
						|
	Convey("uint 默认值", t, func() {
 | 
						|
		So(GetDataTypeDefaultValue(DataTypeUint), ShouldEqual, uint(0))
 | 
						|
	})
 | 
						|
	Convey("uint8 默认值", t, func() {
 | 
						|
		So(GetDataTypeDefaultValue(DataTypeUint8), ShouldEqual, uint8(0))
 | 
						|
	})
 | 
						|
	Convey("uint16 默认值", t, func() {
 | 
						|
		So(GetDataTypeDefaultValue(DataTypeUint16), ShouldEqual, uint16(0))
 | 
						|
	})
 | 
						|
	Convey("uint32 默认值", t, func() {
 | 
						|
		So(GetDataTypeDefaultValue(DataTypeUint32), ShouldEqual, uint32(0))
 | 
						|
	})
 | 
						|
	Convey("uint64 默认值", t, func() {
 | 
						|
		So(GetDataTypeDefaultValue(DataTypeUint64), ShouldEqual, uint64(0))
 | 
						|
	})
 | 
						|
	Convey("float32 默认值", t, func() {
 | 
						|
		So(GetDataTypeDefaultValue(DataTypeFloat32), ShouldEqual, float32(0))
 | 
						|
	})
 | 
						|
	Convey("uint64 默认值", t, func() {
 | 
						|
		So(GetDataTypeDefaultValue(DataTypeFloat64), ShouldEqual, float64(0))
 | 
						|
	})
 | 
						|
	Convey("bool 默认值", t, func() {
 | 
						|
		So(GetDataTypeDefaultValue(DataTypeBool), ShouldBeFalse)
 | 
						|
	})
 | 
						|
	Convey("string 默认值", t, func() {
 | 
						|
		So(GetDataTypeDefaultValue(DataTypeString), ShouldBeEmpty)
 | 
						|
	})
 | 
						|
	Convey("IntPtr 默认值", t, func() {
 | 
						|
		So(GetDataTypeDefaultValue(DataTypeIntPtr), ShouldEqual, new(int))
 | 
						|
	})
 | 
						|
	Convey("Int8Ptr 默认值", t, func() {
 | 
						|
		So(GetDataTypeDefaultValue(DataTypeInt8Ptr), ShouldEqual, new(int8))
 | 
						|
	})
 | 
						|
	Convey("Int16Ptr 默认值", t, func() {
 | 
						|
		So(GetDataTypeDefaultValue(DataTypeInt16Ptr), ShouldEqual, new(int16))
 | 
						|
	})
 | 
						|
	Convey("Int32Ptr 默认值", t, func() {
 | 
						|
		So(GetDataTypeDefaultValue(DataTypeInt32Ptr), ShouldEqual, new(int32))
 | 
						|
	})
 | 
						|
	Convey("Int64Ptr 默认值", t, func() {
 | 
						|
		So(GetDataTypeDefaultValue(DataTypeInt64Ptr), ShouldEqual, new(int64))
 | 
						|
	})
 | 
						|
	Convey("UintPtr 默认值", t, func() {
 | 
						|
		So(GetDataTypeDefaultValue(DataTypeUintPtr), ShouldEqual, new(uint))
 | 
						|
	})
 | 
						|
	Convey("Uint8Ptr 默认值", t, func() {
 | 
						|
		So(GetDataTypeDefaultValue(DataTypeUint8Ptr), ShouldEqual, new(uint8))
 | 
						|
	})
 | 
						|
	Convey("Uint16Ptr 默认值", t, func() {
 | 
						|
		So(GetDataTypeDefaultValue(DataTypeUint16Ptr), ShouldEqual, new(uint16))
 | 
						|
	})
 | 
						|
	Convey("Uint32Ptr 默认值", t, func() {
 | 
						|
		So(GetDataTypeDefaultValue(DataTypeUint32Ptr), ShouldEqual, new(uint32))
 | 
						|
	})
 | 
						|
	Convey("Uint64Ptr 默认值", t, func() {
 | 
						|
		So(GetDataTypeDefaultValue(DataTypeUint64Ptr), ShouldEqual, new(uint64))
 | 
						|
	})
 | 
						|
	Convey("Float32Ptr 默认值", t, func() {
 | 
						|
		So(GetDataTypeDefaultValue(DataTypeFloat32Ptr), ShouldEqual, new(float32))
 | 
						|
	})
 | 
						|
	Convey("Float64Ptr 默认值", t, func() {
 | 
						|
		So(GetDataTypeDefaultValue(DataTypeFloat64Ptr), ShouldEqual, new(float64))
 | 
						|
	})
 | 
						|
	Convey("BoolPtr 默认值", t, func() {
 | 
						|
		So(GetDataTypeDefaultValue(DataTypeBoolPtr), ShouldEqual, new(bool))
 | 
						|
	})
 | 
						|
	Convey("map[string]any 默认值", t, func() {
 | 
						|
		So(GetDataTypeDefaultValue(DataTypeMapStrAny), ShouldEqual, map[string]any{})
 | 
						|
	})
 | 
						|
	Convey("map[any]any 默认值", t, func() {
 | 
						|
		So(GetDataTypeDefaultValue(DataTypeMapAnyAny), ShouldEqual, map[any]any{})
 | 
						|
	})
 | 
						|
	Convey("map[string]bool 默认值", t, func() {
 | 
						|
		So(GetDataTypeDefaultValue(DataTypeMapStrBool), ShouldEqual, map[string]bool{})
 | 
						|
	})
 | 
						|
	Convey("map[string]int 默认值", t, func() {
 | 
						|
		So(GetDataTypeDefaultValue(DataTypeMapStrInt), ShouldEqual, map[string]int{})
 | 
						|
	})
 | 
						|
	Convey("map[string]float 默认值", t, func() {
 | 
						|
		So(GetDataTypeDefaultValue(DataTypeMapStrFloat), ShouldEqual, map[string]float64{})
 | 
						|
	})
 | 
						|
	Convey("map[string]uint 默认值", t, func() {
 | 
						|
		So(GetDataTypeDefaultValue(DataTypeMapStrUint), ShouldEqual, map[string]uint{})
 | 
						|
	})
 | 
						|
	Convey("map[string]slice 默认值", t, func() {
 | 
						|
		So(GetDataTypeDefaultValue(DataTypeMapStrSlice), ShouldEqual, map[string][]any{})
 | 
						|
	})
 | 
						|
	Convey("map[string]string 默认值", t, func() {
 | 
						|
		So(GetDataTypeDefaultValue(DataTypeMapStrStr), ShouldEqual, map[string]string{})
 | 
						|
	})
 | 
						|
	Convey("[]any 默认值", t, func() {
 | 
						|
		So(GetDataTypeDefaultValue(DataTypeSliceAny), ShouldEqual, []any{})
 | 
						|
	})
 | 
						|
	Convey("[]string 默认值", t, func() {
 | 
						|
		So(GetDataTypeDefaultValue(DataTypeSliceString), ShouldEqual, []string{})
 | 
						|
	})
 | 
						|
	Convey("[]bool 默认值", t, func() {
 | 
						|
		So(GetDataTypeDefaultValue(DataTypeSliceBool), ShouldEqual, []bool{})
 | 
						|
	})
 | 
						|
	Convey("[]int 默认值", t, func() {
 | 
						|
		So(GetDataTypeDefaultValue(DataTypeSliceInt), ShouldEqual, []int{})
 | 
						|
	})
 | 
						|
	Convey("[]uint 默认值", t, func() {
 | 
						|
		So(GetDataTypeDefaultValue(DataTypeSliceUint), ShouldEqual, []uint{})
 | 
						|
	})
 | 
						|
	Convey("[]float 默认值", t, func() {
 | 
						|
		So(GetDataTypeDefaultValue(DataTypeSliceFloat), ShouldEqual, []float64{})
 | 
						|
	})
 | 
						|
	Convey("[]map[string]any 默认值", t, func() {
 | 
						|
		So(GetDataTypeDefaultValue(DataTypeSliceMapStringAny), ShouldEqual, []map[string]any{})
 | 
						|
	})
 | 
						|
	Convey("[]map[any]any 默认值", t, func() {
 | 
						|
		So(GetDataTypeDefaultValue(DataTypeSliceMapAnyAny), ShouldEqual, []map[string]any{})
 | 
						|
	})
 | 
						|
	Convey("map marshal 默认值", t, func() {
 | 
						|
		So(GetDataTypeDefaultValue(DataTypeMapStrAnyWithMarshal), ShouldEqual, "{}")
 | 
						|
	})
 | 
						|
	Convey("slice marshal 默认值", t, func() {
 | 
						|
		So(GetDataTypeDefaultValue(DataTypeSliceBoolWithMarshal), ShouldEqual, "[]")
 | 
						|
	})
 | 
						|
	Convey("slice split 默认值", t, func() {
 | 
						|
		So(GetDataTypeDefaultValue(DataTypeSliceIntWithChar), ShouldEqual, "")
 | 
						|
	})
 | 
						|
	Convey("未枚举的alice 默认值", t, func() {
 | 
						|
		So(GetDataTypeDefaultValue(DataTypeSliceSlice), ShouldEqual, []any{})
 | 
						|
	})
 | 
						|
	Convey("unknown 默认值", t, func() {
 | 
						|
		So(GetDataTypeDefaultValue(DataType("unknown")), ShouldEqual, nil)
 | 
						|
	})
 | 
						|
}
 |