1
0
Fork 0
mirror of https://github.com/sourcegraph/jsonrpc2.git synced 2026-06-16 20:20:03 +02:00
jsonrpc2/internal_test.go
Sam Herrmann 846c29e96d
Split jsonrpc2.go file into multiple files (#65)
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.
2023-02-09 07:56:42 +01:00

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)
}
}
}