forked from spiral-modules/php-grpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodec_test.go
78 lines (56 loc) · 1.36 KB
/
codec_test.go
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
package grpc
import (
"encoding/json"
"github.com/stretchr/testify/assert"
"testing"
)
type jsonCodec struct{}
func (jsonCodec) Marshal(v interface{}) ([]byte, error) {
return json.Marshal(v)
}
func (jsonCodec) Unmarshal(data []byte, v interface{}) error {
return json.Unmarshal(data, v)
}
func (jsonCodec) Name() string {
return "json"
}
func TestCodec_String(t *testing.T) {
c := codec{jsonCodec{}}
assert.Equal(t, "raw:json", c.String())
r := rawMessage{}
r.Reset()
r.ProtoMessage()
assert.Equal(t, "rawMessage", r.String())
}
func TestCodec_Unmarshal_ByPass(t *testing.T) {
c := codec{jsonCodec{}}
s := struct {
Name string
}{}
assert.NoError(t, c.Unmarshal([]byte(`{"name":"name"}`), &s))
assert.Equal(t, "name", s.Name)
}
func TestCodec_Marshal_ByPass(t *testing.T) {
c := codec{jsonCodec{}}
s := struct {
Name string
}{
Name: "name",
}
d, err := c.Marshal(s)
assert.NoError(t, err)
assert.Equal(t, `{"Name":"name"}`, string(d))
}
func TestCodec_Unmarshal_Raw(t *testing.T) {
c := codec{jsonCodec{}}
s := rawMessage{}
assert.NoError(t, c.Unmarshal([]byte(`{"name":"name"}`), &s))
assert.Equal(t, `{"name":"name"}`, string(s))
}
func TestCodec_Marshal_Raw(t *testing.T) {
c := codec{jsonCodec{}}
s := rawMessage(`{"Name":"name"}`)
d, err := c.Marshal(s)
assert.NoError(t, err)
assert.Equal(t, `{"Name":"name"}`, string(d))
}