2020-04-07 13:58:08 -06:00
|
|
|
// Copyright 2020 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package jsonrpc2_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"golang.org/x/tools/internal/jsonrpc2"
|
|
|
|
)
|
|
|
|
|
|
|
|
var wireIDTestData = []struct {
|
|
|
|
name string
|
2020-04-21 09:47:21 -06:00
|
|
|
id jsonrpc2.ID
|
2020-04-07 13:58:08 -06:00
|
|
|
encoded []byte
|
|
|
|
plain string
|
|
|
|
quoted string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: `empty`,
|
|
|
|
encoded: []byte(`0`),
|
|
|
|
plain: `0`,
|
|
|
|
quoted: `#0`,
|
|
|
|
}, {
|
|
|
|
name: `number`,
|
2020-04-09 21:00:00 -06:00
|
|
|
id: jsonrpc2.NewIntID(43),
|
2020-04-07 13:58:08 -06:00
|
|
|
encoded: []byte(`43`),
|
|
|
|
plain: `43`,
|
|
|
|
quoted: `#43`,
|
|
|
|
}, {
|
|
|
|
name: `string`,
|
2020-04-09 21:00:00 -06:00
|
|
|
id: jsonrpc2.NewStringID("life"),
|
2020-04-07 13:58:08 -06:00
|
|
|
encoded: []byte(`"life"`),
|
|
|
|
plain: `life`,
|
|
|
|
quoted: `"life"`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIDFormat(t *testing.T) {
|
|
|
|
for _, test := range wireIDTestData {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
if got := fmt.Sprint(test.id); got != test.plain {
|
|
|
|
t.Errorf("got %s expected %s", got, test.plain)
|
|
|
|
}
|
|
|
|
if got := fmt.Sprintf("%q", test.id); got != test.quoted {
|
|
|
|
t.Errorf("got %s want %s", got, test.quoted)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIDEncode(t *testing.T) {
|
|
|
|
for _, test := range wireIDTestData {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2020-04-21 09:47:21 -06:00
|
|
|
data, err := json.Marshal(&test.id)
|
2020-04-07 13:58:08 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
checkJSON(t, data, test.encoded)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIDDecode(t *testing.T) {
|
|
|
|
for _, test := range wireIDTestData {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
var got *jsonrpc2.ID
|
|
|
|
if err := json.Unmarshal(test.encoded, &got); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if got == nil {
|
2020-04-21 09:47:21 -06:00
|
|
|
t.Errorf("got nil want %s", test.id)
|
|
|
|
} else if *got != test.id {
|
2020-04-07 13:58:08 -06:00
|
|
|
t.Errorf("got %s want %s", got, test.id)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkJSON(t *testing.T, got, want []byte) {
|
|
|
|
// compare the compact form, to allow for formatting differences
|
|
|
|
g := &bytes.Buffer{}
|
|
|
|
if err := json.Compact(g, []byte(got)); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
w := &bytes.Buffer{}
|
|
|
|
if err := json.Compact(w, []byte(want)); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if g.String() != w.String() {
|
|
|
|
t.Fatalf("Got:\n%s\nWant:\n%s", g, w)
|
|
|
|
}
|
|
|
|
}
|