github.com/batchcorp/thrift-iterator@v0.0.0-20220918180557-4c4a158fc6e9/test/level_2/struct_of_pointer_test.go (about)

     1  package test
     2  
     3  import (
     4  	"github.com/apache/thrift/lib/go/thrift"
     5  	"github.com/batchcorp/thrift-iterator/test"
     6  	"github.com/batchcorp/thrift-iterator/test/level_2/struct_of_pointer_test"
     7  	"github.com/stretchr/testify/require"
     8  	"testing"
     9  )
    10  
    11  func Test_unmarshal_struct_of_1_ptr(t *testing.T) {
    12  	should := require.New(t)
    13  	for _, c := range test.UnmarshalCombinations {
    14  		buf, proto := c.CreateProtocol()
    15  		proto.WriteStructBegin("hello")
    16  		proto.WriteFieldBegin("field1", thrift.I64, 1)
    17  		proto.WriteI64(1)
    18  		proto.WriteFieldEnd()
    19  		proto.WriteFieldStop()
    20  		proto.WriteStructEnd()
    21  		var val *struct_of_pointer_test.StructOf1Ptr
    22  		should.NoError(c.Unmarshal(buf.Bytes(), &val))
    23  		should.Equal(1, *val.Field1)
    24  	}
    25  }
    26  
    27  func Test_unmarshal_struct_of_2_ptr(t *testing.T) {
    28  	should := require.New(t)
    29  	for _, c := range test.UnmarshalCombinations {
    30  		buf, proto := c.CreateProtocol()
    31  		proto.WriteStructBegin("hello")
    32  		proto.WriteFieldBegin("field1", thrift.I64, 1)
    33  		proto.WriteI64(1)
    34  		proto.WriteFieldEnd()
    35  		proto.WriteFieldBegin("field2", thrift.I64, 2)
    36  		proto.WriteI64(2)
    37  		proto.WriteFieldEnd()
    38  		proto.WriteFieldStop()
    39  		proto.WriteStructEnd()
    40  		var val *struct_of_pointer_test.StructOf2Ptr
    41  		should.NoError(c.Unmarshal(buf.Bytes(), &val))
    42  		should.Equal(1, *val.Field1)
    43  		should.Equal(2, *val.Field2)
    44  	}
    45  }
    46  
    47  func Test_marshal_struct_of_1_ptr(t *testing.T) {
    48  	should := require.New(t)
    49  	for _, c := range test.MarshalCombinations {
    50  		one := 1
    51  		obj := struct_of_pointer_test.StructOf1Ptr{
    52  			&one,
    53  		}
    54  
    55  		output, err := c.Marshal(obj)
    56  		should.NoError(err)
    57  		output1, err := c.Marshal(&obj)
    58  		should.NoError(err)
    59  		should.Equal(output, output1)
    60  		var val *struct_of_pointer_test.StructOf1Ptr
    61  		should.NoError(c.Unmarshal(output, &val))
    62  		should.Equal(1, *val.Field1)
    63  	}
    64  }
    65  
    66  func Test_marshal_struct_of_2_ptr(t *testing.T) {
    67  	should := require.New(t)
    68  	for _, c := range test.MarshalCombinations {
    69  		one := 1
    70  		two := 2
    71  		obj := struct_of_pointer_test.StructOf2Ptr{
    72  			&one, &two,
    73  		}
    74  
    75  		output, err := c.Marshal(obj)
    76  		should.NoError(err)
    77  		output1, err := c.Marshal(&obj)
    78  		should.NoError(err)
    79  		should.Equal(output, output1)
    80  		var val *struct_of_pointer_test.StructOf2Ptr
    81  		should.NoError(c.Unmarshal(output, &val))
    82  		should.Equal(1, *val.Field1)
    83  		should.Equal(2, *val.Field2)
    84  	}
    85  }