github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/types/option_bytes_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/stretchr/testify/assert" 23 24 . "github.com/stafiprotocol/go-substrate-rpc-client/types" 25 ) 26 27 func TestOptionBytes8_EncodeDecode(t *testing.T) { 28 assertRoundtrip(t, NewOptionBytes8(NewBytes8([8]byte{12}))) 29 assertRoundtrip(t, NewOptionBytes8(NewBytes8([8]byte{}))) 30 assertRoundtrip(t, NewOptionBytes8Empty()) 31 } 32 33 func TestOptionBytes8_EncodedLength(t *testing.T) { 34 assertEncodedLength(t, []encodedLengthAssert{ 35 {NewOptionBytes8(NewBytes8([8]byte{})), 9}, 36 {NewOptionBytes8(NewBytes8([8]byte{7, 6, 5, 4, 3, 2, 1, 0})), 9}, 37 {NewOptionBytes8Empty(), 1}, 38 }) 39 } 40 41 func TestOptionBytes8_Encode(t *testing.T) { 42 assertEncode(t, []encodingAssert{ 43 {NewOptionBytes8(NewBytes8([8]byte{0, 0, 0})), MustHexDecodeString("0x010000000000000000")}, 44 {NewOptionBytes8(NewBytes8([8]byte{171, 18, 52})), MustHexDecodeString("0x01ab12340000000000")}, 45 {NewOptionBytes8Empty(), MustHexDecodeString("0x00")}, 46 }) 47 } 48 49 func TestOptionBytes8_Hash(t *testing.T) { 50 assertHash(t, []hashAssert{ 51 {NewOptionBytes8(NewBytes8([8]byte{0, 42, 254})), MustHexDecodeString( 52 "0x80c0970f2247ec2333c8f805187dcb036be18aa08ab8a738debaefa8d8f78a52")}, 53 {NewOptionBytes8(NewBytes8([8]byte{0, 0})), MustHexDecodeString( 54 "0xf7b1ba7f9618366193ada7cf4bb9904c175eab3003dea721d245fd0136b45eee")}, 55 {NewOptionBytes8Empty(), MustHexDecodeString( 56 "0x03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314")}, 57 }) 58 } 59 60 func TestOptionBytes8_Eq(t *testing.T) { 61 assertEq(t, []eqAssert{ 62 {NewOptionBytes8(NewBytes8([8]byte{1, 0, 0})), NewBytes8([8]byte{1, 0}), false}, 63 {NewOptionBytes8(NewBytes8([8]byte{0, 0, 1})), NewOptionBytes8(NewBytes8([8]byte{0, 0, 1})), true}, 64 {NewOptionBytes8Empty(), NewOptionBytes8Empty(), true}, 65 }) 66 } 67 68 func TestOptionBytes8(t *testing.T) { 69 bz := NewOptionBytes8(NewBytes8([8]byte{1, 0, 0})) 70 assert.False(t, bz.IsNone()) 71 assert.True(t, bz.IsSome()) 72 ok, val := bz.Unwrap() 73 assert.True(t, ok) 74 assert.Equal(t, val, NewBytes8([8]byte{1, 0, 0})) 75 bz.SetNone() 76 assert.True(t, bz.IsNone()) 77 assert.False(t, bz.IsSome()) 78 ok2, val2 := bz.Unwrap() 79 assert.False(t, ok2) 80 assert.Equal(t, val2, NewBytes8([8]byte{})) 81 bz.SetSome(NewBytes8([8]byte{3})) 82 assert.False(t, bz.IsNone()) 83 assert.True(t, bz.IsSome()) 84 ok3, val3 := bz.Unwrap() 85 assert.True(t, ok3) 86 assert.Equal(t, val3, NewBytes8([8]byte{3})) 87 } 88 89 func TestOptionBytes_EncodeDecode(t *testing.T) { 90 assertRoundtrip(t, NewOptionBytes(NewBytes([]byte{12}))) 91 assertRoundtrip(t, NewOptionBytes(NewBytes([]byte{2}))) 92 assertRoundtrip(t, NewOptionBytesEmpty()) 93 } 94 95 func TestOptionBytes_Encode(t *testing.T) { 96 assertEncode(t, []encodingAssert{ 97 {NewOptionBytes(NewBytes([]byte{0, 0, 0})), MustHexDecodeString("0x010c000000")}, 98 {NewOptionBytes(NewBytes([]byte{171, 1, 52})), MustHexDecodeString("0x010cab0134")}, 99 {NewOptionBytesEmpty(), MustHexDecodeString("0x00")}, 100 }) 101 } 102 103 func TestOptionBytes_Decode(t *testing.T) { 104 assertDecode(t, []decodingAssert{ 105 {MustHexDecodeString("0x010c000000"), NewOptionBytes(NewBytes([]byte{0, 0, 0}))}, 106 {MustHexDecodeString("0x010cab0134"), NewOptionBytes(NewBytes([]byte{171, 1, 52}))}, 107 {MustHexDecodeString("0x00"), NewOptionBytesEmpty()}, 108 }) 109 } 110 111 func TestOptionBytes16_EncodeDecode(t *testing.T) { 112 assertRoundtrip(t, NewOptionBytes16(NewBytes16([16]byte{12}))) 113 assertRoundtrip(t, NewOptionBytes16(NewBytes16([16]byte{}))) 114 assertRoundtrip(t, NewOptionBytes16Empty()) 115 } 116 117 func TestOptionBytes32_EncodeDecode(t *testing.T) { 118 assertRoundtrip(t, NewOptionBytes32(NewBytes32([32]byte{12}))) 119 assertRoundtrip(t, NewOptionBytes32(NewBytes32([32]byte{}))) 120 assertRoundtrip(t, NewOptionBytes32Empty()) 121 } 122 123 func TestOptionBytes64_EncodeDecode(t *testing.T) { 124 assertRoundtrip(t, NewOptionBytes64(NewBytes64([64]byte{12}))) 125 assertRoundtrip(t, NewOptionBytes64(NewBytes64([64]byte{}))) 126 assertRoundtrip(t, NewOptionBytes64Empty()) 127 } 128 129 func TestOptionBytes128_EncodeDecode(t *testing.T) { 130 assertRoundtrip(t, NewOptionBytes128(NewBytes128([128]byte{12}))) 131 assertRoundtrip(t, NewOptionBytes128(NewBytes128([128]byte{}))) 132 assertRoundtrip(t, NewOptionBytes128Empty()) 133 } 134 135 func TestOptionBytes256_EncodeDecode(t *testing.T) { 136 assertRoundtrip(t, NewOptionBytes256(NewBytes256([256]byte{12}))) 137 assertRoundtrip(t, NewOptionBytes256(NewBytes256([256]byte{}))) 138 assertRoundtrip(t, NewOptionBytes256Empty()) 139 } 140 141 func TestOptionBytes512_EncodeDecode(t *testing.T) { 142 assertRoundtrip(t, NewOptionBytes512(NewBytes512([512]byte{12}))) 143 assertRoundtrip(t, NewOptionBytes512(NewBytes512([512]byte{}))) 144 assertRoundtrip(t, NewOptionBytes512Empty()) 145 } 146 147 func TestOptionBytes1024_EncodeDecode(t *testing.T) { 148 assertRoundtrip(t, NewOptionBytes1024(NewBytes1024([1024]byte{12}))) 149 assertRoundtrip(t, NewOptionBytes1024(NewBytes1024([1024]byte{}))) 150 assertRoundtrip(t, NewOptionBytes1024Empty()) 151 } 152 153 func TestOptionBytes2048_EncodeDecode(t *testing.T) { 154 assertRoundtrip(t, NewOptionBytes2048(NewBytes2048([2048]byte{12}))) 155 assertRoundtrip(t, NewOptionBytes2048(NewBytes2048([2048]byte{}))) 156 assertRoundtrip(t, NewOptionBytes2048Empty()) 157 }