mirror of
https://github.com/sourcegraph/jsonrpc2.git
synced 2026-06-16 04:04:56 +02:00
This merge request moves some of the contents from the jsonrpc2.go file into their own designated file. The new files being introduced (excluding test files) are as follows: * conn.go * request.go * response.go The motive of this change is to make it easier to navigate the code. Without this change, the jsonrpc2.go file is 813 lines of code.
35 lines
987 B
Go
35 lines
987 B
Go
package jsonrpc2
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
)
|
|
|
|
func TestAnyMessage(t *testing.T) {
|
|
tests := map[string]struct {
|
|
request, response, invalid bool
|
|
}{
|
|
// Single messages
|
|
`{}`: {invalid: true},
|
|
`{"foo":"bar"}`: {invalid: true},
|
|
`{"method":"m"}`: {request: true},
|
|
`{"result":123}`: {response: true},
|
|
`{"result":null}`: {response: true},
|
|
`{"error":{"code":456,"message":"m"}}`: {response: true},
|
|
}
|
|
for s, want := range tests {
|
|
var m anyMessage
|
|
if err := json.Unmarshal([]byte(s), &m); err != nil {
|
|
if !want.invalid {
|
|
t.Errorf("%s: error: %v", s, err)
|
|
}
|
|
continue
|
|
}
|
|
if (m.request != nil) != want.request {
|
|
t.Errorf("%s: got request %v, want %v", s, m.request != nil, want.request)
|
|
}
|
|
if (m.response != nil) != want.response {
|
|
t.Errorf("%s: got response %v, want %v", s, m.response != nil, want.response)
|
|
}
|
|
}
|
|
}
|