| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package validate |
| 16 |
|
| 17 |
import ( |
| 18 |
"reflect" |
| 19 |
"testing" |
| 20 |
) |
| 21 |
|
| 22 |
func TestGetFields(t *testing.T) { |
| 23 |
|
| 24 |
type Test1 struct { |
| 25 |
A int |
| 26 |
B string |
| 27 |
} |
| 28 |
test1 := Test1{ |
| 29 |
A: 1, |
| 30 |
B: "one", |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
type Test2 struct { |
| 35 |
C int |
| 36 |
Test1 |
| 37 |
} |
| 38 |
test2 := Test2{ |
| 39 |
C: 5, |
| 40 |
Test1: test1, |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
type Test3 struct { |
| 45 |
D int |
| 46 |
Test2 |
| 47 |
} |
| 48 |
test3 := Test3{ |
| 49 |
D: 3, |
| 50 |
Test2: test2, |
| 51 |
} |
| 52 |
|
| 53 |
type Anything interface{} |
| 54 |
|
| 55 |
test4 := struct { |
| 56 |
E int |
| 57 |
Anything |
| 58 |
}{ |
| 59 |
E: 7, |
| 60 |
Anything: test3, |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
test5 := struct { |
| 65 |
E int |
| 66 |
F Test3 |
| 67 |
}{ |
| 68 |
E: 2, |
| 69 |
F: test3, |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
test6 := struct { |
| 74 |
E int |
| 75 |
Anything |
| 76 |
}{ |
| 77 |
E: 5, |
| 78 |
Anything: 65, |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
test7 := struct { |
| 83 |
E int |
| 84 |
Anything |
| 85 |
}{ |
| 86 |
E: 5, |
| 87 |
} |
| 88 |
|
| 89 |
tests := []struct { |
| 90 |
in reflect.Value |
| 91 |
out []StructField |
| 92 |
}{ |
| 93 |
{ |
| 94 |
reflect.ValueOf(test1), |
| 95 |
[]StructField{ |
| 96 |
{ |
| 97 |
StructField: reflect.TypeOf(test1).Field(0), |
| 98 |
Value: reflect.ValueOf(test1.A), |
| 99 |
}, |
| 100 |
{ |
| 101 |
StructField: reflect.TypeOf(test1).Field(1), |
| 102 |
Value: reflect.ValueOf(test1.B), |
| 103 |
}, |
| 104 |
}, |
| 105 |
}, |
| 106 |
{ |
| 107 |
reflect.ValueOf(test2), |
| 108 |
[]StructField{ |
| 109 |
{ |
| 110 |
StructField: reflect.TypeOf(test2).Field(0), |
| 111 |
Value: reflect.ValueOf(test2.C), |
| 112 |
}, |
| 113 |
{ |
| 114 |
StructField: reflect.TypeOf(test1).Field(0), |
| 115 |
Value: reflect.ValueOf(test1.A), |
| 116 |
}, |
| 117 |
{ |
| 118 |
StructField: reflect.TypeOf(test1).Field(1), |
| 119 |
Value: reflect.ValueOf(test1.B), |
| 120 |
}, |
| 121 |
}, |
| 122 |
}, |
| 123 |
{ |
| 124 |
reflect.ValueOf(test3), |
| 125 |
[]StructField{ |
| 126 |
{ |
| 127 |
StructField: reflect.TypeOf(test3).Field(0), |
| 128 |
Value: reflect.ValueOf(test3.D), |
| 129 |
}, |
| 130 |
{ |
| 131 |
StructField: reflect.TypeOf(test2).Field(0), |
| 132 |
Value: reflect.ValueOf(test2.C), |
| 133 |
}, |
| 134 |
{ |
| 135 |
StructField: reflect.TypeOf(test1).Field(0), |
| 136 |
Value: reflect.ValueOf(test1.A), |
| 137 |
}, |
| 138 |
{ |
| 139 |
StructField: reflect.TypeOf(test1).Field(1), |
| 140 |
Value: reflect.ValueOf(test1.B), |
| 141 |
}, |
| 142 |
}, |
| 143 |
}, |
| 144 |
{ |
| 145 |
reflect.ValueOf(test4), |
| 146 |
[]StructField{ |
| 147 |
{ |
| 148 |
StructField: reflect.TypeOf(test4).Field(0), |
| 149 |
Value: reflect.ValueOf(test4.E), |
| 150 |
}, |
| 151 |
{ |
| 152 |
StructField: reflect.TypeOf(test3).Field(0), |
| 153 |
Value: reflect.ValueOf(test3.D), |
| 154 |
}, |
| 155 |
{ |
| 156 |
StructField: reflect.TypeOf(test2).Field(0), |
| 157 |
Value: reflect.ValueOf(test2.C), |
| 158 |
}, |
| 159 |
{ |
| 160 |
StructField: reflect.TypeOf(test1).Field(0), |
| 161 |
Value: reflect.ValueOf(test1.A), |
| 162 |
}, |
| 163 |
{ |
| 164 |
StructField: reflect.TypeOf(test1).Field(1), |
| 165 |
Value: reflect.ValueOf(test1.B), |
| 166 |
}, |
| 167 |
}, |
| 168 |
}, |
| 169 |
{ |
| 170 |
reflect.ValueOf(test5), |
| 171 |
[]StructField{ |
| 172 |
{ |
| 173 |
StructField: reflect.TypeOf(test5).Field(0), |
| 174 |
Value: reflect.ValueOf(test5.E), |
| 175 |
}, |
| 176 |
{ |
| 177 |
StructField: reflect.TypeOf(test5).Field(1), |
| 178 |
Value: reflect.ValueOf(test5.F), |
| 179 |
}, |
| 180 |
}, |
| 181 |
}, |
| 182 |
{ |
| 183 |
reflect.ValueOf(test6), |
| 184 |
[]StructField{ |
| 185 |
{ |
| 186 |
StructField: reflect.TypeOf(test6).Field(0), |
| 187 |
Value: reflect.ValueOf(test6.E), |
| 188 |
}, |
| 189 |
{ |
| 190 |
StructField: reflect.TypeOf(test6).Field(1), |
| 191 |
Value: reflect.ValueOf(65), |
| 192 |
}, |
| 193 |
}, |
| 194 |
}, |
| 195 |
{ |
| 196 |
reflect.ValueOf(test7), |
| 197 |
[]StructField{ |
| 198 |
{ |
| 199 |
StructField: reflect.TypeOf(test7).Field(0), |
| 200 |
Value: reflect.ValueOf(test7.E), |
| 201 |
}, |
| 202 |
{ |
| 203 |
StructField: reflect.TypeOf(test7).Field(1), |
| 204 |
Value: reflect.ValueOf(nil), |
| 205 |
}, |
| 206 |
}, |
| 207 |
}, |
| 208 |
} |
| 209 |
|
| 210 |
for i, test := range tests { |
| 211 |
fields := GetFields(test.in) |
| 212 |
|
| 213 |
|
| 214 |
for idx, f := range fields { |
| 215 |
if !reflect.DeepEqual(f.Type, test.out[idx].Type) { |
| 216 |
t.Errorf("#%d: bad error with type: want \n%+v, got \n%+v", i, fields, test.out) |
| 217 |
} |
| 218 |
if !reflect.DeepEqual(f.Value.Interface(), test.out[idx].Value.Interface()) { |
| 219 |
t.Errorf("#%d: bad error: want \n%+v, got \n%+v", i, fields, test.out) |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
} |