github.com/coveo/gotemplate@v2.7.7+incompatible/collections/convert_data_test.go (about) 1 package collections 2 3 import ( 4 "reflect" 5 "testing" 6 ) 7 8 type dictionary = map[string]interface{} 9 10 func TestToNativeRepresentation(t *testing.T) { 11 t.Parallel() 12 13 type SubStruct struct { 14 U int64 15 I interface{} 16 } 17 type a struct { 18 private int 19 I int 20 F float64 21 S string 22 A []interface{} 23 M dictionary 24 SS SubStruct 25 } 26 tests := []struct { 27 name string 28 args interface{} 29 want interface{} 30 }{ 31 {"Struct conversion", a{ 32 private: 0, 33 I: 123, 34 F: 1.23, 35 S: "123", 36 A: []interface{}{1, "2"}, 37 M: dictionary{ 38 "a": "a", 39 "b": 2, 40 }, 41 SS: SubStruct{64, "Foo"}, 42 }, dictionary{ 43 "I": 123, 44 "F": float64(1.23), 45 "S": "123", 46 "A": []interface{}{1, "2"}, 47 "M": dictionary{ 48 "a": "a", 49 "b": 2, 50 }, 51 "SS": dictionary{ 52 "U": int64(64), 53 "I": "Foo", 54 }, 55 }}, 56 } 57 for _, tt := range tests { 58 t.Run(tt.name, func(t *testing.T) { 59 if got := ToNativeRepresentation(tt.args); !reflect.DeepEqual(got, tt.want) { 60 t.Errorf("ToNativeRepresentation()\ngot : %v\nwant: %v", got, tt.want) 61 for k, v := range tt.want.(dictionary) { 62 if reflect.DeepEqual(v, got.(dictionary)[k]) { 63 continue 64 } 65 t.Errorf("key %v: %T vs %T", k, v, got.(dictionary)[k]) 66 } 67 68 } 69 }) 70 } 71 } 72 73 func Test_quote(t *testing.T) { 74 t.Parallel() 75 76 tests := []struct { 77 name string 78 arg string 79 want string 80 }{ 81 {"Simple value", "Foo", "Foo"}, 82 {"Simple value", "Foo Bar", `"Foo Bar"`}, 83 } 84 for _, tt := range tests { 85 t.Run(tt.name, func(t *testing.T) { 86 if got := quote(tt.arg); got != tt.want { 87 t.Errorf("quote() = %v, want %v", got, tt.want) 88 } 89 }) 90 } 91 }