1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-16 18:52:27 +08:00

refactor: rewrite all unit test functions with assert

This commit is contained in:
dudaodong
2022-01-09 13:30:08 +08:00
parent 3438f3b18a
commit f490ef2404

View File

@@ -2,13 +2,14 @@ package convertor
import ( import (
"fmt" "fmt"
"reflect"
"testing" "testing"
"github.com/duke-git/lancet/internal" "github.com/duke-git/lancet/internal"
) )
func TestToChar(t *testing.T) { func TestToChar(t *testing.T) {
assert := internal.NewAssert(t, "TestToChar")
cases := []string{"", "abc", "1 2#3"} cases := []string{"", "abc", "1 2#3"}
expected := [][]string{ expected := [][]string{
{""}, {""},
@@ -16,28 +17,25 @@ func TestToChar(t *testing.T) {
{"1", " ", "2", "#", "3"}, {"1", " ", "2", "#", "3"},
} }
for i := 0; i < len(cases); i++ { for i := 0; i < len(cases); i++ {
res := ToChar(cases[i]) assert.Equal(expected[i], ToChar(cases[i]))
if !reflect.DeepEqual(res, expected[i]) {
internal.LogFailedTestInfo(t, "ToChar", cases[i], expected[i], res)
t.FailNow()
}
} }
} }
func TestToBool(t *testing.T) { func TestToBool(t *testing.T) {
assert := internal.NewAssert(t, "TestToBool")
cases := []string{"true", "True", "false", "False", "0", "1", "123"} cases := []string{"true", "True", "false", "False", "0", "1", "123"}
expected := []bool{true, true, false, false, false, true, false} expected := []bool{true, true, false, false, false, true, false}
for i := 0; i < len(cases); i++ { for i := 0; i < len(cases); i++ {
res, _ := ToBool(cases[i]) actual, _ := ToBool(cases[i])
if res != expected[i] { assert.Equal(expected[i], actual)
internal.LogFailedTestInfo(t, "ToBool", cases[i], expected[i], res)
t.FailNow()
}
} }
} }
func TestToBytes(t *testing.T) { func TestToBytes(t *testing.T) {
assert := internal.NewAssert(t, "TestToBytes")
cases := []interface{}{ cases := []interface{}{
0, 0,
false, false,
@@ -49,16 +47,14 @@ func TestToBytes(t *testing.T) {
{4, 12, 0, 1, 49}, {4, 12, 0, 1, 49},
} }
for i := 0; i < len(cases); i++ { for i := 0; i < len(cases); i++ {
res, _ := ToBytes(cases[i]) actual, _ := ToBytes(cases[i])
fmt.Println(res) assert.Equal(expected[i], actual)
if !reflect.DeepEqual(res, expected[i]) {
internal.LogFailedTestInfo(t, "ToBytes", cases[i], expected[i], res)
t.FailNow()
}
} }
} }
func TestToInt(t *testing.T) { func TestToInt(t *testing.T) {
assert := internal.NewAssert(t, "TestToInt")
cases := []interface{}{"123", "-123", 123, cases := []interface{}{"123", "-123", 123,
uint(123), uint8(123), uint16(123), uint32(123), uint64(123), uint(123), uint8(123), uint16(123), uint32(123), uint64(123),
float32(12.3), float64(12.3), 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} expected := []int64{123, -123, 123, 123, 123, 123, 123, 123, 12, 12, 0, 0, 0}
for i := 0; i < len(cases); i++ { for i := 0; i < len(cases); i++ {
res, _ := ToInt(cases[i]) actual, _ := ToInt(cases[i])
if res != expected[i] { assert.Equal(expected[i], actual)
internal.LogFailedTestInfo(t, "ToInt", cases[i], expected[i], res)
t.FailNow()
}
} }
} }
func TestToFloat(t *testing.T) { func TestToFloat(t *testing.T) {
assert := internal.NewAssert(t, "TestToFloat")
cases := []interface{}{ cases := []interface{}{
"", "-1", "-.11", "1.23e3", ".123e10", "abc", "", "-1", "-.11", "1.23e3", ".123e10", "abc",
int(0), int8(1), int16(-1), int32(123), int64(123), 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} 0, 1, -1, 123, 123, 123, 123, 123, 123, 123, 12.3, 12.300000190734863}
for i := 0; i < len(cases); i++ { for i := 0; i < len(cases); i++ {
res, _ := ToFloat(cases[i]) actual, _ := ToFloat(cases[i])
if res != expected[i] { assert.Equal(expected[i], actual)
internal.LogFailedTestInfo(t, "ToFloat", cases[i], expected[i], res)
t.FailNow()
}
} }
} }
func TestToString(t *testing.T) { func TestToString(t *testing.T) {
// map assert := internal.NewAssert(t, "TestToString")
aMap := make(map[string]int) aMap := make(map[string]int)
aMap["a"] = 1 aMap["a"] = 1
aMap["b"] = 2 aMap["b"] = 2
aMap["c"] = 3 aMap["c"] = 3
// struct
type TestStruct struct { type TestStruct struct {
Name string Name string
} }
@@ -124,75 +116,39 @@ func TestToString(t *testing.T) {
"[1,2,3]", "{\"a\":1,\"b\":2,\"c\":3}", "{\"Name\":\"TestStruct\"}", "hello"} "[1,2,3]", "{\"a\":1,\"b\":2,\"c\":3}", "{\"Name\":\"TestStruct\"}", "hello"}
for i := 0; i < len(cases); i++ { for i := 0; i < len(cases); i++ {
res := ToString(cases[i]) actual := ToString(cases[i])
if res != expected[i] { assert.Equal(expected[i], actual)
internal.LogFailedTestInfo(t, "ToString", cases[i], expected[i], res)
t.FailNow()
}
} }
} }
func TestToJson(t *testing.T) { func TestToJson(t *testing.T) {
// map assert := internal.NewAssert(t, "TestToJson")
aMap := make(map[string]int)
aMap["a"] = 1
aMap["b"] = 2
aMap["c"] = 3
mapJson := "{\"a\":1,\"b\":2,\"c\":3}" var aMap = map[string]int{"a": 1, "b": 2, "c": 3}
r1, _ := ToJson(aMap) mapJsonStr, _ := ToJson(aMap)
if r1 != mapJson { assert.Equal("{\"a\":1,\"b\":2,\"c\":3}", mapJsonStr)
internal.LogFailedTestInfo(t, "ToJson", aMap, mapJson, r1)
t.FailNow()
}
// struct
type TestStruct struct { type TestStruct struct {
Name string Name string
} }
aStruct := TestStruct{Name: "TestStruct"} aStruct := TestStruct{Name: "TestStruct"}
structJson := "{\"Name\":\"TestStruct\"}" structJsonStr, _ := ToJson(aStruct)
r2, _ := ToJson(aStruct) assert.Equal("{\"Name\":\"TestStruct\"}", structJsonStr)
if r2 != structJson {
internal.LogFailedTestInfo(t, "ToJson", aMap, mapJson, r1)
t.FailNow()
}
} }
func TestStructToMap(t *testing.T) { func TestStructToMap(t *testing.T) {
assert := internal.NewAssert(t, "TestStructToMap")
type People struct { type People struct {
Name string `json:"name"` Name string `json:"name"`
age int age int
} }
p := People{
p1 := People{
"test", "test",
100, 100,
} }
pm, _ := StructToMap(p)
pm1, _ := StructToMap(p1) var expected = map[string]interface{}{"name": "test"}
m1 := make(map[string]interface{}) assert.Equal(expected, pm)
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()
}
} }
func TestColorHexToRGB(t *testing.T) { func TestColorHexToRGB(t *testing.T) {
@@ -201,22 +157,17 @@ func TestColorHexToRGB(t *testing.T) {
colorRGB := fmt.Sprintf("%d,%d,%d", r, g, b) colorRGB := fmt.Sprintf("%d,%d,%d", r, g, b)
expected := "0,51,102" expected := "0,51,102"
if colorRGB != expected { assert := internal.NewAssert(t, "TestColorHexToRGB")
internal.LogFailedTestInfo(t, "ColorHexToRGB", colorHex, expected, colorRGB) assert.Equal(expected, colorRGB)
t.FailNow()
}
} }
func TestColorRGBToHex(t *testing.T) { func TestColorRGBToHex(t *testing.T) {
r := 0 r := 0
g := 51 g := 51
b := 102 b := 102
colorRGB := fmt.Sprintf("%d,%d,%d", r, g, b)
colorHex := ColorRGBToHex(r, g, b) colorHex := ColorRGBToHex(r, g, b)
expected := "#003366" expected := "#003366"
if colorHex != expected { assert := internal.NewAssert(t, "TestColorRGBToHex")
internal.LogFailedTestInfo(t, "ColorHexToRGB", colorRGB, expected, colorHex) assert.Equal(expected, colorHex)
t.FailNow()
}
} }