mirror of
https://github.com/golang/go
synced 2024-11-05 16:46:10 -07:00
c92aeb7438
Replace the String method with a Format method so we can use it for extra formats. Add some tests to make sure it is all correct Change-Id: I39f361ffba036fad99c93f8c0944164f7cf199ec Reviewed-on: https://go-review.googlesource.com/c/tools/+/227486 Run-TryBot: Ian Cottrell <iancottrell@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
110 lines
2.3 KiB
Go
110 lines
2.3 KiB
Go
// 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"
|
|
"math"
|
|
"testing"
|
|
|
|
"golang.org/x/tools/internal/jsonrpc2"
|
|
)
|
|
|
|
var wireIDTestData = []struct {
|
|
name string
|
|
id *jsonrpc2.ID
|
|
encoded []byte
|
|
plain string
|
|
quoted string
|
|
}{
|
|
{
|
|
name: `nil`,
|
|
id: nil,
|
|
encoded: []byte(`null`),
|
|
plain: fmt.Sprintf(`%d`, int64(math.MaxInt64)),
|
|
quoted: fmt.Sprintf(`#%d`, int64(math.MaxInt64)),
|
|
}, {
|
|
name: `empty`,
|
|
id: &jsonrpc2.ID{},
|
|
encoded: []byte(`0`),
|
|
plain: `0`,
|
|
quoted: `#0`,
|
|
}, {
|
|
name: `number`,
|
|
id: &jsonrpc2.ID{Number: 43},
|
|
encoded: []byte(`43`),
|
|
plain: `43`,
|
|
quoted: `#43`,
|
|
}, {
|
|
name: `string`,
|
|
id: &jsonrpc2.ID{Name: "life"},
|
|
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) {
|
|
data, err := json.Marshal(test.id)
|
|
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 {
|
|
if test.id != nil {
|
|
t.Errorf("got nil want %s", test.id)
|
|
}
|
|
} else if test.id == nil {
|
|
t.Errorf("got %s want nil", got)
|
|
} else if *got != *test.id {
|
|
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)
|
|
}
|
|
}
|