message_test.go 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  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 impl
  5. import (
  6. "fmt"
  7. "math"
  8. "strings"
  9. "testing"
  10. "github.com/google/go-cmp/cmp"
  11. "github.com/google/go-cmp/cmp/cmpopts"
  12. "github.com/golang/protobuf/proto"
  13. protoV1 "github.com/golang/protobuf/proto"
  14. descriptorV1 "github.com/golang/protobuf/protoc-gen-go/descriptor"
  15. pref "github.com/golang/protobuf/v2/reflect/protoreflect"
  16. ptype "github.com/golang/protobuf/v2/reflect/prototype"
  17. )
  18. func mustMakeMessageDesc(t ptype.StandaloneMessage) pref.MessageDescriptor {
  19. md, err := ptype.NewMessage(&t)
  20. if err != nil {
  21. panic(err)
  22. }
  23. return md
  24. }
  25. var V = pref.ValueOf
  26. type (
  27. MyBool bool
  28. MyInt32 int32
  29. MyInt64 int64
  30. MyUint32 uint32
  31. MyUint64 uint64
  32. MyFloat32 float32
  33. MyFloat64 float64
  34. MyString string
  35. MyBytes []byte
  36. VectorStrings []MyString
  37. VectorBytes []MyBytes
  38. MapStrings map[MyString]MyString
  39. MapBytes map[MyString]MyBytes
  40. MyEnumV1 pref.EnumNumber
  41. MyEnumV2 string
  42. myEnumV2 MyEnumV2
  43. MyMessageV1 struct {
  44. // SubMessage *Message
  45. }
  46. MyMessageV2 map[pref.FieldNumber]pref.Value
  47. myMessageV2 MyMessageV2
  48. )
  49. func (e MyEnumV2) ProtoReflect() pref.Enum { return myEnumV2(e) }
  50. func (e myEnumV2) Type() pref.EnumType { return nil } // TODO
  51. func (e myEnumV2) Number() pref.EnumNumber { return 0 } // TODO
  52. func (m *MyMessageV2) ProtoReflect() pref.Message { return (*myMessageV2)(m) }
  53. func (m *myMessageV2) Type() pref.MessageType { return nil } // TODO
  54. func (m *myMessageV2) KnownFields() pref.KnownFields { return nil } // TODO
  55. func (m *myMessageV2) UnknownFields() pref.UnknownFields { return nil } // TODO
  56. func (m *myMessageV2) Interface() pref.ProtoMessage { return (*MyMessageV2)(m) }
  57. func (m *myMessageV2) ProtoMutable() {}
  58. // List of test operations to perform on messages, vectors, or maps.
  59. type (
  60. messageOp interface{} // equalMessage | hasFields | getFields | setFields | clearFields | vectorFields | mapFields
  61. messageOps []messageOp
  62. vectorOp interface{} // equalVector | lenVector | getVector | setVector | appendVector | truncVector
  63. vectorOps []vectorOp
  64. mapOp interface{} // equalMap | lenMap | hasMap | getMap | setMap | clearMap | rangeMap
  65. mapOps []mapOp
  66. )
  67. // Test operations performed on a message.
  68. type (
  69. equalMessage pref.Message
  70. hasFields map[pref.FieldNumber]bool
  71. getFields map[pref.FieldNumber]pref.Value
  72. setFields map[pref.FieldNumber]pref.Value
  73. clearFields map[pref.FieldNumber]bool
  74. vectorFields map[pref.FieldNumber]vectorOps
  75. mapFields map[pref.FieldNumber]mapOps
  76. messageFields map[pref.FieldNumber]messageOps
  77. // TODO: Mutable, Range, ExtensionTypes
  78. )
  79. // Test operations performed on a vector.
  80. type (
  81. equalVector pref.Vector
  82. lenVector int
  83. getVector map[int]pref.Value
  84. setVector map[int]pref.Value
  85. appendVector []pref.Value
  86. truncVector int
  87. // TODO: Mutable, MutableAppend
  88. )
  89. // Test operations performed on a map.
  90. type (
  91. equalMap pref.Map
  92. lenMap int
  93. hasMap map[interface{}]bool
  94. getMap map[interface{}]pref.Value
  95. setMap map[interface{}]pref.Value
  96. clearMap map[interface{}]bool
  97. rangeMap map[interface{}]pref.Value
  98. // TODO: Mutable
  99. )
  100. type ScalarProto2 struct {
  101. Bool *bool `protobuf:"1"`
  102. Int32 *int32 `protobuf:"2"`
  103. Int64 *int64 `protobuf:"3"`
  104. Uint32 *uint32 `protobuf:"4"`
  105. Uint64 *uint64 `protobuf:"5"`
  106. Float32 *float32 `protobuf:"6"`
  107. Float64 *float64 `protobuf:"7"`
  108. String *string `protobuf:"8"`
  109. StringA []byte `protobuf:"9"`
  110. Bytes []byte `protobuf:"10"`
  111. BytesA *string `protobuf:"11"`
  112. MyBool *MyBool `protobuf:"12"`
  113. MyInt32 *MyInt32 `protobuf:"13"`
  114. MyInt64 *MyInt64 `protobuf:"14"`
  115. MyUint32 *MyUint32 `protobuf:"15"`
  116. MyUint64 *MyUint64 `protobuf:"16"`
  117. MyFloat32 *MyFloat32 `protobuf:"17"`
  118. MyFloat64 *MyFloat64 `protobuf:"18"`
  119. MyString *MyString `protobuf:"19"`
  120. MyStringA MyBytes `protobuf:"20"`
  121. MyBytes MyBytes `protobuf:"21"`
  122. MyBytesA *MyString `protobuf:"22"`
  123. }
  124. func TestScalarProto2(t *testing.T) {
  125. mi := MessageType{Desc: mustMakeMessageDesc(ptype.StandaloneMessage{
  126. Syntax: pref.Proto2,
  127. FullName: "ScalarProto2",
  128. Fields: []ptype.Field{
  129. {Name: "f1", Number: 1, Cardinality: pref.Optional, Kind: pref.BoolKind, Default: V(bool(true))},
  130. {Name: "f2", Number: 2, Cardinality: pref.Optional, Kind: pref.Int32Kind, Default: V(int32(2))},
  131. {Name: "f3", Number: 3, Cardinality: pref.Optional, Kind: pref.Int64Kind, Default: V(int64(3))},
  132. {Name: "f4", Number: 4, Cardinality: pref.Optional, Kind: pref.Uint32Kind, Default: V(uint32(4))},
  133. {Name: "f5", Number: 5, Cardinality: pref.Optional, Kind: pref.Uint64Kind, Default: V(uint64(5))},
  134. {Name: "f6", Number: 6, Cardinality: pref.Optional, Kind: pref.FloatKind, Default: V(float32(6))},
  135. {Name: "f7", Number: 7, Cardinality: pref.Optional, Kind: pref.DoubleKind, Default: V(float64(7))},
  136. {Name: "f8", Number: 8, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("8"))},
  137. {Name: "f9", Number: 9, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("9"))},
  138. {Name: "f10", Number: 10, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("10"))},
  139. {Name: "f11", Number: 11, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("11"))},
  140. {Name: "f12", Number: 12, Cardinality: pref.Optional, Kind: pref.BoolKind, Default: V(bool(true))},
  141. {Name: "f13", Number: 13, Cardinality: pref.Optional, Kind: pref.Int32Kind, Default: V(int32(13))},
  142. {Name: "f14", Number: 14, Cardinality: pref.Optional, Kind: pref.Int64Kind, Default: V(int64(14))},
  143. {Name: "f15", Number: 15, Cardinality: pref.Optional, Kind: pref.Uint32Kind, Default: V(uint32(15))},
  144. {Name: "f16", Number: 16, Cardinality: pref.Optional, Kind: pref.Uint64Kind, Default: V(uint64(16))},
  145. {Name: "f17", Number: 17, Cardinality: pref.Optional, Kind: pref.FloatKind, Default: V(float32(17))},
  146. {Name: "f18", Number: 18, Cardinality: pref.Optional, Kind: pref.DoubleKind, Default: V(float64(18))},
  147. {Name: "f19", Number: 19, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("19"))},
  148. {Name: "f20", Number: 20, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("20"))},
  149. {Name: "f21", Number: 21, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("21"))},
  150. {Name: "f22", Number: 22, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("22"))},
  151. },
  152. })}
  153. testMessage(t, nil, mi.MessageOf(&ScalarProto2{}), messageOps{
  154. hasFields{
  155. 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
  156. 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
  157. },
  158. getFields{
  159. 1: V(bool(true)), 2: V(int32(2)), 3: V(int64(3)), 4: V(uint32(4)), 5: V(uint64(5)), 6: V(float32(6)), 7: V(float64(7)), 8: V(string("8")), 9: V(string("9")), 10: V([]byte("10")), 11: V([]byte("11")),
  160. 12: V(bool(true)), 13: V(int32(13)), 14: V(int64(14)), 15: V(uint32(15)), 16: V(uint64(16)), 17: V(float32(17)), 18: V(float64(18)), 19: V(string("19")), 20: V(string("20")), 21: V([]byte("21")), 22: V([]byte("22")),
  161. },
  162. setFields{
  163. 1: V(bool(false)), 2: V(int32(0)), 3: V(int64(0)), 4: V(uint32(0)), 5: V(uint64(0)), 6: V(float32(0)), 7: V(float64(0)), 8: V(string("")), 9: V(string("")), 10: V([]byte(nil)), 11: V([]byte(nil)),
  164. 12: V(bool(false)), 13: V(int32(0)), 14: V(int64(0)), 15: V(uint32(0)), 16: V(uint64(0)), 17: V(float32(0)), 18: V(float64(0)), 19: V(string("")), 20: V(string("")), 21: V([]byte(nil)), 22: V([]byte(nil)),
  165. },
  166. hasFields{
  167. 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true,
  168. 12: true, 13: true, 14: true, 15: true, 16: true, 17: true, 18: true, 19: true, 20: true, 21: true, 22: true,
  169. },
  170. equalMessage(mi.MessageOf(&ScalarProto2{
  171. new(bool), new(int32), new(int64), new(uint32), new(uint64), new(float32), new(float64), new(string), []byte{}, []byte{}, new(string),
  172. new(MyBool), new(MyInt32), new(MyInt64), new(MyUint32), new(MyUint64), new(MyFloat32), new(MyFloat64), new(MyString), MyBytes{}, MyBytes{}, new(MyString),
  173. })),
  174. clearFields{
  175. 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true,
  176. 12: true, 13: true, 14: true, 15: true, 16: true, 17: true, 18: true, 19: true, 20: true, 21: true, 22: true,
  177. },
  178. equalMessage(mi.MessageOf(&ScalarProto2{})),
  179. })
  180. }
  181. type ScalarProto3 struct {
  182. Bool bool `protobuf:"1"`
  183. Int32 int32 `protobuf:"2"`
  184. Int64 int64 `protobuf:"3"`
  185. Uint32 uint32 `protobuf:"4"`
  186. Uint64 uint64 `protobuf:"5"`
  187. Float32 float32 `protobuf:"6"`
  188. Float64 float64 `protobuf:"7"`
  189. String string `protobuf:"8"`
  190. StringA []byte `protobuf:"9"`
  191. Bytes []byte `protobuf:"10"`
  192. BytesA string `protobuf:"11"`
  193. MyBool MyBool `protobuf:"12"`
  194. MyInt32 MyInt32 `protobuf:"13"`
  195. MyInt64 MyInt64 `protobuf:"14"`
  196. MyUint32 MyUint32 `protobuf:"15"`
  197. MyUint64 MyUint64 `protobuf:"16"`
  198. MyFloat32 MyFloat32 `protobuf:"17"`
  199. MyFloat64 MyFloat64 `protobuf:"18"`
  200. MyString MyString `protobuf:"19"`
  201. MyStringA MyBytes `protobuf:"20"`
  202. MyBytes MyBytes `protobuf:"21"`
  203. MyBytesA MyString `protobuf:"22"`
  204. }
  205. func TestScalarProto3(t *testing.T) {
  206. mi := MessageType{Desc: mustMakeMessageDesc(ptype.StandaloneMessage{
  207. Syntax: pref.Proto3,
  208. FullName: "ScalarProto3",
  209. Fields: []ptype.Field{
  210. {Name: "f1", Number: 1, Cardinality: pref.Optional, Kind: pref.BoolKind},
  211. {Name: "f2", Number: 2, Cardinality: pref.Optional, Kind: pref.Int32Kind},
  212. {Name: "f3", Number: 3, Cardinality: pref.Optional, Kind: pref.Int64Kind},
  213. {Name: "f4", Number: 4, Cardinality: pref.Optional, Kind: pref.Uint32Kind},
  214. {Name: "f5", Number: 5, Cardinality: pref.Optional, Kind: pref.Uint64Kind},
  215. {Name: "f6", Number: 6, Cardinality: pref.Optional, Kind: pref.FloatKind},
  216. {Name: "f7", Number: 7, Cardinality: pref.Optional, Kind: pref.DoubleKind},
  217. {Name: "f8", Number: 8, Cardinality: pref.Optional, Kind: pref.StringKind},
  218. {Name: "f9", Number: 9, Cardinality: pref.Optional, Kind: pref.StringKind},
  219. {Name: "f10", Number: 10, Cardinality: pref.Optional, Kind: pref.BytesKind},
  220. {Name: "f11", Number: 11, Cardinality: pref.Optional, Kind: pref.BytesKind},
  221. {Name: "f12", Number: 12, Cardinality: pref.Optional, Kind: pref.BoolKind},
  222. {Name: "f13", Number: 13, Cardinality: pref.Optional, Kind: pref.Int32Kind},
  223. {Name: "f14", Number: 14, Cardinality: pref.Optional, Kind: pref.Int64Kind},
  224. {Name: "f15", Number: 15, Cardinality: pref.Optional, Kind: pref.Uint32Kind},
  225. {Name: "f16", Number: 16, Cardinality: pref.Optional, Kind: pref.Uint64Kind},
  226. {Name: "f17", Number: 17, Cardinality: pref.Optional, Kind: pref.FloatKind},
  227. {Name: "f18", Number: 18, Cardinality: pref.Optional, Kind: pref.DoubleKind},
  228. {Name: "f19", Number: 19, Cardinality: pref.Optional, Kind: pref.StringKind},
  229. {Name: "f20", Number: 20, Cardinality: pref.Optional, Kind: pref.StringKind},
  230. {Name: "f21", Number: 21, Cardinality: pref.Optional, Kind: pref.BytesKind},
  231. {Name: "f22", Number: 22, Cardinality: pref.Optional, Kind: pref.BytesKind},
  232. },
  233. })}
  234. testMessage(t, nil, mi.MessageOf(&ScalarProto3{}), messageOps{
  235. hasFields{
  236. 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
  237. 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
  238. },
  239. getFields{
  240. 1: V(bool(false)), 2: V(int32(0)), 3: V(int64(0)), 4: V(uint32(0)), 5: V(uint64(0)), 6: V(float32(0)), 7: V(float64(0)), 8: V(string("")), 9: V(string("")), 10: V([]byte(nil)), 11: V([]byte(nil)),
  241. 12: V(bool(false)), 13: V(int32(0)), 14: V(int64(0)), 15: V(uint32(0)), 16: V(uint64(0)), 17: V(float32(0)), 18: V(float64(0)), 19: V(string("")), 20: V(string("")), 21: V([]byte(nil)), 22: V([]byte(nil)),
  242. },
  243. setFields{
  244. 1: V(bool(false)), 2: V(int32(0)), 3: V(int64(0)), 4: V(uint32(0)), 5: V(uint64(0)), 6: V(float32(0)), 7: V(float64(0)), 8: V(string("")), 9: V(string("")), 10: V([]byte(nil)), 11: V([]byte(nil)),
  245. 12: V(bool(false)), 13: V(int32(0)), 14: V(int64(0)), 15: V(uint32(0)), 16: V(uint64(0)), 17: V(float32(0)), 18: V(float64(0)), 19: V(string("")), 20: V(string("")), 21: V([]byte(nil)), 22: V([]byte(nil)),
  246. },
  247. hasFields{
  248. 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
  249. 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
  250. },
  251. equalMessage(mi.MessageOf(&ScalarProto3{})),
  252. setFields{
  253. 1: V(bool(true)), 2: V(int32(2)), 3: V(int64(3)), 4: V(uint32(4)), 5: V(uint64(5)), 6: V(float32(6)), 7: V(float64(7)), 8: V(string("8")), 9: V(string("9")), 10: V([]byte("10")), 11: V([]byte("11")),
  254. 12: V(bool(true)), 13: V(int32(13)), 14: V(int64(14)), 15: V(uint32(15)), 16: V(uint64(16)), 17: V(float32(17)), 18: V(float64(18)), 19: V(string("19")), 20: V(string("20")), 21: V([]byte("21")), 22: V([]byte("22")),
  255. },
  256. hasFields{
  257. 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true,
  258. 12: true, 13: true, 14: true, 15: true, 16: true, 17: true, 18: true, 19: true, 20: true, 21: true, 22: true,
  259. },
  260. equalMessage(mi.MessageOf(&ScalarProto3{
  261. true, 2, 3, 4, 5, 6, 7, "8", []byte("9"), []byte("10"), "11",
  262. true, 13, 14, 15, 16, 17, 18, "19", []byte("20"), []byte("21"), "22",
  263. })),
  264. clearFields{
  265. 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true,
  266. 12: true, 13: true, 14: true, 15: true, 16: true, 17: true, 18: true, 19: true, 20: true, 21: true, 22: true,
  267. },
  268. equalMessage(mi.MessageOf(&ScalarProto3{})),
  269. })
  270. }
  271. type RepeatedScalars struct {
  272. Bools []bool `protobuf:"1"`
  273. Int32s []int32 `protobuf:"2"`
  274. Int64s []int64 `protobuf:"3"`
  275. Uint32s []uint32 `protobuf:"4"`
  276. Uint64s []uint64 `protobuf:"5"`
  277. Float32s []float32 `protobuf:"6"`
  278. Float64s []float64 `protobuf:"7"`
  279. Strings []string `protobuf:"8"`
  280. StringsA [][]byte `protobuf:"9"`
  281. Bytes [][]byte `protobuf:"10"`
  282. BytesA []string `protobuf:"11"`
  283. MyStrings1 []MyString `protobuf:"12"`
  284. MyStrings2 []MyBytes `protobuf:"13"`
  285. MyBytes1 []MyBytes `protobuf:"14"`
  286. MyBytes2 []MyString `protobuf:"15"`
  287. MyStrings3 VectorStrings `protobuf:"16"`
  288. MyStrings4 VectorBytes `protobuf:"17"`
  289. MyBytes3 VectorBytes `protobuf:"18"`
  290. MyBytes4 VectorStrings `protobuf:"19"`
  291. }
  292. func TestRepeatedScalars(t *testing.T) {
  293. mi := MessageType{Desc: mustMakeMessageDesc(ptype.StandaloneMessage{
  294. Syntax: pref.Proto2,
  295. FullName: "RepeatedScalars",
  296. Fields: []ptype.Field{
  297. {Name: "f1", Number: 1, Cardinality: pref.Repeated, Kind: pref.BoolKind},
  298. {Name: "f2", Number: 2, Cardinality: pref.Repeated, Kind: pref.Int32Kind},
  299. {Name: "f3", Number: 3, Cardinality: pref.Repeated, Kind: pref.Int64Kind},
  300. {Name: "f4", Number: 4, Cardinality: pref.Repeated, Kind: pref.Uint32Kind},
  301. {Name: "f5", Number: 5, Cardinality: pref.Repeated, Kind: pref.Uint64Kind},
  302. {Name: "f6", Number: 6, Cardinality: pref.Repeated, Kind: pref.FloatKind},
  303. {Name: "f7", Number: 7, Cardinality: pref.Repeated, Kind: pref.DoubleKind},
  304. {Name: "f8", Number: 8, Cardinality: pref.Repeated, Kind: pref.StringKind},
  305. {Name: "f9", Number: 9, Cardinality: pref.Repeated, Kind: pref.StringKind},
  306. {Name: "f10", Number: 10, Cardinality: pref.Repeated, Kind: pref.BytesKind},
  307. {Name: "f11", Number: 11, Cardinality: pref.Repeated, Kind: pref.BytesKind},
  308. {Name: "f12", Number: 12, Cardinality: pref.Repeated, Kind: pref.StringKind},
  309. {Name: "f13", Number: 13, Cardinality: pref.Repeated, Kind: pref.StringKind},
  310. {Name: "f14", Number: 14, Cardinality: pref.Repeated, Kind: pref.BytesKind},
  311. {Name: "f15", Number: 15, Cardinality: pref.Repeated, Kind: pref.BytesKind},
  312. {Name: "f16", Number: 16, Cardinality: pref.Repeated, Kind: pref.StringKind},
  313. {Name: "f17", Number: 17, Cardinality: pref.Repeated, Kind: pref.StringKind},
  314. {Name: "f18", Number: 18, Cardinality: pref.Repeated, Kind: pref.BytesKind},
  315. {Name: "f19", Number: 19, Cardinality: pref.Repeated, Kind: pref.BytesKind},
  316. },
  317. })}
  318. empty := mi.MessageOf(&RepeatedScalars{})
  319. emptyFS := empty.KnownFields()
  320. want := mi.MessageOf(&RepeatedScalars{
  321. Bools: []bool{true, false, true},
  322. Int32s: []int32{2, math.MinInt32, math.MaxInt32},
  323. Int64s: []int64{3, math.MinInt64, math.MaxInt64},
  324. Uint32s: []uint32{4, math.MaxUint32 / 2, math.MaxUint32},
  325. Uint64s: []uint64{5, math.MaxUint64 / 2, math.MaxUint64},
  326. Float32s: []float32{6, math.SmallestNonzeroFloat32, float32(math.NaN()), math.MaxFloat32},
  327. Float64s: []float64{7, math.SmallestNonzeroFloat64, float64(math.NaN()), math.MaxFloat64},
  328. Strings: []string{"8", "", "eight"},
  329. StringsA: [][]byte{[]byte("9"), nil, []byte("nine")},
  330. Bytes: [][]byte{[]byte("10"), nil, []byte("ten")},
  331. BytesA: []string{"11", "", "eleven"},
  332. MyStrings1: []MyString{"12", "", "twelve"},
  333. MyStrings2: []MyBytes{[]byte("13"), nil, []byte("thirteen")},
  334. MyBytes1: []MyBytes{[]byte("14"), nil, []byte("fourteen")},
  335. MyBytes2: []MyString{"15", "", "fifteen"},
  336. MyStrings3: VectorStrings{"16", "", "sixteen"},
  337. MyStrings4: VectorBytes{[]byte("17"), nil, []byte("seventeen")},
  338. MyBytes3: VectorBytes{[]byte("18"), nil, []byte("eighteen")},
  339. MyBytes4: VectorStrings{"19", "", "nineteen"},
  340. })
  341. wantFS := want.KnownFields()
  342. testMessage(t, nil, mi.MessageOf(&RepeatedScalars{}), messageOps{
  343. hasFields{1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false, 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false},
  344. getFields{1: emptyFS.Get(1), 3: emptyFS.Get(3), 5: emptyFS.Get(5), 7: emptyFS.Get(7), 9: emptyFS.Get(9), 11: emptyFS.Get(11), 13: emptyFS.Get(13), 15: emptyFS.Get(15), 17: emptyFS.Get(17), 19: emptyFS.Get(19)},
  345. setFields{1: wantFS.Get(1), 3: wantFS.Get(3), 5: wantFS.Get(5), 7: wantFS.Get(7), 9: wantFS.Get(9), 11: wantFS.Get(11), 13: wantFS.Get(13), 15: wantFS.Get(15), 17: wantFS.Get(17), 19: wantFS.Get(19)},
  346. vectorFields{
  347. 2: {
  348. lenVector(0),
  349. appendVector{V(int32(2)), V(int32(math.MinInt32)), V(int32(math.MaxInt32))},
  350. getVector{0: V(int32(2)), 1: V(int32(math.MinInt32)), 2: V(int32(math.MaxInt32))},
  351. equalVector(wantFS.Get(2).Vector()),
  352. },
  353. 4: {
  354. appendVector{V(uint32(0)), V(uint32(0)), V(uint32(0))},
  355. setVector{0: V(uint32(4)), 1: V(uint32(math.MaxUint32 / 2)), 2: V(uint32(math.MaxUint32))},
  356. lenVector(3),
  357. },
  358. 6: {
  359. appendVector{V(float32(6)), V(float32(math.SmallestNonzeroFloat32)), V(float32(math.NaN())), V(float32(math.MaxFloat32))},
  360. equalVector(wantFS.Get(6).Vector()),
  361. },
  362. 8: {
  363. appendVector{V(""), V(""), V(""), V(""), V(""), V("")},
  364. lenVector(6),
  365. setVector{0: V("8"), 2: V("eight")},
  366. truncVector(3),
  367. equalVector(wantFS.Get(8).Vector()),
  368. },
  369. 10: {
  370. appendVector{V([]byte(nil)), V([]byte(nil))},
  371. setVector{0: V([]byte("10"))},
  372. appendVector{V([]byte("wrong"))},
  373. setVector{2: V([]byte("ten"))},
  374. equalVector(wantFS.Get(10).Vector()),
  375. },
  376. 12: {
  377. appendVector{V("12"), V("wrong"), V("twelve")},
  378. setVector{1: V("")},
  379. equalVector(wantFS.Get(12).Vector()),
  380. },
  381. 14: {
  382. appendVector{V([]byte("14")), V([]byte(nil)), V([]byte("fourteen"))},
  383. equalVector(wantFS.Get(14).Vector()),
  384. },
  385. 16: {
  386. appendVector{V("16"), V(""), V("sixteen"), V("extra")},
  387. truncVector(3),
  388. equalVector(wantFS.Get(16).Vector()),
  389. },
  390. 18: {
  391. appendVector{V([]byte("18")), V([]byte(nil)), V([]byte("eighteen"))},
  392. equalVector(wantFS.Get(18).Vector()),
  393. },
  394. },
  395. hasFields{1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true, 12: true, 13: true, 14: true, 15: true, 16: true, 17: true, 18: true, 19: true},
  396. equalMessage(want),
  397. clearFields{1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true, 12: true, 13: true, 14: true, 15: true, 16: true, 17: true, 18: true, 19: true},
  398. equalMessage(empty),
  399. })
  400. }
  401. type MapScalars struct {
  402. KeyBools map[bool]string `protobuf:"1"`
  403. KeyInt32s map[int32]string `protobuf:"2"`
  404. KeyInt64s map[int64]string `protobuf:"3"`
  405. KeyUint32s map[uint32]string `protobuf:"4"`
  406. KeyUint64s map[uint64]string `protobuf:"5"`
  407. KeyStrings map[string]string `protobuf:"6"`
  408. ValBools map[string]bool `protobuf:"7"`
  409. ValInt32s map[string]int32 `protobuf:"8"`
  410. ValInt64s map[string]int64 `protobuf:"9"`
  411. ValUint32s map[string]uint32 `protobuf:"10"`
  412. ValUint64s map[string]uint64 `protobuf:"11"`
  413. ValFloat32s map[string]float32 `protobuf:"12"`
  414. ValFloat64s map[string]float64 `protobuf:"13"`
  415. ValStrings map[string]string `protobuf:"14"`
  416. ValStringsA map[string][]byte `protobuf:"15"`
  417. ValBytes map[string][]byte `protobuf:"16"`
  418. ValBytesA map[string]string `protobuf:"17"`
  419. MyStrings1 map[MyString]MyString `protobuf:"18"`
  420. MyStrings2 map[MyString]MyBytes `protobuf:"19"`
  421. MyBytes1 map[MyString]MyBytes `protobuf:"20"`
  422. MyBytes2 map[MyString]MyString `protobuf:"21"`
  423. MyStrings3 MapStrings `protobuf:"22"`
  424. MyStrings4 MapBytes `protobuf:"23"`
  425. MyBytes3 MapBytes `protobuf:"24"`
  426. MyBytes4 MapStrings `protobuf:"25"`
  427. }
  428. func TestMapScalars(t *testing.T) {
  429. mustMakeMapEntry := func(n pref.FieldNumber, keyKind, valKind pref.Kind) ptype.Field {
  430. return ptype.Field{
  431. Name: pref.Name(fmt.Sprintf("f%d", n)),
  432. Number: n,
  433. Cardinality: pref.Repeated,
  434. Kind: pref.MessageKind,
  435. MessageType: mustMakeMessageDesc(ptype.StandaloneMessage{
  436. Syntax: pref.Proto2,
  437. FullName: pref.FullName(fmt.Sprintf("MapScalars.F%dEntry", n)),
  438. Fields: []ptype.Field{
  439. {Name: "key", Number: 1, Cardinality: pref.Optional, Kind: keyKind},
  440. {Name: "value", Number: 2, Cardinality: pref.Optional, Kind: valKind},
  441. },
  442. Options: &descriptorV1.MessageOptions{MapEntry: protoV1.Bool(true)},
  443. }),
  444. }
  445. }
  446. mi := MessageType{Desc: mustMakeMessageDesc(ptype.StandaloneMessage{
  447. Syntax: pref.Proto2,
  448. FullName: "MapScalars",
  449. Fields: []ptype.Field{
  450. mustMakeMapEntry(1, pref.BoolKind, pref.StringKind),
  451. mustMakeMapEntry(2, pref.Int32Kind, pref.StringKind),
  452. mustMakeMapEntry(3, pref.Int64Kind, pref.StringKind),
  453. mustMakeMapEntry(4, pref.Uint32Kind, pref.StringKind),
  454. mustMakeMapEntry(5, pref.Uint64Kind, pref.StringKind),
  455. mustMakeMapEntry(6, pref.StringKind, pref.StringKind),
  456. mustMakeMapEntry(7, pref.StringKind, pref.BoolKind),
  457. mustMakeMapEntry(8, pref.StringKind, pref.Int32Kind),
  458. mustMakeMapEntry(9, pref.StringKind, pref.Int64Kind),
  459. mustMakeMapEntry(10, pref.StringKind, pref.Uint32Kind),
  460. mustMakeMapEntry(11, pref.StringKind, pref.Uint64Kind),
  461. mustMakeMapEntry(12, pref.StringKind, pref.FloatKind),
  462. mustMakeMapEntry(13, pref.StringKind, pref.DoubleKind),
  463. mustMakeMapEntry(14, pref.StringKind, pref.StringKind),
  464. mustMakeMapEntry(15, pref.StringKind, pref.StringKind),
  465. mustMakeMapEntry(16, pref.StringKind, pref.BytesKind),
  466. mustMakeMapEntry(17, pref.StringKind, pref.BytesKind),
  467. mustMakeMapEntry(18, pref.StringKind, pref.StringKind),
  468. mustMakeMapEntry(19, pref.StringKind, pref.StringKind),
  469. mustMakeMapEntry(20, pref.StringKind, pref.BytesKind),
  470. mustMakeMapEntry(21, pref.StringKind, pref.BytesKind),
  471. mustMakeMapEntry(22, pref.StringKind, pref.StringKind),
  472. mustMakeMapEntry(23, pref.StringKind, pref.StringKind),
  473. mustMakeMapEntry(24, pref.StringKind, pref.BytesKind),
  474. mustMakeMapEntry(25, pref.StringKind, pref.BytesKind),
  475. },
  476. })}
  477. empty := mi.MessageOf(&MapScalars{})
  478. emptyFS := empty.KnownFields()
  479. want := mi.MessageOf(&MapScalars{
  480. KeyBools: map[bool]string{true: "true", false: "false"},
  481. KeyInt32s: map[int32]string{0: "zero", -1: "one", 2: "two"},
  482. KeyInt64s: map[int64]string{0: "zero", -10: "ten", 20: "twenty"},
  483. KeyUint32s: map[uint32]string{0: "zero", 1: "one", 2: "two"},
  484. KeyUint64s: map[uint64]string{0: "zero", 10: "ten", 20: "twenty"},
  485. KeyStrings: map[string]string{"": "", "foo": "bar"},
  486. ValBools: map[string]bool{"true": true, "false": false},
  487. ValInt32s: map[string]int32{"one": 1, "two": 2, "three": 3},
  488. ValInt64s: map[string]int64{"ten": 10, "twenty": -20, "thirty": 30},
  489. ValUint32s: map[string]uint32{"0x00": 0x00, "0xff": 0xff, "0xdead": 0xdead},
  490. ValUint64s: map[string]uint64{"0x00": 0x00, "0xff": 0xff, "0xdead": 0xdead},
  491. ValFloat32s: map[string]float32{"nan": float32(math.NaN()), "pi": float32(math.Pi)},
  492. ValFloat64s: map[string]float64{"nan": float64(math.NaN()), "pi": float64(math.Pi)},
  493. ValStrings: map[string]string{"s1": "s1", "s2": "s2"},
  494. ValStringsA: map[string][]byte{"s1": []byte("s1"), "s2": []byte("s2")},
  495. ValBytes: map[string][]byte{"s1": []byte("s1"), "s2": []byte("s2")},
  496. ValBytesA: map[string]string{"s1": "s1", "s2": "s2"},
  497. MyStrings1: map[MyString]MyString{"s1": "s1", "s2": "s2"},
  498. MyStrings2: map[MyString]MyBytes{"s1": []byte("s1"), "s2": []byte("s2")},
  499. MyBytes1: map[MyString]MyBytes{"s1": []byte("s1"), "s2": []byte("s2")},
  500. MyBytes2: map[MyString]MyString{"s1": "s1", "s2": "s2"},
  501. MyStrings3: MapStrings{"s1": "s1", "s2": "s2"},
  502. MyStrings4: MapBytes{"s1": []byte("s1"), "s2": []byte("s2")},
  503. MyBytes3: MapBytes{"s1": []byte("s1"), "s2": []byte("s2")},
  504. MyBytes4: MapStrings{"s1": "s1", "s2": "s2"},
  505. })
  506. wantFS := want.KnownFields()
  507. testMessage(t, nil, mi.MessageOf(&MapScalars{}), messageOps{
  508. hasFields{1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false, 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false, 23: false, 24: false, 25: false},
  509. getFields{1: emptyFS.Get(1), 3: emptyFS.Get(3), 5: emptyFS.Get(5), 7: emptyFS.Get(7), 9: emptyFS.Get(9), 11: emptyFS.Get(11), 13: emptyFS.Get(13), 15: emptyFS.Get(15), 17: emptyFS.Get(17), 19: emptyFS.Get(19), 21: emptyFS.Get(21), 23: emptyFS.Get(23), 25: emptyFS.Get(25)},
  510. setFields{1: wantFS.Get(1), 3: wantFS.Get(3), 5: wantFS.Get(5), 7: wantFS.Get(7), 9: wantFS.Get(9), 11: wantFS.Get(11), 13: wantFS.Get(13), 15: wantFS.Get(15), 17: wantFS.Get(17), 19: wantFS.Get(19), 21: wantFS.Get(21), 23: wantFS.Get(23), 25: wantFS.Get(25)},
  511. mapFields{
  512. 2: {
  513. lenMap(0),
  514. hasMap{int32(0): false, int32(-1): false, int32(2): false},
  515. setMap{int32(0): V("zero")},
  516. lenMap(1),
  517. hasMap{int32(0): true, int32(-1): false, int32(2): false},
  518. setMap{int32(-1): V("one")},
  519. lenMap(2),
  520. hasMap{int32(0): true, int32(-1): true, int32(2): false},
  521. setMap{int32(2): V("two")},
  522. lenMap(3),
  523. hasMap{int32(0): true, int32(-1): true, int32(2): true},
  524. },
  525. 4: {
  526. setMap{uint32(0): V("zero"), uint32(1): V("one"), uint32(2): V("two")},
  527. equalMap(wantFS.Get(4).Map()),
  528. },
  529. 6: {
  530. clearMap{"noexist": true},
  531. setMap{"foo": V("bar")},
  532. setMap{"": V("empty")},
  533. getMap{"": V("empty"), "foo": V("bar"), "noexist": V(nil)},
  534. setMap{"": V(""), "extra": V("extra")},
  535. clearMap{"extra": true, "noexist": true},
  536. },
  537. 8: {
  538. equalMap(emptyFS.Get(8).Map()),
  539. setMap{"one": V(int32(1)), "two": V(int32(2)), "three": V(int32(3))},
  540. },
  541. 10: {
  542. setMap{"0x00": V(uint32(0x00)), "0xff": V(uint32(0xff)), "0xdead": V(uint32(0xdead))},
  543. lenMap(3),
  544. equalMap(wantFS.Get(10).Map()),
  545. getMap{"0x00": V(uint32(0x00)), "0xff": V(uint32(0xff)), "0xdead": V(uint32(0xdead)), "0xdeadbeef": V(nil)},
  546. },
  547. 12: {
  548. setMap{"nan": V(float32(math.NaN())), "pi": V(float32(math.Pi)), "e": V(float32(math.E))},
  549. clearMap{"e": true, "phi": true},
  550. rangeMap{"nan": V(float32(math.NaN())), "pi": V(float32(math.Pi))},
  551. },
  552. 14: {
  553. equalMap(emptyFS.Get(14).Map()),
  554. setMap{"s1": V("s1"), "s2": V("s2")},
  555. },
  556. 16: {
  557. setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))},
  558. equalMap(wantFS.Get(16).Map()),
  559. },
  560. 18: {
  561. hasMap{"s1": false, "s2": false, "s3": false},
  562. setMap{"s1": V("s1"), "s2": V("s2")},
  563. hasMap{"s1": true, "s2": true, "s3": false},
  564. },
  565. 20: {
  566. equalMap(emptyFS.Get(20).Map()),
  567. setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))},
  568. },
  569. 22: {
  570. rangeMap{},
  571. setMap{"s1": V("s1"), "s2": V("s2")},
  572. rangeMap{"s1": V("s1"), "s2": V("s2")},
  573. lenMap(2),
  574. },
  575. 24: {
  576. setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))},
  577. equalMap(wantFS.Get(24).Map()),
  578. },
  579. },
  580. hasFields{1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true, 12: true, 13: true, 14: true, 15: true, 16: true, 17: true, 18: true, 19: true, 20: true, 21: true, 22: true, 23: true, 24: true, 25: true},
  581. equalMessage(want),
  582. clearFields{1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true, 12: true, 13: true, 14: true, 15: true, 16: true, 17: true, 18: true, 19: true, 20: true, 21: true, 22: true, 23: true, 24: true, 25: true},
  583. equalMessage(empty),
  584. })
  585. }
  586. type (
  587. OneofScalars struct {
  588. Union isOneofScalars_Union `protobuf_oneof:"union"`
  589. }
  590. isOneofScalars_Union interface {
  591. isOneofScalars_Union()
  592. }
  593. OneofScalars_Bool struct {
  594. Bool bool `protobuf:"1"`
  595. }
  596. OneofScalars_Int32 struct {
  597. Int32 MyInt32 `protobuf:"2"`
  598. }
  599. OneofScalars_Int64 struct {
  600. Int64 int64 `protobuf:"3"`
  601. }
  602. OneofScalars_Uint32 struct {
  603. Uint32 MyUint32 `protobuf:"4"`
  604. }
  605. OneofScalars_Uint64 struct {
  606. Uint64 uint64 `protobuf:"5"`
  607. }
  608. OneofScalars_Float32 struct {
  609. Float32 MyFloat32 `protobuf:"6"`
  610. }
  611. OneofScalars_Float64 struct {
  612. Float64 float64 `protobuf:"7"`
  613. }
  614. OneofScalars_String struct {
  615. String string `protobuf:"8"`
  616. }
  617. OneofScalars_StringA struct {
  618. StringA []byte `protobuf:"9"`
  619. }
  620. OneofScalars_StringB struct {
  621. StringB MyString `protobuf:"10"`
  622. }
  623. OneofScalars_Bytes struct {
  624. Bytes []byte `protobuf:"11"`
  625. }
  626. OneofScalars_BytesA struct {
  627. BytesA string `protobuf:"12"`
  628. }
  629. OneofScalars_BytesB struct {
  630. BytesB MyBytes `protobuf:"13"`
  631. }
  632. )
  633. func (*OneofScalars) XXX_OneofFuncs() (func(proto.Message, *proto.Buffer) error, func(proto.Message, int, int, *proto.Buffer) (bool, error), func(proto.Message) int, []interface{}) {
  634. return nil, nil, nil, []interface{}{
  635. (*OneofScalars_Bool)(nil),
  636. (*OneofScalars_Int32)(nil),
  637. (*OneofScalars_Int64)(nil),
  638. (*OneofScalars_Uint32)(nil),
  639. (*OneofScalars_Uint64)(nil),
  640. (*OneofScalars_Float32)(nil),
  641. (*OneofScalars_Float64)(nil),
  642. (*OneofScalars_String)(nil),
  643. (*OneofScalars_StringA)(nil),
  644. (*OneofScalars_StringB)(nil),
  645. (*OneofScalars_Bytes)(nil),
  646. (*OneofScalars_BytesA)(nil),
  647. (*OneofScalars_BytesB)(nil),
  648. }
  649. }
  650. func (*OneofScalars_Bool) isOneofScalars_Union() {}
  651. func (*OneofScalars_Int32) isOneofScalars_Union() {}
  652. func (*OneofScalars_Int64) isOneofScalars_Union() {}
  653. func (*OneofScalars_Uint32) isOneofScalars_Union() {}
  654. func (*OneofScalars_Uint64) isOneofScalars_Union() {}
  655. func (*OneofScalars_Float32) isOneofScalars_Union() {}
  656. func (*OneofScalars_Float64) isOneofScalars_Union() {}
  657. func (*OneofScalars_String) isOneofScalars_Union() {}
  658. func (*OneofScalars_StringA) isOneofScalars_Union() {}
  659. func (*OneofScalars_StringB) isOneofScalars_Union() {}
  660. func (*OneofScalars_Bytes) isOneofScalars_Union() {}
  661. func (*OneofScalars_BytesA) isOneofScalars_Union() {}
  662. func (*OneofScalars_BytesB) isOneofScalars_Union() {}
  663. func TestOneofs(t *testing.T) {
  664. mi := MessageType{Desc: mustMakeMessageDesc(ptype.StandaloneMessage{
  665. Syntax: pref.Proto2,
  666. FullName: "ScalarProto2",
  667. Fields: []ptype.Field{
  668. {Name: "f1", Number: 1, Cardinality: pref.Optional, Kind: pref.BoolKind, Default: V(bool(true)), OneofName: "union"},
  669. {Name: "f2", Number: 2, Cardinality: pref.Optional, Kind: pref.Int32Kind, Default: V(int32(2)), OneofName: "union"},
  670. {Name: "f3", Number: 3, Cardinality: pref.Optional, Kind: pref.Int64Kind, Default: V(int64(3)), OneofName: "union"},
  671. {Name: "f4", Number: 4, Cardinality: pref.Optional, Kind: pref.Uint32Kind, Default: V(uint32(4)), OneofName: "union"},
  672. {Name: "f5", Number: 5, Cardinality: pref.Optional, Kind: pref.Uint64Kind, Default: V(uint64(5)), OneofName: "union"},
  673. {Name: "f6", Number: 6, Cardinality: pref.Optional, Kind: pref.FloatKind, Default: V(float32(6)), OneofName: "union"},
  674. {Name: "f7", Number: 7, Cardinality: pref.Optional, Kind: pref.DoubleKind, Default: V(float64(7)), OneofName: "union"},
  675. {Name: "f8", Number: 8, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("8")), OneofName: "union"},
  676. {Name: "f9", Number: 9, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("9")), OneofName: "union"},
  677. {Name: "f10", Number: 10, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("10")), OneofName: "union"},
  678. {Name: "f11", Number: 11, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("11")), OneofName: "union"},
  679. {Name: "f12", Number: 12, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("12")), OneofName: "union"},
  680. {Name: "f13", Number: 13, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("13")), OneofName: "union"},
  681. },
  682. Oneofs: []ptype.Oneof{{Name: "union"}},
  683. })}
  684. empty := mi.MessageOf(&OneofScalars{})
  685. want1 := mi.MessageOf(&OneofScalars{Union: &OneofScalars_Bool{true}})
  686. want2 := mi.MessageOf(&OneofScalars{Union: &OneofScalars_Int32{20}})
  687. want3 := mi.MessageOf(&OneofScalars{Union: &OneofScalars_Int64{30}})
  688. want4 := mi.MessageOf(&OneofScalars{Union: &OneofScalars_Uint32{40}})
  689. want5 := mi.MessageOf(&OneofScalars{Union: &OneofScalars_Uint64{50}})
  690. want6 := mi.MessageOf(&OneofScalars{Union: &OneofScalars_Float32{60}})
  691. want7 := mi.MessageOf(&OneofScalars{Union: &OneofScalars_Float64{70}})
  692. want8 := mi.MessageOf(&OneofScalars{Union: &OneofScalars_String{string("80")}})
  693. want9 := mi.MessageOf(&OneofScalars{Union: &OneofScalars_StringA{[]byte("90")}})
  694. want10 := mi.MessageOf(&OneofScalars{Union: &OneofScalars_StringB{MyString("100")}})
  695. want11 := mi.MessageOf(&OneofScalars{Union: &OneofScalars_Bytes{[]byte("110")}})
  696. want12 := mi.MessageOf(&OneofScalars{Union: &OneofScalars_BytesA{string("120")}})
  697. want13 := mi.MessageOf(&OneofScalars{Union: &OneofScalars_BytesB{MyBytes("130")}})
  698. testMessage(t, nil, mi.MessageOf(&OneofScalars{}), messageOps{
  699. hasFields{1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false, 12: false, 13: false},
  700. getFields{1: V(bool(true)), 2: V(int32(2)), 3: V(int64(3)), 4: V(uint32(4)), 5: V(uint64(5)), 6: V(float32(6)), 7: V(float64(7)), 8: V(string("8")), 9: V(string("9")), 10: V(string("10")), 11: V([]byte("11")), 12: V([]byte("12")), 13: V([]byte("13"))},
  701. setFields{1: V(bool(true))}, hasFields{1: true}, equalMessage(want1),
  702. setFields{2: V(int32(20))}, hasFields{2: true}, equalMessage(want2),
  703. setFields{3: V(int64(30))}, hasFields{3: true}, equalMessage(want3),
  704. setFields{4: V(uint32(40))}, hasFields{4: true}, equalMessage(want4),
  705. setFields{5: V(uint64(50))}, hasFields{5: true}, equalMessage(want5),
  706. setFields{6: V(float32(60))}, hasFields{6: true}, equalMessage(want6),
  707. setFields{7: V(float64(70))}, hasFields{7: true}, equalMessage(want7),
  708. setFields{8: V(string("80"))}, hasFields{8: true}, equalMessage(want8),
  709. setFields{9: V(string("90"))}, hasFields{9: true}, equalMessage(want9),
  710. setFields{10: V(string("100"))}, hasFields{10: true}, equalMessage(want10),
  711. setFields{11: V([]byte("110"))}, hasFields{11: true}, equalMessage(want11),
  712. setFields{12: V([]byte("120"))}, hasFields{12: true}, equalMessage(want12),
  713. setFields{13: V([]byte("130"))}, hasFields{13: true}, equalMessage(want13),
  714. hasFields{1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false, 12: false, 13: true},
  715. getFields{1: V(bool(true)), 2: V(int32(2)), 3: V(int64(3)), 4: V(uint32(4)), 5: V(uint64(5)), 6: V(float32(6)), 7: V(float64(7)), 8: V(string("8")), 9: V(string("9")), 10: V(string("10")), 11: V([]byte("11")), 12: V([]byte("12")), 13: V([]byte("130"))},
  716. clearFields{1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true, 12: true},
  717. equalMessage(want13),
  718. clearFields{13: true},
  719. equalMessage(empty),
  720. })
  721. }
  722. // TODO: Need to test singular and repeated messages
  723. var cmpOpts = cmp.Options{
  724. cmp.Transformer("UnwrapValue", func(v pref.Value) interface{} {
  725. return v.Interface()
  726. }),
  727. cmp.Transformer("UnwrapMessage", func(m pref.Message) interface{} {
  728. v := m.Interface()
  729. if v, ok := v.(interface{ Unwrap() interface{} }); ok {
  730. return v.Unwrap()
  731. }
  732. return v
  733. }),
  734. cmp.Transformer("UnwrapVector", func(v pref.Vector) interface{} {
  735. return v.(interface{ Unwrap() interface{} }).Unwrap()
  736. }),
  737. cmp.Transformer("UnwrapMap", func(m pref.Map) interface{} {
  738. return m.(interface{ Unwrap() interface{} }).Unwrap()
  739. }),
  740. cmpopts.EquateNaNs(),
  741. }
  742. func testMessage(t *testing.T, p path, m pref.Message, tt messageOps) {
  743. fs := m.KnownFields()
  744. for i, op := range tt {
  745. p.Push(i)
  746. switch op := op.(type) {
  747. case equalMessage:
  748. if diff := cmp.Diff(op, m, cmpOpts); diff != "" {
  749. t.Errorf("operation %v, message mismatch (-want, +got):\n%s", p, diff)
  750. }
  751. case hasFields:
  752. got := map[pref.FieldNumber]bool{}
  753. want := map[pref.FieldNumber]bool(op)
  754. for n := range want {
  755. got[n] = fs.Has(n)
  756. }
  757. if diff := cmp.Diff(want, got); diff != "" {
  758. t.Errorf("operation %v, KnownFields.Has mismatch (-want, +got):\n%s", p, diff)
  759. }
  760. case getFields:
  761. got := map[pref.FieldNumber]pref.Value{}
  762. want := map[pref.FieldNumber]pref.Value(op)
  763. for n := range want {
  764. got[n] = fs.Get(n)
  765. }
  766. if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
  767. t.Errorf("operation %v, KnownFields.Get mismatch (-want, +got):\n%s", p, diff)
  768. }
  769. case setFields:
  770. for n, v := range op {
  771. fs.Set(n, v)
  772. }
  773. case clearFields:
  774. for n, ok := range op {
  775. if ok {
  776. fs.Clear(n)
  777. }
  778. }
  779. case vectorFields:
  780. for n, tt := range op {
  781. p.Push(int(n))
  782. testVectors(t, p, fs.Mutable(n).(pref.Vector), tt)
  783. p.Pop()
  784. }
  785. case mapFields:
  786. for n, tt := range op {
  787. p.Push(int(n))
  788. testMaps(t, p, fs.Mutable(n).(pref.Map), tt)
  789. p.Pop()
  790. }
  791. default:
  792. t.Fatalf("operation %v, invalid operation: %T", p, op)
  793. }
  794. p.Pop()
  795. }
  796. }
  797. func testVectors(t *testing.T, p path, v pref.Vector, tt vectorOps) {
  798. for i, op := range tt {
  799. p.Push(i)
  800. switch op := op.(type) {
  801. case equalVector:
  802. if diff := cmp.Diff(op, v, cmpOpts); diff != "" {
  803. t.Errorf("operation %v, vector mismatch (-want, +got):\n%s", p, diff)
  804. }
  805. case lenVector:
  806. if got, want := v.Len(), int(op); got != want {
  807. t.Errorf("operation %v, Vector.Len = %d, want %d", p, got, want)
  808. }
  809. case getVector:
  810. got := map[int]pref.Value{}
  811. want := map[int]pref.Value(op)
  812. for n := range want {
  813. got[n] = v.Get(n)
  814. }
  815. if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
  816. t.Errorf("operation %v, Vector.Get mismatch (-want, +got):\n%s", p, diff)
  817. }
  818. case setVector:
  819. for n, e := range op {
  820. v.Set(n, e)
  821. }
  822. case appendVector:
  823. for _, e := range op {
  824. v.Append(e)
  825. }
  826. case truncVector:
  827. v.Truncate(int(op))
  828. default:
  829. t.Fatalf("operation %v, invalid operation: %T", p, op)
  830. }
  831. p.Pop()
  832. }
  833. }
  834. func testMaps(t *testing.T, p path, m pref.Map, tt mapOps) {
  835. for i, op := range tt {
  836. p.Push(i)
  837. switch op := op.(type) {
  838. case equalMap:
  839. if diff := cmp.Diff(op, m, cmpOpts); diff != "" {
  840. t.Errorf("operation %v, map mismatch (-want, +got):\n%s", p, diff)
  841. }
  842. case lenMap:
  843. if got, want := m.Len(), int(op); got != want {
  844. t.Errorf("operation %v, Map.Len = %d, want %d", p, got, want)
  845. }
  846. case hasMap:
  847. got := map[interface{}]bool{}
  848. want := map[interface{}]bool(op)
  849. for k := range want {
  850. got[k] = m.Has(V(k).MapKey())
  851. }
  852. if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
  853. t.Errorf("operation %v, Map.Has mismatch (-want, +got):\n%s", p, diff)
  854. }
  855. case getMap:
  856. got := map[interface{}]pref.Value{}
  857. want := map[interface{}]pref.Value(op)
  858. for k := range want {
  859. got[k] = m.Get(V(k).MapKey())
  860. }
  861. if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
  862. t.Errorf("operation %v, Map.Get mismatch (-want, +got):\n%s", p, diff)
  863. }
  864. case setMap:
  865. for k, v := range op {
  866. m.Set(V(k).MapKey(), v)
  867. }
  868. case clearMap:
  869. for v, ok := range op {
  870. if ok {
  871. m.Clear(V(v).MapKey())
  872. }
  873. }
  874. case rangeMap:
  875. got := map[interface{}]pref.Value{}
  876. want := map[interface{}]pref.Value(op)
  877. m.Range(func(k pref.MapKey, v pref.Value) bool {
  878. got[k.Interface()] = v
  879. return true
  880. })
  881. if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
  882. t.Errorf("operation %v, Map.Range mismatch (-want, +got):\n%s", p, diff)
  883. }
  884. default:
  885. t.Fatalf("operation %v, invalid operation: %T", p, op)
  886. }
  887. p.Pop()
  888. }
  889. }
  890. type path []int
  891. func (p *path) Push(i int) { *p = append(*p, i) }
  892. func (p *path) Pop() { *p = (*p)[:len(*p)-1] }
  893. func (p path) String() string {
  894. var ss []string
  895. for _, i := range p {
  896. ss = append(ss, fmt.Sprint(i))
  897. }
  898. return strings.Join(ss, ".")
  899. }