github.com/batchcorp/thrift-iterator@v0.0.0-20220918180557-4c4a158fc6e9/test/level_2/struct_of_struct_test.go (about) 1 package test 2 3 import ( 4 "github.com/apache/thrift/lib/go/thrift" 5 "github.com/batchcorp/thrift-iterator/general" 6 "github.com/batchcorp/thrift-iterator/protocol" 7 "github.com/batchcorp/thrift-iterator/test" 8 "github.com/batchcorp/thrift-iterator/test/level_2/struct_of_struct_test" 9 "github.com/stretchr/testify/require" 10 "testing" 11 ) 12 13 func Test_skip_struct_of_struct(t *testing.T) { 14 should := require.New(t) 15 for _, c := range test.Combinations { 16 buf, proto := c.CreateProtocol() 17 proto.WriteStructBegin("hello") 18 proto.WriteFieldBegin("field1", thrift.STRUCT, 1) 19 20 proto.WriteStructBegin("hello") 21 proto.WriteFieldBegin("field1", thrift.STRING, 1) 22 proto.WriteString("abc") 23 proto.WriteFieldEnd() 24 proto.WriteFieldStop() 25 proto.WriteStructEnd() 26 27 proto.WriteFieldEnd() 28 proto.WriteFieldStop() 29 proto.WriteStructEnd() 30 iter := c.CreateIterator(buf.Bytes()) 31 should.Equal(buf.Bytes(), iter.SkipStruct(nil)) 32 } 33 } 34 35 func Test_unmarshal_general_struct_of_struct(t *testing.T) { 36 should := require.New(t) 37 for _, c := range test.Combinations { 38 buf, proto := c.CreateProtocol() 39 proto.WriteStructBegin("hello") 40 proto.WriteFieldBegin("field1", thrift.STRUCT, 1) 41 42 proto.WriteStructBegin("hello") 43 proto.WriteFieldBegin("field1", thrift.STRING, 1) 44 proto.WriteString("abc") 45 proto.WriteFieldEnd() 46 proto.WriteFieldStop() 47 proto.WriteStructEnd() 48 49 proto.WriteFieldEnd() 50 proto.WriteFieldStop() 51 proto.WriteStructEnd() 52 var val general.Struct 53 should.NoError(c.Unmarshal(buf.Bytes(), &val)) 54 should.Equal(general.Struct{ 55 protocol.FieldId(1): "abc", 56 }, val[protocol.FieldId(1)]) 57 } 58 } 59 60 func Test_unmarshal_struct_of_struct(t *testing.T) { 61 should := require.New(t) 62 for _, c := range test.UnmarshalCombinations { 63 buf, proto := c.CreateProtocol() 64 proto.WriteStructBegin("hello") 65 proto.WriteFieldBegin("field1", thrift.STRUCT, 1) 66 67 proto.WriteStructBegin("hello") 68 proto.WriteFieldBegin("field1", thrift.STRING, 1) 69 proto.WriteString("abc") 70 proto.WriteFieldEnd() 71 proto.WriteFieldStop() 72 proto.WriteStructEnd() 73 74 proto.WriteFieldEnd() 75 proto.WriteFieldStop() 76 proto.WriteStructEnd() 77 var val struct_of_struct_test.TestObject 78 should.NoError(c.Unmarshal(buf.Bytes(), &val)) 79 should.Equal(struct_of_struct_test.TestObject{ 80 struct_of_struct_test.EmbeddedObject{"abc"}, 81 }, val) 82 } 83 } 84 85 func Test_marshal_general_struct_of_struct(t *testing.T) { 86 should := require.New(t) 87 for _, c := range test.Combinations { 88 obj := general.Struct{ 89 protocol.FieldId(1): general.Struct{ 90 protocol.FieldId(1): "abc", 91 }, 92 } 93 94 output, err := c.Marshal(obj) 95 should.NoError(err) 96 output1, err := c.Marshal(&obj) 97 should.NoError(err) 98 should.Equal(output, output1) 99 var val general.Struct 100 should.NoError(c.Unmarshal(output, &val)) 101 should.Equal(general.Struct{ 102 protocol.FieldId(1): "abc", 103 }, val[protocol.FieldId(1)]) 104 } 105 } 106 107 func Test_marshal_struct_of_struct(t *testing.T) { 108 should := require.New(t) 109 for _, c := range test.MarshalCombinations { 110 obj := struct_of_struct_test.TestObject{ 111 struct_of_struct_test.EmbeddedObject{"abc"}, 112 } 113 114 output, err := c.Marshal(obj) 115 should.NoError(err) 116 output1, err := c.Marshal(&obj) 117 should.NoError(err) 118 should.Equal(output, output1) 119 var val general.Struct 120 should.NoError(c.Unmarshal(output, &val)) 121 should.Equal(general.Struct{ 122 protocol.FieldId(1): "abc", 123 }, val[protocol.FieldId(1)]) 124 } 125 }