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 msgfmt_test 6*1c12ee1eSDan Willemsen 7*1c12ee1eSDan Willemsenimport ( 8*1c12ee1eSDan Willemsen "math" 9*1c12ee1eSDan Willemsen "sync" 10*1c12ee1eSDan Willemsen "testing" 11*1c12ee1eSDan Willemsen 12*1c12ee1eSDan Willemsen "github.com/google/go-cmp/cmp" 13*1c12ee1eSDan Willemsen 14*1c12ee1eSDan Willemsen "google.golang.org/protobuf/internal/detrand" 15*1c12ee1eSDan Willemsen "google.golang.org/protobuf/internal/msgfmt" 16*1c12ee1eSDan Willemsen "google.golang.org/protobuf/proto" 17*1c12ee1eSDan Willemsen "google.golang.org/protobuf/testing/protocmp" 18*1c12ee1eSDan Willemsen "google.golang.org/protobuf/testing/protopack" 19*1c12ee1eSDan Willemsen 20*1c12ee1eSDan Willemsen testpb "google.golang.org/protobuf/internal/testprotos/test" 21*1c12ee1eSDan Willemsen textpb "google.golang.org/protobuf/internal/testprotos/textpb2" 22*1c12ee1eSDan Willemsen dynpb "google.golang.org/protobuf/types/dynamicpb" 23*1c12ee1eSDan Willemsen anypb "google.golang.org/protobuf/types/known/anypb" 24*1c12ee1eSDan Willemsen durpb "google.golang.org/protobuf/types/known/durationpb" 25*1c12ee1eSDan Willemsen tspb "google.golang.org/protobuf/types/known/timestamppb" 26*1c12ee1eSDan Willemsen wpb "google.golang.org/protobuf/types/known/wrapperspb" 27*1c12ee1eSDan Willemsen) 28*1c12ee1eSDan Willemsen 29*1c12ee1eSDan Willemsenfunc init() { 30*1c12ee1eSDan Willemsen detrand.Disable() 31*1c12ee1eSDan Willemsen} 32*1c12ee1eSDan Willemsen 33*1c12ee1eSDan Willemsenfunc TestFormat(t *testing.T) { 34*1c12ee1eSDan Willemsen optMsg := &testpb.TestAllTypes{ 35*1c12ee1eSDan Willemsen OptionalBool: proto.Bool(false), 36*1c12ee1eSDan Willemsen OptionalInt32: proto.Int32(-32), 37*1c12ee1eSDan Willemsen OptionalInt64: proto.Int64(-64), 38*1c12ee1eSDan Willemsen OptionalUint32: proto.Uint32(32), 39*1c12ee1eSDan Willemsen OptionalUint64: proto.Uint64(64), 40*1c12ee1eSDan Willemsen OptionalFloat: proto.Float32(32.32), 41*1c12ee1eSDan Willemsen OptionalDouble: proto.Float64(64.64), 42*1c12ee1eSDan Willemsen OptionalString: proto.String("string"), 43*1c12ee1eSDan Willemsen OptionalBytes: []byte("bytes"), 44*1c12ee1eSDan Willemsen OptionalNestedEnum: testpb.TestAllTypes_NEG.Enum(), 45*1c12ee1eSDan Willemsen OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{A: proto.Int32(5)}, 46*1c12ee1eSDan Willemsen } 47*1c12ee1eSDan Willemsen repMsg := &testpb.TestAllTypes{ 48*1c12ee1eSDan Willemsen RepeatedBool: []bool{false, true}, 49*1c12ee1eSDan Willemsen RepeatedInt32: []int32{32, -32}, 50*1c12ee1eSDan Willemsen RepeatedInt64: []int64{64, -64}, 51*1c12ee1eSDan Willemsen RepeatedUint32: []uint32{0, 32}, 52*1c12ee1eSDan Willemsen RepeatedUint64: []uint64{0, 64}, 53*1c12ee1eSDan Willemsen RepeatedFloat: []float32{0, 32.32}, 54*1c12ee1eSDan Willemsen RepeatedDouble: []float64{0, 64.64}, 55*1c12ee1eSDan Willemsen RepeatedString: []string{"s1", "s2"}, 56*1c12ee1eSDan Willemsen RepeatedBytes: [][]byte{{1}, {2}}, 57*1c12ee1eSDan Willemsen RepeatedNestedEnum: []testpb.TestAllTypes_NestedEnum{ 58*1c12ee1eSDan Willemsen testpb.TestAllTypes_FOO, 59*1c12ee1eSDan Willemsen testpb.TestAllTypes_BAR, 60*1c12ee1eSDan Willemsen }, 61*1c12ee1eSDan Willemsen RepeatedNestedMessage: []*testpb.TestAllTypes_NestedMessage{ 62*1c12ee1eSDan Willemsen {A: proto.Int32(5)}, 63*1c12ee1eSDan Willemsen {A: proto.Int32(-5)}, 64*1c12ee1eSDan Willemsen }, 65*1c12ee1eSDan Willemsen } 66*1c12ee1eSDan Willemsen mapMsg := &testpb.TestAllTypes{ 67*1c12ee1eSDan Willemsen MapBoolBool: map[bool]bool{true: false}, 68*1c12ee1eSDan Willemsen MapInt32Int32: map[int32]int32{-32: 32}, 69*1c12ee1eSDan Willemsen MapInt64Int64: map[int64]int64{-64: 64}, 70*1c12ee1eSDan Willemsen MapUint32Uint32: map[uint32]uint32{0: 32}, 71*1c12ee1eSDan Willemsen MapUint64Uint64: map[uint64]uint64{0: 64}, 72*1c12ee1eSDan Willemsen MapInt32Float: map[int32]float32{32: 32.32}, 73*1c12ee1eSDan Willemsen MapInt32Double: map[int32]float64{64: 64.64}, 74*1c12ee1eSDan Willemsen MapStringString: map[string]string{"k": "v"}, 75*1c12ee1eSDan Willemsen MapStringBytes: map[string][]byte{"k": []byte("v")}, 76*1c12ee1eSDan Willemsen MapStringNestedEnum: map[string]testpb.TestAllTypes_NestedEnum{ 77*1c12ee1eSDan Willemsen "k": testpb.TestAllTypes_FOO, 78*1c12ee1eSDan Willemsen }, 79*1c12ee1eSDan Willemsen MapStringNestedMessage: map[string]*testpb.TestAllTypes_NestedMessage{ 80*1c12ee1eSDan Willemsen "k": {A: proto.Int32(5)}, 81*1c12ee1eSDan Willemsen }, 82*1c12ee1eSDan Willemsen } 83*1c12ee1eSDan Willemsen 84*1c12ee1eSDan Willemsen tests := []struct { 85*1c12ee1eSDan Willemsen in proto.Message 86*1c12ee1eSDan Willemsen want string 87*1c12ee1eSDan Willemsen }{{ 88*1c12ee1eSDan Willemsen in: optMsg, 89*1c12ee1eSDan Willemsen want: `{optional_int32:-32, optional_int64:-64, optional_uint32:32, optional_uint64:64, optional_float:32.32, optional_double:64.64, optional_bool:false, optional_string:"string", optional_bytes:"bytes", optional_nested_message:{a:5}, optional_nested_enum:NEG}`, 90*1c12ee1eSDan Willemsen }, { 91*1c12ee1eSDan Willemsen in: repMsg, 92*1c12ee1eSDan Willemsen want: `{repeated_int32:[32, -32], repeated_int64:[64, -64], repeated_uint32:[0, 32], repeated_uint64:[0, 64], repeated_float:[0, 32.32], repeated_double:[0, 64.64], repeated_bool:[false, true], repeated_string:["s1", "s2"], repeated_bytes:["\x01", "\x02"], repeated_nested_message:[{a:5}, {a:-5}], repeated_nested_enum:[FOO, BAR]}`, 93*1c12ee1eSDan Willemsen }, { 94*1c12ee1eSDan Willemsen in: mapMsg, 95*1c12ee1eSDan Willemsen want: `{map_int32_int32:{-32:32}, map_int64_int64:{-64:64}, map_uint32_uint32:{0:32}, map_uint64_uint64:{0:64}, map_int32_float:{32:32.32}, map_int32_double:{64:64.64}, map_bool_bool:{true:false}, map_string_string:{"k":"v"}, map_string_bytes:{"k":"v"}, map_string_nested_message:{"k":{a:5}}, map_string_nested_enum:{"k":FOO}}`, 96*1c12ee1eSDan Willemsen }, { 97*1c12ee1eSDan Willemsen in: func() proto.Message { 98*1c12ee1eSDan Willemsen m := &testpb.TestAllExtensions{} 99*1c12ee1eSDan Willemsen proto.SetExtension(m, testpb.E_OptionalBool, bool(false)) 100*1c12ee1eSDan Willemsen proto.SetExtension(m, testpb.E_OptionalInt32, int32(-32)) 101*1c12ee1eSDan Willemsen proto.SetExtension(m, testpb.E_OptionalInt64, int64(-64)) 102*1c12ee1eSDan Willemsen proto.SetExtension(m, testpb.E_OptionalUint32, uint32(32)) 103*1c12ee1eSDan Willemsen proto.SetExtension(m, testpb.E_OptionalUint64, uint64(64)) 104*1c12ee1eSDan Willemsen proto.SetExtension(m, testpb.E_OptionalFloat, float32(32.32)) 105*1c12ee1eSDan Willemsen proto.SetExtension(m, testpb.E_OptionalDouble, float64(64.64)) 106*1c12ee1eSDan Willemsen proto.SetExtension(m, testpb.E_OptionalString, string("string")) 107*1c12ee1eSDan Willemsen proto.SetExtension(m, testpb.E_OptionalBytes, []byte("bytes")) 108*1c12ee1eSDan Willemsen proto.SetExtension(m, testpb.E_OptionalNestedEnum, testpb.TestAllTypes_NEG) 109*1c12ee1eSDan Willemsen proto.SetExtension(m, testpb.E_OptionalNestedMessage, &testpb.TestAllExtensions_NestedMessage{A: proto.Int32(5)}) 110*1c12ee1eSDan Willemsen return m 111*1c12ee1eSDan Willemsen }(), 112*1c12ee1eSDan Willemsen want: `{[goproto.proto.test.optional_bool]:false, [goproto.proto.test.optional_bytes]:"bytes", [goproto.proto.test.optional_double]:64.64, [goproto.proto.test.optional_float]:32.32, [goproto.proto.test.optional_int32]:-32, [goproto.proto.test.optional_int64]:-64, [goproto.proto.test.optional_nested_enum]:NEG, [goproto.proto.test.optional_nested_message]:{a:5}, [goproto.proto.test.optional_string]:"string", [goproto.proto.test.optional_uint32]:32, [goproto.proto.test.optional_uint64]:64}`, 113*1c12ee1eSDan Willemsen }, { 114*1c12ee1eSDan Willemsen in: func() proto.Message { 115*1c12ee1eSDan Willemsen m := &testpb.TestAllExtensions{} 116*1c12ee1eSDan Willemsen proto.SetExtension(m, testpb.E_RepeatedBool, []bool{false, true}) 117*1c12ee1eSDan Willemsen proto.SetExtension(m, testpb.E_RepeatedInt32, []int32{32, -32}) 118*1c12ee1eSDan Willemsen proto.SetExtension(m, testpb.E_RepeatedInt64, []int64{64, -64}) 119*1c12ee1eSDan Willemsen proto.SetExtension(m, testpb.E_RepeatedUint32, []uint32{0, 32}) 120*1c12ee1eSDan Willemsen proto.SetExtension(m, testpb.E_RepeatedUint64, []uint64{0, 64}) 121*1c12ee1eSDan Willemsen proto.SetExtension(m, testpb.E_RepeatedFloat, []float32{0, 32.32}) 122*1c12ee1eSDan Willemsen proto.SetExtension(m, testpb.E_RepeatedDouble, []float64{0, 64.64}) 123*1c12ee1eSDan Willemsen proto.SetExtension(m, testpb.E_RepeatedString, []string{"s1", "s2"}) 124*1c12ee1eSDan Willemsen proto.SetExtension(m, testpb.E_RepeatedBytes, [][]byte{{1}, {2}}) 125*1c12ee1eSDan Willemsen proto.SetExtension(m, testpb.E_RepeatedNestedEnum, []testpb.TestAllTypes_NestedEnum{ 126*1c12ee1eSDan Willemsen testpb.TestAllTypes_FOO, 127*1c12ee1eSDan Willemsen testpb.TestAllTypes_BAR, 128*1c12ee1eSDan Willemsen }) 129*1c12ee1eSDan Willemsen proto.SetExtension(m, testpb.E_RepeatedNestedMessage, []*testpb.TestAllExtensions_NestedMessage{ 130*1c12ee1eSDan Willemsen {A: proto.Int32(5)}, 131*1c12ee1eSDan Willemsen {A: proto.Int32(-5)}, 132*1c12ee1eSDan Willemsen }) 133*1c12ee1eSDan Willemsen return m 134*1c12ee1eSDan Willemsen }(), 135*1c12ee1eSDan Willemsen want: `{[goproto.proto.test.repeated_bool]:[false, true], [goproto.proto.test.repeated_bytes]:["\x01", "\x02"], [goproto.proto.test.repeated_double]:[0, 64.64], [goproto.proto.test.repeated_float]:[0, 32.32], [goproto.proto.test.repeated_int32]:[32, -32], [goproto.proto.test.repeated_int64]:[64, -64], [goproto.proto.test.repeated_nested_enum]:[FOO, BAR], [goproto.proto.test.repeated_nested_message]:[{a:5}, {a:-5}], [goproto.proto.test.repeated_string]:["s1", "s2"], [goproto.proto.test.repeated_uint32]:[0, 32], [goproto.proto.test.repeated_uint64]:[0, 64]}`, 136*1c12ee1eSDan Willemsen }, { 137*1c12ee1eSDan Willemsen in: func() proto.Message { 138*1c12ee1eSDan Willemsen m := &testpb.TestAllTypes{} 139*1c12ee1eSDan Willemsen m.ProtoReflect().SetUnknown(protopack.Message{ 140*1c12ee1eSDan Willemsen protopack.Tag{Number: 50000, Type: protopack.VarintType}, protopack.Uvarint(100), 141*1c12ee1eSDan Willemsen protopack.Tag{Number: 50001, Type: protopack.Fixed32Type}, protopack.Uint32(200), 142*1c12ee1eSDan Willemsen protopack.Tag{Number: 50002, Type: protopack.Fixed64Type}, protopack.Uint64(300), 143*1c12ee1eSDan Willemsen protopack.Tag{Number: 50003, Type: protopack.BytesType}, protopack.String("hello"), 144*1c12ee1eSDan Willemsen protopack.Message{ 145*1c12ee1eSDan Willemsen protopack.Tag{Number: 50004, Type: protopack.StartGroupType}, 146*1c12ee1eSDan Willemsen protopack.Tag{Number: 1, Type: protopack.VarintType}, protopack.Uvarint(100), 147*1c12ee1eSDan Willemsen protopack.Tag{Number: 1, Type: protopack.Fixed32Type}, protopack.Uint32(200), 148*1c12ee1eSDan Willemsen protopack.Tag{Number: 1, Type: protopack.Fixed64Type}, protopack.Uint64(300), 149*1c12ee1eSDan Willemsen protopack.Tag{Number: 1, Type: protopack.BytesType}, protopack.String("hello"), 150*1c12ee1eSDan Willemsen protopack.Message{ 151*1c12ee1eSDan Willemsen protopack.Tag{Number: 1, Type: protopack.StartGroupType}, 152*1c12ee1eSDan Willemsen protopack.Tag{Number: 1, Type: protopack.VarintType}, protopack.Uvarint(100), 153*1c12ee1eSDan Willemsen protopack.Tag{Number: 1, Type: protopack.Fixed32Type}, protopack.Uint32(200), 154*1c12ee1eSDan Willemsen protopack.Tag{Number: 1, Type: protopack.Fixed64Type}, protopack.Uint64(300), 155*1c12ee1eSDan Willemsen protopack.Tag{Number: 1, Type: protopack.BytesType}, protopack.String("hello"), 156*1c12ee1eSDan Willemsen protopack.Tag{Number: 1, Type: protopack.EndGroupType}, 157*1c12ee1eSDan Willemsen }, 158*1c12ee1eSDan Willemsen protopack.Tag{Number: 50004, Type: protopack.EndGroupType}, 159*1c12ee1eSDan Willemsen }, 160*1c12ee1eSDan Willemsen }.Marshal()) 161*1c12ee1eSDan Willemsen return m 162*1c12ee1eSDan Willemsen }(), 163*1c12ee1eSDan Willemsen want: `{50000:100, 50001:0x000000c8, 50002:0x000000000000012c, 50003:"hello", 50004:{1:[100, 0x000000c8, 0x000000000000012c, "hello", {1:[100, 0x000000c8, 0x000000000000012c, "hello"]}]}}`, 164*1c12ee1eSDan Willemsen }, { 165*1c12ee1eSDan Willemsen in: &textpb.KnownTypes{ 166*1c12ee1eSDan Willemsen OptAny: &anypb.Any{ 167*1c12ee1eSDan Willemsen TypeUrl: "google.golang.org/goproto.proto.test.TestAllTypes", 168*1c12ee1eSDan Willemsen Value: func() []byte { 169*1c12ee1eSDan Willemsen b1, _ := proto.MarshalOptions{Deterministic: true}.Marshal(optMsg) 170*1c12ee1eSDan Willemsen b2, _ := proto.MarshalOptions{Deterministic: true}.Marshal(repMsg) 171*1c12ee1eSDan Willemsen b3, _ := proto.MarshalOptions{Deterministic: true}.Marshal(mapMsg) 172*1c12ee1eSDan Willemsen return append(append(append([]byte(nil), b1...), b2...), b3...) 173*1c12ee1eSDan Willemsen }(), 174*1c12ee1eSDan Willemsen }, 175*1c12ee1eSDan Willemsen }, 176*1c12ee1eSDan Willemsen want: `{opt_any:{[google.golang.org/goproto.proto.test.TestAllTypes]:{optional_int32:-32, optional_int64:-64, optional_uint32:32, optional_uint64:64, optional_float:32.32, optional_double:64.64, optional_bool:false, optional_string:"string", optional_bytes:"bytes", optional_nested_message:{a:5}, optional_nested_enum:NEG, repeated_int32:[32, -32], repeated_int64:[64, -64], repeated_uint32:[0, 32], repeated_uint64:[0, 64], repeated_float:[0, 32.32], repeated_double:[0, 64.64], repeated_bool:[false, true], repeated_string:["s1", "s2"], repeated_bytes:["\x01", "\x02"], repeated_nested_message:[{a:5}, {a:-5}], repeated_nested_enum:[FOO, BAR], map_int32_int32:{-32:32}, map_int64_int64:{-64:64}, map_uint32_uint32:{0:32}, map_uint64_uint64:{0:64}, map_int32_float:{32:32.32}, map_int32_double:{64:64.64}, map_bool_bool:{true:false}, map_string_string:{"k":"v"}, map_string_bytes:{"k":"v"}, map_string_nested_message:{"k":{a:5}}, map_string_nested_enum:{"k":FOO}}}}`, 177*1c12ee1eSDan Willemsen }, { 178*1c12ee1eSDan Willemsen in: &textpb.KnownTypes{ 179*1c12ee1eSDan Willemsen OptTimestamp: &tspb.Timestamp{Seconds: math.MinInt64, Nanos: math.MaxInt32}, 180*1c12ee1eSDan Willemsen }, 181*1c12ee1eSDan Willemsen want: `{opt_timestamp:{seconds:-9223372036854775808, nanos:2147483647}}`, 182*1c12ee1eSDan Willemsen }, { 183*1c12ee1eSDan Willemsen in: &textpb.KnownTypes{ 184*1c12ee1eSDan Willemsen OptTimestamp: &tspb.Timestamp{Seconds: 1257894123, Nanos: 456789}, 185*1c12ee1eSDan Willemsen }, 186*1c12ee1eSDan Willemsen want: `{opt_timestamp:2009-11-10T23:02:03.000456789Z}`, 187*1c12ee1eSDan Willemsen }, { 188*1c12ee1eSDan Willemsen in: &textpb.KnownTypes{ 189*1c12ee1eSDan Willemsen OptDuration: &durpb.Duration{Seconds: math.MinInt64, Nanos: math.MaxInt32}, 190*1c12ee1eSDan Willemsen }, 191*1c12ee1eSDan Willemsen want: `{opt_duration:{seconds:-9223372036854775808, nanos:2147483647}}`, 192*1c12ee1eSDan Willemsen }, { 193*1c12ee1eSDan Willemsen in: &textpb.KnownTypes{ 194*1c12ee1eSDan Willemsen OptDuration: &durpb.Duration{Seconds: +1257894123, Nanos: +456789}, 195*1c12ee1eSDan Willemsen }, 196*1c12ee1eSDan Willemsen want: `{opt_duration:1257894123.000456789s}`, 197*1c12ee1eSDan Willemsen }, { 198*1c12ee1eSDan Willemsen in: &textpb.KnownTypes{ 199*1c12ee1eSDan Willemsen OptDuration: &durpb.Duration{Seconds: -1257894123, Nanos: -456789}, 200*1c12ee1eSDan Willemsen }, 201*1c12ee1eSDan Willemsen want: `{opt_duration:-1257894123.000456789s}`, 202*1c12ee1eSDan Willemsen }, { 203*1c12ee1eSDan Willemsen in: &textpb.KnownTypes{ 204*1c12ee1eSDan Willemsen OptDuration: &durpb.Duration{Seconds: 0, Nanos: -1}, 205*1c12ee1eSDan Willemsen }, 206*1c12ee1eSDan Willemsen want: `{opt_duration:-0.000000001s}`, 207*1c12ee1eSDan Willemsen }, { 208*1c12ee1eSDan Willemsen in: &textpb.KnownTypes{ 209*1c12ee1eSDan Willemsen OptBool: &wpb.BoolValue{}, 210*1c12ee1eSDan Willemsen OptInt32: &wpb.Int32Value{}, 211*1c12ee1eSDan Willemsen OptInt64: &wpb.Int64Value{}, 212*1c12ee1eSDan Willemsen OptUint32: &wpb.UInt32Value{}, 213*1c12ee1eSDan Willemsen OptUint64: &wpb.UInt64Value{}, 214*1c12ee1eSDan Willemsen OptFloat: &wpb.FloatValue{}, 215*1c12ee1eSDan Willemsen OptDouble: &wpb.DoubleValue{}, 216*1c12ee1eSDan Willemsen OptString: &wpb.StringValue{}, 217*1c12ee1eSDan Willemsen OptBytes: &wpb.BytesValue{}, 218*1c12ee1eSDan Willemsen }, 219*1c12ee1eSDan Willemsen want: `{opt_bool:false, opt_int32:0, opt_int64:0, opt_uint32:0, opt_uint64:0, opt_float:0, opt_double:0, opt_string:"", opt_bytes:""}`, 220*1c12ee1eSDan Willemsen }} 221*1c12ee1eSDan Willemsen for _, tt := range tests { 222*1c12ee1eSDan Willemsen t.Run("Generated", func(t *testing.T) { 223*1c12ee1eSDan Willemsen got := msgfmt.Format(tt.in) 224*1c12ee1eSDan Willemsen if diff := cmp.Diff(tt.want, got); diff != "" { 225*1c12ee1eSDan Willemsen t.Errorf("Format() mismatch (-want +got):\n%v", diff) 226*1c12ee1eSDan Willemsen } 227*1c12ee1eSDan Willemsen }) 228*1c12ee1eSDan Willemsen t.Run("dynamicpb.Message", func(t *testing.T) { 229*1c12ee1eSDan Willemsen m := dynpb.NewMessage(tt.in.ProtoReflect().Descriptor()) 230*1c12ee1eSDan Willemsen proto.Merge(m, tt.in) 231*1c12ee1eSDan Willemsen got := msgfmt.Format(m) 232*1c12ee1eSDan Willemsen if diff := cmp.Diff(tt.want, got); diff != "" { 233*1c12ee1eSDan Willemsen t.Errorf("Format() mismatch (-want +got):\n%v", diff) 234*1c12ee1eSDan Willemsen } 235*1c12ee1eSDan Willemsen }) 236*1c12ee1eSDan Willemsen t.Run("protocmp.Message", func(t *testing.T) { 237*1c12ee1eSDan Willemsen // This is a roundabout way to obtain a protocmp.Message since there 238*1c12ee1eSDan Willemsen // is no exported API in protocmp to directly transform a message. 239*1c12ee1eSDan Willemsen var m proto.Message 240*1c12ee1eSDan Willemsen var once sync.Once 241*1c12ee1eSDan Willemsen cmp.Equal(tt.in, tt.in, protocmp.Transform(), cmp.FilterPath(func(p cmp.Path) bool { 242*1c12ee1eSDan Willemsen if v, _ := p.Index(1).Values(); v.IsValid() { 243*1c12ee1eSDan Willemsen once.Do(func() { m = v.Interface().(protocmp.Message) }) 244*1c12ee1eSDan Willemsen } 245*1c12ee1eSDan Willemsen return false 246*1c12ee1eSDan Willemsen }, cmp.Ignore())) 247*1c12ee1eSDan Willemsen 248*1c12ee1eSDan Willemsen got := msgfmt.Format(m) 249*1c12ee1eSDan Willemsen if diff := cmp.Diff(tt.want, got); diff != "" { 250*1c12ee1eSDan Willemsen t.Errorf("Format() mismatch (-want +got):\n%v", diff) 251*1c12ee1eSDan Willemsen } 252*1c12ee1eSDan Willemsen }) 253*1c12ee1eSDan Willemsen } 254*1c12ee1eSDan Willemsen} 255