|
1 | 1 | package core
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "reflect" |
4 | 5 | "testing"
|
5 | 6 |
|
6 | 7 | "github.com/alecthomas/assert"
|
@@ -36,3 +37,35 @@ func TestOneOf(t *testing.T) {
|
36 | 37 | assert.False(t, a.ConflictWith(c))
|
37 | 38 | assert.False(t, e.ConflictWith(e))
|
38 | 39 | }
|
| 40 | + |
| 41 | +func TestArgSpecGetArgsTypeField(t *testing.T) { |
| 42 | + data := struct { |
| 43 | + Field string |
| 44 | + FieldStruct struct { |
| 45 | + NestedField int |
| 46 | + } |
| 47 | + FieldSlice []float32 |
| 48 | + FieldMap map[string]bool |
| 49 | + }{} |
| 50 | + dataType := reflect.TypeOf(data) |
| 51 | + |
| 52 | + fieldSpec := ArgSpec{Name: "field"} |
| 53 | + typ, err := fieldSpec.GetArgsTypeField(dataType) |
| 54 | + assert.Nil(t, err) |
| 55 | + assert.Equal(t, reflect.TypeOf("string"), typ, "%s is not string", typ.Name()) |
| 56 | + |
| 57 | + fieldSpec = ArgSpec{Name: "field-struct.nested-field"} |
| 58 | + typ, err = fieldSpec.GetArgsTypeField(dataType) |
| 59 | + assert.Nil(t, err) |
| 60 | + assert.Equal(t, reflect.TypeOf(int(1)), typ, "%s is not int", typ.Name()) |
| 61 | + |
| 62 | + fieldSpec = ArgSpec{Name: "field-slice.{index}"} |
| 63 | + typ, err = fieldSpec.GetArgsTypeField(dataType) |
| 64 | + assert.Nil(t, err) |
| 65 | + assert.Equal(t, reflect.TypeOf(float32(1)), typ, "%s is not float32", typ.Name()) |
| 66 | + |
| 67 | + fieldSpec = ArgSpec{Name: "field-map.{key}"} |
| 68 | + typ, err = fieldSpec.GetArgsTypeField(dataType) |
| 69 | + assert.Nil(t, err) |
| 70 | + assert.Equal(t, reflect.TypeOf(true), typ, "%s is not bool", typ.Name()) |
| 71 | +} |
0 commit comments