github.com/okex/exchain@v1.8.0/libs/tendermint/rpc/jsonrpc/client/args_test.go (about)

     1  package client
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  	"github.com/stretchr/testify/require"
     8  	amino "github.com/tendermint/go-amino"
     9  )
    10  
    11  type Tx []byte
    12  
    13  type Foo struct {
    14  	Bar int
    15  	Baz string
    16  }
    17  
    18  func TestArgToJSON(t *testing.T) {
    19  	assert := assert.New(t)
    20  	require := require.New(t)
    21  
    22  	cases := []struct {
    23  		input    interface{}
    24  		expected string
    25  	}{
    26  		{[]byte("1234"), "0x31323334"},
    27  		{Tx("654"), "0x363534"},
    28  		{Foo{7, "hello"}, `{"Bar":"7","Baz":"hello"}`},
    29  	}
    30  
    31  	cdc := amino.NewCodec()
    32  
    33  	for i, tc := range cases {
    34  		args := map[string]interface{}{"data": tc.input}
    35  		err := argsToJSON(cdc, args)
    36  		require.Nil(err, "%d: %+v", i, err)
    37  		require.Equal(1, len(args), "%d", i)
    38  		data, ok := args["data"].(string)
    39  		require.True(ok, "%d: %#v", i, args["data"])
    40  		assert.Equal(tc.expected, data, "%d", i)
    41  	}
    42  }