github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/types/data_test.go (about)

     1  // Go Substrate RPC Client (GSRPC) provides APIs and types around Polkadot and any Substrate-based chain RPC calls
     2  //
     3  // Copyright 2020 Stafi Protocol
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package types_test
    18  
    19  import (
    20  	"testing"
    21  
    22  	. "github.com/stafiprotocol/go-substrate-rpc-client/types"
    23  	"github.com/stretchr/testify/assert"
    24  )
    25  
    26  func TestData_EncodedLength(t *testing.T) {
    27  	assertEncodedLength(t, []encodedLengthAssert{
    28  		{Data([]byte{12, 251, 42}), 3},
    29  		{Data([]byte{}), 0},
    30  	})
    31  }
    32  
    33  func TestData_Encode(t *testing.T) {
    34  	bz := []byte{12, 251, 42}
    35  	data := Data(bz)
    36  	encoded, err := EncodeToBytes(data)
    37  	assert.NoError(t, err)
    38  	assert.Equal(t, bz, encoded)
    39  }
    40  
    41  func TestData_Decode(t *testing.T) {
    42  	bz := []byte{12, 251, 42}
    43  	var decoded Data
    44  	err := DecodeFromBytes(bz, &decoded)
    45  	assert.NoError(t, err)
    46  	assert.Equal(t, Data(bz), decoded)
    47  }
    48  
    49  func TestData_Hash(t *testing.T) {
    50  	assertHash(t, []hashAssert{
    51  		{Data([]byte{0, 42, 254}), MustHexDecodeString(
    52  			"0x537db36f5b5970b679a28a3df8d219317d658014fb9c3d409c0c799d8ecf149d")},
    53  		{Data([]byte{}), MustHexDecodeString(
    54  			"0x0e5751c026e543b2e8ab2eb06099daa1d1e5df47778f7787faab45cdf12fe3a8")},
    55  	})
    56  }
    57  
    58  func TestData_Hex(t *testing.T) {
    59  	assertEncodeToHex(t, []encodeToHexAssert{
    60  		{Data([]byte{0, 0, 0}), "0x000000"},
    61  		{Data([]byte{171, 18, 52}), "0xab1234"},
    62  		{Data([]byte{0, 1}), "0x0001"},
    63  		{Data([]byte{18, 52, 86}), "0x123456"},
    64  	})
    65  }
    66  
    67  func TestData_String(t *testing.T) {
    68  	assertString(t, []stringAssert{
    69  		{Data([]byte{0, 0, 0}), "[0 0 0]"},
    70  		{Data([]byte{171, 18, 52}), "[171 18 52]"},
    71  		{Data([]byte{0, 1}), "[0 1]"},
    72  		{Data([]byte{18, 52, 86}), "[18 52 86]"},
    73  	})
    74  }
    75  
    76  func TestData_Eq(t *testing.T) {
    77  	assertEq(t, []eqAssert{
    78  		{Data([]byte{1, 0, 0}), Data([]byte{1, 0}), false},
    79  		{Data([]byte{0, 0, 1}), Data([]byte{0, 1}), false},
    80  		{Data([]byte{0, 0, 0}), Data([]byte{0, 0}), false},
    81  		{Data([]byte{12, 48, 255}), Data([]byte{12, 48, 255}), true},
    82  		{Data([]byte{0}), Data([]byte{0}), true},
    83  		{Data([]byte{1}), NewBool(true), false},
    84  		{Data([]byte{0}), NewBool(false), false},
    85  	})
    86  }