-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotojson_test.go
55 lines (46 loc) · 1.11 KB
/
protojson_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
package zfmt
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/zillow/zfmt/testdata/example"
)
func TestProtoJSONFormatter_MarshallUnmarshall(t *testing.T) {
ein := example.ExampleDef{
Allowed: "happy",
Disallowed: 2,
Example: &example.ExampleDef_Foo{
Foo: &example.Foo{
Name: "sad",
},
},
}
fmtr := ProtoJSONFormatter{}
b, err := fmtr.Marshall(&ein)
if err != nil {
t.Fatal(err)
}
eout := &example.ExampleDef{}
err = fmtr.Unmarshal(b, eout)
if err != nil {
t.Fatal(err)
}
if ein.Allowed != eout.Allowed {
t.Error("Not allowed")
}
if e, ok := eout.Example.(*example.ExampleDef_Foo); ok {
if e.Foo.Name != "sad" {
t.Error("foo sad")
}
} else {
t.Error("example not foo")
}
}
func TestProtoJSONFormatter_UnmarshallWithUnknown(t *testing.T) {
data := "{\n \"allowed\": \"happy\",\n \"disallowed\": 2,\n \"MyName\": \"Stewart\"\n}"
fmtr := ProtoJSONFormatter{}
eout := &example.ExampleDef{}
err := fmtr.Unmarshal([]byte(data), eout)
require.NoError(t, err)
require.Equal(t, "happy", eout.Allowed)
require.Equal(t, int32(2), eout.Disallowed)
}