~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/httprequest/fields_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 2015 Canonical Ltd.
 
2
// Licensed under the LGPLv3, see LICENCE file for details.
 
3
 
 
4
package httprequest
 
5
 
 
6
import (
 
7
        "reflect"
 
8
 
 
9
        jc "github.com/juju/testing/checkers"
 
10
        gc "gopkg.in/check.v1"
 
11
)
 
12
 
 
13
type fieldsSuite struct{}
 
14
 
 
15
var _ = gc.Suite(&fieldsSuite{})
 
16
 
 
17
type structField struct {
 
18
        name  string
 
19
        index []int
 
20
}
 
21
 
 
22
var fieldsTests = []struct {
 
23
        about  string
 
24
        val    interface{}
 
25
        expect []structField
 
26
}{{
 
27
        about: "simple struct",
 
28
        val: struct {
 
29
                A int
 
30
                B string
 
31
                C bool
 
32
        }{},
 
33
        expect: []structField{{
 
34
                name:  "A",
 
35
                index: []int{0},
 
36
        }, {
 
37
                name:  "B",
 
38
                index: []int{1},
 
39
        }, {
 
40
                name:  "C",
 
41
                index: []int{2},
 
42
        }},
 
43
}, {
 
44
        about: "non-embedded struct member",
 
45
        val: struct {
 
46
                A struct {
 
47
                        X int
 
48
                }
 
49
        }{},
 
50
        expect: []structField{{
 
51
                name:  "A",
 
52
                index: []int{0},
 
53
        }},
 
54
}, {
 
55
        about: "embedded exported struct",
 
56
        val: struct {
 
57
                SFG
 
58
        }{},
 
59
        expect: []structField{{
 
60
                name:  "SFG",
 
61
                index: []int{0},
 
62
        }, {
 
63
                name:  "F",
 
64
                index: []int{0, 0},
 
65
        }, {
 
66
                name:  "G",
 
67
                index: []int{0, 1},
 
68
        }},
 
69
}, {
 
70
        about: "embedded unexported struct",
 
71
        val: struct {
 
72
                sFG
 
73
        }{},
 
74
        expect: []structField{{
 
75
                name:  "sFG",
 
76
                index: []int{0},
 
77
        }, {
 
78
                name:  "F",
 
79
                index: []int{0, 0},
 
80
        }, {
 
81
                name:  "G",
 
82
                index: []int{0, 1},
 
83
        }},
 
84
}, {
 
85
        about: "two embedded structs with cancelling members",
 
86
        val: struct {
 
87
                SFG
 
88
                SF
 
89
        }{},
 
90
        expect: []structField{{
 
91
                name:  "SFG",
 
92
                index: []int{0},
 
93
        }, {
 
94
                name:  "G",
 
95
                index: []int{0, 1},
 
96
        }, {
 
97
                name:  "SF",
 
98
                index: []int{1},
 
99
        }},
 
100
}, {
 
101
        about: "embedded structs with same fields at different depths",
 
102
        val: struct {
 
103
                SFGH3
 
104
                SG1
 
105
                SFG2
 
106
                SF2
 
107
                L int
 
108
        }{},
 
109
        expect: []structField{{
 
110
                name:  "SFGH3",
 
111
                index: []int{0},
 
112
        }, {
 
113
                name:  "SFGH2",
 
114
                index: []int{0, 0},
 
115
        }, {
 
116
                name:  "SFGH1",
 
117
                index: []int{0, 0, 0},
 
118
        }, {
 
119
                name:  "SFGH",
 
120
                index: []int{0, 0, 0, 0},
 
121
        }, {
 
122
                name:  "H",
 
123
                index: []int{0, 0, 0, 0, 2},
 
124
        }, {
 
125
                name:  "SG1",
 
126
                index: []int{1},
 
127
        }, {
 
128
                name:  "SG",
 
129
                index: []int{1, 0},
 
130
        }, {
 
131
                name:  "G",
 
132
                index: []int{1, 0, 0},
 
133
        }, {
 
134
                name:  "SFG2",
 
135
                index: []int{2},
 
136
        }, {
 
137
                name:  "SFG1",
 
138
                index: []int{2, 0},
 
139
        }, {
 
140
                name:  "SFG",
 
141
                index: []int{2, 0, 0},
 
142
        }, {
 
143
                name:  "SF2",
 
144
                index: []int{3},
 
145
        }, {
 
146
                name:  "SF1",
 
147
                index: []int{3, 0},
 
148
        }, {
 
149
                name:  "SF",
 
150
                index: []int{3, 0, 0},
 
151
        }, {
 
152
                name:  "L",
 
153
                index: []int{4},
 
154
        }},
 
155
}, {
 
156
        about: "embedded pointer struct",
 
157
        val: struct {
 
158
                *SF
 
159
        }{},
 
160
        expect: []structField{{
 
161
                name:  "SF",
 
162
                index: []int{0},
 
163
        }, {
 
164
                name:  "F",
 
165
                index: []int{0, 0},
 
166
        }},
 
167
}, {
 
168
        about: "embedded not a pointer",
 
169
        val: struct {
 
170
                M
 
171
        }{},
 
172
        expect: []structField{{
 
173
                name:  "M",
 
174
                index: []int{0},
 
175
        }},
 
176
}}
 
177
 
 
178
type SFG struct {
 
179
        F int `httprequest:",form"`
 
180
        G int `httprequest:",form"`
 
181
}
 
182
 
 
183
type SFG1 struct {
 
184
        SFG
 
185
}
 
186
 
 
187
type SFG2 struct {
 
188
        SFG1
 
189
}
 
190
 
 
191
type SFGH struct {
 
192
        F int `httprequest:",form"`
 
193
        G int `httprequest:",form"`
 
194
        H int `httprequest:",form"`
 
195
}
 
196
 
 
197
type SFGH1 struct {
 
198
        SFGH
 
199
}
 
200
 
 
201
type SFGH2 struct {
 
202
        SFGH1
 
203
}
 
204
 
 
205
type SFGH3 struct {
 
206
        SFGH2
 
207
}
 
208
 
 
209
type SF struct {
 
210
        F int `httprequest:",form"`
 
211
}
 
212
 
 
213
type SF1 struct {
 
214
        SF
 
215
}
 
216
 
 
217
type SF2 struct {
 
218
        SF1
 
219
}
 
220
 
 
221
type SG struct {
 
222
        G int `httprequest:",form"`
 
223
}
 
224
 
 
225
type SG1 struct {
 
226
        SG
 
227
}
 
228
 
 
229
type sFG struct {
 
230
        F int `httprequest:",form"`
 
231
        G int `httprequest:",form"`
 
232
}
 
233
 
 
234
type M map[string]interface{}
 
235
 
 
236
func (*fieldsSuite) TestFields(c *gc.C) {
 
237
        for i, test := range fieldsTests {
 
238
                c.Logf("%d: %s", i, test.about)
 
239
                t := reflect.TypeOf(test.val)
 
240
                got := fields(t)
 
241
                c.Assert(got, gc.HasLen, len(test.expect))
 
242
                for j, field := range got {
 
243
                        expect := test.expect[j]
 
244
                        c.Logf("field %d: %s", j, expect.name)
 
245
                        gotField := t.FieldByIndex(field.Index)
 
246
                        // Unfortunately, FieldByIndex does not return
 
247
                        // a field with the same index that we passed in,
 
248
                        // so we set it to the expected value so that
 
249
                        // it can be compared later with the result of FieldByName.
 
250
                        gotField.Index = field.Index
 
251
                        expectField := t.FieldByIndex(expect.index)
 
252
                        // ditto.
 
253
                        expectField.Index = expect.index
 
254
                        c.Assert(gotField, jc.DeepEquals, expectField)
 
255
 
 
256
                        // Sanity check that we can actually access the field by the
 
257
                        // expected name.
 
258
                        expectField1, ok := t.FieldByName(expect.name)
 
259
                        c.Assert(ok, jc.IsTrue)
 
260
                        c.Assert(expectField1, jc.DeepEquals, expectField)
 
261
                }
 
262
        }
 
263
}