-
Notifications
You must be signed in to change notification settings - Fork 0
/
struct_utils_test.go
94 lines (72 loc) · 1.7 KB
/
struct_utils_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
package struct_copy
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/vcqr/struct-copy/examples/pkg_api/dto"
"github.com/vcqr/struct-copy/examples/pkg_article"
)
var util = NewStructUtils()
type StructEasyCaseA struct {
A int
B float64
C string
}
type StructEasyCaseB struct {
a int
B float64
C string
}
func TestStructUtils_CopyProperties(t *testing.T) {
a := assert.New(t)
articleService := pkg_article.NewArticle()
article := articleService.GetArticle()
var articleVo dto.ArticleDto
util.CopyProperties(&articleVo, article)
a.Equal(int64(10000), articleVo.Id)
a.GreaterOrEqual(int64(100), articleVo.Hits)
// 数组
a.Equal("116.404", articleVo.Coords[0].X)
// 切片
a.Equal(2, len(articleVo.Author))
a.Equal(int64(2), articleVo.Author[1].Id)
// map
a.Equal(2, len(articleVo.Comment))
a.Equal("游客1", articleVo.Comment[1000].Author.NickName)
}
func BenchmarkStructUtils_CopyProperties_Easy(b *testing.B) {
var src = StructEasyCaseA{}
var dest StructEasyCaseB
b.ResetTimer()
for i := 0; i < b.N; i++ {
util.CopyProperties(&dest, src)
}
}
func BenchmarkStructUtils_CopyProperties(b *testing.B) {
articleService := pkg_article.NewArticle()
article := articleService.GetArticle()
var articleVo dto.ArticleDto
b.ResetTimer()
for i := 0; i < b.N; i++ {
util.CopyProperties(&articleVo, article)
}
}
func TestStructUtils_StructToMap(t *testing.T) {
type TestCase struct {
A int `json:"a"`
B string `json:"b"`
C float64
D string `json:"-"`
E string `json:"e,omitempty"`
f string
}
cases := TestCase{
A: 123456,
B: "abcdef",
C: 0.001,
D: "abcdef",
f: "abcdef",
}
ret := util.StructToMap(&cases)
a := assert.New(t)
a.Equal(3, len(ret))
}