-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaccessor_test.go
196 lines (164 loc) · 6.29 KB
/
accessor_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
package googp
import (
"reflect"
"testing"
"time"
)
func Test_ValueAccessor(t *testing.T) {
var s string
ac := newAccessor(nil, reflect.ValueOf(s)) // string
assertError(t, ac.Set("og:title", "title"))
ac = newAccessor(nil, reflect.ValueOf(&s)) // *string
assertNoError(t, ac.Set("og:title", "title"))
assertEqual(t, s, "title")
assertNoError(t, ac.Set("og:title", "title2"))
assertEqual(t, s, "title") // cannot overwrite
var time time.Time
ac = newAccessor(nil, reflect.ValueOf(time)) // Time
assertError(t, ac.Set("video:release_date", "2020-05-20T01:01:25Z"))
ac = newAccessor(nil, reflect.ValueOf(&time)) // *Time
assertNoError(t, ac.Set("video:release_date", "2020-05-20T01:01:25Z"))
assertEqual(t, time.String(), "2020-05-20 01:01:25 +0000 UTC")
var i int
ac = newValueAccessor(reflect.ValueOf(i)) // int
assertError(t, ac.Set("og:number", "123"))
ac = newValueAccessor(reflect.ValueOf(&i)) // *int
assertNoError(t, ac.Set("og:number", "123"))
assertEqual(t, i, 123)
var u uint
ac = newValueAccessor(reflect.ValueOf(u)) // uint
assertError(t, ac.Set("og:number", "123"))
ac = newValueAccessor(reflect.ValueOf(&u)) // *uint
assertNoError(t, ac.Set("og:number", "123"))
assertEqual(t, u, uint(123))
var f float64
ac = newAccessor(nil, reflect.ValueOf(f)) // float64
assertError(t, ac.Set("og:number", "23.5"))
ac = newAccessor(nil, reflect.ValueOf(&f)) // *float64
assertNoError(t, ac.Set("og:number", "23.5"))
assertEqual(t, f, float64(23.5))
}
func Test_ArrayAccessor(t *testing.T) {
var arr [3]string
ac := newAccessor(&tag{names: []string{"og:image"}}, reflect.ValueOf(arr)) // [3]string
assertError(t, ac.Set("og:image", "http://example.com/image.png"))
ac = newAccessor(&tag{names: []string{"og:image"}}, reflect.ValueOf(&arr)) // *[3]string
assertNoError(t, ac.Set("og:image", "http://example.com/image1.png"))
assertNoError(t, ac.Set("og:image", "http://example.com/image2.png"))
assertNoError(t, ac.Set("og:image", "http://example.com/image3.png"))
assertNoError(t, ac.Set("og:image", "http://example.com/image4.png"))
assertEqual(t, arr, [3]string{
"http://example.com/image1.png",
"http://example.com/image2.png",
"http://example.com/image3.png",
})
var pArr [3]*string
ac = newAccessor(&tag{names: []string{"og:image"}}, reflect.ValueOf(pArr)) // [3]*string
assertError(t, ac.Set("og:image", "http://example.com/image.png"))
ac = newAccessor(&tag{names: []string{"og:image"}}, reflect.ValueOf(&pArr)) // *[3]*string
assertNoError(t, ac.Set("og:image", "http://example.com/image1.png"))
assertNoError(t, ac.Set("og:image", "http://example.com/image2.png"))
assertNoError(t, ac.Set("og:image", "http://example.com/image3.png"))
assertNoError(t, ac.Set("og:image", "http://example.com/image4.png"))
assertEqual(t, *pArr[0], "http://example.com/image1.png")
assertEqual(t, *pArr[1], "http://example.com/image2.png")
assertEqual(t, *pArr[2], "http://example.com/image3.png")
var sli []string
ac = newAccessor(&tag{names: []string{"og:image"}}, reflect.ValueOf(sli)) // []string
assertError(t, ac.Set("og:image", "http://example.com/image.png"))
assertEqual(t, len(sli), 0)
ac = newAccessor(&tag{names: []string{"og:image"}}, reflect.ValueOf(&sli)) // *[]string
assertNoError(t, ac.Set("og:image", "http://example.com/image1.png"))
assertNoError(t, ac.Set("og:image", "http://example.com/image2.png"))
assertEqual(t, len(sli), 2)
assertEqual(t, sli, []string{
"http://example.com/image1.png",
"http://example.com/image2.png",
})
var pSli []*string
ac = newAccessor(&tag{names: []string{"og:image"}}, reflect.ValueOf(pSli)) // []*string
assertError(t, ac.Set("og:image", "http://example.com/image.png"))
ac = newAccessor(&tag{names: []string{"og:image"}}, reflect.ValueOf(&pSli)) // *[]*string
assertNoError(t, ac.Set("og:image", "http://example.com/image1.png"))
assertNoError(t, ac.Set("og:image", "http://example.com/image2.png"))
assertEqual(t, len(pSli), 2)
assertEqual(t, *pSli[0], "http://example.com/image1.png")
assertEqual(t, *pSli[1], "http://example.com/image2.png")
}
func Test_StructAccessor(t *testing.T) {
var v struct {
Title string `googp:"og:title"`
Description *string `googp:"og:description"`
URL *string `googp:"og:url"`
}
ac := newAccessor(nil, reflect.ValueOf(v))
assertError(t, ac.Set("og:title", "title"))
assertError(t, ac.Set("og:description", "description"))
ac = newAccessor(nil, reflect.ValueOf(&v))
assertNoError(t, ac.Set("og:title", "title"))
assertNoError(t, ac.Set("og:description", "description"))
assertEqual(t, v.Title, "title")
assertEqual(t, *v.Description, "description")
assertEqual(t, v.URL, (*string)(nil))
}
func Test_StructAccessor_ConflictTag(t *testing.T) {
var v struct {
A string `googp:"og:title"`
B string `googp:"og:title"`
}
ac := newAccessor(nil, reflect.ValueOf(&v))
assertNoError(t, ac.Set("og:title", "title"))
assertNoError(t, ac.Set("og:title", "title"))
assertEqual(t, v.A, "title")
// If there is a conflict, the backward will be ignored.
assertEqual(t, v.B, "")
}
func Test_StructAccessor_NoTag(t *testing.T) {
var v struct {
A string
}
ac := newAccessor(nil, reflect.ValueOf(&v))
assertNoError(t, ac.Set("og:a", "title"))
// default is treated as `og:${acName}`
assertEqual(t, v.A, "title")
}
func Test_StructAccessor_MultipleNames(t *testing.T) {
var v struct {
A string `googp:"og:title,og:description"`
}
ac := newAccessor(nil, reflect.ValueOf(&v))
assertNoError(t, ac.Set("og:title", "title"))
assertNoError(t, ac.Set("og:description", "description"))
// The one set first has priority
assertEqual(t, v.A, "title")
}
func Test_StructAccessor_Anonymous(t *testing.T) {
var og1 struct {
OGP
}
ac := newAccessor(nil, reflect.ValueOf(&og1))
assertNoError(t, ac.Set("og:title", "title"))
assertEqual(t, og1.Title, "title")
var og2 struct {
*OGP
}
ac = newAccessor(nil, reflect.ValueOf(&og2))
assertNoError(t, ac.Set("og:title", "title"))
assertEqual(t, og2.Title, "title")
}
func Test_StructAccessor_Private(t *testing.T) {
var og1 struct {
ogp OGP
A string
}
ac := newAccessor(nil, reflect.ValueOf(&og1))
assertNoError(t, ac.Set("og:title", "title"))
assertEqual(t, og1.ogp.Title, "")
var og2 struct {
ogp *OGP
A string
}
ac = newAccessor(nil, reflect.ValueOf(&og2))
assertNoError(t, ac.Set("og:title", "title"))
assertEqual(t, og2.ogp, (*OGP)(nil))
}