mirror of
https://github.com/jefferyjob/go-easy-utils.git
synced 2025-10-17 20:40:47 +08:00
unit test jsonToStruct
This commit is contained in:
128
jsonUtil/to_uint_test.go
Normal file
128
jsonUtil/to_uint_test.go
Normal file
@@ -0,0 +1,128 @@
|
||||
package jsonUtil
|
||||
|
||||
import (
|
||||
"github.com/jefferyjob/go-easy-utils"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestToUint64(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input interface{}
|
||||
want uint64
|
||||
wantError error
|
||||
}{
|
||||
{
|
||||
name: "Test uint8",
|
||||
input: uint8(42),
|
||||
want: 42,
|
||||
},
|
||||
{
|
||||
name: "Test uint16",
|
||||
input: uint16(42),
|
||||
want: 42,
|
||||
},
|
||||
{
|
||||
name: "Test uint32",
|
||||
input: uint32(42),
|
||||
want: 42,
|
||||
},
|
||||
{
|
||||
name: "Test uint64",
|
||||
input: uint64(42),
|
||||
want: 42,
|
||||
},
|
||||
{
|
||||
name: "Test int8",
|
||||
input: int8(42),
|
||||
want: 42,
|
||||
},
|
||||
{
|
||||
name: "Test int16",
|
||||
input: int16(42),
|
||||
want: 42,
|
||||
},
|
||||
{
|
||||
name: "Test int32",
|
||||
input: int32(42),
|
||||
want: 42,
|
||||
},
|
||||
{
|
||||
name: "Test int64",
|
||||
input: int64(42),
|
||||
want: 42,
|
||||
},
|
||||
{
|
||||
name: "Test float32",
|
||||
input: float32(42.0),
|
||||
want: 42,
|
||||
},
|
||||
{
|
||||
name: "Test float64",
|
||||
input: float64(42.0),
|
||||
want: 42,
|
||||
},
|
||||
{
|
||||
name: "Test complex64",
|
||||
input: complex64(complex(42, 0)),
|
||||
want: 42,
|
||||
},
|
||||
{
|
||||
name: "Test complex128",
|
||||
input: complex128(complex(42, 0)),
|
||||
want: 42,
|
||||
},
|
||||
{
|
||||
name: "Test string",
|
||||
input: "42",
|
||||
want: 42,
|
||||
},
|
||||
{
|
||||
name: "Test invalid string",
|
||||
input: "not a number",
|
||||
wantError: go_easy_utils.ErrSyntax,
|
||||
},
|
||||
{
|
||||
name: "Test nil pointer",
|
||||
input: (*int)(nil),
|
||||
want: 0,
|
||||
wantError: nil,
|
||||
},
|
||||
{
|
||||
name: "Test bool true",
|
||||
input: true,
|
||||
want: 1,
|
||||
},
|
||||
{
|
||||
name: "Test bool false",
|
||||
input: false,
|
||||
want: 0,
|
||||
},
|
||||
{
|
||||
name: "Test invalid type",
|
||||
input: make(chan int),
|
||||
wantError: go_easy_utils.ErrType,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := toUint64(tt.input)
|
||||
if err != tt.wantError {
|
||||
t.Errorf("toUint64(%v) error = %v, wantError %v", tt.input, err, tt.wantError)
|
||||
}
|
||||
if got != tt.want {
|
||||
t.Errorf("toUint64(%v) = %v, want %v", tt.input, got, tt.want)
|
||||
}
|
||||
|
||||
// Reflect
|
||||
got2, err2 := toUint64Reflect(tt.input)
|
||||
if err2 != tt.wantError {
|
||||
t.Errorf("toUint64Reflect(%v) error = %v, wantError %v", tt.input, err2, tt.wantError)
|
||||
}
|
||||
if got2 != tt.want {
|
||||
t.Errorf("toUint64Reflect(%v) = %v, want %v", tt.input, got2, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user