...
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 package mapdefaults
30
31 import (
32 "testing"
33
34 "github.com/gogo/protobuf/proto"
35 )
36
37 func TestUnmarshalImplicitDefaultKeyValue1(t *testing.T) {
38 fm := &FakeMap{
39 Entries: []*FakeMapEntry{
40 {
41 Key: "foo",
42 Value: "",
43 },
44 {
45 Key: "",
46 Value: "bar",
47 },
48 {
49 Key: "as",
50 Value: "df",
51 },
52 },
53 }
54
55 serializedMsg, err := proto.Marshal(fm)
56 if err != nil {
57 t.Fatalf("Failed to serialize msg: %s", err)
58 }
59
60 msg := MapTest{}
61 err = proto.Unmarshal(serializedMsg, &msg)
62
63 if err != nil {
64 t.Fatalf("Unexpected error: %s", err)
65 }
66
67 strStr := msg.StrStr
68 if len(strStr) != 3 {
69 t.Fatal("StrStr map should have 3 key/value pairs")
70 }
71
72 val, ok := strStr["foo"]
73 if !ok {
74 t.Fatal("\"foo\" not found in StrStr map.")
75 }
76 if val != "" {
77 t.Fatalf("Unexpected value for \"foo\": %s", val)
78 }
79
80 val, ok = strStr[""]
81 if !ok {
82 t.Fatal("\"\" not found in StrStr map.")
83 }
84 if val != "bar" {
85 t.Fatalf("Unexpected value for \"\": %s", val)
86 }
87
88 val, ok = strStr["as"]
89 if !ok {
90 t.Fatal("\"as\" not found in StrStr map.")
91 }
92 if val != "df" {
93 t.Fatalf("Unexpected value for \"as\": %s", val)
94 }
95 }
96
97 func TestUnmarshalImplicitDefaultKeyValue2(t *testing.T) {
98 fm := &FakeMap{
99 Entries: []*FakeMapEntry{
100 {
101 Key: "",
102 Value: "",
103 },
104 },
105 }
106
107 serializedMsg, err := proto.Marshal(fm)
108 if err != nil {
109 t.Fatalf("Failed to serialize msg: %s", err)
110 }
111
112
113 if string(serializedMsg) != "\n\x00" {
114 t.Fatal("Serialized bytes mismatched")
115 }
116
117 msg := MapTest{}
118 err = proto.Unmarshal(serializedMsg, &msg)
119
120 if err != nil {
121 t.Fatalf("Unexpected error: %s", err)
122 }
123
124 strStr := msg.StrStr
125 if len(strStr) != 1 {
126 t.Fatal("StrStr map should have 1 key/value pairs")
127 }
128
129 val, ok := strStr[""]
130 if !ok {
131 t.Fatal("\"\" not found in StrStr map.")
132 }
133 if val != "" {
134 t.Fatalf("Unexpected value for \"\": %s", val)
135 }
136 }
137
View as plain text