1*1c12ee1eSDan Willemsen// Copyright 2020 The Go Authors. All rights reserved. 2*1c12ee1eSDan Willemsen// Use of this source code is governed by a BSD-style 3*1c12ee1eSDan Willemsen// license that can be found in the LICENSE file. 4*1c12ee1eSDan Willemsen 5*1c12ee1eSDan Willemsenpackage protocmp 6*1c12ee1eSDan Willemsen 7*1c12ee1eSDan Willemsenimport ( 8*1c12ee1eSDan Willemsen "testing" 9*1c12ee1eSDan Willemsen 10*1c12ee1eSDan Willemsen "github.com/google/go-cmp/cmp" 11*1c12ee1eSDan Willemsen 12*1c12ee1eSDan Willemsen "google.golang.org/protobuf/proto" 13*1c12ee1eSDan Willemsen 14*1c12ee1eSDan Willemsen testpb "google.golang.org/protobuf/internal/testprotos/test" 15*1c12ee1eSDan Willemsen textpb "google.golang.org/protobuf/internal/testprotos/textpb2" 16*1c12ee1eSDan Willemsen anypb "google.golang.org/protobuf/types/known/anypb" 17*1c12ee1eSDan Willemsen wrapperspb "google.golang.org/protobuf/types/known/wrapperspb" 18*1c12ee1eSDan Willemsen) 19*1c12ee1eSDan Willemsen 20*1c12ee1eSDan Willemsenfunc TestReflect(t *testing.T) { 21*1c12ee1eSDan Willemsen optMsg := &testpb.TestAllTypes{ 22*1c12ee1eSDan Willemsen OptionalInt32: proto.Int32(-32), 23*1c12ee1eSDan Willemsen OptionalInt64: proto.Int64(-64), 24*1c12ee1eSDan Willemsen OptionalUint32: proto.Uint32(32), 25*1c12ee1eSDan Willemsen OptionalUint64: proto.Uint64(64), 26*1c12ee1eSDan Willemsen OptionalFloat: proto.Float32(32.32), 27*1c12ee1eSDan Willemsen OptionalDouble: proto.Float64(64.64), 28*1c12ee1eSDan Willemsen OptionalBool: proto.Bool(true), 29*1c12ee1eSDan Willemsen OptionalString: proto.String("string"), 30*1c12ee1eSDan Willemsen OptionalBytes: []byte("bytes"), 31*1c12ee1eSDan Willemsen OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{A: proto.Int32(-32)}, 32*1c12ee1eSDan Willemsen OptionalNestedEnum: testpb.TestAllTypes_NEG.Enum(), 33*1c12ee1eSDan Willemsen } 34*1c12ee1eSDan Willemsen repMsg := &testpb.TestAllTypes{ 35*1c12ee1eSDan Willemsen RepeatedInt32: []int32{-32, +32}, 36*1c12ee1eSDan Willemsen RepeatedInt64: []int64{-64, +64}, 37*1c12ee1eSDan Willemsen RepeatedUint32: []uint32{0, 32}, 38*1c12ee1eSDan Willemsen RepeatedUint64: []uint64{0, 64}, 39*1c12ee1eSDan Willemsen RepeatedFloat: []float32{-32.32, +32.32}, 40*1c12ee1eSDan Willemsen RepeatedDouble: []float64{-64.64, +64.64}, 41*1c12ee1eSDan Willemsen RepeatedBool: []bool{false, true}, 42*1c12ee1eSDan Willemsen RepeatedString: []string{"hello", "goodbye"}, 43*1c12ee1eSDan Willemsen RepeatedBytes: [][]byte{[]byte("hello"), []byte("goodbye")}, 44*1c12ee1eSDan Willemsen RepeatedNestedMessage: []*testpb.TestAllTypes_NestedMessage{{A: proto.Int32(-32)}, {A: proto.Int32(+32)}}, 45*1c12ee1eSDan Willemsen RepeatedNestedEnum: []testpb.TestAllTypes_NestedEnum{testpb.TestAllTypes_FOO, testpb.TestAllTypes_NEG}, 46*1c12ee1eSDan Willemsen } 47*1c12ee1eSDan Willemsen mapMsg := &testpb.TestAllTypes{ 48*1c12ee1eSDan Willemsen MapInt32Int32: map[int32]int32{-1: -32, +1: +32}, 49*1c12ee1eSDan Willemsen MapInt64Int64: map[int64]int64{-1: -32, +1: +64}, 50*1c12ee1eSDan Willemsen MapUint32Uint32: map[uint32]uint32{0: 0, 1: 32}, 51*1c12ee1eSDan Willemsen MapUint64Uint64: map[uint64]uint64{0: 0, 1: 64}, 52*1c12ee1eSDan Willemsen MapInt32Float: map[int32]float32{-1: -32.32, +1: +32.32}, 53*1c12ee1eSDan Willemsen MapInt32Double: map[int32]float64{-1: -64.64, +1: +64.64}, 54*1c12ee1eSDan Willemsen MapBoolBool: map[bool]bool{false: true, true: false}, 55*1c12ee1eSDan Willemsen MapStringString: map[string]string{"k1": "v1", "k2": "v2"}, 56*1c12ee1eSDan Willemsen MapStringBytes: map[string][]byte{"k1": []byte("v1"), "k2": []byte("v2")}, 57*1c12ee1eSDan Willemsen MapStringNestedMessage: map[string]*testpb.TestAllTypes_NestedMessage{"k1": {A: proto.Int32(-32)}, "k2": {A: proto.Int32(+32)}}, 58*1c12ee1eSDan Willemsen MapStringNestedEnum: map[string]testpb.TestAllTypes_NestedEnum{"k1": testpb.TestAllTypes_FOO, "k2": testpb.TestAllTypes_NEG}, 59*1c12ee1eSDan Willemsen } 60*1c12ee1eSDan Willemsen 61*1c12ee1eSDan Willemsen tests := []proto.Message{ 62*1c12ee1eSDan Willemsen optMsg, 63*1c12ee1eSDan Willemsen repMsg, 64*1c12ee1eSDan Willemsen mapMsg, 65*1c12ee1eSDan Willemsen &testpb.TestAllTypes{ 66*1c12ee1eSDan Willemsen OneofField: &testpb.TestAllTypes_OneofUint32{32}, 67*1c12ee1eSDan Willemsen }, 68*1c12ee1eSDan Willemsen &testpb.TestAllTypes{ 69*1c12ee1eSDan Willemsen OneofField: &testpb.TestAllTypes_OneofUint64{64}, 70*1c12ee1eSDan Willemsen }, 71*1c12ee1eSDan Willemsen &testpb.TestAllTypes{ 72*1c12ee1eSDan Willemsen OneofField: &testpb.TestAllTypes_OneofFloat{32.32}, 73*1c12ee1eSDan Willemsen }, 74*1c12ee1eSDan Willemsen &testpb.TestAllTypes{ 75*1c12ee1eSDan Willemsen OneofField: &testpb.TestAllTypes_OneofDouble{64.64}, 76*1c12ee1eSDan Willemsen }, 77*1c12ee1eSDan Willemsen &testpb.TestAllTypes{ 78*1c12ee1eSDan Willemsen OneofField: &testpb.TestAllTypes_OneofBool{true}, 79*1c12ee1eSDan Willemsen }, 80*1c12ee1eSDan Willemsen &testpb.TestAllTypes{ 81*1c12ee1eSDan Willemsen OneofField: &testpb.TestAllTypes_OneofString{"string"}, 82*1c12ee1eSDan Willemsen }, 83*1c12ee1eSDan Willemsen &testpb.TestAllTypes{ 84*1c12ee1eSDan Willemsen OneofField: &testpb.TestAllTypes_OneofBytes{[]byte("bytes")}, 85*1c12ee1eSDan Willemsen }, 86*1c12ee1eSDan Willemsen &testpb.TestAllTypes{ 87*1c12ee1eSDan Willemsen OneofField: &testpb.TestAllTypes_OneofNestedMessage{&testpb.TestAllTypes_NestedMessage{A: proto.Int32(-32)}}, 88*1c12ee1eSDan Willemsen }, 89*1c12ee1eSDan Willemsen &testpb.TestAllTypes{ 90*1c12ee1eSDan Willemsen OneofField: &testpb.TestAllTypes_OneofEnum{testpb.TestAllTypes_NEG}, 91*1c12ee1eSDan Willemsen }, 92*1c12ee1eSDan Willemsen func() proto.Message { 93*1c12ee1eSDan Willemsen m := new(testpb.TestAllExtensions) 94*1c12ee1eSDan Willemsen proto.SetExtension(m, testpb.E_OptionalInt32, int32(-32)) 95*1c12ee1eSDan Willemsen proto.SetExtension(m, testpb.E_OptionalInt64, int64(-64)) 96*1c12ee1eSDan Willemsen proto.SetExtension(m, testpb.E_OptionalUint32, uint32(32)) 97*1c12ee1eSDan Willemsen proto.SetExtension(m, testpb.E_OptionalUint64, uint64(64)) 98*1c12ee1eSDan Willemsen proto.SetExtension(m, testpb.E_OptionalFloat, float32(32.32)) 99*1c12ee1eSDan Willemsen proto.SetExtension(m, testpb.E_OptionalDouble, float64(64.64)) 100*1c12ee1eSDan Willemsen proto.SetExtension(m, testpb.E_OptionalBool, bool(true)) 101*1c12ee1eSDan Willemsen proto.SetExtension(m, testpb.E_OptionalString, string("string")) 102*1c12ee1eSDan Willemsen proto.SetExtension(m, testpb.E_OptionalBytes, []byte("bytes")) 103*1c12ee1eSDan Willemsen proto.SetExtension(m, testpb.E_OptionalNestedMessage, &testpb.TestAllExtensions_NestedMessage{A: proto.Int32(-32)}) 104*1c12ee1eSDan Willemsen proto.SetExtension(m, testpb.E_OptionalNestedEnum, testpb.TestAllTypes_NEG) 105*1c12ee1eSDan Willemsen return m 106*1c12ee1eSDan Willemsen }(), 107*1c12ee1eSDan Willemsen func() proto.Message { 108*1c12ee1eSDan Willemsen m := new(testpb.TestAllExtensions) 109*1c12ee1eSDan Willemsen proto.SetExtension(m, testpb.E_RepeatedInt32, []int32{-32, +32}) 110*1c12ee1eSDan Willemsen proto.SetExtension(m, testpb.E_RepeatedInt64, []int64{-64, +64}) 111*1c12ee1eSDan Willemsen proto.SetExtension(m, testpb.E_RepeatedUint32, []uint32{0, 32}) 112*1c12ee1eSDan Willemsen proto.SetExtension(m, testpb.E_RepeatedUint64, []uint64{0, 64}) 113*1c12ee1eSDan Willemsen proto.SetExtension(m, testpb.E_RepeatedFloat, []float32{-32.32, +32.32}) 114*1c12ee1eSDan Willemsen proto.SetExtension(m, testpb.E_RepeatedDouble, []float64{-64.64, +64.64}) 115*1c12ee1eSDan Willemsen proto.SetExtension(m, testpb.E_RepeatedBool, []bool{false, true}) 116*1c12ee1eSDan Willemsen proto.SetExtension(m, testpb.E_RepeatedString, []string{"hello", "goodbye"}) 117*1c12ee1eSDan Willemsen proto.SetExtension(m, testpb.E_RepeatedBytes, [][]byte{[]byte("hello"), []byte("goodbye")}) 118*1c12ee1eSDan Willemsen proto.SetExtension(m, testpb.E_RepeatedNestedMessage, []*testpb.TestAllExtensions_NestedMessage{{A: proto.Int32(-32)}, {A: proto.Int32(+32)}}) 119*1c12ee1eSDan Willemsen proto.SetExtension(m, testpb.E_RepeatedNestedEnum, []testpb.TestAllTypes_NestedEnum{testpb.TestAllTypes_FOO, testpb.TestAllTypes_NEG}) 120*1c12ee1eSDan Willemsen return m 121*1c12ee1eSDan Willemsen }(), 122*1c12ee1eSDan Willemsen &textpb.KnownTypes{ 123*1c12ee1eSDan Willemsen OptBool: &wrapperspb.BoolValue{Value: true}, 124*1c12ee1eSDan Willemsen OptInt32: &wrapperspb.Int32Value{Value: -32}, 125*1c12ee1eSDan Willemsen OptInt64: &wrapperspb.Int64Value{Value: -64}, 126*1c12ee1eSDan Willemsen OptUint32: &wrapperspb.UInt32Value{Value: +32}, 127*1c12ee1eSDan Willemsen OptUint64: &wrapperspb.UInt64Value{Value: +64}, 128*1c12ee1eSDan Willemsen OptFloat: &wrapperspb.FloatValue{Value: 32.32}, 129*1c12ee1eSDan Willemsen OptDouble: &wrapperspb.DoubleValue{Value: 64.64}, 130*1c12ee1eSDan Willemsen OptString: &wrapperspb.StringValue{Value: "string"}, 131*1c12ee1eSDan Willemsen OptBytes: &wrapperspb.BytesValue{Value: []byte("bytes")}, 132*1c12ee1eSDan Willemsen }, 133*1c12ee1eSDan Willemsen &textpb.KnownTypes{ 134*1c12ee1eSDan Willemsen OptAny: &anypb.Any{ 135*1c12ee1eSDan Willemsen TypeUrl: "google.golang.org/goproto.proto.test.TestAllTypes", 136*1c12ee1eSDan Willemsen Value: func() []byte { 137*1c12ee1eSDan Willemsen b1, _ := proto.MarshalOptions{Deterministic: true}.Marshal(optMsg) 138*1c12ee1eSDan Willemsen b2, _ := proto.MarshalOptions{Deterministic: true}.Marshal(repMsg) 139*1c12ee1eSDan Willemsen b3, _ := proto.MarshalOptions{Deterministic: true}.Marshal(mapMsg) 140*1c12ee1eSDan Willemsen return append(append(append([]byte(nil), b1...), b2...), b3...) 141*1c12ee1eSDan Willemsen }(), 142*1c12ee1eSDan Willemsen }, 143*1c12ee1eSDan Willemsen }, 144*1c12ee1eSDan Willemsen &textpb.KnownTypes{ 145*1c12ee1eSDan Willemsen OptAny: &anypb.Any{ 146*1c12ee1eSDan Willemsen TypeUrl: "unknown_type", 147*1c12ee1eSDan Willemsen Value: []byte("invalid_value"), 148*1c12ee1eSDan Willemsen }, 149*1c12ee1eSDan Willemsen }, 150*1c12ee1eSDan Willemsen } 151*1c12ee1eSDan Willemsen 152*1c12ee1eSDan Willemsen for _, src := range tests { 153*1c12ee1eSDan Willemsen dst := src.ProtoReflect().Type().New().Interface() 154*1c12ee1eSDan Willemsen proto.Merge(dst, transformMessage(src.ProtoReflect())) 155*1c12ee1eSDan Willemsen if diff := cmp.Diff(src, dst, Transform()); diff != "" { 156*1c12ee1eSDan Willemsen t.Errorf("Merge mismatch (-want +got):\n%s", diff) 157*1c12ee1eSDan Willemsen } 158*1c12ee1eSDan Willemsen } 159*1c12ee1eSDan Willemsen} 160