encoding/textpb: add tests for nil message in repeated and map
Updates golang/protobuf#798 by adding testcases to show the intention.
Also, slightly move code blocks in encode.go w/o affecting logic to make
it cleaner.
Change-Id: I14575f6e7139a0908483bd318b599339c2daf8ad
Reviewed-on: https://go-review.googlesource.com/c/161717
Reviewed-by: Damien Neil <dneil@google.com>
diff --git a/encoding/textpb/encode_test.go b/encoding/textpb/encode_test.go
index 963fafe..b32d2db 100644
--- a/encoding/textpb/encode_test.go
+++ b/encoding/textpb/encode_test.go
@@ -531,6 +531,14 @@
rpt_nested: {}
`,
}, {
+ desc: "repeated nested messages contains nil value",
+ input: &pb2.Nests{
+ RptNested: []*pb2.Nested{nil, {}},
+ },
+ want: `rpt_nested: {}
+rpt_nested: {}
+`,
+ }, {
desc: "repeated group fields",
input: &pb2.Nests{
Rptgroup: []*pb2.Nests_RptGroup{
@@ -679,6 +687,18 @@
}
`,
}, {
+ desc: "map field nil message value",
+ input: &pb2.Maps{
+ StrToNested: map[string]*pb2.Nested{
+ "nil": nil,
+ },
+ },
+ want: `str_to_nested: {
+ key: "nil"
+ value: {}
+}
+`,
+ }, {
desc: "proto2 required fields not set",
input: &pb2.Requireds{},
want: "\n",