forked from cnf/structhash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
structhash_test.go
286 lines (246 loc) · 5.12 KB
/
structhash_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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package structhash
import (
"fmt"
"testing"
)
type First struct {
Bool bool `version:"1"`
String string `version:"2"`
Int int `version:"1" lastversion:"1"`
Float float64 `version:"1"`
Struct *Second `version:"1"`
Uint uint `version:"1"`
}
type Second struct {
Map map[string]string `version:"1"`
Slice []int `version:"1"`
}
type Tags1 struct {
Int int `hash:"-"`
Str string `hash:"name:Foo version:1 lastversion:2"`
Bar string `hash:"version:1"`
}
type Tags2 struct {
Foo string
Bar string
}
type Tags3 struct {
Bar string
}
type Tags4 struct {
Data1 ambiguousData `hash:"method:Serialize"`
Data2 ambiguousData `hash:"method:Normalize"`
}
type Tags5 struct {
Data1 ambiguousData `hash:"method:UnknownMethod"`
}
type Nils struct {
Str *string
Int *int
Bool *bool
Map map[string]string
Slice []string
}
type unexportedTags struct {
foo string
bar string
aMap map[string]string
}
type interfaceStruct struct {
Name string
Interface1 interface{}
InterfaceIgnore interface{} `hash:"-"`
}
type ambiguousData struct {
Prefix string
Suffix string
}
func (p ambiguousData) Serialize() string {
return p.Prefix + p.Suffix
}
func (p ambiguousData) Normalize() ambiguousData {
return ambiguousData{p.Prefix + p.Suffix, ""}
}
func dataSetup() *First {
tmpmap := make(map[string]string)
tmpmap["foo"] = "bar"
tmpmap["baz"] = "go"
tmpslice := make([]int, 3)
tmpslice[0] = 0
tmpslice[1] = 1
tmpslice[2] = 2
return &First{
Bool: true,
String: "test",
Int: 123456789,
Float: 65.3458,
Struct: &Second{
Map: tmpmap,
Slice: tmpslice,
},
Uint: 1,
}
}
func TestHash(t *testing.T) {
v1Hash := "v1_e8e67581aee36d7237603381a9cbd9fc"
v2Hash := "v2_5e51490d7c24c4b7a9e63c04f55734eb"
data := dataSetup()
v1, err := Hash(data, 1)
if err != nil {
t.Error(err)
}
// fmt.Println(string(Dump(data, 1)))
if v1 != v1Hash {
t.Errorf("%s is not %s", v1, v1Hash)
}
v2, err := Hash(data, 2)
if err != nil {
t.Error(err)
}
// fmt.Println(string(Dump(data, 2)))
if v2 != v2Hash {
t.Errorf("%s is not %s", v2, v2Hash)
}
v1md5 := fmt.Sprintf("v1_%x", Md5(data, 1))
if v1md5 != v1Hash {
t.Errorf("%s is not %s", v1md5, v1Hash[3:])
}
v2md5 := fmt.Sprintf("v2_%x", Md5(data, 2))
if v2md5 != v2Hash {
t.Errorf("%s is not %s", v2md5, v2Hash[3:])
}
}
func TestTags(t *testing.T) {
t1 := Tags1{11, "foo", "bar"}
t1x := Tags1{22, "foo", "bar"}
t2 := Tags2{"foo", "bar"}
t3 := Tags3{"bar"}
t1_dump := string(Dump(t1, 1))
t1x_dump := string(Dump(t1x, 1))
if t1_dump != t1x_dump {
t.Errorf("%s is not %s", t1_dump, t1x_dump)
}
t2_dump := string(Dump(t2, 1))
if t1_dump != t2_dump {
t.Errorf("%s is not %s", t1_dump, t2_dump)
}
t1v3_dump := string(Dump(t1, 3))
t3v3_dump := string(Dump(t3, 3))
if t1v3_dump != t3v3_dump {
t.Errorf("%s is not %s", t1v3_dump, t3v3_dump)
}
}
func TestNils(t *testing.T) {
s1 := Nils{
Str: nil,
Int: nil,
Bool: nil,
Map: nil,
Slice: nil,
}
s1_dump := string(Dump(s1, 1))
s2_dump := `{Bool:"_nil",Int:"_nil",Map:"_nil",Slice:"_nil",Str:"_nil"}`
if s1_dump != s2_dump {
t.Errorf("%s is not %s", s1_dump, s2_dump)
}
}
func TestUnexportedFields(t *testing.T) {
v1Hash := "v1_750efb7c919caf87f2ab0d119650c87d"
data := unexportedTags{
foo: "foo",
bar: "bar",
aMap: map[string]string{
"key1": "val",
},
}
v1, err := Hash(data, 1)
if err != nil {
t.Error(err)
}
if v1 != v1Hash {
t.Errorf("%s is not %s", v1, v1Hash)
}
v1md5 := fmt.Sprintf("v1_%x", Md5(data, 1))
if v1md5 != v1Hash {
t.Errorf("%s is not %s", v1md5, v1Hash[3:])
}
}
func TestInterfaceField(t *testing.T) {
a := interfaceStruct{
Name: "name",
Interface1: "a",
InterfaceIgnore: "b",
}
b := interfaceStruct{
Name: "name",
Interface1: "b",
InterfaceIgnore: "b",
}
c := interfaceStruct{
Name: "name",
Interface1: "b",
InterfaceIgnore: "c",
}
ha, err := Hash(a, 1)
if err != nil {
t.Error(err)
}
hb, err := Hash(b, 1)
if err != nil {
t.Error(err)
}
hc, err := Hash(c, 1)
if err != nil {
t.Error(err)
}
if ha == hb {
t.Errorf("%s equals %s", ha, hb)
}
if hb != hc {
t.Errorf("%s is not %s", hb, hc)
}
b.Interface1 = map[string]string{"key": "value"}
c.Interface1 = map[string]string{"key": "value"}
hb, err = Hash(b, 1)
if err != nil {
t.Error(err)
}
hc, err = Hash(c, 1)
if err != nil {
t.Error(err)
}
if hb != hc {
t.Errorf("%s is not %s", hb, hc)
}
c.Interface1 = map[string]string{"key1": "value1"}
hc, err = Hash(c, 1)
if err != nil {
t.Error(err)
}
if hb == hc {
t.Errorf("%s equals %s", hb, hc)
}
}
func TestMethod(t *testing.T) {
dump1 := Dump(Tags4{
ambiguousData{"123", "45"},
ambiguousData{"12", "345"},
}, 1)
dump2 := Dump(Tags4{
ambiguousData{"12", "345"},
ambiguousData{"123", "45"},
}, 1)
if string(dump1) != string(dump2) {
t.Errorf("%s not equals %s", dump1, dump2)
}
}
func TestMethodPanic(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("dumping via incorrect \"method\" tag did not panic")
}
}()
_ = Dump(Tags5{
ambiguousData{"123", "45"},
}, 1)
}