...

Source file src/google.golang.org/protobuf/internal/msgfmt/format_test.go

Documentation: google.golang.org/protobuf/internal/msgfmt

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

View as plain text