mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-06 13:42:28 +08:00
refactor: rewrite all unit test functions with assert
This commit is contained in:
@@ -2,13 +2,14 @@ package convertor
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"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{
|
||||
{""},
|
||||
@@ -16,28 +17,25 @@ func TestToChar(t *testing.T) {
|
||||
{"1", " ", "2", "#", "3"},
|
||||
}
|
||||
for i := 0; i < len(cases); i++ {
|
||||
res := ToChar(cases[i])
|
||||
if !reflect.DeepEqual(res, expected[i]) {
|
||||
internal.LogFailedTestInfo(t, "ToChar", cases[i], expected[i], res)
|
||||
t.FailNow()
|
||||
}
|
||||
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++ {
|
||||
res, _ := ToBool(cases[i])
|
||||
if res != expected[i] {
|
||||
internal.LogFailedTestInfo(t, "ToBool", cases[i], expected[i], res)
|
||||
t.FailNow()
|
||||
}
|
||||
actual, _ := ToBool(cases[i])
|
||||
assert.Equal(expected[i], actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestToBytes(t *testing.T) {
|
||||
assert := internal.NewAssert(t, "TestToBytes")
|
||||
|
||||
cases := []interface{}{
|
||||
0,
|
||||
false,
|
||||
@@ -49,16 +47,14 @@ func TestToBytes(t *testing.T) {
|
||||
{4, 12, 0, 1, 49},
|
||||
}
|
||||
for i := 0; i < len(cases); i++ {
|
||||
res, _ := ToBytes(cases[i])
|
||||
fmt.Println(res)
|
||||
if !reflect.DeepEqual(res, expected[i]) {
|
||||
internal.LogFailedTestInfo(t, "ToBytes", cases[i], expected[i], res)
|
||||
t.FailNow()
|
||||
}
|
||||
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),
|
||||
@@ -67,15 +63,14 @@ func TestToInt(t *testing.T) {
|
||||
expected := []int64{123, -123, 123, 123, 123, 123, 123, 123, 12, 12, 0, 0, 0}
|
||||
|
||||
for i := 0; i < len(cases); i++ {
|
||||
res, _ := ToInt(cases[i])
|
||||
if res != expected[i] {
|
||||
internal.LogFailedTestInfo(t, "ToInt", cases[i], expected[i], res)
|
||||
t.FailNow()
|
||||
}
|
||||
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),
|
||||
@@ -86,22 +81,19 @@ func TestToFloat(t *testing.T) {
|
||||
0, 1, -1, 123, 123, 123, 123, 123, 123, 123, 12.3, 12.300000190734863}
|
||||
|
||||
for i := 0; i < len(cases); i++ {
|
||||
res, _ := ToFloat(cases[i])
|
||||
if res != expected[i] {
|
||||
internal.LogFailedTestInfo(t, "ToFloat", cases[i], expected[i], res)
|
||||
t.FailNow()
|
||||
}
|
||||
actual, _ := ToFloat(cases[i])
|
||||
assert.Equal(expected[i], actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestToString(t *testing.T) {
|
||||
// map
|
||||
assert := internal.NewAssert(t, "TestToString")
|
||||
|
||||
aMap := make(map[string]int)
|
||||
aMap["a"] = 1
|
||||
aMap["b"] = 2
|
||||
aMap["c"] = 3
|
||||
|
||||
// struct
|
||||
type TestStruct struct {
|
||||
Name string
|
||||
}
|
||||
@@ -124,75 +116,39 @@ func TestToString(t *testing.T) {
|
||||
"[1,2,3]", "{\"a\":1,\"b\":2,\"c\":3}", "{\"Name\":\"TestStruct\"}", "hello"}
|
||||
|
||||
for i := 0; i < len(cases); i++ {
|
||||
res := ToString(cases[i])
|
||||
if res != expected[i] {
|
||||
internal.LogFailedTestInfo(t, "ToString", cases[i], expected[i], res)
|
||||
t.FailNow()
|
||||
}
|
||||
actual := ToString(cases[i])
|
||||
assert.Equal(expected[i], actual)
|
||||
}
|
||||
}
|
||||
func TestToJson(t *testing.T) {
|
||||
// map
|
||||
aMap := make(map[string]int)
|
||||
aMap["a"] = 1
|
||||
aMap["b"] = 2
|
||||
aMap["c"] = 3
|
||||
assert := internal.NewAssert(t, "TestToJson")
|
||||
|
||||
mapJson := "{\"a\":1,\"b\":2,\"c\":3}"
|
||||
r1, _ := ToJson(aMap)
|
||||
if r1 != mapJson {
|
||||
internal.LogFailedTestInfo(t, "ToJson", aMap, mapJson, r1)
|
||||
t.FailNow()
|
||||
}
|
||||
var aMap = map[string]int{"a": 1, "b": 2, "c": 3}
|
||||
mapJsonStr, _ := ToJson(aMap)
|
||||
assert.Equal("{\"a\":1,\"b\":2,\"c\":3}", mapJsonStr)
|
||||
|
||||
// struct
|
||||
type TestStruct struct {
|
||||
Name string
|
||||
}
|
||||
aStruct := TestStruct{Name: "TestStruct"}
|
||||
structJson := "{\"Name\":\"TestStruct\"}"
|
||||
r2, _ := ToJson(aStruct)
|
||||
if r2 != structJson {
|
||||
internal.LogFailedTestInfo(t, "ToJson", aMap, mapJson, r1)
|
||||
t.FailNow()
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
p1 := People{
|
||||
p := People{
|
||||
"test",
|
||||
100,
|
||||
}
|
||||
|
||||
pm1, _ := StructToMap(p1)
|
||||
m1 := make(map[string]interface{})
|
||||
m1["name"] = "test"
|
||||
//exp1["100"] = 100
|
||||
|
||||
if !reflect.DeepEqual(pm1, m1) {
|
||||
internal.LogFailedTestInfo(t, "StructToMap", p1, m1, pm1)
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
p2 := People{
|
||||
"test",
|
||||
100,
|
||||
}
|
||||
|
||||
pm2, _ := StructToMap(p1)
|
||||
m2 := make(map[string]interface{})
|
||||
m2["name"] = "test"
|
||||
m2["100"] = 100
|
||||
|
||||
if reflect.DeepEqual(pm2, m2) {
|
||||
internal.LogFailedTestInfo(t, "StructToMap", p2, m2, pm2)
|
||||
t.FailNow()
|
||||
}
|
||||
pm, _ := StructToMap(p)
|
||||
var expected = map[string]interface{}{"name": "test"}
|
||||
assert.Equal(expected, pm)
|
||||
}
|
||||
|
||||
func TestColorHexToRGB(t *testing.T) {
|
||||
@@ -201,22 +157,17 @@ func TestColorHexToRGB(t *testing.T) {
|
||||
colorRGB := fmt.Sprintf("%d,%d,%d", r, g, b)
|
||||
expected := "0,51,102"
|
||||
|
||||
if colorRGB != expected {
|
||||
internal.LogFailedTestInfo(t, "ColorHexToRGB", colorHex, expected, colorRGB)
|
||||
t.FailNow()
|
||||
}
|
||||
assert := internal.NewAssert(t, "TestColorHexToRGB")
|
||||
assert.Equal(expected, colorRGB)
|
||||
}
|
||||
|
||||
func TestColorRGBToHex(t *testing.T) {
|
||||
r := 0
|
||||
g := 51
|
||||
b := 102
|
||||
colorRGB := fmt.Sprintf("%d,%d,%d", r, g, b)
|
||||
colorHex := ColorRGBToHex(r, g, b)
|
||||
expected := "#003366"
|
||||
|
||||
if colorHex != expected {
|
||||
internal.LogFailedTestInfo(t, "ColorHexToRGB", colorRGB, expected, colorHex)
|
||||
t.FailNow()
|
||||
}
|
||||
assert := internal.NewAssert(t, "TestColorRGBToHex")
|
||||
assert.Equal(expected, colorHex)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user