Skip to content

Commit e71a727

Browse files
committed
all: unify test files
Signed-off-by: Koichi Shiraishi <[email protected]>
1 parent 74bf4c5 commit e71a727

35 files changed

+1965
-2796
lines changed

base.go

+75
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33

44
package protocol
55

6+
import (
7+
"fmt"
8+
9+
"github.com/segmentio/encoding/json"
10+
)
11+
612
// CancelParams params of cancelRequest.
713
type CancelParams struct {
814
// ID is the request id to cancel.
@@ -19,3 +25,72 @@ type ProgressParams struct {
1925
// Value is the progress data.
2026
Value interface{} `json:"value"`
2127
}
28+
29+
// ProgressToken is the progress token provided by the client or server.
30+
//
31+
// @since 3.15.0.
32+
type ProgressToken struct {
33+
name string
34+
number int32
35+
}
36+
37+
// compile time check whether the ProgressToken implements a fmt.Formatter, fmt.Stringer, json.Marshaler and json.Unmarshaler interfaces.
38+
var (
39+
_ fmt.Formatter = (*ProgressToken)(nil)
40+
_ fmt.Stringer = (*ProgressToken)(nil)
41+
_ json.Marshaler = (*ProgressToken)(nil)
42+
_ json.Unmarshaler = (*ProgressToken)(nil)
43+
)
44+
45+
// NewProgressToken returns a new ProgressToken.
46+
func NewProgressToken(s string) *ProgressToken {
47+
return &ProgressToken{name: s}
48+
}
49+
50+
// NewNumberProgressToken returns a new number ProgressToken.
51+
func NewNumberProgressToken(n int32) *ProgressToken {
52+
return &ProgressToken{number: n}
53+
}
54+
55+
// Format writes the ProgressToken to the formatter.
56+
//
57+
// If the rune is q the representation is non ambiguous,
58+
// string forms are quoted.
59+
func (v ProgressToken) Format(f fmt.State, r rune) {
60+
const numF = `%d`
61+
strF := `%s`
62+
if r == 'q' {
63+
strF = `%q`
64+
}
65+
66+
switch {
67+
case v.name != "":
68+
fmt.Fprintf(f, strF, v.name)
69+
default:
70+
fmt.Fprintf(f, numF, v.number)
71+
}
72+
}
73+
74+
// String returns a string representation of the ProgressToken.
75+
func (v ProgressToken) String() string {
76+
return fmt.Sprint(v)
77+
}
78+
79+
// MarshalJSON implements json.Marshaler.
80+
func (v *ProgressToken) MarshalJSON() ([]byte, error) {
81+
if v.name != "" {
82+
return json.Marshal(v.name)
83+
}
84+
85+
return json.Marshal(v.number)
86+
}
87+
88+
// UnmarshalJSON implements json.Unmarshaler.
89+
func (v *ProgressToken) UnmarshalJSON(data []byte) error {
90+
*v = ProgressToken{}
91+
if err := json.Unmarshal(data, &v.number); err == nil {
92+
return nil
93+
}
94+
95+
return json.Unmarshal(data, &v.name)
96+
}

base_json.go

-82
This file was deleted.

base_json_test.go

-25
This file was deleted.

base_test.go

+11-6
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ import (
1010

1111
"github.com/google/go-cmp/cmp"
1212
"github.com/google/go-cmp/cmp/cmpopts"
13+
"github.com/segmentio/encoding/json"
1314
)
1415

15-
func testCancelParams(t *testing.T, marshal marshalFunc, unmarshal unmarshalFunc) {
16+
func TestCancelParams(t *testing.T) {
17+
t.Parallel()
18+
1619
const want = `{"id":"testID"}`
1720
wantType := CancelParams{
1821
ID: "testID",
@@ -42,7 +45,7 @@ func testCancelParams(t *testing.T, marshal marshalFunc, unmarshal unmarshalFunc
4245
t.Run(tt.name, func(t *testing.T) {
4346
t.Parallel()
4447

45-
got, err := marshal(&tt.field)
48+
got, err := json.Marshal(&tt.field)
4649
if (err != nil) != tt.wantMarshalErr {
4750
t.Fatal(err)
4851
}
@@ -79,7 +82,7 @@ func testCancelParams(t *testing.T, marshal marshalFunc, unmarshal unmarshalFunc
7982
t.Parallel()
8083

8184
var got CancelParams
82-
if err := unmarshal([]byte(tt.field), &got); (err != nil) != tt.wantUnmarshalErr {
85+
if err := json.Unmarshal([]byte(tt.field), &got); (err != nil) != tt.wantUnmarshalErr {
8386
t.Fatal(err)
8487
}
8588

@@ -91,7 +94,9 @@ func testCancelParams(t *testing.T, marshal marshalFunc, unmarshal unmarshalFunc
9194
})
9295
}
9396

94-
func testProgressParams(t *testing.T, marshal marshalFunc, unmarshal unmarshalFunc) {
97+
func TestProgressParams(t *testing.T) {
98+
t.Parallel()
99+
95100
const wantWorkDoneToken = "156edea9-9d8d-422f-b7ee-81a84594afbb"
96101
const want = `{"token":"` + wantWorkDoneToken + `","value":"testValue"}`
97102

@@ -125,7 +130,7 @@ func testProgressParams(t *testing.T, marshal marshalFunc, unmarshal unmarshalFu
125130
t.Run(tt.name, func(t *testing.T) {
126131
t.Parallel()
127132

128-
got, err := marshal(&tt.field)
133+
got, err := json.Marshal(&tt.field)
129134
if (err != nil) != tt.wantMarshalErr {
130135
t.Fatal(err)
131136
}
@@ -162,7 +167,7 @@ func testProgressParams(t *testing.T, marshal marshalFunc, unmarshal unmarshalFu
162167
t.Parallel()
163168

164169
var got ProgressParams
165-
if err := unmarshal([]byte(tt.field), &got); (err != nil) != tt.wantUnmarshalErr {
170+
if err := json.Unmarshal([]byte(tt.field), &got); (err != nil) != tt.wantUnmarshalErr {
166171
t.Fatal(err)
167172
}
168173

0 commit comments

Comments
 (0)