From 2dcfd037d1e064163c53433078ff8c0593976808 Mon Sep 17 00:00:00 2001 From: Silas Davis <silas@erisindustries.com> Date: Mon, 22 Aug 2016 14:22:36 +0100 Subject: [PATCH] Fix mapAndValues and even write some, like, test --- rpc/tendermint/client/client.go | 3 ++- rpc/tendermint/client/client_test.go | 34 ++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 rpc/tendermint/client/client_test.go diff --git a/rpc/tendermint/client/client.go b/rpc/tendermint/client/client.go index 9750b373..0758eb20 100644 --- a/rpc/tendermint/client/client.go +++ b/rpc/tendermint/client/client.go @@ -141,6 +141,7 @@ func performCall(client rpcclient.Client, method string, return } + func mapAndValues(orderedKeyVals ...interface{}) (map[string]interface{}, []interface{}, error) { if len(orderedKeyVals)%2 != 0 { @@ -158,7 +159,7 @@ func mapAndValues(orderedKeyVals ...interface{}) (map[string]interface{}, } val := orderedKeyVals[i+1] paramsMap[key] = val - paramsSlice = append(paramsSlice, val) + paramsSlice[i/2] = val } return paramsMap, paramsSlice, nil } diff --git a/rpc/tendermint/client/client_test.go b/rpc/tendermint/client/client_test.go new file mode 100644 index 00000000..b860fefb --- /dev/null +++ b/rpc/tendermint/client/client_test.go @@ -0,0 +1,34 @@ +package client + +import ( + "testing" + "github.com/stretchr/testify/assert" +) + +func TestMapsAndValues(t *testing.T) { + type aStruct struct { + Baz int + } + dict, vals, err := mapAndValues("Foo", aStruct{5}, + "Bar", "Nibbles") + assert.Equal(t, map[string]interface{}{ + "Foo": aStruct{5}, + "Bar": "Nibbles", + }, dict) + assert.Equal(t, []interface{}{aStruct{5}, "Nibbles"}, vals) + + // Empty map + dict, vals, err = mapAndValues() + assert.Equal(t, map[string]interface{}{}, dict) + assert.Equal(t, []interface{}{}, vals) + assert.NoError(t, err, "Empty mapsAndValues call should be fine") + + // Invalid maps + assert.NoError(t, err, "Empty mapsAndValues call should be fine") + _, _, err = mapAndValues("Foo", 4, "Bar") + assert.Error(t, err, "Should be an error to get an odd number of arguments") + + _, _, err = mapAndValues("Foo", 4, 4, "Bar") + assert.Error(t, err, "Should be an error to provide non-string keys") + +} -- GitLab