gohelpers/pkg/convertor/convertor_test.go
2022-05-10 14:46:32 +08:00

163 lines
3.7 KiB
Go

package convertor
import (
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
func TestToChar(t *testing.T) {
cases := []string{"", "abc", "1 2#3"}
expected := [][]string{
{""},
{"a", "b", "c"},
{"1", " ", "2", "#", "3"},
}
for i := 0; i < len(cases); i++ {
require.Equal(t, expected[i], ToChar(cases[i]))
}
}
func TestToBool(t *testing.T) {
cases := []string{"1", "true", "True", "false", "False", "0", "123", "0.0", "abc"}
expected := []bool{true, true, true, false, false, false, false, false, false}
for i := 0; i < len(cases); i++ {
actual, _ := ToBool(cases[i])
require.Equal(t, expected[i], actual)
}
}
func TestToBytes(t *testing.T) {
cases := []any{
0,
false,
"1",
}
expected := [][]byte{
{0, 0, 0, 0, 0, 0, 0, 0},
{102, 97, 108, 115, 101},
{49},
}
for i := 0; i < len(cases); i++ {
actual, _ := ToBytes(cases[i])
require.Equal(t, expected[i], actual)
}
bytesData, err := ToBytes("abc")
if err != nil {
t.Error(err)
t.Fail()
}
require.Equal(t, "abc", ToString(bytesData))
}
func TestToInt(t *testing.T) {
cases := []any{"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])
require.Equal(t, expected[i], actual)
}
}
func TestToFloat(t *testing.T) {
cases := []any{
"", "-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])
require.Equal(t, expected[i], actual)
}
}
func TestToString(t *testing.T) {
aMap := make(map[string]int)
aMap["a"] = 1
aMap["b"] = 2
aMap["c"] = 3
type TestStruct struct {
Name string
}
aStruct := TestStruct{Name: "TestStruct"}
cases := []any{
"", 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])
require.Equal(t, expected[i], actual)
}
}
func TestToJson(t *testing.T) {
var aMap = map[string]int{"a": 1, "b": 2, "c": 3}
mapJsonStr, _ := ToJson(aMap)
require.Equal(t, "{\"a\":1,\"b\":2,\"c\":3}", mapJsonStr)
type TestStruct struct {
Name string
}
aStruct := TestStruct{Name: "TestStruct"}
structJsonStr, _ := ToJson(aStruct)
require.Equal(t, "{\"Name\":\"TestStruct\"}", structJsonStr)
}
func TestStructToMap(t *testing.T) {
type People struct {
Name string `json:"name"`
age int
}
p := People{
"test",
100,
}
pm, _ := StructToMap(p)
var expected = map[string]any{"name": "test"}
require.Equal(t, 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"
require.Equal(t, expected, colorRGB)
}
func TestColorRGBToHex(t *testing.T) {
r := 0
g := 51
b := 102
colorHex := ColorRGBToHex(r, g, b)
expected := "#003366"
require.Equal(t, expected, colorHex)
}