Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ut cases for package gconv #2272

Merged
merged 7 commits into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions util/gconv/gconv_z_unit_all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package gconv_test

import (
"github.com/gogf/gf/v2/container/gvar"
"math"
"testing"
"time"
Expand Down Expand Up @@ -583,6 +584,7 @@ func Test_Float32_All(t *testing.T) {
t.AssertEQ(gconv.Float32([2]int{1, 2}), float32(0))
t.AssertEQ(gconv.Float32([]interface{}{}), float32(0))
t.AssertEQ(gconv.Float32([]map[int]int{}), float32(0))
t.AssertEQ(gconv.Float32(gvar.New(float32(0))), float32(0))

var countryCapitalMap = make(map[string]string)
/* map插入key - value对,各个国家对应的首都 */
Expand Down Expand Up @@ -618,6 +620,7 @@ func Test_Float64_All(t *testing.T) {
t.AssertEQ(gconv.Float64([2]int{1, 2}), float64(0))
t.AssertEQ(gconv.Float64([]interface{}{}), float64(0))
t.AssertEQ(gconv.Float64([]map[int]int{}), float64(0))
t.AssertEQ(gconv.Float64(gvar.New(float64(0))), float64(0))

var countryCapitalMap = make(map[string]string)
/* map插入key - value对,各个国家对应的首都 */
Expand Down Expand Up @@ -716,6 +719,7 @@ func Test_Bytes_All(t *testing.T) {
t.AssertEQ(gconv.Bytes(int32(0)), []uint8{0, 0, 0, 0})
t.AssertEQ(gconv.Bytes("s"), []uint8{115})
t.AssertEQ(gconv.Bytes([]byte("s")), []uint8{115})
t.AssertEQ(gconv.Bytes(gvar.New([]byte("s"))), []uint8{115})
})
}

Expand Down Expand Up @@ -748,6 +752,7 @@ func Test_Convert_All(t *testing.T) {
t.AssertEQ(gconv.Convert([]byte{}, "[]byte"), []uint8{})
t.AssertEQ(gconv.Convert([]string{}, "[]string"), []string{})
t.AssertEQ(gconv.Convert([2]int{1, 2}, "[]int"), []int{1, 2})
t.AssertEQ(gconv.Convert([2]uint8{1, 2}, "[]uint8"), []uint8{1, 2})
t.AssertEQ(gconv.Convert("1989-01-02", "Time", "Y-m-d"), gconv.Time("1989-01-02", "Y-m-d"))
t.AssertEQ(gconv.Convert(1989, "Time"), gconv.Time("1970-01-01 08:33:09 +0800 CST"))
t.AssertEQ(gconv.Convert(gtime.Now(), "gtime.Time", 1), *gtime.New())
Expand All @@ -758,6 +763,63 @@ func Test_Convert_All(t *testing.T) {
t.AssertEQ(gconv.Convert(1989, "Duration"), time.Duration(int64(1989)))
t.AssertEQ(gconv.Convert("1989", "Duration"), time.Duration(int64(1989)))
t.AssertEQ(gconv.Convert("1989", ""), "1989")

var intNum int = 1
t.Assert(gconv.Convert(&intNum, "*int"), int(1))
var int8Num int8 = 1
t.Assert(gconv.Convert(int8Num, "*int8"), int(1))
t.Assert(gconv.Convert(&int8Num, "*int8"), int(1))
var int16Num int16 = 1
t.Assert(gconv.Convert(int16Num, "*int16"), int(1))
t.Assert(gconv.Convert(&int16Num, "*int16"), int(1))
var int32Num int32 = 1
t.Assert(gconv.Convert(int32Num, "*int32"), int(1))
t.Assert(gconv.Convert(&int32Num, "*int32"), int(1))
var int64Num int64 = 1
t.Assert(gconv.Convert(int64Num, "*int64"), int(1))
t.Assert(gconv.Convert(&int64Num, "*int64"), int(1))

var uintNum uint = 1
t.Assert(gconv.Convert(&uintNum, "*uint"), int(1))
var uint8Num uint8 = 1
t.Assert(gconv.Convert(uint8Num, "*uint8"), int(1))
t.Assert(gconv.Convert(&uint8Num, "*uint8"), int(1))
var uint16Num uint16 = 1
t.Assert(gconv.Convert(uint16Num, "*uint16"), int(1))
t.Assert(gconv.Convert(&uint16Num, "*uint16"), int(1))
var uint32Num uint32 = 1
t.Assert(gconv.Convert(uint32Num, "*uint32"), int(1))
t.Assert(gconv.Convert(&uint32Num, "*uint32"), int(1))
var uint64Num uint64 = 1
t.Assert(gconv.Convert(uint64Num, "*uint64"), int(1))
t.Assert(gconv.Convert(&uint64Num, "*uint64"), int(1))

var float32Num float32 = 1.1
t.Assert(gconv.Convert(float32Num, "*float32"), float32(1.1))
t.Assert(gconv.Convert(&float32Num, "*float32"), float32(1.1))

var float64Num float64 = 1.1
t.Assert(gconv.Convert(float64Num, "*float64"), float64(1.1))
t.Assert(gconv.Convert(&float64Num, "*float64"), float64(1.1))

var boolValue bool = true
t.Assert(gconv.Convert(boolValue, "*bool"), true)
t.Assert(gconv.Convert(&boolValue, "*bool"), true)

var stringValue string = "1"
t.Assert(gconv.Convert(stringValue, "*string"), "1")
t.Assert(gconv.Convert(&stringValue, "*string"), "1")

var durationValue time.Duration = 1989
var expectDurationValue = time.Duration(int64(1989))
t.AssertEQ(gconv.Convert(&durationValue, "*time.Duration"), &expectDurationValue)
t.AssertEQ(gconv.Convert(durationValue, "*time.Duration"), &expectDurationValue)

var string_interface_map = map[string]interface{}{"k1": 1}
var string_int_map = map[string]int{"k1": 1}
var string_string_map = map[string]string{"k1": "1"}
t.AssertEQ(gconv.Convert(string_int_map, "map[string]string"), string_string_map)
t.AssertEQ(gconv.Convert(string_int_map, "map[string]interface{}"), string_interface_map)
})
}

Expand Down
129 changes: 129 additions & 0 deletions util/gconv/gconv_z_unit_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ func Test_Map_Basic(t *testing.T) {
t.Assert(gconv.Map(m3), g.Map{
"1.22": "3.1",
})
t.Assert(gconv.Map(`{"name":"goframe"}`), g.Map{
"name": "goframe",
})
t.Assert(gconv.Map(`{"name":"goframe"`), nil)
t.Assert(gconv.Map(`{goframe}`), nil)
t.Assert(gconv.Map([]byte(`{"name":"goframe"}`)), g.Map{
"name": "goframe",
})
t.Assert(gconv.Map([]byte(`{"name":"goframe"`)), nil)
t.Assert(gconv.Map([]byte(`{goframe}`)), nil)
})
}

Expand Down Expand Up @@ -69,6 +79,44 @@ func Test_Maps_Basic(t *testing.T) {
t.Assert(list[0]["id"], 100)
t.Assert(list[1]["id"], 200)
})
gtest.C(t, func(t *gtest.T) {
list := gconv.SliceMap(params)
t.Assert(len(list), 2)
t.Assert(list[0]["id"], 100)
t.Assert(list[1]["id"], 200)
})
gtest.C(t, func(t *gtest.T) {
list := gconv.SliceMapDeep(params)
t.Assert(len(list), 2)
t.Assert(list[0]["id"], 100)
t.Assert(list[1]["id"], 200)
})
gtest.C(t, func(t *gtest.T) {
type Base struct {
Age int
}
type User struct {
Id int
Name string
Base
}

users := make([]User, 0)
params := []g.Map{
{"id": 1, "name": "john", "age": 18},
{"id": 2, "name": "smith", "age": 20},
}
err := gconv.SliceStruct(params, &users)
t.AssertNil(err)
t.Assert(len(users), 2)
t.Assert(users[0].Id, params[0]["id"])
t.Assert(users[0].Name, params[0]["name"])
t.Assert(users[0].Age, 18)

t.Assert(users[1].Id, params[1]["id"])
t.Assert(users[1].Name, params[1]["name"])
t.Assert(users[1].Age, 20)
})
}

func Test_Maps_JsonStr(t *testing.T) {
Expand All @@ -78,6 +126,18 @@ func Test_Maps_JsonStr(t *testing.T) {
t.Assert(len(list), 2)
t.Assert(list[0]["id"], 100)
t.Assert(list[1]["id"], 200)

list = gconv.Maps([]byte(jsonStr))
t.Assert(len(list), 2)
t.Assert(list[0]["id"], 100)
t.Assert(list[1]["id"], 200)
})

gtest.C(t, func(t *gtest.T) {
t.Assert(gconv.Maps(`[id]`), nil)
t.Assert(gconv.Maps(`test`), nil)
t.Assert(gconv.Maps([]byte(`[id]`)), nil)
t.Assert(gconv.Maps([]byte(`test`)), nil)
})
}

Expand Down Expand Up @@ -477,3 +537,72 @@ field3:
t.Assert(string(jsonData), `{"field3":{"123":"integer_key"},"outer_struct":{"field1":{"inner1":123,"inner2":345},"field2":{"inner1":123,"inner2":345,"inner3":456,"inner4":789}}}`)
})
}

func TestMapStrStr(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
t.Assert(gconv.MapStrStr(map[string]string{"k": "v"}), map[string]string{"k": "v"})
t.Assert(gconv.MapStrStr(`{}`), nil)
})
}

func TestMapStrStrDeep(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
t.Assert(gconv.MapStrStrDeep(map[string]string{"k": "v"}), map[string]string{"k": "v"})
t.Assert(gconv.MapStrStrDeep(`{"k":"v"}`), map[string]string{"k": "v"})
t.Assert(gconv.MapStrStrDeep(`{}`), nil)
})
}

func TestMapsDeep(t *testing.T) {
jsonStr := `[{"id":100, "name":"john"},{"id":200, "name":"smith"}]`
params := g.Slice{
g.Map{"id": 100, "name": "john"},
g.Map{"id": 200, "name": "smith"},
}

gtest.C(t, func(t *gtest.T) {
t.Assert(gconv.MapsDeep(nil), nil)
})

gtest.C(t, func(t *gtest.T) {
list := gconv.MapsDeep(params)
t.Assert(len(list), 2)
t.Assert(list[0]["id"], 100)
t.Assert(list[1]["id"], 200)
})

gtest.C(t, func(t *gtest.T) {
list := gconv.MapsDeep(jsonStr)
t.Assert(len(list), 2)
t.Assert(list[0]["id"], 100)
t.Assert(list[1]["id"], 200)

list = gconv.MapsDeep([]byte(jsonStr))
t.Assert(len(list), 2)
t.Assert(list[0]["id"], 100)
t.Assert(list[1]["id"], 200)
})

gtest.C(t, func(t *gtest.T) {
t.Assert(gconv.MapsDeep(`[id]`), nil)
t.Assert(gconv.MapsDeep(`test`), nil)
t.Assert(gconv.MapsDeep([]byte(`[id]`)), nil)
t.Assert(gconv.MapsDeep([]byte(`test`)), nil)
t.Assert(gconv.MapsDeep([]string{}), nil)
})

gtest.C(t, func(t *gtest.T) {
string_interface_map_list := []map[string]interface{}{}
string_interface_map_list = append(string_interface_map_list, map[string]interface{}{"id": 100})
string_interface_map_list = append(string_interface_map_list, map[string]interface{}{"id": 200})
list := gconv.MapsDeep(string_interface_map_list)
t.Assert(len(list), 2)
t.Assert(list[0]["id"], 100)
t.Assert(list[1]["id"], 200)

list = gconv.MapsDeep([]byte(jsonStr))
t.Assert(len(list), 2)
t.Assert(list[0]["id"], 100)
t.Assert(list[1]["id"], 200)
})
}
24 changes: 24 additions & 0 deletions util/gconv/gconv_z_unit_maptomap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@ func Test_MapToMap1(t *testing.T) {
t.Assert(m2["k1"], m1["k1"])
t.Assert(m2["k2"], m1["k2"])
})
// string -> map[string]interface{}
gtest.C(t, func(t *gtest.T) {
jsonStr := `{"id":100, "name":"john"}`

m1 := g.MapStrAny{}
t.Assert(gconv.MapToMap(jsonStr, &m1), nil)
t.Assert(m1["id"], 100)

m2 := g.MapStrAny{}
t.Assert(gconv.MapToMap([]byte(jsonStr), &m2), nil)
t.Assert(m2["id"], 100)
})
}

func Test_MapToMap2(t *testing.T) {
Expand Down Expand Up @@ -162,6 +174,18 @@ func Test_MapToMaps(t *testing.T) {
t.Assert(len(s), 2)
t.Assert(s, params)
})
gtest.C(t, func(t *gtest.T) {
jsonStr := `[{"id":100, "name":"john"},{"id":200, "name":"smith"}]`

var m1 []g.Map
t.Assert(gconv.MapToMaps(jsonStr, &m1), nil)
t.Assert(m1[0]["id"], 100)
t.Assert(m1[1]["id"], 200)

t.Assert(gconv.MapToMaps([]byte(jsonStr), &m1), nil)
t.Assert(m1[0]["id"], 100)
t.Assert(m1[1]["id"], 200)
})
}

func Test_MapToMaps_StructParams(t *testing.T) {
Expand Down
10 changes: 8 additions & 2 deletions util/gconv/gconv_z_unit_string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
package gconv_test

import (
"testing"

"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/test/gtest"
"github.com/gogf/gf/v2/util/gconv"
"testing"
"time"
)

type stringStruct1 struct {
Expand Down Expand Up @@ -61,5 +62,10 @@ func Test_String(t *testing.T) {

t.AssertEQ(gconv.String(stringStruct2{"john"}), `{"Name":"john"}`)
t.AssertEQ(gconv.String(&stringStruct2{"john"}), `{"Name":"john"}`)

var nilTime *time.Time = nil
t.AssertEQ(gconv.String(nilTime), "")
var nilGTime *gtime.Time = nil
t.AssertEQ(gconv.String(nilGTime), "")
})
}