github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/types/events_test.go (about) 1 package types 2 3 import ( 4 "bytes" 5 "testing" 6 7 "github.com/stafiprotocol/go-substrate-rpc-client/pkg/scale" 8 "github.com/stretchr/testify/assert" 9 ) 10 11 func TestVoteThreshold_Decoder(t *testing.T) { 12 // SuperMajorityAgainst 13 decoder := scale.NewDecoder(bytes.NewReader([]byte{1})) 14 vt := VoteThreshold(0) 15 err := decoder.Decode(&vt) 16 assert.NoError(t, err) 17 assert.Equal(t, vt, SuperMajorityAgainst) 18 19 // Error 20 decoder = scale.NewDecoder(bytes.NewReader([]byte{3})) 21 err = decoder.Decode(&vt) 22 assert.Error(t, err) 23 } 24 25 func TestVoteThreshold_Encode(t *testing.T) { 26 vt := SuperMajorityAgainst 27 var buf bytes.Buffer 28 encoder := scale.NewEncoder(&buf) 29 assert.NoError(t, encoder.Encode(vt)) 30 assert.Equal(t, buf.Len(), 1) 31 assert.Equal(t, buf.Bytes(), []byte{1}) 32 } 33 34 func TestDispatchResult_Decode(t *testing.T) { 35 // ok 36 decoder := scale.NewDecoder(bytes.NewReader([]byte{0})) 37 var res DispatchResult 38 err := decoder.Decode(&res) 39 assert.NoError(t, err) 40 assert.True(t, res.Ok) 41 42 // Dispatch Error 43 decoder = scale.NewDecoder(bytes.NewReader([]byte{1, 3, 1, 1})) 44 res = DispatchResult{} 45 assert.NoError(t, decoder.Decode(&res)) 46 assert.False(t, res.Ok) 47 assert.True(t, res.Error.HasModule) 48 assert.Equal(t, res.Error.Module, byte(1)) 49 assert.Equal(t, res.Error.Error, byte(1)) 50 51 // decoder error 52 decoder = scale.NewDecoder(bytes.NewReader([]byte{1, 3, 1})) 53 res = DispatchResult{} 54 assert.Error(t, decoder.Decode(&res)) 55 } 56 57 func TestProxyTypeEncodeDecode(t *testing.T) { 58 // encode 59 pt := Governance 60 var buf bytes.Buffer 61 encoder := scale.NewEncoder(&buf) 62 assert.NoError(t, encoder.Encode(pt)) 63 assert.Equal(t, buf.Len(), 1) 64 assert.Equal(t, buf.Bytes(), []byte{2}) 65 66 //decode 67 decoder := scale.NewDecoder(bytes.NewReader(buf.Bytes())) 68 pt0 := ProxyType(0) 69 err := decoder.Decode(&pt0) 70 assert.NoError(t, err) 71 assert.Equal(t, pt0, Governance) 72 73 //decode error 74 decoder = scale.NewDecoder(bytes.NewReader([]byte{5})) 75 pt0 = ProxyType(0) 76 err = decoder.Decode(&pt0) 77 assert.Error(t, err) 78 }