github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/packages/rubygems/marshal_test.go (about)

     1  // Copyright 2023 The GitBundle Inc. All rights reserved.
     2  // Copyright 2017 The Gitea Authors. All rights reserved.
     3  // Use of this source code is governed by a MIT-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package rubygems
     7  
     8  import (
     9  	"bytes"
    10  	"testing"
    11  
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  func TestMinimalEncoder(t *testing.T) {
    16  	cases := []struct {
    17  		Value    interface{}
    18  		Expected []byte
    19  		Error    error
    20  	}{
    21  		{
    22  			Value:    nil,
    23  			Expected: []byte{4, 8, 0x30},
    24  		},
    25  		{
    26  			Value:    true,
    27  			Expected: []byte{4, 8, 'T'},
    28  		},
    29  		{
    30  			Value:    false,
    31  			Expected: []byte{4, 8, 'F'},
    32  		},
    33  		{
    34  			Value:    0,
    35  			Expected: []byte{4, 8, 'i', 0},
    36  		},
    37  		{
    38  			Value:    1,
    39  			Expected: []byte{4, 8, 'i', 6},
    40  		},
    41  		{
    42  			Value:    -1,
    43  			Expected: []byte{4, 8, 'i', 0xfa},
    44  		},
    45  		{
    46  			Value:    0x1fffffff,
    47  			Expected: []byte{4, 8, 'i', 4, 0xff, 0xff, 0xff, 0x1f},
    48  		},
    49  		{
    50  			Value: 0x41000000,
    51  			Error: ErrInvalidIntRange,
    52  		},
    53  		{
    54  			Value:    "test",
    55  			Expected: []byte{4, 8, 'I', '"', 9, 't', 'e', 's', 't', 6, ':', 6, 'E', 'T'},
    56  		},
    57  		{
    58  			Value:    []int{1, 2},
    59  			Expected: []byte{4, 8, '[', 7, 'i', 6, 'i', 7},
    60  		},
    61  		{
    62  			Value: &RubyUserMarshal{
    63  				Name:  "Test",
    64  				Value: 4,
    65  			},
    66  			Expected: []byte{4, 8, 'U', ':', 9, 'T', 'e', 's', 't', 'i', 9},
    67  		},
    68  		{
    69  			Value: &RubyUserDef{
    70  				Name:  "Test",
    71  				Value: 4,
    72  			},
    73  			Expected: []byte{4, 8, 'u', ':', 9, 'T', 'e', 's', 't', 9, 4, 8, 'i', 9},
    74  		},
    75  		{
    76  			Value: &RubyObject{
    77  				Name: "Test",
    78  				Member: map[string]interface{}{
    79  					"test": 4,
    80  				},
    81  			},
    82  			Expected: []byte{4, 8, 'o', ':', 9, 'T', 'e', 's', 't', 6, ':', 9, 't', 'e', 's', 't', 'i', 9},
    83  		},
    84  		{
    85  			Value: &struct {
    86  				Name string
    87  			}{
    88  				"test",
    89  			},
    90  			Error: ErrUnsupportedType,
    91  		},
    92  	}
    93  
    94  	for i, c := range cases {
    95  		var b bytes.Buffer
    96  		err := NewMarshalEncoder(&b).Encode(c.Value)
    97  		assert.ErrorIs(t, err, c.Error)
    98  		assert.Equal(t, c.Expected, b.Bytes(), "case %d", i)
    99  	}
   100  }