gf/util/gutil/gutil_z_unit_test.go

139 lines
2.4 KiB
Go
Raw Normal View History

// Copyright 2019 gf Author(https://github.com/gogf/gf). 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.
2019-06-25 19:01:27 +08:00
package gutil_test
import (
"github.com/gogf/gf/frame/g"
2019-06-25 19:01:27 +08:00
"testing"
2019-07-29 21:01:19 +08:00
"github.com/gogf/gf/test/gtest"
"github.com/gogf/gf/util/gutil"
2019-06-25 19:01:27 +08:00
)
func Test_Dump(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-25 19:01:27 +08:00
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)
})
}
2019-06-25 19:01:27 +08:00
func Test_TryCatch(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-25 19:01:27 +08:00
gutil.TryCatch(func() {
panic("gutil TryCatch test")
})
})
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-25 19:01:27 +08:00
gutil.TryCatch(func() {
panic("gutil TryCatch test")
}, func(err error) {
2020-03-19 22:56:12 +08:00
t.Assert(err, "gutil TryCatch test")
2019-06-25 19:01:27 +08:00
})
})
}
func Test_IsEmpty(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
t.Assert(gutil.IsEmpty(1), false)
2019-06-25 19:01:27 +08:00
})
}
func Test_Throw(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-25 19:01:27 +08:00
defer func() {
2020-03-19 22:56:12 +08:00
t.Assert(recover(), "gutil Throw test")
2019-06-25 19:01:27 +08:00
}()
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
2020-11-06 21:21:09 +08:00
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})
})
}