| 26 | ) |
| 27 | |
| 28 | func TestEncode(t *testing.T) { |
| 29 | for _, test := range testValidMessages { |
| 30 | for _, want := range test.decodeTo { |
| 31 | t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) { |
| 32 | opts := proto.MarshalOptions{ |
| 33 | AllowPartial: test.partial, |
| 34 | } |
| 35 | wire, err := opts.Marshal(want) |
| 36 | if err != nil { |
| 37 | t.Fatalf("Marshal error: %v\nMessage:\n%v", err, prototext.Format(want)) |
| 38 | } |
| 39 | |
| 40 | size := proto.Size(want) |
| 41 | if size != len(wire) { |
| 42 | t.Errorf("Size and marshal disagree: Size(m)=%v; len(Marshal(m))=%v\nMessage:\n%v", size, len(wire), prototext.Format(want)) |
| 43 | } |
| 44 | |
| 45 | got := want.ProtoReflect().New().Interface() |
| 46 | uopts := proto.UnmarshalOptions{ |
| 47 | AllowPartial: test.partial, |
| 48 | } |
| 49 | if err := uopts.Unmarshal(wire, got); err != nil { |
| 50 | t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, prototext.Format(want)) |
| 51 | return |
| 52 | } |
| 53 | if !proto.Equal(got, want) && got.ProtoReflect().IsValid() && want.ProtoReflect().IsValid() { |
| 54 | t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", prototext.Format(got), prototext.Format(want)) |
| 55 | } |
| 56 | }) |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | func TestEncodeDeterministic(t *testing.T) { |
| 62 | for _, test := range testValidMessages { |