~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/google/go-querystring/query/encode_test.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.
 
4
 
 
5
package query
 
6
 
 
7
import (
 
8
        "fmt"
 
9
        "net/url"
 
10
        "reflect"
 
11
        "testing"
 
12
        "time"
 
13
)
 
14
 
 
15
type Nested struct {
 
16
        A   SubNested  `url:"a"`
 
17
        B   *SubNested `url:"b"`
 
18
        Ptr *SubNested `url:"ptr,omitempty"`
 
19
}
 
20
 
 
21
type SubNested struct {
 
22
        Value string `url:"value"`
 
23
}
 
24
 
 
25
func TestValues_types(t *testing.T) {
 
26
        str := "string"
 
27
        strPtr := &str
 
28
 
 
29
        tests := []struct {
 
30
                in   interface{}
 
31
                want url.Values
 
32
        }{
 
33
                {
 
34
                        // basic primitives
 
35
                        struct {
 
36
                                A string
 
37
                                B int
 
38
                                C uint
 
39
                                D float32
 
40
                                E bool
 
41
                        }{},
 
42
                        url.Values{
 
43
                                "A": {""},
 
44
                                "B": {"0"},
 
45
                                "C": {"0"},
 
46
                                "D": {"0"},
 
47
                                "E": {"false"},
 
48
                        },
 
49
                },
 
50
                {
 
51
                        // pointers
 
52
                        struct {
 
53
                                A *string
 
54
                                B *int
 
55
                                C **string
 
56
                        }{A: strPtr, C: &strPtr},
 
57
                        url.Values{
 
58
                                "A": {str},
 
59
                                "B": {""},
 
60
                                "C": {str},
 
61
                        },
 
62
                },
 
63
                {
 
64
                        // slices and arrays
 
65
                        struct {
 
66
                                A []string
 
67
                                B []string `url:",comma"`
 
68
                                C []string `url:",space"`
 
69
                                D [2]string
 
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"`
 
77
                        }{
 
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"},
 
89
                        },
 
90
                        url.Values{
 
91
                                "A":   {"a", "b"},
 
92
                                "B":   {"a,b"},
 
93
                                "C":   {"a b"},
 
94
                                "D":   {"a", "b"},
 
95
                                "E":   {"a,b"},
 
96
                                "F":   {"a b"},
 
97
                                "G":   {"string string"},
 
98
                                "H":   {"1 0"},
 
99
                                "I[]": {"a", "b"},
 
100
                                "J":   {"a;b"},
 
101
                                "K0":  {"a"},
 
102
                                "K1":  {"b"},
 
103
                        },
 
104
                },
 
105
                {
 
106
                        // other types
 
107
                        struct {
 
108
                                A time.Time
 
109
                                B time.Time `url:",unix"`
 
110
                                C bool      `url:",int"`
 
111
                                D bool      `url:",int"`
 
112
                        }{
 
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),
 
115
                                C: true,
 
116
                                D: false,
 
117
                        },
 
118
                        url.Values{
 
119
                                "A": {"2000-01-01T12:34:56Z"},
 
120
                                "B": {"946730096"},
 
121
                                "C": {"1"},
 
122
                                "D": {"0"},
 
123
                        },
 
124
                },
 
125
                {
 
126
                        struct {
 
127
                                Nest Nested `url:"nest"`
 
128
                        }{
 
129
                                Nested{
 
130
                                        A: SubNested{
 
131
                                                Value: "that",
 
132
                                        },
 
133
                                },
 
134
                        },
 
135
                        url.Values{
 
136
                                "nest[a][value]": {"that"},
 
137
                                "nest[b]":        {""},
 
138
                        },
 
139
                },
 
140
                {
 
141
                        struct {
 
142
                                Nest Nested `url:"nest"`
 
143
                        }{
 
144
                                Nested{
 
145
                                        Ptr: &SubNested{
 
146
                                                Value: "that",
 
147
                                        },
 
148
                                },
 
149
                        },
 
150
                        url.Values{
 
151
                                "nest[a][value]":   {""},
 
152
                                "nest[b]":          {""},
 
153
                                "nest[ptr][value]": {"that"},
 
154
                        },
 
155
                },
 
156
                {
 
157
                        nil,
 
158
                        url.Values{},
 
159
                },
 
160
        }
 
161
 
 
162
        for i, tt := range tests {
 
163
                v, err := Values(tt.in)
 
164
                if err != nil {
 
165
                        t.Errorf("%d. Values(%q) returned error: %v", i, tt.in, err)
 
166
                }
 
167
 
 
168
                if !reflect.DeepEqual(tt.want, v) {
 
169
                        t.Errorf("%d. Values(%q) returned %v, want %v", i, tt.in, v, tt.want)
 
170
                }
 
171
        }
 
172
}
 
173
 
 
174
func TestValues_omitEmpty(t *testing.T) {
 
175
        str := ""
 
176
        s := struct {
 
177
                a string
 
178
                A string
 
179
                B string  `url:",omitempty"`
 
180
                C string  `url:"-"`
 
181
                D string  `url:"omitempty"` // actually named omitempty, not an option
 
182
                E *string `url:",omitempty"`
 
183
        }{E: &str}
 
184
 
 
185
        v, err := Values(s)
 
186
        if err != nil {
 
187
                t.Errorf("Values(%q) returned error: %v", s, err)
 
188
        }
 
189
 
 
190
        want := url.Values{
 
191
                "A":         {""},
 
192
                "omitempty": {""},
 
193
                "E":         {""}, // E is included because the pointer is not empty, even though the string being pointed to is
 
194
        }
 
195
        if !reflect.DeepEqual(want, v) {
 
196
                t.Errorf("Values(%q) returned %v, want %v", s, v, want)
 
197
        }
 
198
}
 
199
 
 
200
type A struct {
 
201
        B
 
202
}
 
203
 
 
204
type B struct {
 
205
        C string
 
206
}
 
207
 
 
208
type D struct {
 
209
        B
 
210
        C string
 
211
}
 
212
 
 
213
type e struct {
 
214
        B
 
215
        C string
 
216
}
 
217
 
 
218
type F struct {
 
219
        e
 
220
}
 
221
 
 
222
func TestValues_embeddedStructs(t *testing.T) {
 
223
        tests := []struct {
 
224
                in   interface{}
 
225
                want url.Values
 
226
        }{
 
227
                {
 
228
                        A{B{C: "foo"}},
 
229
                        url.Values{"C": {"foo"}},
 
230
                },
 
231
                {
 
232
                        D{B: B{C: "bar"}, C: "foo"},
 
233
                        url.Values{"C": {"foo", "bar"}},
 
234
                },
 
235
                {
 
236
                        F{e{B: B{C: "bar"}, C: "foo"}}, // With unexported embed
 
237
                        url.Values{"C": {"foo", "bar"}},
 
238
                },
 
239
        }
 
240
 
 
241
        for i, tt := range tests {
 
242
                v, err := Values(tt.in)
 
243
                if err != nil {
 
244
                        t.Errorf("%d. Values(%q) returned error: %v", i, tt.in, err)
 
245
                }
 
246
 
 
247
                if !reflect.DeepEqual(tt.want, v) {
 
248
                        t.Errorf("%d. Values(%q) returned %v, want %v", i, tt.in, v, tt.want)
 
249
                }
 
250
        }
 
251
}
 
252
 
 
253
func TestValues_invalidInput(t *testing.T) {
 
254
        _, err := Values("")
 
255
        if err == nil {
 
256
                t.Errorf("expected Values() to return an error on invalid input")
 
257
        }
 
258
}
 
259
 
 
260
type EncodedArgs []string
 
261
 
 
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)
 
265
        }
 
266
        return nil
 
267
}
 
268
 
 
269
func TestValues_Marshaler(t *testing.T) {
 
270
        s := struct {
 
271
                Args EncodedArgs `url:"arg"`
 
272
        }{[]string{"a", "b", "c"}}
 
273
        v, err := Values(s)
 
274
        if err != nil {
 
275
                t.Errorf("Values(%q) returned error: %v", s, err)
 
276
        }
 
277
 
 
278
        want := url.Values{
 
279
                "arg.0": {"a"},
 
280
                "arg.1": {"b"},
 
281
                "arg.2": {"c"},
 
282
        }
 
283
        if !reflect.DeepEqual(want, v) {
 
284
                t.Errorf("Values(%q) returned %v, want %v", s, v, want)
 
285
        }
 
286
}
 
287
 
 
288
func TestValues_MarshalerWithNilPointer(t *testing.T) {
 
289
        s := struct {
 
290
                Args *EncodedArgs `url:"arg"`
 
291
        }{}
 
292
        v, err := Values(s)
 
293
        if err != nil {
 
294
                t.Errorf("Values(%q) returned error: %v", s, err)
 
295
        }
 
296
 
 
297
        want := url.Values{}
 
298
        if !reflect.DeepEqual(want, v) {
 
299
                t.Errorf("Values(%q) returned %v, want %v", s, v, want)
 
300
        }
 
301
}
 
302
 
 
303
func TestTagParsing(t *testing.T) {
 
304
        name, opts := parseTag("field,foobar,foo")
 
305
        if name != "field" {
 
306
                t.Fatalf("name = %q, want field", name)
 
307
        }
 
308
        for _, tt := range []struct {
 
309
                opt  string
 
310
                want bool
 
311
        }{
 
312
                {"foobar", true},
 
313
                {"foo", true},
 
314
                {"bar", false},
 
315
                {"field", false},
 
316
        } {
 
317
                if opts.Contains(tt.opt) != tt.want {
 
318
                        t.Errorf("Contains(%q) = %v", tt.opt, !tt.want)
 
319
                }
 
320
        }
 
321
}