GoPLS Viewer

Home|gopls/internal/jsonrpc2/wire_test.go
1// Copyright 2020 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package jsonrpc2_test
6
7import (
8    "bytes"
9    "encoding/json"
10    "fmt"
11    "testing"
12
13    "golang.org/x/tools/internal/jsonrpc2"
14)
15
16var wireIDTestData = []struct {
17    name    string
18    id      jsonrpc2.ID
19    encoded []byte
20    plain   string
21    quoted  string
22}{
23    {
24        name:    `empty`,
25        encoded: []byte(`0`),
26        plain:   `0`,
27        quoted:  `#0`,
28    }, {
29        name:    `number`,
30        id:      jsonrpc2.NewIntID(43),
31        encoded: []byte(`43`),
32        plain:   `43`,
33        quoted:  `#43`,
34    }, {
35        name:    `string`,
36        id:      jsonrpc2.NewStringID("life"),
37        encoded: []byte(`"life"`),
38        plain:   `life`,
39        quoted:  `"life"`,
40    },
41}
42
43func TestIDFormat(t *testing.T) {
44    for _test := range wireIDTestData {
45        t.Run(test.name, func(t *testing.T) {
46            if got := fmt.Sprint(test.id); got != test.plain {
47                t.Errorf("got %s expected %s"gottest.plain)
48            }
49            if got := fmt.Sprintf("%q"test.id); got != test.quoted {
50                t.Errorf("got %s want %s"gottest.quoted)
51            }
52        })
53    }
54}
55
56func TestIDEncode(t *testing.T) {
57    for _test := range wireIDTestData {
58        t.Run(test.name, func(t *testing.T) {
59            dataerr := json.Marshal(&test.id)
60            if err != nil {
61                t.Fatal(err)
62            }
63            checkJSON(tdatatest.encoded)
64        })
65    }
66}
67
68func TestIDDecode(t *testing.T) {
69    for _test := range wireIDTestData {
70        t.Run(test.name, func(t *testing.T) {
71            var got *jsonrpc2.ID
72            if err := json.Unmarshal(test.encoded, &got); err != nil {
73                t.Fatal(err)
74            }
75            if got == nil {
76                t.Errorf("got nil want %s"test.id)
77            } else if *got != test.id {
78                t.Errorf("got %s want %s"gottest.id)
79            }
80        })
81    }
82}
83
84func TestErrorEncode(t *testing.T) {
85    berr := json.Marshal(jsonrpc2.NewError(0""))
86    if err != nil {
87        t.Fatal(err)
88    }
89    checkJSON(tb, []byte(`{
90        "code": 0,
91        "message": ""
92    }`))
93}
94
95func TestErrorResponse(t *testing.T) {
96    // originally reported in #39719, this checks that result is not present if
97    // it is an error response
98    r_ := jsonrpc2.NewResponse(jsonrpc2.NewIntID(3), nilfmt.Errorf("computing fix edits"))
99    dataerr := json.Marshal(r)
100    if err != nil {
101        t.Fatal(err)
102    }
103    checkJSON(tdata, []byte(`{
104        "jsonrpc":"2.0",
105        "error":{
106            "code":0,
107            "message":"computing fix edits"
108        },
109        "id":3
110    }`))
111}
112
113func checkJSON(t *testing.Tgotwant []byte) {
114    // compare the compact form, to allow for formatting differences
115    g := &bytes.Buffer{}
116    if err := json.Compact(g, []byte(got)); err != nil {
117        t.Fatal(err)
118    }
119    w := &bytes.Buffer{}
120    if err := json.Compact(w, []byte(want)); err != nil {
121        t.Fatal(err)
122    }
123    if g.String() != w.String() {
124        t.Fatalf("Got:\n%s\nWant:\n%s"gw)
125    }
126}
127
MembersX
TestErrorResponse.r
checkJSON.g
TestIDEncode.RangeStmt_1132.BlockStmt.BlockStmt.data
TestIDDecode.t
TestErrorEncode
TestErrorResponse
TestErrorResponse._
checkJSON.err
TestIDEncode.RangeStmt_1132.test
TestErrorEncode.b
TestErrorResponse.t
TestErrorResponse.err
checkJSON
checkJSON.t
bytes
TestIDFormat.RangeStmt_781.BlockStmt.BlockStmt.got
TestIDEncode
TestIDDecode
TestErrorEncode.err
checkJSON.w
TestIDFormat.t
TestIDEncode.t
TestIDDecode.RangeStmt_1372.test
checkJSON.got
checkJSON.want
TestErrorResponse.data
TestIDFormat
TestIDFormat.RangeStmt_781.test
TestIDEncode.RangeStmt_1132.BlockStmt.BlockStmt.err
TestIDDecode.RangeStmt_1372.BlockStmt.BlockStmt.got
TestIDDecode.RangeStmt_1372.BlockStmt.BlockStmt.err
TestErrorEncode.t
Members
X