This repository has been archived by the owner on Oct 26, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil_test.go
122 lines (100 loc) · 2.48 KB
/
util_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package gormx
import (
"fmt"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_packPanicError(t *testing.T) {
as := assert.New(t)
t.Run("panic-string", func(t *testing.T) {
defer func() {
err := packPanicError(recover())
as.NotNil(err)
as.Equal("gormx panic: test", err.Error())
}()
panic("test")
})
t.Run("panic-error", func(t *testing.T) {
defer func() {
err := packPanicError(recover())
as.NotNil(err)
as.Equal("error", err.Error())
}()
panic(fmt.Errorf("error"))
})
}
func Test_interfaceToSlice(t *testing.T) {
tests := []struct {
name string
args any
want []any
err error
}{
{"1", []int{1, 2, 3}, []any{1, 2, 3}, nil},
{"2", [...]int{1, 2, 3}, []any{1, 2, 3}, nil},
{"3", "string", nil, fmt.Errorf("gormx panic: interfaceToSlice: v must be slice")},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.err == nil {
assert.Equalf(t, tt.want, interfaceToSlice(tt.args), "interfaceToSlice(%v)", tt.args)
} else {
defer func() {
err := packPanicError(recover())
assert.NotNil(t, err)
assert.Equal(t, tt.err.Error(), err.Error())
}()
interfaceToSlice(tt.args)
}
})
}
}
func Test_isEmptyValue(t *testing.T) {
tests := []struct {
name string
args any
want bool
}{
{"1", nil, true},
{"int-0", 0, true},
{"int-1", 1, false},
{"uint-0", uint(0), true},
{"uint-1", uint(1), false},
{"float-0.0", 0.0, true},
{"float-1.0", 1.0, false},
{"complex-0+0i", 0 + 0i, true},
{"complex-1+0i", 1 + 0i, false},
{"bool-false", false, true},
{"bool-true", true, false},
{"string-''", "", true},
{"string-str", "str", false},
{"slice-[]", []int{}, true},
{"slice-[1]", []int{1}, false},
{"map-{}", map[string]int{}, true},
{"map-{1}", map[string]int{"1": 1}, false},
{"struct-{}", struct{}{}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
v := reflect.ValueOf(tt.args)
assert.Equalf(t, tt.want, isEmptyValue(v), "isEmptyValue(%v)", tt.args)
})
}
}
func Test_getValueAndType(t *testing.T) {
as := assert.New(t)
t.Run("invalid", func(t *testing.T) {
_, _, err := getValueAndType(nil)
as.NotNil(err)
as.Equal("gormx's data is invalid", err.Error())
})
t.Run("pointer", func(t *testing.T) {
_, _, _ = getValueAndType(&struct{}{})
})
t.Run("not struct", func(t *testing.T) {
_, _, err := getValueAndType(1)
as.NotNil(err)
as.Equal("data's kind must be struct, but got 'int'", err.Error())
})
}