proto: add generic Size

Change-Id: I4ed123f4a9747fb4aba392bc5b9608d294bacc4d
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/169697
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
diff --git a/proto/encode_test.go b/proto/encode_test.go
index 4c5034d..d467b74 100644
--- a/proto/encode_test.go
+++ b/proto/encode_test.go
@@ -7,7 +7,6 @@
 	"testing"
 
 	protoV1 "github.com/golang/protobuf/proto"
-	//_ "github.com/golang/protobuf/v2/internal/legacy"
 	"github.com/golang/protobuf/v2/proto"
 	"github.com/google/go-cmp/cmp"
 )
@@ -18,7 +17,12 @@
 			t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
 				wire, err := proto.Marshal(want)
 				if err != nil {
-					t.Fatalf("Marshal error: %v\nMessage:\n%v", err, protoV1.MarshalTextString(want.(protoV1.Message)))
+					t.Fatalf("Marshal error: %v\nMessage:\n%v", err, marshalText(want))
+				}
+
+				size := proto.Size(want)
+				if size != len(wire) {
+					t.Errorf("Size and marshal disagree: Size(m)=%v; len(Marshal(m))=%v\nMessage:\n%v", size, len(wire), marshalText(want))
 				}
 
 				got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message)
@@ -41,12 +45,12 @@
 			t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
 				wire, err := proto.MarshalOptions{Deterministic: true}.Marshal(want)
 				if err != nil {
-					t.Fatalf("Marshal error: %v\nMessage:\n%v", err, protoV1.MarshalTextString(want.(protoV1.Message)))
+					t.Fatalf("Marshal error: %v\nMessage:\n%v", err, marshalText(want))
 				}
 
 				wire2, err := proto.MarshalOptions{Deterministic: true}.Marshal(want)
 				if err != nil {
-					t.Fatalf("Marshal error: %v\nMessage:\n%v", err, protoV1.MarshalTextString(want.(protoV1.Message)))
+					t.Fatalf("Marshal error: %v\nMessage:\n%v", err, marshalText(want))
 				}
 
 				if !bytes.Equal(wire, wire2) {
@@ -55,12 +59,12 @@
 
 				got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message)
 				if err := proto.Unmarshal(wire, got); err != nil {
-					t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, protoV1.MarshalTextString(want.(protoV1.Message)))
+					t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, marshalText(want))
 					return
 				}
 
 				if !protoV1.Equal(got.(protoV1.Message), want.(protoV1.Message)) {
-					t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", protoV1.MarshalTextString(got.(protoV1.Message)), protoV1.MarshalTextString(want.(protoV1.Message)))
+					t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", marshalText(got), marshalText(want))
 				}
 			})
 		}