github.com/adoriasoft/tendermint@v0.34.0-dev1.0.20200722151356-96d84601a75a/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  )
     9  
    10  type Tx []byte
    11  
    12  type Foo struct {
    13  	Bar int
    14  	Baz string
    15  }
    16  
    17  func TestArgToJSON(t *testing.T) {
    18  	assert := assert.New(t)
    19  	require := require.New(t)
    20  
    21  	cases := []struct {
    22  		input    interface{}
    23  		expected string
    24  	}{
    25  		{[]byte("1234"), "0x31323334"},
    26  		{Tx("654"), "0x363534"},
    27  		{Foo{7, "hello"}, `{"Bar":"7","Baz":"hello"}`},
    28  	}
    29  
    30  	for i, tc := range cases {
    31  		args := map[string]interface{}{"data": tc.input}
    32  		err := argsToJSON(args)
    33  		require.Nil(err, "%d: %+v", i, err)
    34  		require.Equal(1, len(args), "%d", i)
    35  		data, ok := args["data"].(string)
    36  		require.True(ok, "%d: %#v", i, args["data"])
    37  		assert.Equal(tc.expected, data, "%d", i)
    38  	}
    39  }