github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/types/option_bool_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 TestOptionBool_EncodeDecode(t *testing.T) { 28 assertRoundtrip(t, NewOptionBool(NewBool(true))) 29 assertRoundtrip(t, NewOptionBool(NewBool(false))) 30 assertRoundtrip(t, NewOptionBoolEmpty()) 31 } 32 33 func TestOptionBool_EncodedLength(t *testing.T) { 34 assertEncodedLength(t, []encodedLengthAssert{ 35 {NewOptionBool(NewBool(false)), 1}, 36 {NewOptionBool(NewBool(true)), 1}, 37 {NewOptionBoolEmpty(), 1}, 38 }) 39 } 40 41 func TestOptionBool_Encode(t *testing.T) { 42 assertEncode(t, []encodingAssert{ 43 {NewOptionBool(NewBool(false)), MustHexDecodeString("0x02")}, 44 {NewOptionBool(NewBool(true)), MustHexDecodeString("0x01")}, 45 {NewOptionBoolEmpty(), MustHexDecodeString("0x00")}, 46 }) 47 } 48 49 func TestOptionBool_Hash(t *testing.T) { 50 assertHash(t, []hashAssert{ 51 {NewOptionBool(NewBool(true)), MustHexDecodeString( 52 "0xee155ace9c40292074cb6aff8c9ccdd273c81648ff1149ef36bcea6ebb8a3e25")}, 53 {NewOptionBool(NewBool(false)), MustHexDecodeString( 54 "0xbb30a42c1e62f0afda5f0a4e8a562f7a13a24cea00ee81917b86b89e801314aa")}, 55 {NewOptionBoolEmpty(), MustHexDecodeString( 56 "0x03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314")}, 57 }) 58 } 59 60 func TestOptionBool_Eq(t *testing.T) { 61 assertEq(t, []eqAssert{ 62 {NewOptionBool(NewBool(true)), NewBool(true), false}, 63 {NewOptionBool(NewBool(false)), NewOptionBool(NewBool(false)), true}, 64 {NewOptionBoolEmpty(), NewOptionBoolEmpty(), true}, 65 }) 66 } 67 68 func TestOptionBool(t *testing.T) { 69 bz := NewOptionBool(NewBool(true)) 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, NewBool(true)) 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, NewBool(false)) 81 bz.SetSome(NewBool(false)) 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, NewBool(false)) 87 }