github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/types/election_compute_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 TestElectionComputeEncodeDecode(t *testing.T) {
    12  	// encode
    13  	ec := OnChain
    14  	var buf bytes.Buffer
    15  	encoder := scale.NewEncoder(&buf)
    16  	assert.NoError(t, encoder.Encode(ec))
    17  	assert.Equal(t, buf.Len(), 1)
    18  	assert.Equal(t, buf.Bytes(), []byte{0})
    19  
    20  	//decode
    21  	decoder := scale.NewDecoder(bytes.NewReader(buf.Bytes()))
    22  	ec0 := ElectionCompute(0)
    23  	err := decoder.Decode(&ec0)
    24  	assert.NoError(t, err)
    25  	assert.Equal(t, ec0, OnChain)
    26  
    27  	//decode error
    28  	decoder = scale.NewDecoder(bytes.NewReader([]byte{5}))
    29  	ec0 = ElectionCompute(0)
    30  	err = decoder.Decode(&ec0)
    31  	assert.Error(t, err)
    32  }