1
// Copyright 2013 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
17
B *SubNested `url:"b"`
18
Ptr *SubNested `url:"ptr,omitempty"`
21
type SubNested struct {
22
Value string `url:"value"`
25
func TestValues_types(t *testing.T) {
56
}{A: strPtr, C: &strPtr},
67
B []string `url:",comma"`
68
C []string `url:",space"`
70
E [2]string `url:",comma"`
71
F [2]string `url:",space"`
72
G []*string `url:",space"`
73
H []bool `url:",int,space"`
74
I []string `url:",brackets"`
75
J []string `url:",semicolon"`
76
K []string `url:",numbered"`
78
A: []string{"a", "b"},
79
B: []string{"a", "b"},
80
C: []string{"a", "b"},
81
D: [2]string{"a", "b"},
82
E: [2]string{"a", "b"},
83
F: [2]string{"a", "b"},
84
G: []*string{&str, &str},
85
H: []bool{true, false},
86
I: []string{"a", "b"},
87
J: []string{"a", "b"},
88
K: []string{"a", "b"},
97
"G": {"string string"},
109
B time.Time `url:",unix"`
113
A: time.Date(2000, 1, 1, 12, 34, 56, 0, time.UTC),
114
B: time.Date(2000, 1, 1, 12, 34, 56, 0, time.UTC),
119
"A": {"2000-01-01T12:34:56Z"},
127
Nest Nested `url:"nest"`
136
"nest[a][value]": {"that"},
142
Nest Nested `url:"nest"`
151
"nest[a][value]": {""},
153
"nest[ptr][value]": {"that"},
162
for i, tt := range tests {
163
v, err := Values(tt.in)
165
t.Errorf("%d. Values(%q) returned error: %v", i, tt.in, err)
168
if !reflect.DeepEqual(tt.want, v) {
169
t.Errorf("%d. Values(%q) returned %v, want %v", i, tt.in, v, tt.want)
174
func TestValues_omitEmpty(t *testing.T) {
179
B string `url:",omitempty"`
181
D string `url:"omitempty"` // actually named omitempty, not an option
182
E *string `url:",omitempty"`
187
t.Errorf("Values(%q) returned error: %v", s, err)
193
"E": {""}, // E is included because the pointer is not empty, even though the string being pointed to is
195
if !reflect.DeepEqual(want, v) {
196
t.Errorf("Values(%q) returned %v, want %v", s, v, want)
222
func TestValues_embeddedStructs(t *testing.T) {
229
url.Values{"C": {"foo"}},
232
D{B: B{C: "bar"}, C: "foo"},
233
url.Values{"C": {"foo", "bar"}},
236
F{e{B: B{C: "bar"}, C: "foo"}}, // With unexported embed
237
url.Values{"C": {"foo", "bar"}},
241
for i, tt := range tests {
242
v, err := Values(tt.in)
244
t.Errorf("%d. Values(%q) returned error: %v", i, tt.in, err)
247
if !reflect.DeepEqual(tt.want, v) {
248
t.Errorf("%d. Values(%q) returned %v, want %v", i, tt.in, v, tt.want)
253
func TestValues_invalidInput(t *testing.T) {
256
t.Errorf("expected Values() to return an error on invalid input")
260
type EncodedArgs []string
262
func (m EncodedArgs) EncodeValues(key string, v *url.Values) error {
263
for i, arg := range m {
264
v.Set(fmt.Sprintf("%s.%d", key, i), arg)
269
func TestValues_Marshaler(t *testing.T) {
271
Args EncodedArgs `url:"arg"`
272
}{[]string{"a", "b", "c"}}
275
t.Errorf("Values(%q) returned error: %v", s, err)
283
if !reflect.DeepEqual(want, v) {
284
t.Errorf("Values(%q) returned %v, want %v", s, v, want)
288
func TestValues_MarshalerWithNilPointer(t *testing.T) {
290
Args *EncodedArgs `url:"arg"`
294
t.Errorf("Values(%q) returned error: %v", s, err)
298
if !reflect.DeepEqual(want, v) {
299
t.Errorf("Values(%q) returned %v, want %v", s, v, want)
303
func TestTagParsing(t *testing.T) {
304
name, opts := parseTag("field,foobar,foo")
306
t.Fatalf("name = %q, want field", name)
308
for _, tt := range []struct {
317
if opts.Contains(tt.opt) != tt.want {
318
t.Errorf("Contains(%q) = %v", tt.opt, !tt.want)