2014-12-19 11:56:46 +01:00
|
|
|
package schema
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMapFieldReader_impl(t *testing.T) {
|
|
|
|
var _ FieldReader = new(MapFieldReader)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMapFieldReader(t *testing.T) {
|
2015-01-10 21:42:15 +01:00
|
|
|
testFieldReader(t, func(s map[string]*Schema) FieldReader {
|
|
|
|
return &MapFieldReader{
|
|
|
|
Schema: s,
|
|
|
|
|
|
|
|
Map: BasicMapReader(map[string]string{
|
|
|
|
"bool": "true",
|
|
|
|
"int": "42",
|
2015-01-11 01:04:01 +01:00
|
|
|
"float": "3.1415",
|
2015-01-10 21:42:15 +01:00
|
|
|
"string": "string",
|
|
|
|
|
|
|
|
"list.#": "2",
|
|
|
|
"list.0": "foo",
|
|
|
|
"list.1": "bar",
|
|
|
|
|
|
|
|
"listInt.#": "2",
|
|
|
|
"listInt.0": "21",
|
|
|
|
"listInt.1": "42",
|
|
|
|
|
2015-01-15 23:12:24 +01:00
|
|
|
"map.#": "2",
|
2015-01-10 21:42:15 +01:00
|
|
|
"map.foo": "bar",
|
|
|
|
"map.bar": "baz",
|
|
|
|
|
|
|
|
"set.#": "2",
|
|
|
|
"set.10": "10",
|
|
|
|
"set.50": "50",
|
|
|
|
|
|
|
|
"setDeep.#": "2",
|
|
|
|
"setDeep.10.index": "10",
|
|
|
|
"setDeep.10.value": "foo",
|
|
|
|
"setDeep.50.index": "50",
|
|
|
|
"setDeep.50.value": "bar",
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMapFieldReader_extra(t *testing.T) {
|
2014-12-19 11:56:46 +01:00
|
|
|
r := &MapFieldReader{
|
2015-01-03 18:13:46 +01:00
|
|
|
Schema: map[string]*Schema{
|
2015-01-10 00:07:02 +01:00
|
|
|
"mapDel": &Schema{Type: TypeMap},
|
2015-01-03 18:13:46 +01:00
|
|
|
},
|
|
|
|
|
2015-01-09 03:02:19 +01:00
|
|
|
Map: BasicMapReader(map[string]string{
|
2015-01-10 00:07:02 +01:00
|
|
|
"mapDel": "",
|
2015-01-09 03:02:19 +01:00
|
|
|
}),
|
2014-12-19 11:56:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
cases := map[string]struct {
|
2014-12-19 12:15:04 +01:00
|
|
|
Addr []string
|
|
|
|
Out interface{}
|
|
|
|
OutOk bool
|
|
|
|
OutComputed bool
|
|
|
|
OutErr bool
|
2014-12-19 11:56:46 +01:00
|
|
|
}{
|
2015-01-10 00:07:02 +01:00
|
|
|
"mapDel": {
|
|
|
|
[]string{"mapDel"},
|
|
|
|
map[string]interface{}{},
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
},
|
2014-12-19 11:56:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range cases {
|
2015-01-03 18:13:46 +01:00
|
|
|
out, err := r.ReadField(tc.Addr)
|
2014-12-19 21:47:35 +01:00
|
|
|
if (err != nil) != tc.OutErr {
|
|
|
|
t.Fatalf("%s: err: %s", name, err)
|
2014-12-19 11:56:46 +01:00
|
|
|
}
|
2014-12-19 21:47:35 +01:00
|
|
|
if out.Computed != tc.OutComputed {
|
|
|
|
t.Fatalf("%s: err: %#v", name, out.Computed)
|
2014-12-19 12:15:04 +01:00
|
|
|
}
|
2014-12-19 11:56:46 +01:00
|
|
|
|
2014-12-19 21:47:35 +01:00
|
|
|
if s, ok := out.Value.(*Set); ok {
|
2014-12-19 11:56:46 +01:00
|
|
|
// If it is a set, convert to a list so its more easily checked.
|
2014-12-19 21:47:35 +01:00
|
|
|
out.Value = s.List()
|
2014-12-19 11:56:46 +01:00
|
|
|
}
|
|
|
|
|
2014-12-19 21:47:35 +01:00
|
|
|
if !reflect.DeepEqual(out.Value, tc.Out) {
|
|
|
|
t.Fatalf("%s: out: %#v", name, out.Value)
|
2014-12-19 11:56:46 +01:00
|
|
|
}
|
2014-12-19 21:47:35 +01:00
|
|
|
if out.Exists != tc.OutOk {
|
|
|
|
t.Fatalf("%s: outOk: %#v", name, out.Exists)
|
2014-12-19 11:56:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|