1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52:28 +08:00
Files
lancet/convertor/convertor_test.go

174 lines
4.0 KiB
Go

package convertor
import (
"fmt"
"testing"
"github.com/duke-git/lancet/internal"
)
func TestToChar(t *testing.T) {
assert := internal.NewAssert(t, "TestToChar")
cases := []string{"", "abc", "1 2#3"}
expected := [][]string{
{""},
{"a", "b", "c"},
{"1", " ", "2", "#", "3"},
}
for i := 0; i < len(cases); i++ {
assert.Equal(expected[i], ToChar(cases[i]))
}
}
func TestToBool(t *testing.T) {
assert := internal.NewAssert(t, "TestToBool")
cases := []string{"true", "True", "false", "False", "0", "1", "123"}
expected := []bool{true, true, false, false, false, true, false}
for i := 0; i < len(cases); i++ {
actual, _ := ToBool(cases[i])
assert.Equal(expected[i], actual)
}
}
func TestToBytes(t *testing.T) {
assert := internal.NewAssert(t, "TestToBytes")
cases := []interface{}{
0,
false,
"1",
}
expected := [][]byte{
{3, 4, 0, 0},
{3, 2, 0, 0},
{4, 12, 0, 1, 49},
}
for i := 0; i < len(cases); i++ {
actual, _ := ToBytes(cases[i])
assert.Equal(expected[i], actual)
}
}
func TestToInt(t *testing.T) {
assert := internal.NewAssert(t, "TestToInt")
cases := []interface{}{"123", "-123", 123,
uint(123), uint8(123), uint16(123), uint32(123), uint64(123),
float32(12.3), float64(12.3),
"abc", false, "111111111111111111111111111111111111111"}
expected := []int64{123, -123, 123, 123, 123, 123, 123, 123, 12, 12, 0, 0, 0}
for i := 0; i < len(cases); i++ {
actual, _ := ToInt(cases[i])
assert.Equal(expected[i], actual)
}
}
func TestToFloat(t *testing.T) {
assert := internal.NewAssert(t, "TestToFloat")
cases := []interface{}{
"", "-1", "-.11", "1.23e3", ".123e10", "abc",
int(0), int8(1), int16(-1), int32(123), int64(123),
uint(123), uint8(123), uint16(123), uint32(123), uint64(123),
float64(12.3), float32(12.3),
}
expected := []float64{0, -1, -0.11, 1230, 0.123e10, 0,
0, 1, -1, 123, 123, 123, 123, 123, 123, 123, 12.3, 12.300000190734863}
for i := 0; i < len(cases); i++ {
actual, _ := ToFloat(cases[i])
assert.Equal(expected[i], actual)
}
}
func TestToString(t *testing.T) {
assert := internal.NewAssert(t, "TestToString")
aMap := make(map[string]int)
aMap["a"] = 1
aMap["b"] = 2
aMap["c"] = 3
type TestStruct struct {
Name string
}
aStruct := TestStruct{Name: "TestStruct"}
cases := []interface{}{
"", nil,
int(0), int8(1), int16(-1), int32(123), int64(123),
uint(123), uint8(123), uint16(123), uint32(123), uint64(123),
float64(12.3), float32(12.3),
true, false,
[]int{1, 2, 3}, aMap, aStruct, []byte{104, 101, 108, 108, 111}}
expected := []string{
"", "",
"0", "1", "-1",
"123", "123", "123", "123", "123", "123", "123",
"12.3", "12.300000190734863",
"true", "false",
"[1,2,3]", "{\"a\":1,\"b\":2,\"c\":3}", "{\"Name\":\"TestStruct\"}", "hello"}
for i := 0; i < len(cases); i++ {
actual := ToString(cases[i])
assert.Equal(expected[i], actual)
}
}
func TestToJson(t *testing.T) {
assert := internal.NewAssert(t, "TestToJson")
var aMap = map[string]int{"a": 1, "b": 2, "c": 3}
mapJsonStr, _ := ToJson(aMap)
assert.Equal("{\"a\":1,\"b\":2,\"c\":3}", mapJsonStr)
type TestStruct struct {
Name string
}
aStruct := TestStruct{Name: "TestStruct"}
structJsonStr, _ := ToJson(aStruct)
assert.Equal("{\"Name\":\"TestStruct\"}", structJsonStr)
}
func TestStructToMap(t *testing.T) {
assert := internal.NewAssert(t, "TestStructToMap")
type People struct {
Name string `json:"name"`
age int
}
p := People{
"test",
100,
}
pm, _ := StructToMap(p)
var expected = map[string]interface{}{"name": "test"}
assert.Equal(expected, pm)
}
func TestColorHexToRGB(t *testing.T) {
colorHex := "#003366"
r, g, b := ColorHexToRGB(colorHex)
colorRGB := fmt.Sprintf("%d,%d,%d", r, g, b)
expected := "0,51,102"
assert := internal.NewAssert(t, "TestColorHexToRGB")
assert.Equal(expected, colorRGB)
}
func TestColorRGBToHex(t *testing.T) {
r := 0
g := 51
b := 102
colorHex := ColorRGBToHex(r, g, b)
expected := "#003366"
assert := internal.NewAssert(t, "TestColorRGBToHex")
assert.Equal(expected, colorHex)
}