github.com/number571/tendermint@v0.34.11-gost/abci/types/messages_test.go (about)

     1  package types
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/gogo/protobuf/proto"
    10  	"github.com/stretchr/testify/assert"
    11  
    12  	tmproto "github.com/number571/tendermint/proto/tendermint/types"
    13  )
    14  
    15  func TestMarshalJSON(t *testing.T) {
    16  	b, err := json.Marshal(&ResponseDeliverTx{})
    17  	assert.Nil(t, err)
    18  	// include empty fields.
    19  	assert.True(t, strings.Contains(string(b), "code"))
    20  	r1 := ResponseCheckTx{
    21  		Code:      1,
    22  		Data:      []byte("hello"),
    23  		GasWanted: 43,
    24  		Events: []Event{
    25  			{
    26  				Type: "testEvent",
    27  				Attributes: []EventAttribute{
    28  					{Key: "pho", Value: "bo"},
    29  				},
    30  			},
    31  		},
    32  	}
    33  	b, err = json.Marshal(&r1)
    34  	assert.Nil(t, err)
    35  
    36  	var r2 ResponseCheckTx
    37  	err = json.Unmarshal(b, &r2)
    38  	assert.Nil(t, err)
    39  	assert.Equal(t, r1, r2)
    40  }
    41  
    42  func TestWriteReadMessageSimple(t *testing.T) {
    43  	cases := []proto.Message{
    44  		&RequestEcho{
    45  			Message: "Hello",
    46  		},
    47  	}
    48  
    49  	for _, c := range cases {
    50  		buf := new(bytes.Buffer)
    51  		err := WriteMessage(c, buf)
    52  		assert.Nil(t, err)
    53  
    54  		msg := new(RequestEcho)
    55  		err = ReadMessage(buf, msg)
    56  		assert.Nil(t, err)
    57  
    58  		assert.True(t, proto.Equal(c, msg))
    59  	}
    60  }
    61  
    62  func TestWriteReadMessage(t *testing.T) {
    63  	cases := []proto.Message{
    64  		&tmproto.Header{
    65  			Height:  4,
    66  			ChainID: "test",
    67  		},
    68  		// TODO: add the rest
    69  	}
    70  
    71  	for _, c := range cases {
    72  		buf := new(bytes.Buffer)
    73  		err := WriteMessage(c, buf)
    74  		assert.Nil(t, err)
    75  
    76  		msg := new(tmproto.Header)
    77  		err = ReadMessage(buf, msg)
    78  		assert.Nil(t, err)
    79  
    80  		assert.True(t, proto.Equal(c, msg))
    81  	}
    82  }
    83  
    84  func TestWriteReadMessage2(t *testing.T) {
    85  	phrase := "hello-world"
    86  	cases := []proto.Message{
    87  		&ResponseCheckTx{
    88  			Data:      []byte(phrase),
    89  			Log:       phrase,
    90  			GasWanted: 10,
    91  			Events: []Event{
    92  				{
    93  					Type: "testEvent",
    94  					Attributes: []EventAttribute{
    95  						{Key: "abc", Value: "def"},
    96  					},
    97  				},
    98  			},
    99  		},
   100  		// TODO: add the rest
   101  	}
   102  
   103  	for _, c := range cases {
   104  		buf := new(bytes.Buffer)
   105  		err := WriteMessage(c, buf)
   106  		assert.Nil(t, err)
   107  
   108  		msg := new(ResponseCheckTx)
   109  		err = ReadMessage(buf, msg)
   110  		assert.Nil(t, err)
   111  
   112  		assert.True(t, proto.Equal(c, msg))
   113  	}
   114  }