github.com/bhameyie/otto@v0.2.1-0.20160406174117-16052efa52ec/helper/schema/field_data_test.go (about) 1 package schema 2 3 import ( 4 "reflect" 5 "testing" 6 ) 7 8 func TestFieldDataGet(t *testing.T) { 9 cases := map[string]struct { 10 Schema map[string]*FieldSchema 11 Raw map[string]interface{} 12 Key string 13 Value interface{} 14 }{ 15 "string type, string value": { 16 map[string]*FieldSchema{ 17 "foo": &FieldSchema{Type: TypeString}, 18 }, 19 map[string]interface{}{ 20 "foo": "bar", 21 }, 22 "foo", 23 "bar", 24 }, 25 26 "string type, int value": { 27 map[string]*FieldSchema{ 28 "foo": &FieldSchema{Type: TypeString}, 29 }, 30 map[string]interface{}{ 31 "foo": 42, 32 }, 33 "foo", 34 "42", 35 }, 36 37 "string type, unset value": { 38 map[string]*FieldSchema{ 39 "foo": &FieldSchema{Type: TypeString}, 40 }, 41 map[string]interface{}{}, 42 "foo", 43 "", 44 }, 45 46 "string type, unset value with default": { 47 map[string]*FieldSchema{ 48 "foo": &FieldSchema{ 49 Type: TypeString, 50 Default: "bar", 51 }, 52 }, 53 map[string]interface{}{}, 54 "foo", 55 "bar", 56 }, 57 58 "int type, int value": { 59 map[string]*FieldSchema{ 60 "foo": &FieldSchema{Type: TypeInt}, 61 }, 62 map[string]interface{}{ 63 "foo": 42, 64 }, 65 "foo", 66 42, 67 }, 68 69 "bool type, bool value": { 70 map[string]*FieldSchema{ 71 "foo": &FieldSchema{Type: TypeBool}, 72 }, 73 map[string]interface{}{ 74 "foo": false, 75 }, 76 "foo", 77 false, 78 }, 79 80 "map type, map value": { 81 map[string]*FieldSchema{ 82 "foo": &FieldSchema{Type: TypeMap}, 83 }, 84 map[string]interface{}{ 85 "foo": map[string]interface{}{ 86 "child": true, 87 }, 88 }, 89 "foo", 90 map[string]interface{}{ 91 "child": true, 92 }, 93 }, 94 } 95 96 for name, tc := range cases { 97 data := &FieldData{ 98 Raw: tc.Raw, 99 Schema: tc.Schema, 100 } 101 102 actual := data.Get(tc.Key) 103 if !reflect.DeepEqual(actual, tc.Value) { 104 t.Fatalf( 105 "bad: %s\n\nExpected: %#v\nGot: %#v", 106 name, tc.Value, actual) 107 } 108 } 109 }