Skip to content

Commit 3a3cefd

Browse files
authored
all: use google.golang.org/protobuf/testing/protopack for tests (#1063)
Use protopack for tests instead of hard-coded binary data.
1 parent c8ad453 commit 3a3cefd

File tree

6 files changed

+736
-1129
lines changed

6 files changed

+736
-1129
lines changed

jsonpb/json_test.go

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -550,28 +550,28 @@ func TestMarshalJSONPBMarshaler(t *testing.T) {
550550
msg := dynamicMessage{RawJson: rawJson}
551551
str, err := new(Marshaler).MarshalToString(&msg)
552552
if err != nil {
553-
t.Errorf("an unexpected error occurred when marshalling JSONPBMarshaler: %v", err)
553+
t.Errorf("an unexpected error while marshaling JSONPBMarshaler: %v", err)
554554
}
555555
if str != rawJson {
556-
t.Errorf("marshalling JSON produced incorrect output: got %s, wanted %s", str, rawJson)
556+
t.Errorf("marshaling JSON produced incorrect output: got %s, wanted %s", str, rawJson)
557557
}
558558
}
559559

560560
func TestMarshalAnyJSONPBMarshaler(t *testing.T) {
561561
msg := dynamicMessage{RawJson: `{ "foo": "bar", "baz": [0, 1, 2, 3] }`}
562562
a, err := ptypes.MarshalAny(&msg)
563563
if err != nil {
564-
t.Errorf("an unexpected error occurred when marshalling to Any: %v", err)
564+
t.Errorf("an unexpected error while marshaling to Any: %v", err)
565565
}
566566
str, err := new(Marshaler).MarshalToString(a)
567567
if err != nil {
568-
t.Errorf("an unexpected error occurred when marshalling Any to JSON: %v", err)
568+
t.Errorf("an unexpected error while marshaling Any to JSON: %v", err)
569569
}
570570
// after custom marshaling, it's round-tripped through JSON decoding/encoding already,
571571
// so the keys are sorted, whitespace is compacted, and "@type" key has been added
572-
expected := `{"@type":"type.googleapis.com/` + dynamicMessageName + `","baz":[0,1,2,3],"foo":"bar"}`
573-
if str != expected {
574-
t.Errorf("marshalling JSON produced incorrect output: got %s, wanted %s", str, expected)
572+
want := `{"@type":"type.googleapis.com/` + dynamicMessageName + `","baz":[0,1,2,3],"foo":"bar"}`
573+
if str != want {
574+
t.Errorf("marshaling JSON produced incorrect output: got %s, wanted %s", str, want)
575575
}
576576
}
577577

@@ -580,11 +580,11 @@ func TestMarshalWithCustomValidation(t *testing.T) {
580580

581581
js, err := new(Marshaler).MarshalToString(&msg)
582582
if err != nil {
583-
t.Errorf("an unexpected error occurred when marshalling to json: %v", err)
583+
t.Errorf("an unexpected error while marshaling to json: %v", err)
584584
}
585585
err = Unmarshal(strings.NewReader(js), &msg)
586586
if err != nil {
587-
t.Errorf("an unexpected error occurred when unmarshalling from json: %v", err)
587+
t.Errorf("an unexpected error while unmarshaling from json: %v", err)
588588
}
589589
}
590590

@@ -668,7 +668,7 @@ func TestMarshalUnsetRequiredFields(t *testing.T) {
668668

669669
for _, tc := range tests {
670670
if _, err := tc.marshaler.MarshalToString(tc.pb); err == nil {
671-
t.Errorf("%s: expecting error in marshaling with unset required fields %+v", tc.desc, tc.pb)
671+
t.Errorf("%s: expected error while marshaling with unset required fields %+v", tc.desc, tc.pb)
672672
}
673673
}
674674
}
@@ -822,12 +822,12 @@ var unmarshalingTests = []struct {
822822

823823
func TestUnmarshaling(t *testing.T) {
824824
for _, tt := range unmarshalingTests {
825-
// Make a new instance of the type of our expected object.
825+
// Make a new instance of the type of our wanted object.
826826
p := reflect.New(reflect.TypeOf(tt.pb).Elem()).Interface().(proto.Message)
827827

828828
err := tt.unmarshaler.Unmarshal(strings.NewReader(tt.json), p)
829829
if err != nil {
830-
t.Errorf("unmarshalling %s: %v", tt.desc, err)
830+
t.Errorf("unmarshaling %s: %v", tt.desc, err)
831831
continue
832832
}
833833

@@ -872,7 +872,7 @@ func TestUnmarshalNext(t *testing.T) {
872872

873873
dec := json.NewDecoder(&b)
874874
for _, tt := range tests {
875-
// Make a new instance of the type of our expected object.
875+
// Make a new instance of the type of our wanted object.
876876
p := reflect.New(reflect.TypeOf(tt.pb).Elem()).Interface().(proto.Message)
877877

878878
err := tt.unmarshaler.UnmarshalNext(dec, p)
@@ -892,7 +892,7 @@ func TestUnmarshalNext(t *testing.T) {
892892
p := &pb2.Simple{}
893893
err := new(Unmarshaler).UnmarshalNext(dec, p)
894894
if err != io.EOF {
895-
t.Errorf("eof: got %v, expected io.EOF", err)
895+
t.Errorf("eof: got %v, want io.EOF", err)
896896
}
897897
}
898898

@@ -918,7 +918,7 @@ func TestUnmarshalingBadInput(t *testing.T) {
918918
for _, tt := range unmarshalingShouldError {
919919
err := UnmarshalString(tt.in, tt.pb)
920920
if err == nil {
921-
t.Errorf("an error was expected when parsing %q instead of an object", tt.desc)
921+
t.Errorf("expected error while parsing %q", tt.desc)
922922
}
923923
}
924924
}
@@ -943,7 +943,7 @@ func TestAnyWithCustomResolver(t *testing.T) {
943943
}
944944
msgBytes, err := proto.Marshal(msg)
945945
if err != nil {
946-
t.Errorf("an unexpected error occurred when marshaling message: %v", err)
946+
t.Errorf("an unexpected error while marshaling message: %v", err)
947947
}
948948
// make an Any with a type URL that won't resolve w/out custom resolver
949949
any := &anypb.Any{
@@ -954,7 +954,7 @@ func TestAnyWithCustomResolver(t *testing.T) {
954954
m := Marshaler{AnyResolver: resolver}
955955
js, err := m.MarshalToString(any)
956956
if err != nil {
957-
t.Errorf("an unexpected error occurred when marshaling any to JSON: %v", err)
957+
t.Errorf("an unexpected error while marshaling any to JSON: %v", err)
958958
}
959959
if len(resolvedTypeUrls) != 1 {
960960
t.Errorf("custom resolver was not invoked during marshaling")
@@ -963,33 +963,33 @@ func TestAnyWithCustomResolver(t *testing.T) {
963963
}
964964
wanted := `{"@type":"https://foobar.com/some.random.MessageKind","oBool":true,"oInt64":"1020304","oString":"foobar","oBytes":"AQIDBA=="}`
965965
if js != wanted {
966-
t.Errorf("marshalling JSON produced incorrect output: got %s, wanted %s", js, wanted)
966+
t.Errorf("marshaling JSON produced incorrect output: got %s, wanted %s", js, wanted)
967967
}
968968

969969
u := Unmarshaler{AnyResolver: resolver}
970970
roundTrip := &anypb.Any{}
971971
err = u.Unmarshal(bytes.NewReader([]byte(js)), roundTrip)
972972
if err != nil {
973-
t.Errorf("an unexpected error occurred when unmarshaling any from JSON: %v", err)
973+
t.Errorf("an unexpected error while unmarshaling any from JSON: %v", err)
974974
}
975975
if len(resolvedTypeUrls) != 2 {
976976
t.Errorf("custom resolver was not invoked during marshaling")
977977
} else if resolvedTypeUrls[1] != "https://foobar.com/some.random.MessageKind" {
978978
t.Errorf("custom resolver was invoked with wrong URL: got %q, wanted %q", resolvedTypeUrls[1], "https://foobar.com/some.random.MessageKind")
979979
}
980980
if !proto.Equal(any, roundTrip) {
981-
t.Errorf("message contents not set correctly after unmarshalling JSON: got %s, wanted %s", roundTrip, any)
981+
t.Errorf("message contents not set correctly after unmarshaling JSON: got %s, wanted %s", roundTrip, any)
982982
}
983983
}
984984

985985
func TestUnmarshalJSONPBUnmarshaler(t *testing.T) {
986986
rawJson := `{ "foo": "bar", "baz": [0, 1, 2, 3] }`
987987
var msg dynamicMessage
988988
if err := Unmarshal(strings.NewReader(rawJson), &msg); err != nil {
989-
t.Errorf("an unexpected error occurred when parsing into JSONPBUnmarshaler: %v", err)
989+
t.Errorf("an unexpected error while parsing into JSONPBUnmarshaler: %v", err)
990990
}
991991
if msg.RawJson != rawJson {
992-
t.Errorf("message contents not set correctly after unmarshalling JSON: got %s, wanted %s", msg.RawJson, rawJson)
992+
t.Errorf("message contents not set correctly after unmarshaling JSON: got %s, wanted %s", msg.RawJson, rawJson)
993993
}
994994
}
995995

@@ -1010,20 +1010,20 @@ func TestUnmarshalAnyJSONPBUnmarshaler(t *testing.T) {
10101010
rawJson := `{ "@type": "blah.com/` + dynamicMessageName + `", "foo": "bar", "baz": [0, 1, 2, 3] }`
10111011
var got anypb.Any
10121012
if err := Unmarshal(strings.NewReader(rawJson), &got); err != nil {
1013-
t.Errorf("an unexpected error occurred when parsing into JSONPBUnmarshaler: %v", err)
1013+
t.Errorf("an unexpected error while parsing into JSONPBUnmarshaler: %v", err)
10141014
}
10151015

10161016
dm := &dynamicMessage{RawJson: `{"baz":[0,1,2,3],"foo":"bar"}`}
10171017
var want anypb.Any
10181018
if b, err := proto.Marshal(dm); err != nil {
1019-
t.Errorf("an unexpected error occurred when marshaling message: %v", err)
1019+
t.Errorf("an unexpected error while marshaling message: %v", err)
10201020
} else {
10211021
want.TypeUrl = "blah.com/" + dynamicMessageName
10221022
want.Value = b
10231023
}
10241024

10251025
if !proto.Equal(&got, &want) {
1026-
t.Errorf("message contents not set correctly after unmarshalling JSON: got %v, wanted %v", &got, &want)
1026+
t.Errorf("message contents not set correctly after unmarshaling JSON: got %v, wanted %v", &got, &want)
10271027
}
10281028
}
10291029

@@ -1247,7 +1247,7 @@ func TestUnmarshalUnsetRequiredFields(t *testing.T) {
12471247

12481248
for _, tc := range tests {
12491249
if err := UnmarshalString(tc.json, tc.pb); err == nil {
1250-
t.Errorf("%s: expecting error in unmarshaling with unset required fields %s", tc.desc, tc.json)
1250+
t.Errorf("%s: expected error while unmarshaling with unset required fields %s", tc.desc, tc.json)
12511251
}
12521252
}
12531253
}

proto/discard_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@ import (
88
"testing"
99

1010
"github.com/golang/protobuf/proto"
11+
"google.golang.org/protobuf/testing/protopack"
1112

1213
pb2 "github.com/golang/protobuf/internal/testprotos/proto2_proto"
1314
pb3 "github.com/golang/protobuf/internal/testprotos/proto3_proto"
1415
)
1516

16-
const rawFields = "\x2d\xc3\xd2\xe1\xf0"
17+
var rawFields = protopack.Message{
18+
protopack.Tag{5, protopack.Fixed32Type}, protopack.Uint32(4041331395),
19+
}.Marshal()
1720

1821
func TestDiscardUnknown(t *testing.T) {
1922
tests := []struct {

0 commit comments

Comments
 (0)