@@ -30,6 +30,80 @@ import (
3030 "go.uber.org/thriftrw/ptr"
3131)
3232
33+ func Test_anyToString (t * testing.T ) {
34+ testCases := []struct {
35+ name string
36+ thingToSerialize interface {}
37+ expected string
38+ }{
39+ {
40+ name : "nil" ,
41+ thingToSerialize : nil ,
42+ expected : "<nil>" ,
43+ },
44+ {
45+ name : "int" ,
46+ thingToSerialize : 1 ,
47+ expected : "1" ,
48+ },
49+ {
50+ name : "string" ,
51+ thingToSerialize : "test" ,
52+ expected : "test" ,
53+ },
54+ {
55+ name : "struct" ,
56+ thingToSerialize : struct {
57+ A string
58+ B int
59+ C bool
60+ }{A : "test" , B : 1 , C : true },
61+ expected : "(A:test, B:1, C:true)" ,
62+ },
63+ {
64+ name : "pointer" ,
65+ thingToSerialize : & struct {
66+ A string
67+ B int
68+ C bool
69+ }{A : "test" , B : 1 , C : true },
70+ expected : "(A:test, B:1, C:true)" ,
71+ },
72+ {
73+ name : "slice" ,
74+ thingToSerialize : []int {1 , 2 , 3 },
75+ expected : "[1 2 3]" ,
76+ },
77+ {
78+ name : "struct with slice" ,
79+ thingToSerialize : struct { A []int }{A : []int {1 , 2 , 3 }},
80+ expected : "(A:[len=3])" ,
81+ },
82+ {
83+ name : "struct with blob" ,
84+ thingToSerialize : struct { A []byte }{A : []byte ("blob-data" )},
85+ expected : "(A:[blob-data])" ,
86+ },
87+ {
88+ name : "struct with struct" ,
89+ thingToSerialize : struct { A struct { B int } }{A : struct { B int }{B : 1 }},
90+ expected : "(A:(B:1))" ,
91+ },
92+ {
93+ name : "struct with pointer" ,
94+ thingToSerialize : struct { A * int }{A : new (int )},
95+ expected : "(A:0)" ,
96+ },
97+ }
98+
99+ for _ , tc := range testCases {
100+ t .Run (tc .name , func (t * testing.T ) {
101+ actual := anyToString (tc .thingToSerialize )
102+ require .Equal (t , tc .expected , actual )
103+ })
104+ }
105+ }
106+
33107func Test_byteSliceToString (t * testing.T ) {
34108 data := []byte ("blob-data" )
35109 v := reflect .ValueOf (data )
0 commit comments