github.com/batchcorp/thrift-iterator@v0.0.0-20220918180557-4c4a158fc6e9/test/level_2/struct_of_string_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_string_test"
     9  	"github.com/stretchr/testify/require"
    10  	"testing"
    11  )
    12  
    13  func Test_skip_struct_of_string(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.STRING, 1)
    19  		proto.WriteString("abc")
    20  		proto.WriteFieldEnd()
    21  		proto.WriteFieldStop()
    22  		proto.WriteStructEnd()
    23  		iter := c.CreateIterator(buf.Bytes())
    24  		should.Equal(buf.Bytes(), iter.SkipStruct(nil))
    25  	}
    26  }
    27  
    28  func Test_unmarshal_general_struct_of_string(t *testing.T) {
    29  	should := require.New(t)
    30  	for _, c := range test.Combinations {
    31  		buf, proto := c.CreateProtocol()
    32  		proto.WriteStructBegin("hello")
    33  		proto.WriteFieldBegin("field1", thrift.STRING, 1)
    34  		proto.WriteString("abc")
    35  		proto.WriteFieldEnd()
    36  		proto.WriteFieldStop()
    37  		proto.WriteStructEnd()
    38  		var val general.Struct
    39  		should.NoError(c.Unmarshal(buf.Bytes(), &val))
    40  		should.Equal("abc", val[protocol.FieldId(1)])
    41  	}
    42  }
    43  
    44  func Test_unmarshal_struct_of_string(t *testing.T) {
    45  	should := require.New(t)
    46  	for _, c := range test.UnmarshalCombinations {
    47  		buf, proto := c.CreateProtocol()
    48  		proto.WriteStructBegin("hello")
    49  		proto.WriteFieldBegin("field1", thrift.STRING, 1)
    50  		proto.WriteString("abc")
    51  		proto.WriteFieldEnd()
    52  		proto.WriteFieldStop()
    53  		proto.WriteStructEnd()
    54  		var val struct_of_string_test.TestObject
    55  		should.NoError(c.Unmarshal(buf.Bytes(), &val))
    56  		should.Equal(struct_of_string_test.TestObject{
    57  			"abc",
    58  		}, val)
    59  	}
    60  }
    61  
    62  func Test_marshal_general_struct_of_string(t *testing.T) {
    63  	should := require.New(t)
    64  	for _, c := range test.Combinations {
    65  		obj := general.Struct{
    66  			protocol.FieldId(1): "abc",
    67  		}
    68  
    69  		output, err := c.Marshal(obj)
    70  		should.NoError(err)
    71  		output1, err := c.Marshal(&obj)
    72  		should.NoError(err)
    73  		should.Equal(output, output1)
    74  		var val general.Struct
    75  		should.NoError(c.Unmarshal(output, &val))
    76  		should.Equal("abc", val[protocol.FieldId(1)])
    77  	}
    78  }
    79  
    80  func Test_marshal_struct_of_string(t *testing.T) {
    81  	should := require.New(t)
    82  	for _, c := range test.MarshalCombinations {
    83  		obj := struct_of_string_test.TestObject{
    84  			"abc",
    85  		}
    86  
    87  		output, err := c.Marshal(obj)
    88  		should.NoError(err)
    89  		output1, err := c.Marshal(&obj)
    90  		should.NoError(err)
    91  		should.Equal(output, output1)
    92  		var val general.Struct
    93  		should.NoError(c.Unmarshal(output, &val))
    94  		should.Equal("abc", val[protocol.FieldId(1)])
    95  	}
    96  }