File: wire_test.go

package info (click to toggle)
golang-golang-x-tools 1%3A0.25.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 22,724 kB
  • sloc: javascript: 2,027; asm: 1,645; sh: 166; yacc: 155; makefile: 49; ansic: 8
file content (126 lines) | stat: -rw-r--r-- 2,709 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// 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
	id      jsonrpc2.ID
	encoded []byte
	plain   string
	quoted  string
}{
	{
		name:    `empty`,
		encoded: []byte(`0`),
		plain:   `0`,
		quoted:  `#0`,
	}, {
		name:    `number`,
		id:      jsonrpc2.NewIntID(43),
		encoded: []byte(`43`),
		plain:   `43`,
		quoted:  `#43`,
	}, {
		name:    `string`,
		id:      jsonrpc2.NewStringID("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 {
				t.Errorf("got nil want %s", test.id)
			} else if *got != test.id {
				t.Errorf("got %s want %s", got, test.id)
			}
		})
	}
}

func TestErrorEncode(t *testing.T) {
	b, err := json.Marshal(jsonrpc2.NewError(0, ""))
	if err != nil {
		t.Fatal(err)
	}
	checkJSON(t, b, []byte(`{
		"code": 0,
		"message": ""
	}`))
}

func TestErrorResponse(t *testing.T) {
	// originally reported in #39719, this checks that result is not present if
	// it is an error response
	r, _ := jsonrpc2.NewResponse(jsonrpc2.NewIntID(3), nil, fmt.Errorf("computing fix edits"))
	data, err := json.Marshal(r)
	if err != nil {
		t.Fatal(err)
	}
	checkJSON(t, data, []byte(`{
		"jsonrpc":"2.0",
		"error":{
			"code":0,
			"message":"computing fix edits"
		},
		"id":3
	}`))
}

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