1
// Copyright 2015 Canonical Ltd.
2
// Licensed under the LGPLv3, see LICENCE file for details.
9
jc "github.com/juju/testing/checkers"
10
gc "gopkg.in/check.v1"
13
type fieldsSuite struct{}
15
var _ = gc.Suite(&fieldsSuite{})
17
type structField struct {
22
var fieldsTests = []struct {
27
about: "simple struct",
33
expect: []structField{{
44
about: "non-embedded struct member",
50
expect: []structField{{
55
about: "embedded exported struct",
59
expect: []structField{{
70
about: "embedded unexported struct",
74
expect: []structField{{
85
about: "two embedded structs with cancelling members",
90
expect: []structField{{
101
about: "embedded structs with same fields at different depths",
109
expect: []structField{{
117
index: []int{0, 0, 0},
120
index: []int{0, 0, 0, 0},
123
index: []int{0, 0, 0, 0, 2},
132
index: []int{1, 0, 0},
141
index: []int{2, 0, 0},
150
index: []int{3, 0, 0},
156
about: "embedded pointer struct",
160
expect: []structField{{
168
about: "embedded not a pointer",
172
expect: []structField{{
179
F int `httprequest:",form"`
180
G int `httprequest:",form"`
192
F int `httprequest:",form"`
193
G int `httprequest:",form"`
194
H int `httprequest:",form"`
210
F int `httprequest:",form"`
222
G int `httprequest:",form"`
230
F int `httprequest:",form"`
231
G int `httprequest:",form"`
234
type M map[string]interface{}
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)
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)
253
expectField.Index = expect.index
254
c.Assert(gotField, jc.DeepEquals, expectField)
256
// Sanity check that we can actually access the field by the
258
expectField1, ok := t.FieldByName(expect.name)
259
c.Assert(ok, jc.IsTrue)
260
c.Assert(expectField1, jc.DeepEquals, expectField)