-
Notifications
You must be signed in to change notification settings - Fork 0
/
goval_test.go
162 lines (149 loc) · 3.8 KB
/
goval_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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package goval
import (
"testing"
)
func TestToGoTypeString(t *testing.T) {
type user struct {
id int
Name string
}
userA := user{id: 1, Name: "userA"}
userB := user{id: 1, Name: "userB"}
tests := []struct {
name string
v interface{}
want string
}{
{"nil", nil, "<nil>"},
{"int", 1, "1"},
{"bool-false", false, "false"},
{"bool-true", true, "true"},
{"struct-normal", userA, `goval.user{id:1, Name:"userA"}`},
{"struct-pointer", &userA, `&goval.user{id:1, Name:"userA"}`},
{"slice-nil", nil, `<nil>`},
{"slice-normal", []user{userA, userB},
`[]goval.user[goval.user{id:1, Name:"userA"}, goval.user{id:1, Name:"userB"}]`},
{"slice-pointer", []*user{&userA, &userB},
`[]*goval.user[&goval.user{id:1, Name:"userA"}, &goval.user{id:1, Name:"userB"}]`},
{"map-nil", nil, `<nil>`},
{"map-normal", map[string]interface{}{"user1": userA, "user2": userB},
`map[string]interface {}[{"user1":&goval.user{id:1, Name:"userA"}} {"user2":&goval.user{id:1, Name:"userB"}}]`},
{"map-pointer", map[string]*user{"user1": &userA},
`map[string]*goval.user[{"user1":&goval.user{id:1, Name:"userA"}}]`},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := ToTypeString(tt.v)
if got != tt.want {
t.Errorf("ToTypeString() = %v, want %v", got, tt.want)
}
})
}
}
func TestToString(t *testing.T) {
type user struct {
id int
Name string
}
userA := user{id: 1, Name: "userA"}
userB := user{id: 1, Name: "userB"}
tests := []struct {
name string
v interface{}
want string
}{
{"nil", nil, "<nil>"},
{"int", 1, "1"},
{"bool-false", false, "false"},
{"bool-true", true, "true"},
{"struct-normal", userA, `{id:1, Name:"userA"}`},
{"struct-pointer", &userA, `&{id:1, Name:"userA"}`},
{"slice-nil", nil, `<nil>`},
{"slice-normal", []user{userA, userB}, `[{id:1, Name:"userA"}, {id:1, Name:"userB"}]`},
{"slice-pointer", []*user{&userA, &userB}, `[&{id:1, Name:"userA"}, &{id:1, Name:"userB"}]`},
{"map-nil", nil, `<nil>`},
{"map-normal", map[string]interface{}{"user1": userA, "user2": userB},
`[{"user1":&{id:1, Name:"userA"}} {"user2":&{id:1, Name:"userB"}}]`},
{"map-pointer", map[string]*user{"user1": &userA}, `[{"user1":&{id:1, Name:"userA"}}]`},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := ToString(tt.v)
if got != tt.want {
t.Errorf("ToString() = %v, want %v", got, tt.want)
}
})
}
}
func TestPrintNestUserInfo(t *testing.T) {
type ExtInfo struct {
From string
Intro string
SalePrice map[string]float64
}
// 结构体指针嵌套
type Goods struct {
id int
Price float64
Name string
LabelIDs []int
ExtInfo *ExtInfo
}
apple := &Goods{
id: 100,
Price: 10.25,
Name: "apple",
LabelIDs: []int{4, 5},
ExtInfo: &ExtInfo{
From: "Hk",
Intro: "red apple...",
SalePrice: map[string]float64{
"high": 15.00,
"low": 3.00,
},
},
}
banner := &Goods{
id: 200,
Price: 6.18,
Name: "banner",
LabelIDs: []int{4, 5},
ExtInfo: nil,
}
goods := []*Goods{apple, banner}
// 不带类型
t.Logf("user1 => %s", ToString(goods))
// 带Go Type类型
t.Logf("user1 type val => %s", ToTypeString(goods))
}
// old -> BenchmarkGetGoString-10 350077 3407 ns/op 2473 B/op 97 allocs/op
func BenchmarkGetGoString(b *testing.B) {
type Extend struct {
School string
Home string
}
// 结构体指针嵌套
type NestUser struct {
ID int
Name string
Favs []int
Extend *Extend
Family map[string]string
}
u1 := &NestUser{
ID: 100,
Name: "UserA",
Favs: []int{4, 5},
Extend: &Extend{
School: "SchoolXiao",
Home: "HomeTang",
},
Family: map[string]string{
"mother": "mo",
"father": "fa",
},
}
for i := 0; i < b.N; i++ {
ToString(u1)
}
}