github.com/gagliardetto/solana-go@v1.11.0/text/encoder_test.go (about)

     1  // Copyright 2020 dfuse Platform Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package text
    16  
    17  import (
    18  	"bytes"
    19  	"encoding/hex"
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  )
    24  
    25  type nested struct {
    26  	F1 string
    27  	F2 string
    28  }
    29  
    30  func TestEncoder_TestStruct(t *testing.T) {
    31  	s := &binaryTestStruct{
    32  		F1:  "abc",
    33  		F2:  -75,
    34  		F3:  99,
    35  		F4:  -231,
    36  		F5:  999,
    37  		F6:  -13231,
    38  		F7:  99999,
    39  		F8:  -23.13,
    40  		F9:  3.92,
    41  		F10: []string{"def", "789"},
    42  		F11: [2]string{"foo", "bar"},
    43  		F12: 0xff,
    44  		F13: []byte{1, 2, 3, 4, 5},
    45  		F14: true,
    46  		F15: Int64(-23),
    47  		F16: Uint64(23),
    48  		F17: JSONFloat64(3.14),
    49  		F18: Uint128{
    50  			Lo: 10,
    51  			Hi: 82,
    52  		},
    53  		F19: Int128{
    54  			Lo: 7,
    55  			Hi: 3,
    56  		},
    57  		F20: Float128{
    58  			Lo: 10,
    59  			Hi: 82,
    60  		},
    61  		F21: Varuint32(999),
    62  		F22: Varint32(-999),
    63  		F23: Bool(true),
    64  		F24: HexBytes([]byte{1, 2, 3, 4, 5}),
    65  		NESTED1: &nested{
    66  			F1: "NF1",
    67  			F2: "NF2",
    68  		},
    69  		NESTED2: &nested{
    70  			F1: "NF1",
    71  			F2: "NF2",
    72  		},
    73  	}
    74  
    75  	buf := new(bytes.Buffer)
    76  	enc := NewEncoder(buf)
    77  	err := enc.Encode(s, nil)
    78  	assert.NoError(t, err)
    79  
    80  	assert.Equal(t,
    81  		"0a2062696e617279546573745374727563740a204e4553544544323a200a20206e65737465640a202046313a204e46310a202046323a204e46320a0a204e4553544544313a200a20206e65737465642046313a204e46312046323a204e4632200a2046313a206162630a2046323a202d37350a2046333a2039390a2046343a202d3233310a2046353a203939390a2046363a202d31333233310a2046373a2039393939390a2046383a202d32332e3132393939390a2046393a20332e3932303030300a204631303a200a20205b305d206465660a20205b315d203738390a204631313a200a20205b305d20666f6f0a20205b315d206261720a204631323a203235350a204631333a200a20205b305d20310a20205b315d20320a20205b325d20330a20205b335d20340a20205b345d20350a204631343a20747275650a204631353a202d32330a204631363a2032330a204631373a20332e3134303030300a204631383a20313531323633333031343034343138333233323532320a204631393a2035353334303233323232313132383635343835350a204632303a20313531323633333031343034343138333233323532320a204632313a203939390a204632323a202d3939390a204632333a20747275650a204632343a20303130323033303430350a",
    82  		hex.EncodeToString(buf.Bytes()),
    83  	)
    84  }
    85  
    86  type binaryTestStruct struct {
    87  	NESTED2 *nested `bin:"sss" text:"notype"`
    88  	NESTED1 *nested `text:"linear,notype"`
    89  	F1      string
    90  	F2      int16
    91  	F3      uint16
    92  	F4      int32
    93  	F5      uint32
    94  	F6      int64
    95  	F7      uint64
    96  	F8      float32
    97  	F9      float64
    98  	F10     []string
    99  	F11     [2]string
   100  	F12     byte
   101  	F13     []byte
   102  	F14     bool
   103  	F15     Int64
   104  	F16     Uint64
   105  	F17     JSONFloat64
   106  	F18     Uint128
   107  	F19     Int128
   108  	F20     Float128
   109  	F21     Varuint32
   110  	F22     Varint32
   111  	F23     Bool
   112  	F24     HexBytes
   113  }