Skip to content

Commit

Permalink
Fix codecov for `rpc/jsonrpc: Unmarshal RPCRequest correctly (bp #619…
Browse files Browse the repository at this point in the history
…1) (#6193)`
  • Loading branch information
tnasu committed Dec 8, 2021
1 parent 1c568c2 commit 9b3faee
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions rpc/jsonrpc/types/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"reflect"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -81,3 +82,43 @@ func TestRPCError(t *testing.T) {
Message: "Badness",
}))
}

func TestRPCRequestUnmarshalJSON(t *testing.T) {
tcs := []struct {
assert bool
expected RPCRequest
}{
{
assert: true,
expected: RPCRequest{ID: JSONRPCIntID(1), Method: "id int", Params: json.RawMessage(`{"p1":"v1"}`)},
}, {
assert: true,
expected: RPCRequest{ID: JSONRPCStringID("s"), Method: "id string", Params: json.RawMessage(`{"p1":"v1"}`)},
}, {
assert: true,
expected: RPCRequest{ID: JSONRPCStringID(""), Method: "id empty", Params: json.RawMessage(`{"p1":"v1"}`)},
}, {
// can't treat nil as jsonrpcid
assert: false,
expected: RPCRequest{ID: nil, Method: "id nil", Params: json.RawMessage(`{"p1":"v1"}`)},
}, {
assert: true,
expected: RPCRequest{ID: JSONRPCIntID(1), Method: "params null", Params: json.RawMessage(`null`)},
}, {
// can't treat nil as json.RawMessage: the value of `nil` become "null" string
assert: false,
expected: RPCRequest{ID: JSONRPCIntID(1), Method: "params nil", Params: nil},
},
}
for _, tc := range tcs {
data, _ := json.Marshal(tc.expected)
actual := RPCRequest{}
json.Unmarshal(data, &actual) // nolint: errcheck
assert.Equal(t, reflect.DeepEqual(tc.expected, actual), tc.assert,
"expected:", tc.expected, "actual:", actual)
actual2 := RPCRequest{}
actual2.UnmarshalJSON(data) // nolint: errcheck
assert.Equal(t, reflect.DeepEqual(tc.expected, actual2), tc.assert,
"expected:", tc.expected, "actual2:", actual2)
}
}

0 comments on commit 9b3faee

Please sign in to comment.