pbdump_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright 2018 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. package main
  5. import (
  6. "fmt"
  7. "strings"
  8. "testing"
  9. "google.golang.org/protobuf/encoding/prototext"
  10. "google.golang.org/protobuf/proto"
  11. pref "google.golang.org/protobuf/reflect/protoreflect"
  12. "google.golang.org/protobuf/types/descriptorpb"
  13. )
  14. func mustMakeMessage(s string) *descriptorpb.DescriptorProto {
  15. s = fmt.Sprintf(`name:"test.proto" syntax:"proto2" message_type:[{%s}]`, s)
  16. pb := new(descriptorpb.FileDescriptorProto)
  17. if err := prototext.Unmarshal([]byte(s), pb); err != nil {
  18. panic(err)
  19. }
  20. return pb.MessageType[0]
  21. }
  22. func TestFields(t *testing.T) {
  23. type fieldsKind struct {
  24. kind pref.Kind
  25. fields string
  26. }
  27. tests := []struct {
  28. inFields []fieldsKind
  29. wantMsg *descriptorpb.DescriptorProto
  30. wantErr string
  31. }{{
  32. inFields: []fieldsKind{{pref.MessageKind, ""}},
  33. wantMsg: mustMakeMessage(`name:"X"`),
  34. }, {
  35. inFields: []fieldsKind{{pref.MessageKind, "987654321"}},
  36. wantErr: "invalid field: 987654321",
  37. }, {
  38. inFields: []fieldsKind{{pref.MessageKind, "-1"}},
  39. wantErr: "invalid field: -1",
  40. }, {
  41. inFields: []fieldsKind{{pref.MessageKind, "k"}},
  42. wantErr: "invalid field: k",
  43. }, {
  44. inFields: []fieldsKind{{pref.MessageKind, "1.2"}, {pref.Int32Kind, "1"}},
  45. wantErr: "field 1 of int32 type cannot have sub-fields",
  46. }, {
  47. inFields: []fieldsKind{{pref.Int32Kind, "1"}, {pref.MessageKind, "1.2"}},
  48. wantErr: "field 1 of int32 type cannot have sub-fields",
  49. }, {
  50. inFields: []fieldsKind{{pref.Int32Kind, "30"}, {pref.Int32Kind, "30"}},
  51. wantErr: "field 30 already set as int32 type",
  52. }, {
  53. inFields: []fieldsKind{
  54. {pref.Int32Kind, "10.20.31"},
  55. {pref.MessageKind, " 10.20.30, 10.21 "},
  56. {pref.GroupKind, "10"},
  57. },
  58. wantMsg: mustMakeMessage(`
  59. name: "X"
  60. field: [
  61. {name:"x10" number:10 label:LABEL_OPTIONAL type:TYPE_GROUP type_name:".X.X10"}
  62. ]
  63. nested_type: [{
  64. name: "X10"
  65. field: [
  66. {name:"x20" number:20 label:LABEL_OPTIONAL type:TYPE_MESSAGE type_name:".X.X10.X20"},
  67. {name:"x21" number:21 label:LABEL_OPTIONAL type:TYPE_MESSAGE type_name:".X.X10.X21"}
  68. ]
  69. nested_type: [{
  70. name: "X20"
  71. field:[
  72. {name:"x30" number:30 label:LABEL_OPTIONAL type:TYPE_MESSAGE, type_name:".X.X10.X20.X30"},
  73. {name:"x31" number:31 label:LABEL_REPEATED type:TYPE_INT32 options:{packed:true}}
  74. ]
  75. nested_type: [{
  76. name: "X30"
  77. }]
  78. }, {
  79. name: "X21"
  80. }]
  81. }]
  82. `),
  83. }}
  84. for _, tt := range tests {
  85. t.Run("", func(t *testing.T) {
  86. var fields fields
  87. for i, tc := range tt.inFields {
  88. gotErr := fields.Set(tc.fields, tc.kind)
  89. if gotErr != nil {
  90. if tt.wantErr == "" || !strings.Contains(fmt.Sprint(gotErr), tt.wantErr) {
  91. t.Fatalf("fields %d, Set(%q, %v) = %v, want %v", i, tc.fields, tc.kind, gotErr, tt.wantErr)
  92. }
  93. return
  94. }
  95. }
  96. if tt.wantErr != "" {
  97. t.Errorf("all Set calls succeeded, want %v error", tt.wantErr)
  98. }
  99. gotMsg := fields.messageDescriptor("X")
  100. if !proto.Equal(gotMsg, tt.wantMsg) {
  101. t.Errorf("messageDescriptor() mismatch:\ngot %v\nwant %v", gotMsg, tt.wantMsg)
  102. }
  103. if _, err := fields.Descriptor(); err != nil {
  104. t.Errorf("Descriptor() = %v, want nil error", err)
  105. }
  106. })
  107. }
  108. }