code.gitea.io/gitea@v1.19.3/modules/packages/rubygems/marshal_test.go (about)

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