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

refactor(util/gconv): refactor code unit testing #3591

Merged
merged 47 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
104a548
-v
oldme-git May 14, 2024
1e3de90
exit
oldme-git May 14, 2024
f0ebd16
Enhance/unittest/gconv (#6)
oldme-git May 14, 2024
7fef2cd
bool done
oldme-git May 14, 2024
881fbf9
int done
oldme-git May 14, 2024
adca8a8
map and uint
oldme-git May 14, 2024
e16d8bd
map and uint
oldme-git May 14, 2024
297f886
int
oldme-git May 14, 2024
f4c5da4
reset code source
oldme-git May 14, 2024
39682be
scan
oldme-git May 14, 2024
8363f91
map
oldme-git May 14, 2024
43417c5
map
oldme-git May 15, 2024
98d6e2d
还原c-main
oldme-git May 15, 2024
1395f56
屏蔽gtime
oldme-git May 15, 2024
ebcc307
scan and map
oldme-git May 15, 2024
645a91f
map nil
oldme-git May 15, 2024
f23dc16
byte rune string int
oldme-git May 16, 2024
e9ecf77
sliceStr convert
oldme-git May 16, 2024
21bc85e
sliceStr convert
oldme-git May 16, 2024
2fedeb9
convert string
oldme-git May 16, 2024
d5f6351
ptr scanlist
oldme-git May 17, 2024
ddd6eab
slice
oldme-git May 17, 2024
023de73
slice unsafe
oldme-git May 17, 2024
7008c53
slice unsafe
oldme-git May 17, 2024
f364b5c
scan_list
oldme-git May 17, 2024
615f005
scanlist code
oldme-git May 17, 2024
02e20e8
还原code
oldme-git May 17, 2024
21ed184
time struct
oldme-git May 17, 2024
fc939a5
恢复代码
oldme-git May 17, 2024
63e8638
issue
oldme-git May 17, 2024
c788e1c
up
oldme-git May 18, 2024
1722b10
up
oldme-git May 18, 2024
0948760
up
oldme-git May 18, 2024
568800d
up
oldme-git May 18, 2024
1e2de79
TODO: 上传其他分支codecov
oldme-git May 18, 2024
c53beaf
TODO
oldme-git May 18, 2024
930a72f
up
oldme-git May 18, 2024
5488ffa
up
oldme-git May 19, 2024
b5bf22f
up
oldme-git May 19, 2024
9cdaf97
up
oldme-git May 19, 2024
acb305d
up
oldme-git May 19, 2024
ca2525c
up
oldme-git May 19, 2024
adcf2cc
up
oldme-git May 19, 2024
f1bea2d
up
oldme-git May 19, 2024
4748cee
up
oldme-git May 19, 2024
55accbb
恢复
oldme-git May 19, 2024
db651de
恢复
oldme-git May 19, 2024
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
1,448 changes: 0 additions & 1,448 deletions util/gconv/gconv_z_unit_all_test.go

This file was deleted.

60 changes: 0 additions & 60 deletions util/gconv/gconv_z_unit_basic_test.go

This file was deleted.

60 changes: 38 additions & 22 deletions util/gconv/gconv_z_unit_bool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,45 @@ import (
"github.com/gogf/gf/v2/util/gconv"
)

type boolStruct struct{}
var boolTests = []struct {
value interface{}
expect bool
}{
{true, true},
{false, false},

func Test_Bool(t *testing.T) {
{0, false},
{1, true},

{[]byte(""), false},

{"", false},
{"0", false},
{"1", true},
{"123.456", true},
{"true", true},
{"false", false},
{"on", true},
{"off", false},

{complex(1, 2), true},
{complex(123.456, 789.123), true},

{[3]int{1, 2, 3}, true},
{[]int{1, 2, 3}, true},

{map[int]int{1: 1}, true},
{map[string]string{"Earth": "印度洋"}, true},

{struct{}{}, true},
{&struct{}{}, true},
{nil, false},
}

func TestBool(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var any interface{} = nil
t.AssertEQ(gconv.Bool(any), false)
t.AssertEQ(gconv.Bool(false), false)
t.AssertEQ(gconv.Bool(nil), false)
t.AssertEQ(gconv.Bool(0), false)
t.AssertEQ(gconv.Bool("0"), false)
t.AssertEQ(gconv.Bool(""), false)
t.AssertEQ(gconv.Bool("false"), false)
t.AssertEQ(gconv.Bool("off"), false)
t.AssertEQ(gconv.Bool([]byte{}), false)
t.AssertEQ(gconv.Bool([]string{}), false)
t.AssertEQ(gconv.Bool([]interface{}{}), false)
t.AssertEQ(gconv.Bool([]map[int]int{}), false)

t.AssertEQ(gconv.Bool("1"), true)
t.AssertEQ(gconv.Bool("on"), true)
t.AssertEQ(gconv.Bool(1), true)
t.AssertEQ(gconv.Bool(123.456), true)
t.AssertEQ(gconv.Bool(boolStruct{}), true)
t.AssertEQ(gconv.Bool(&boolStruct{}), true)
for _, test := range boolTests {
t.AssertEQ(gconv.Bool(test.value), test.expect)
}
})
}
83 changes: 83 additions & 0 deletions util/gconv/gconv_z_unit_byte_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.

package gconv_test

import (
"testing"

"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/test/gtest"
"github.com/gogf/gf/v2/util/gconv"
)

var byteTests = []struct {
value interface{}
expect byte
expects []byte
}{
{true, 1, []byte{1}},
{false, 0, []byte{0}},

{int(0), 0, []byte{0}},
{int(123), 123, []byte{123}},
{int8(123), 123, []byte{123}},
{int16(123), 123, []byte{123, 0}},
{int32(123123123), 179, []byte{179, 181, 86, 7}},
{int64(123123123123123123), 179, []byte{179, 243, 99, 1, 212, 107, 181, 1}},

{uint(0), 0, []byte{0}},
{uint(123), 123, []byte{123}},
{uint8(123), 123, []byte{123}},
{uint16(123), 123, []byte{123, 0}},
{uint32(123123123), 179, []byte{179, 181, 86, 7}},
{uint64(123123123123123123), 179, []byte{179, 243, 99, 1, 212, 107, 181, 1}},

{uintptr(0), 0, []byte{48}},
{uintptr(123), 123, []byte{49, 50, 51}},

{rune(0), 0, []byte{0, 0, 0, 0}},
{rune(49), 49, []byte{49, 0, 0, 0}},

{float32(123), 123, []byte{0, 0, 246, 66}},
{float64(123.456), 123, []byte{119, 190, 159, 26, 47, 221, 94, 64}},

{[]byte(""), 0, []byte("")},

{"Uranus", 0, []byte("Uranus")},

{complex(1, 2), 0,
[]byte{0, 0, 0, 0, 0, 0, 240, 63, 0, 0, 0, 0, 0, 0, 0, 64}},

{[3]int{1, 2, 3}, 0, []byte{1, 2, 3}},
{[]int{1, 2, 3}, 0, []byte{1, 2, 3}},

{map[int]int{1: 1}, 0, []byte(`{"1":1}`)},
{map[string]string{"Earth": "印度洋"}, 0, []byte(`{"Earth":"印度洋"}`)},

{gvar.New(123), 123, []byte{123}},
{gvar.New(123.456), 123, []byte{119, 190, 159, 26, 47, 221, 94, 64}},
}

func TestByte(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
for _, test := range byteTests {
t.AssertEQ(gconv.Byte(test.value), test.expect)
}
})
}

func TestBytes(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
for _, test := range byteTests {
t.AssertEQ(gconv.Bytes(test.value), test.expects)
}
})

gtest.C(t, func(t *gtest.T) {
t.AssertEQ(gconv.Bytes(nil), nil)
})
}
138 changes: 138 additions & 0 deletions util/gconv/gconv_z_unit_convert_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.

package gconv_test

import (
"testing"
"time"

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

func TestConvert(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
t.AssertEQ(gconv.Convert(true, "bool"), true)
t.AssertEQ(gconv.Convert(false, "bool"), false)

t.Assert(gconv.Convert(int(0), "int"), int(0))
t.Assert(gconv.Convert(int8(0), "int8"), int8(0))
t.Assert(gconv.Convert(int16(0), "int16"), int16(0))
t.Assert(gconv.Convert(int32(0), "int32"), int32(0))
t.Assert(gconv.Convert(int64(1), "int64"), int64(1))

t.Assert(gconv.Convert(uint(0), "uint"), uint(0))
t.Assert(gconv.Convert(uint8(0), "uint8"), uint8(0))
t.Assert(gconv.Convert(uint16(0), "uint16"), uint16(0))
t.Assert(gconv.Convert(uint32(0), "uint32"), uint32(0))
t.Assert(gconv.Convert(uint64(0), "uint64"), uint64(0))

t.Assert(gconv.Convert(float32(0), "float32"), float32(0))
t.Assert(gconv.Convert(float64(0), "float64"), float64(0))

t.AssertEQ(gconv.Convert([]int{1, 2}, "[]int"), []int{1, 2})
t.AssertEQ(gconv.Convert([]int8{1, 2}, "[]int8}"), []int8{1, 2})
t.AssertEQ(gconv.Convert([]int16{1, 2}, "[]int16"), []int16{1, 2})
t.AssertEQ(gconv.Convert([]int32{1, 2}, "[]int32"), []int32{1, 2})
t.AssertEQ(gconv.Convert([]int64{1, 2}, "[]int64"), []int64{1, 2})
t.AssertEQ(gconv.Convert([]uint{1, 2}, "[]uint"), []uint{1, 2})
t.AssertEQ(gconv.Convert([]uint8{1, 2}, "[]uint8}"), []uint8{1, 2})
t.AssertEQ(gconv.Convert([]uint16{1, 2}, "[]uint16"), []uint16{1, 2})
t.AssertEQ(gconv.Convert([]uint32{1, 2}, "[]uint32"), []uint32{1, 2})
t.AssertEQ(gconv.Convert([]uint64{1, 2}, "[]uint64"), []uint64{1, 2})
t.AssertEQ(gconv.Convert([]float32{1, 2}, "[]float32"), []float32{1, 2})
t.AssertEQ(gconv.Convert([]float64{1, 2}, "[]float64"), []float64{1, 2})
t.AssertEQ(gconv.Convert([]string{"1", "2"}, "[]string"), []string{"1", "2"})
t.AssertEQ(gconv.Convert([]byte{}, "[]byte"), []uint8{})

var anyTest interface{} = nil
t.AssertEQ(gconv.Convert(anyTest, "string"), "")
t.AssertEQ(gconv.Convert("1", "string"), "1")

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(1989, "gtime.Time"),
*gconv.GTime("1970-01-01 08:33:09 +0800 CST"))
t.AssertEQ(gconv.Convert(1989, "*gtime.Time"),
gconv.GTime(1989))
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")

// TODO gconv.Convert(gtime.Now(), "gtime.Time", 1) = {{0001-01-01 00:00:00 +0000 UTC}}
t.AssertEQ(gconv.Convert(gtime.Now(), "gtime.Time", 1), *gtime.New())
t.AssertEQ(gconv.Convert(gtime.Now(), "*gtime.Time", 1), gtime.New())
t.AssertEQ(gconv.Convert(gtime.Now(), "GTime", 1), *gtime.New())

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

var intNum int = 1
t.Assert(gconv.Convert(intNum, "*int"), 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 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 mapStrInt = map[string]int{"k1": 1}
var mapStrStr = map[string]string{"k1": "1"}
var mapStrAny = map[string]interface{}{"k1": 1}
t.AssertEQ(gconv.Convert(mapStrInt, "map[string]string"), mapStrStr)
t.AssertEQ(gconv.Convert(mapStrInt, "map[string]interface{}"), mapStrAny)
})
}
Loading
Loading