proto: add explicit test for a non-nil but empty byte slice I encountered this usage pattern in a production application and noticed that when breaking the size calculation, only the conformance_test fails (indirectly), but there was no explicit test before this commit. Change-Id: I15a125b7cff3364f24fe372e4a3a046bb7552413 Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/710855 Reviewed-by: Chressie Himpel <chressie@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
diff --git a/proto/size_test.go b/proto/size_test.go index d46479e..ed6883a 100644 --- a/proto/size_test.go +++ b/proto/size_test.go
@@ -5,7 +5,12 @@ package proto_test import ( + "testing" + + "google.golang.org/protobuf/encoding/prototext" + "google.golang.org/protobuf/encoding/protowire" "google.golang.org/protobuf/proto" + "google.golang.org/protobuf/types/known/anypb" ) // Checking if [Size] returns 0 is an easy way to recognize empty messages: @@ -16,3 +21,15 @@ // skip processing this message, or return an error, or similar. } } + +func TestSizeAnyNonNilButEmpty(t *testing.T) { + ne := &anypb.Any{ + TypeUrl: "abc", + Value: []byte{}, + } + + want := protowire.SizeBytes(len("abc")) + protowire.SizeTag(1 /* TypeUrl */) + if got := proto.Size(ne); got != want { + t.Errorf("proto.Size(%v) = %v, want %v", prototext.Format(ne), got, want) + } +}