gf/util/gutil/gutil_z_unit_test.go
2021-01-17 21:46:25 +08:00

139 lines
2.4 KiB
Go
Executable File

// 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 gutil_test
import (
"github.com/gogf/gf/frame/g"
"testing"
"github.com/gogf/gf/test/gtest"
"github.com/gogf/gf/util/gutil"
)
func Test_Dump(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
gutil.Dump(map[int]int{
100: 100,
})
})
}
func Test_Try(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
s := `gutil Try test`
t.Assert(gutil.Try(func() {
panic(s)
}), s)
})
}
func Test_TryCatch(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
gutil.TryCatch(func() {
panic("gutil TryCatch test")
})
})
gtest.C(t, func(t *gtest.T) {
gutil.TryCatch(func() {
panic("gutil TryCatch test")
}, func(err error) {
t.Assert(err, "gutil TryCatch test")
})
})
}
func Test_IsEmpty(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
t.Assert(gutil.IsEmpty(1), false)
})
}
func Test_Throw(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
defer func() {
t.Assert(recover(), "gutil Throw test")
}()
gutil.Throw("gutil Throw test")
})
}
func Test_Keys(t *testing.T) {
// map
gtest.C(t, func(t *gtest.T) {
keys := gutil.Keys(map[int]int{
1: 10,
2: 20,
})
t.AssertIN("1", keys)
t.AssertIN("2", keys)
})
// *map
gtest.C(t, func(t *gtest.T) {
keys := gutil.Keys(&map[int]int{
1: 10,
2: 20,
})
t.AssertIN("1", keys)
t.AssertIN("2", keys)
})
// *struct
gtest.C(t, func(t *gtest.T) {
type T struct {
A string
B int
}
keys := gutil.Keys(new(T))
t.Assert(keys, g.SliceStr{"A", "B"})
})
// *struct nil
gtest.C(t, func(t *gtest.T) {
type T struct {
A string
B int
}
var pointer *T
keys := gutil.Keys(pointer)
t.Assert(keys, g.SliceStr{"A", "B"})
})
// **struct nil
gtest.C(t, func(t *gtest.T) {
type T struct {
A string
B int
}
var pointer *T
keys := gutil.Keys(&pointer)
t.Assert(keys, g.SliceStr{"A", "B"})
})
}
func Test_Values(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
values := gutil.Keys(map[int]int{
1: 10,
2: 20,
})
t.AssertIN("1", values)
t.AssertIN("2", values)
})
gtest.C(t, func(t *gtest.T) {
type T struct {
A string
B int
}
keys := gutil.Values(T{
A: "1",
B: 2,
})
t.Assert(keys, g.Slice{"1", 2})
})
}