github.com/batchcorp/thrift-iterator@v0.0.0-20220918180557-4c4a158fc6e9/test/level_2/map_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/test"
     7  	"github.com/stretchr/testify/require"
     8  	"testing"
     9  )
    10  
    11  func Test_skip_map_of_string_key(t *testing.T) {
    12  	should := require.New(t)
    13  	for _, c := range test.Combinations {
    14  		buf, proto := c.CreateProtocol()
    15  		proto.WriteMapBegin(thrift.STRING, thrift.I64, 1)
    16  		proto.WriteString("1")
    17  		proto.WriteI64(1)
    18  		proto.WriteMapEnd()
    19  		iter := c.CreateIterator(buf.Bytes())
    20  		should.Equal(buf.Bytes(), iter.SkipMap(nil))
    21  	}
    22  }
    23  
    24  func Test_skip_map_of_string_elem(t *testing.T) {
    25  	should := require.New(t)
    26  	for _, c := range test.Combinations {
    27  		buf, proto := c.CreateProtocol()
    28  		proto.WriteMapBegin(thrift.I64, thrift.STRING, 1)
    29  		proto.WriteI64(1)
    30  		proto.WriteString("1")
    31  		proto.WriteMapEnd()
    32  		iter := c.CreateIterator(buf.Bytes())
    33  		should.Equal(buf.Bytes(), iter.SkipMap(nil))
    34  	}
    35  }
    36  
    37  func Test_unmarshal_general_map_of_string_key(t *testing.T) {
    38  	should := require.New(t)
    39  	for _, c := range test.Combinations {
    40  		buf, proto := c.CreateProtocol()
    41  		proto.WriteMapBegin(thrift.STRING, thrift.I64, 1)
    42  		proto.WriteString("1")
    43  		proto.WriteI64(1)
    44  		proto.WriteMapEnd()
    45  		var val general.Map
    46  		should.NoError(c.Unmarshal(buf.Bytes(), &val))
    47  		should.Equal(general.Map{
    48  			"1": int64(1),
    49  		}, val)
    50  	}
    51  }
    52  
    53  func Test_unmarshal_map_of_string_key(t *testing.T) {
    54  	should := require.New(t)
    55  	for _, c := range test.UnmarshalCombinations {
    56  		buf, proto := c.CreateProtocol()
    57  		proto.WriteMapBegin(thrift.STRING, thrift.I64, 1)
    58  		proto.WriteString("1")
    59  		proto.WriteI64(1)
    60  		proto.WriteMapEnd()
    61  		var val map[string]int64
    62  		should.NoError(c.Unmarshal(buf.Bytes(), &val))
    63  		should.Equal(map[string]int64{
    64  			"1": 1,
    65  		}, val)
    66  	}
    67  }
    68  
    69  func Test_marshal_general_map_of_string_key(t *testing.T) {
    70  	should := require.New(t)
    71  	for _, c := range test.Combinations {
    72  		m := general.Map{
    73  			"1": int64(1),
    74  		}
    75  
    76  		output, err := c.Marshal(m)
    77  		should.NoError(err)
    78  		output1, err := c.Marshal(&m)
    79  		should.NoError(err)
    80  		should.Equal(output, output1)
    81  		var val general.Map
    82  		should.NoError(c.Unmarshal(output, &val))
    83  		should.Equal(general.Map{
    84  			"1": int64(1),
    85  		}, val)
    86  	}
    87  }
    88  
    89  func Test_marshal_map_of_string_key(t *testing.T) {
    90  	should := require.New(t)
    91  	for _, c := range test.MarshalCombinations {
    92  		m := map[string]int64{
    93  			"1": 1,
    94  		}
    95  
    96  		output, err := c.Marshal(m)
    97  		should.NoError(err)
    98  		output1, err := c.Marshal(&m)
    99  		should.NoError(err)
   100  		should.Equal(output, output1)
   101  		var val general.Map
   102  		should.NoError(c.Unmarshal(output, &val))
   103  		should.Equal(general.Map{
   104  			"1": int64(1),
   105  		}, val)
   106  	}
   107  }