message_test.go 40 KB

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