message_test.go 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367
  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_test
  5. import (
  6. "fmt"
  7. "math"
  8. "strings"
  9. "testing"
  10. protoV1 "github.com/golang/protobuf/proto"
  11. pimpl "github.com/golang/protobuf/v2/internal/impl"
  12. scalar "github.com/golang/protobuf/v2/internal/scalar"
  13. pvalue "github.com/golang/protobuf/v2/internal/value"
  14. pref "github.com/golang/protobuf/v2/reflect/protoreflect"
  15. ptype "github.com/golang/protobuf/v2/reflect/prototype"
  16. cmp "github.com/google/go-cmp/cmp"
  17. cmpopts "github.com/google/go-cmp/cmp/cmpopts"
  18. // The legacy package must be imported prior to use of any legacy messages.
  19. // TODO: Remove this when protoV1 registers these hooks for you.
  20. _ "github.com/golang/protobuf/v2/internal/legacy"
  21. proto2_20180125 "github.com/golang/protobuf/v2/internal/testprotos/legacy/proto2.v1.0.0-20180125-92554152"
  22. descriptorpb "github.com/golang/protobuf/v2/types/descriptor"
  23. )
  24. // List of test operations to perform on messages, lists, or maps.
  25. type (
  26. messageOp interface{ isMessageOp() }
  27. messageOps []messageOp
  28. listOp interface{ isListOp() }
  29. listOps []listOp
  30. mapOp interface{ isMapOp() }
  31. mapOps []mapOp
  32. )
  33. // Test operations performed on a message.
  34. type (
  35. // check that the message contents match
  36. equalMessage struct{ pref.Message }
  37. // check presence for specific fields in the message
  38. hasFields map[pref.FieldNumber]bool
  39. // check that specific message fields match
  40. getFields map[pref.FieldNumber]pref.Value
  41. // set specific message fields
  42. setFields map[pref.FieldNumber]pref.Value
  43. // clear specific fields in the message
  44. clearFields []pref.FieldNumber
  45. // apply messageOps on each specified message field
  46. messageFields map[pref.FieldNumber]messageOps
  47. // apply listOps on each specified list field
  48. listFields map[pref.FieldNumber]listOps
  49. // apply mapOps on each specified map fields
  50. mapFields map[pref.FieldNumber]mapOps
  51. // range through all fields and check that they match
  52. rangeFields map[pref.FieldNumber]pref.Value
  53. )
  54. func (equalMessage) isMessageOp() {}
  55. func (hasFields) isMessageOp() {}
  56. func (getFields) isMessageOp() {}
  57. func (setFields) isMessageOp() {}
  58. func (clearFields) isMessageOp() {}
  59. func (messageFields) isMessageOp() {}
  60. func (listFields) isMessageOp() {}
  61. func (mapFields) isMessageOp() {}
  62. func (rangeFields) isMessageOp() {}
  63. // Test operations performed on a list.
  64. type (
  65. // check that the list contents match
  66. equalList struct{ pref.List }
  67. // check that list length matches
  68. lenList int
  69. // check that specific list entries match
  70. getList map[int]pref.Value
  71. // set specific list entries
  72. setList map[int]pref.Value
  73. // append entries to the list
  74. appendList []pref.Value
  75. // apply messageOps on a newly appended message
  76. appendMessageList messageOps
  77. // truncate the list to the specified length
  78. truncList int
  79. )
  80. func (equalList) isListOp() {}
  81. func (lenList) isListOp() {}
  82. func (getList) isListOp() {}
  83. func (setList) isListOp() {}
  84. func (appendList) isListOp() {}
  85. func (appendMessageList) isListOp() {}
  86. func (truncList) isListOp() {}
  87. // Test operations performed on a map.
  88. type (
  89. // check that the map contents match
  90. equalMap struct{ pref.Map }
  91. // check that map length matches
  92. lenMap int
  93. // check presence for specific entries in the map
  94. hasMap map[interface{}]bool
  95. // check that specific map entries match
  96. getMap map[interface{}]pref.Value
  97. // set specific map entries
  98. setMap map[interface{}]pref.Value
  99. // clear specific entries in the map
  100. clearMap []interface{}
  101. // apply messageOps on each specified message entry
  102. messageMap map[interface{}]messageOps
  103. // range through all entries and check that they match
  104. rangeMap map[interface{}]pref.Value
  105. )
  106. func (equalMap) isMapOp() {}
  107. func (lenMap) isMapOp() {}
  108. func (hasMap) isMapOp() {}
  109. func (getMap) isMapOp() {}
  110. func (setMap) isMapOp() {}
  111. func (clearMap) isMapOp() {}
  112. func (messageMap) isMapOp() {}
  113. func (rangeMap) isMapOp() {}
  114. type ScalarProto2 struct {
  115. Bool *bool `protobuf:"1"`
  116. Int32 *int32 `protobuf:"2"`
  117. Int64 *int64 `protobuf:"3"`
  118. Uint32 *uint32 `protobuf:"4"`
  119. Uint64 *uint64 `protobuf:"5"`
  120. Float32 *float32 `protobuf:"6"`
  121. Float64 *float64 `protobuf:"7"`
  122. String *string `protobuf:"8"`
  123. StringA []byte `protobuf:"9"`
  124. Bytes []byte `protobuf:"10"`
  125. BytesA *string `protobuf:"11"`
  126. MyBool *MyBool `protobuf:"12"`
  127. MyInt32 *MyInt32 `protobuf:"13"`
  128. MyInt64 *MyInt64 `protobuf:"14"`
  129. MyUint32 *MyUint32 `protobuf:"15"`
  130. MyUint64 *MyUint64 `protobuf:"16"`
  131. MyFloat32 *MyFloat32 `protobuf:"17"`
  132. MyFloat64 *MyFloat64 `protobuf:"18"`
  133. MyString *MyString `protobuf:"19"`
  134. MyStringA MyBytes `protobuf:"20"`
  135. MyBytes MyBytes `protobuf:"21"`
  136. MyBytesA *MyString `protobuf:"22"`
  137. }
  138. func mustMakeEnumDesc(t ptype.StandaloneEnum) pref.EnumDescriptor {
  139. ed, err := ptype.NewEnum(&t)
  140. if err != nil {
  141. panic(err)
  142. }
  143. return ed
  144. }
  145. func mustMakeMessageDesc(t ptype.StandaloneMessage) pref.MessageDescriptor {
  146. md, err := ptype.NewMessage(&t)
  147. if err != nil {
  148. panic(err)
  149. }
  150. return md
  151. }
  152. var V = pref.ValueOf
  153. var VE = func(n pref.EnumNumber) pref.Value { return V(n) }
  154. type (
  155. MyBool bool
  156. MyInt32 int32
  157. MyInt64 int64
  158. MyUint32 uint32
  159. MyUint64 uint64
  160. MyFloat32 float32
  161. MyFloat64 float64
  162. MyString string
  163. MyBytes []byte
  164. ListStrings []MyString
  165. ListBytes []MyBytes
  166. MapStrings map[MyString]MyString
  167. MapBytes map[MyString]MyBytes
  168. )
  169. var scalarProto2Type = pimpl.MessageType{Type: ptype.GoMessage(
  170. mustMakeMessageDesc(ptype.StandaloneMessage{
  171. Syntax: pref.Proto2,
  172. FullName: "ScalarProto2",
  173. Fields: []ptype.Field{
  174. {Name: "f1", Number: 1, Cardinality: pref.Optional, Kind: pref.BoolKind, Default: V(bool(true))},
  175. {Name: "f2", Number: 2, Cardinality: pref.Optional, Kind: pref.Int32Kind, Default: V(int32(2))},
  176. {Name: "f3", Number: 3, Cardinality: pref.Optional, Kind: pref.Int64Kind, Default: V(int64(3))},
  177. {Name: "f4", Number: 4, Cardinality: pref.Optional, Kind: pref.Uint32Kind, Default: V(uint32(4))},
  178. {Name: "f5", Number: 5, Cardinality: pref.Optional, Kind: pref.Uint64Kind, Default: V(uint64(5))},
  179. {Name: "f6", Number: 6, Cardinality: pref.Optional, Kind: pref.FloatKind, Default: V(float32(6))},
  180. {Name: "f7", Number: 7, Cardinality: pref.Optional, Kind: pref.DoubleKind, Default: V(float64(7))},
  181. {Name: "f8", Number: 8, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("8"))},
  182. {Name: "f9", Number: 9, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("9"))},
  183. {Name: "f10", Number: 10, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("10"))},
  184. {Name: "f11", Number: 11, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("11"))},
  185. {Name: "f12", Number: 12, Cardinality: pref.Optional, Kind: pref.BoolKind, Default: V(bool(true))},
  186. {Name: "f13", Number: 13, Cardinality: pref.Optional, Kind: pref.Int32Kind, Default: V(int32(13))},
  187. {Name: "f14", Number: 14, Cardinality: pref.Optional, Kind: pref.Int64Kind, Default: V(int64(14))},
  188. {Name: "f15", Number: 15, Cardinality: pref.Optional, Kind: pref.Uint32Kind, Default: V(uint32(15))},
  189. {Name: "f16", Number: 16, Cardinality: pref.Optional, Kind: pref.Uint64Kind, Default: V(uint64(16))},
  190. {Name: "f17", Number: 17, Cardinality: pref.Optional, Kind: pref.FloatKind, Default: V(float32(17))},
  191. {Name: "f18", Number: 18, Cardinality: pref.Optional, Kind: pref.DoubleKind, Default: V(float64(18))},
  192. {Name: "f19", Number: 19, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("19"))},
  193. {Name: "f20", Number: 20, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("20"))},
  194. {Name: "f21", Number: 21, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("21"))},
  195. {Name: "f22", Number: 22, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("22"))},
  196. },
  197. }),
  198. func(pref.MessageType) pref.ProtoMessage {
  199. return new(ScalarProto2)
  200. },
  201. )}
  202. func (m *ScalarProto2) Type() pref.MessageType { return scalarProto2Type.Type }
  203. func (m *ScalarProto2) KnownFields() pref.KnownFields { return scalarProto2Type.KnownFieldsOf(m) }
  204. func (m *ScalarProto2) UnknownFields() pref.UnknownFields { return scalarProto2Type.UnknownFieldsOf(m) }
  205. func (m *ScalarProto2) Interface() pref.ProtoMessage { return m }
  206. func (m *ScalarProto2) ProtoReflect() pref.Message { return m }
  207. func (m *ScalarProto2) ProtoMutable() {}
  208. func TestScalarProto2(t *testing.T) {
  209. testMessage(t, nil, &ScalarProto2{}, messageOps{
  210. hasFields{
  211. 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
  212. 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
  213. },
  214. getFields{
  215. 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")),
  216. 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")),
  217. },
  218. setFields{
  219. 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)),
  220. 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)),
  221. },
  222. hasFields{
  223. 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true,
  224. 12: true, 13: true, 14: true, 15: true, 16: true, 17: true, 18: true, 19: true, 20: true, 21: true, 22: true,
  225. },
  226. equalMessage{&ScalarProto2{
  227. new(bool), new(int32), new(int64), new(uint32), new(uint64), new(float32), new(float64), new(string), []byte{}, []byte{}, new(string),
  228. new(MyBool), new(MyInt32), new(MyInt64), new(MyUint32), new(MyUint64), new(MyFloat32), new(MyFloat64), new(MyString), MyBytes{}, MyBytes{}, new(MyString),
  229. }},
  230. clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22},
  231. equalMessage{&ScalarProto2{}},
  232. })
  233. // Test read-only operations on nil message.
  234. testMessage(t, nil, (*ScalarProto2)(nil), 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(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")),
  241. 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")),
  242. },
  243. })
  244. }
  245. type ScalarProto3 struct {
  246. Bool bool `protobuf:"1"`
  247. Int32 int32 `protobuf:"2"`
  248. Int64 int64 `protobuf:"3"`
  249. Uint32 uint32 `protobuf:"4"`
  250. Uint64 uint64 `protobuf:"5"`
  251. Float32 float32 `protobuf:"6"`
  252. Float64 float64 `protobuf:"7"`
  253. String string `protobuf:"8"`
  254. StringA []byte `protobuf:"9"`
  255. Bytes []byte `protobuf:"10"`
  256. BytesA string `protobuf:"11"`
  257. MyBool MyBool `protobuf:"12"`
  258. MyInt32 MyInt32 `protobuf:"13"`
  259. MyInt64 MyInt64 `protobuf:"14"`
  260. MyUint32 MyUint32 `protobuf:"15"`
  261. MyUint64 MyUint64 `protobuf:"16"`
  262. MyFloat32 MyFloat32 `protobuf:"17"`
  263. MyFloat64 MyFloat64 `protobuf:"18"`
  264. MyString MyString `protobuf:"19"`
  265. MyStringA MyBytes `protobuf:"20"`
  266. MyBytes MyBytes `protobuf:"21"`
  267. MyBytesA MyString `protobuf:"22"`
  268. }
  269. var scalarProto3Type = pimpl.MessageType{Type: ptype.GoMessage(
  270. mustMakeMessageDesc(ptype.StandaloneMessage{
  271. Syntax: pref.Proto3,
  272. FullName: "ScalarProto3",
  273. Fields: []ptype.Field{
  274. {Name: "f1", Number: 1, Cardinality: pref.Optional, Kind: pref.BoolKind},
  275. {Name: "f2", Number: 2, Cardinality: pref.Optional, Kind: pref.Int32Kind},
  276. {Name: "f3", Number: 3, Cardinality: pref.Optional, Kind: pref.Int64Kind},
  277. {Name: "f4", Number: 4, Cardinality: pref.Optional, Kind: pref.Uint32Kind},
  278. {Name: "f5", Number: 5, Cardinality: pref.Optional, Kind: pref.Uint64Kind},
  279. {Name: "f6", Number: 6, Cardinality: pref.Optional, Kind: pref.FloatKind},
  280. {Name: "f7", Number: 7, Cardinality: pref.Optional, Kind: pref.DoubleKind},
  281. {Name: "f8", Number: 8, Cardinality: pref.Optional, Kind: pref.StringKind},
  282. {Name: "f9", Number: 9, Cardinality: pref.Optional, Kind: pref.StringKind},
  283. {Name: "f10", Number: 10, Cardinality: pref.Optional, Kind: pref.BytesKind},
  284. {Name: "f11", Number: 11, Cardinality: pref.Optional, Kind: pref.BytesKind},
  285. {Name: "f12", Number: 12, Cardinality: pref.Optional, Kind: pref.BoolKind},
  286. {Name: "f13", Number: 13, Cardinality: pref.Optional, Kind: pref.Int32Kind},
  287. {Name: "f14", Number: 14, Cardinality: pref.Optional, Kind: pref.Int64Kind},
  288. {Name: "f15", Number: 15, Cardinality: pref.Optional, Kind: pref.Uint32Kind},
  289. {Name: "f16", Number: 16, Cardinality: pref.Optional, Kind: pref.Uint64Kind},
  290. {Name: "f17", Number: 17, Cardinality: pref.Optional, Kind: pref.FloatKind},
  291. {Name: "f18", Number: 18, Cardinality: pref.Optional, Kind: pref.DoubleKind},
  292. {Name: "f19", Number: 19, Cardinality: pref.Optional, Kind: pref.StringKind},
  293. {Name: "f20", Number: 20, Cardinality: pref.Optional, Kind: pref.StringKind},
  294. {Name: "f21", Number: 21, Cardinality: pref.Optional, Kind: pref.BytesKind},
  295. {Name: "f22", Number: 22, Cardinality: pref.Optional, Kind: pref.BytesKind},
  296. },
  297. }),
  298. func(pref.MessageType) pref.ProtoMessage {
  299. return new(ScalarProto3)
  300. },
  301. )}
  302. func (m *ScalarProto3) Type() pref.MessageType { return scalarProto3Type.Type }
  303. func (m *ScalarProto3) KnownFields() pref.KnownFields { return scalarProto3Type.KnownFieldsOf(m) }
  304. func (m *ScalarProto3) UnknownFields() pref.UnknownFields { return scalarProto3Type.UnknownFieldsOf(m) }
  305. func (m *ScalarProto3) Interface() pref.ProtoMessage { return m }
  306. func (m *ScalarProto3) ProtoReflect() pref.Message { return m }
  307. func (m *ScalarProto3) ProtoMutable() {}
  308. func TestScalarProto3(t *testing.T) {
  309. testMessage(t, nil, &ScalarProto3{}, messageOps{
  310. hasFields{
  311. 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
  312. 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
  313. },
  314. getFields{
  315. 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)),
  316. 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)),
  317. },
  318. setFields{
  319. 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)),
  320. 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)),
  321. },
  322. hasFields{
  323. 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
  324. 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
  325. },
  326. equalMessage{&ScalarProto3{}},
  327. setFields{
  328. 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")),
  329. 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")),
  330. },
  331. hasFields{
  332. 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true,
  333. 12: true, 13: true, 14: true, 15: true, 16: true, 17: true, 18: true, 19: true, 20: true, 21: true, 22: true,
  334. },
  335. equalMessage{&ScalarProto3{
  336. true, 2, 3, 4, 5, 6, 7, "8", []byte("9"), []byte("10"), "11",
  337. true, 13, 14, 15, 16, 17, 18, "19", []byte("20"), []byte("21"), "22",
  338. }},
  339. setFields{
  340. 2: V(int32(-2)), 3: V(int64(-3)), 6: V(float32(math.Inf(-1))), 7: V(float64(math.NaN())),
  341. },
  342. hasFields{
  343. 2: true, 3: true, 6: true, 7: true,
  344. },
  345. clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22},
  346. equalMessage{&ScalarProto3{}},
  347. })
  348. // Test read-only operations on nil message.
  349. testMessage(t, nil, (*ScalarProto3)(nil), messageOps{
  350. hasFields{
  351. 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false,
  352. 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false,
  353. },
  354. getFields{
  355. 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)),
  356. 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)),
  357. },
  358. })
  359. }
  360. type ListScalars struct {
  361. Bools []bool `protobuf:"1"`
  362. Int32s []int32 `protobuf:"2"`
  363. Int64s []int64 `protobuf:"3"`
  364. Uint32s []uint32 `protobuf:"4"`
  365. Uint64s []uint64 `protobuf:"5"`
  366. Float32s []float32 `protobuf:"6"`
  367. Float64s []float64 `protobuf:"7"`
  368. Strings []string `protobuf:"8"`
  369. StringsA [][]byte `protobuf:"9"`
  370. Bytes [][]byte `protobuf:"10"`
  371. BytesA []string `protobuf:"11"`
  372. MyStrings1 []MyString `protobuf:"12"`
  373. MyStrings2 []MyBytes `protobuf:"13"`
  374. MyBytes1 []MyBytes `protobuf:"14"`
  375. MyBytes2 []MyString `protobuf:"15"`
  376. MyStrings3 ListStrings `protobuf:"16"`
  377. MyStrings4 ListBytes `protobuf:"17"`
  378. MyBytes3 ListBytes `protobuf:"18"`
  379. MyBytes4 ListStrings `protobuf:"19"`
  380. }
  381. var listScalarsType = pimpl.MessageType{Type: ptype.GoMessage(
  382. mustMakeMessageDesc(ptype.StandaloneMessage{
  383. Syntax: pref.Proto2,
  384. FullName: "ListScalars",
  385. Fields: []ptype.Field{
  386. {Name: "f1", Number: 1, Cardinality: pref.Repeated, Kind: pref.BoolKind},
  387. {Name: "f2", Number: 2, Cardinality: pref.Repeated, Kind: pref.Int32Kind},
  388. {Name: "f3", Number: 3, Cardinality: pref.Repeated, Kind: pref.Int64Kind},
  389. {Name: "f4", Number: 4, Cardinality: pref.Repeated, Kind: pref.Uint32Kind},
  390. {Name: "f5", Number: 5, Cardinality: pref.Repeated, Kind: pref.Uint64Kind},
  391. {Name: "f6", Number: 6, Cardinality: pref.Repeated, Kind: pref.FloatKind},
  392. {Name: "f7", Number: 7, Cardinality: pref.Repeated, Kind: pref.DoubleKind},
  393. {Name: "f8", Number: 8, Cardinality: pref.Repeated, Kind: pref.StringKind},
  394. {Name: "f9", Number: 9, Cardinality: pref.Repeated, Kind: pref.StringKind},
  395. {Name: "f10", Number: 10, Cardinality: pref.Repeated, Kind: pref.BytesKind},
  396. {Name: "f11", Number: 11, Cardinality: pref.Repeated, Kind: pref.BytesKind},
  397. {Name: "f12", Number: 12, Cardinality: pref.Repeated, Kind: pref.StringKind},
  398. {Name: "f13", Number: 13, Cardinality: pref.Repeated, Kind: pref.StringKind},
  399. {Name: "f14", Number: 14, Cardinality: pref.Repeated, Kind: pref.BytesKind},
  400. {Name: "f15", Number: 15, Cardinality: pref.Repeated, Kind: pref.BytesKind},
  401. {Name: "f16", Number: 16, Cardinality: pref.Repeated, Kind: pref.StringKind},
  402. {Name: "f17", Number: 17, Cardinality: pref.Repeated, Kind: pref.StringKind},
  403. {Name: "f18", Number: 18, Cardinality: pref.Repeated, Kind: pref.BytesKind},
  404. {Name: "f19", Number: 19, Cardinality: pref.Repeated, Kind: pref.BytesKind},
  405. },
  406. }),
  407. func(pref.MessageType) pref.ProtoMessage {
  408. return new(ListScalars)
  409. },
  410. )}
  411. func (m *ListScalars) Type() pref.MessageType { return listScalarsType.Type }
  412. func (m *ListScalars) KnownFields() pref.KnownFields { return listScalarsType.KnownFieldsOf(m) }
  413. func (m *ListScalars) UnknownFields() pref.UnknownFields { return listScalarsType.UnknownFieldsOf(m) }
  414. func (m *ListScalars) Interface() pref.ProtoMessage { return m }
  415. func (m *ListScalars) ProtoReflect() pref.Message { return m }
  416. func (m *ListScalars) ProtoMutable() {}
  417. func TestListScalars(t *testing.T) {
  418. empty := &ListScalars{}
  419. emptyFS := empty.KnownFields()
  420. want := &ListScalars{
  421. Bools: []bool{true, false, true},
  422. Int32s: []int32{2, math.MinInt32, math.MaxInt32},
  423. Int64s: []int64{3, math.MinInt64, math.MaxInt64},
  424. Uint32s: []uint32{4, math.MaxUint32 / 2, math.MaxUint32},
  425. Uint64s: []uint64{5, math.MaxUint64 / 2, math.MaxUint64},
  426. Float32s: []float32{6, math.SmallestNonzeroFloat32, float32(math.NaN()), math.MaxFloat32},
  427. Float64s: []float64{7, math.SmallestNonzeroFloat64, float64(math.NaN()), math.MaxFloat64},
  428. Strings: []string{"8", "", "eight"},
  429. StringsA: [][]byte{[]byte("9"), nil, []byte("nine")},
  430. Bytes: [][]byte{[]byte("10"), nil, []byte("ten")},
  431. BytesA: []string{"11", "", "eleven"},
  432. MyStrings1: []MyString{"12", "", "twelve"},
  433. MyStrings2: []MyBytes{[]byte("13"), nil, []byte("thirteen")},
  434. MyBytes1: []MyBytes{[]byte("14"), nil, []byte("fourteen")},
  435. MyBytes2: []MyString{"15", "", "fifteen"},
  436. MyStrings3: ListStrings{"16", "", "sixteen"},
  437. MyStrings4: ListBytes{[]byte("17"), nil, []byte("seventeen")},
  438. MyBytes3: ListBytes{[]byte("18"), nil, []byte("eighteen")},
  439. MyBytes4: ListStrings{"19", "", "nineteen"},
  440. }
  441. wantFS := want.KnownFields()
  442. testMessage(t, nil, &ListScalars{}, messageOps{
  443. 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},
  444. 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)},
  445. 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)},
  446. listFields{
  447. 2: {
  448. lenList(0),
  449. appendList{V(int32(2)), V(int32(math.MinInt32)), V(int32(math.MaxInt32))},
  450. getList{0: V(int32(2)), 1: V(int32(math.MinInt32)), 2: V(int32(math.MaxInt32))},
  451. equalList{wantFS.Get(2).List()},
  452. },
  453. 4: {
  454. appendList{V(uint32(0)), V(uint32(0)), V(uint32(0))},
  455. setList{0: V(uint32(4)), 1: V(uint32(math.MaxUint32 / 2)), 2: V(uint32(math.MaxUint32))},
  456. lenList(3),
  457. },
  458. 6: {
  459. appendList{V(float32(6)), V(float32(math.SmallestNonzeroFloat32)), V(float32(math.NaN())), V(float32(math.MaxFloat32))},
  460. equalList{wantFS.Get(6).List()},
  461. },
  462. 8: {
  463. appendList{V(""), V(""), V(""), V(""), V(""), V("")},
  464. lenList(6),
  465. setList{0: V("8"), 2: V("eight")},
  466. truncList(3),
  467. equalList{wantFS.Get(8).List()},
  468. },
  469. 10: {
  470. appendList{V([]byte(nil)), V([]byte(nil))},
  471. setList{0: V([]byte("10"))},
  472. appendList{V([]byte("wrong"))},
  473. setList{2: V([]byte("ten"))},
  474. equalList{wantFS.Get(10).List()},
  475. },
  476. 12: {
  477. appendList{V("12"), V("wrong"), V("twelve")},
  478. setList{1: V("")},
  479. equalList{wantFS.Get(12).List()},
  480. },
  481. 14: {
  482. appendList{V([]byte("14")), V([]byte(nil)), V([]byte("fourteen"))},
  483. equalList{wantFS.Get(14).List()},
  484. },
  485. 16: {
  486. appendList{V("16"), V(""), V("sixteen"), V("extra")},
  487. truncList(3),
  488. equalList{wantFS.Get(16).List()},
  489. },
  490. 18: {
  491. appendList{V([]byte("18")), V([]byte(nil)), V([]byte("eighteen"))},
  492. equalList{wantFS.Get(18).List()},
  493. },
  494. },
  495. 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},
  496. equalMessage{want},
  497. clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19},
  498. equalMessage{empty},
  499. })
  500. // Test read-only operations on nil message.
  501. testMessage(t, nil, (*ListScalars)(nil), messageOps{
  502. 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},
  503. listFields{2: {lenList(0)}, 4: {lenList(0)}, 6: {lenList(0)}, 8: {lenList(0)}, 10: {lenList(0)}, 12: {lenList(0)}, 14: {lenList(0)}, 16: {lenList(0)}, 18: {lenList(0)}},
  504. })
  505. }
  506. type MapScalars struct {
  507. KeyBools map[bool]string `protobuf:"1"`
  508. KeyInt32s map[int32]string `protobuf:"2"`
  509. KeyInt64s map[int64]string `protobuf:"3"`
  510. KeyUint32s map[uint32]string `protobuf:"4"`
  511. KeyUint64s map[uint64]string `protobuf:"5"`
  512. KeyStrings map[string]string `protobuf:"6"`
  513. ValBools map[string]bool `protobuf:"7"`
  514. ValInt32s map[string]int32 `protobuf:"8"`
  515. ValInt64s map[string]int64 `protobuf:"9"`
  516. ValUint32s map[string]uint32 `protobuf:"10"`
  517. ValUint64s map[string]uint64 `protobuf:"11"`
  518. ValFloat32s map[string]float32 `protobuf:"12"`
  519. ValFloat64s map[string]float64 `protobuf:"13"`
  520. ValStrings map[string]string `protobuf:"14"`
  521. ValStringsA map[string][]byte `protobuf:"15"`
  522. ValBytes map[string][]byte `protobuf:"16"`
  523. ValBytesA map[string]string `protobuf:"17"`
  524. MyStrings1 map[MyString]MyString `protobuf:"18"`
  525. MyStrings2 map[MyString]MyBytes `protobuf:"19"`
  526. MyBytes1 map[MyString]MyBytes `protobuf:"20"`
  527. MyBytes2 map[MyString]MyString `protobuf:"21"`
  528. MyStrings3 MapStrings `protobuf:"22"`
  529. MyStrings4 MapBytes `protobuf:"23"`
  530. MyBytes3 MapBytes `protobuf:"24"`
  531. MyBytes4 MapStrings `protobuf:"25"`
  532. }
  533. func mustMakeMapEntry(n pref.FieldNumber, keyKind, valKind pref.Kind) ptype.Field {
  534. return ptype.Field{
  535. Name: pref.Name(fmt.Sprintf("f%d", n)),
  536. Number: n,
  537. Cardinality: pref.Repeated,
  538. Kind: pref.MessageKind,
  539. MessageType: mustMakeMessageDesc(ptype.StandaloneMessage{
  540. Syntax: pref.Proto2,
  541. FullName: pref.FullName(fmt.Sprintf("MapScalars.F%dEntry", n)),
  542. Fields: []ptype.Field{
  543. {Name: "key", Number: 1, Cardinality: pref.Optional, Kind: keyKind},
  544. {Name: "value", Number: 2, Cardinality: pref.Optional, Kind: valKind},
  545. },
  546. Options: &descriptorpb.MessageOptions{MapEntry: scalar.Bool(true)},
  547. }),
  548. }
  549. }
  550. var mapScalarsType = pimpl.MessageType{Type: ptype.GoMessage(
  551. mustMakeMessageDesc(ptype.StandaloneMessage{
  552. Syntax: pref.Proto2,
  553. FullName: "MapScalars",
  554. Fields: []ptype.Field{
  555. mustMakeMapEntry(1, pref.BoolKind, pref.StringKind),
  556. mustMakeMapEntry(2, pref.Int32Kind, pref.StringKind),
  557. mustMakeMapEntry(3, pref.Int64Kind, pref.StringKind),
  558. mustMakeMapEntry(4, pref.Uint32Kind, pref.StringKind),
  559. mustMakeMapEntry(5, pref.Uint64Kind, pref.StringKind),
  560. mustMakeMapEntry(6, pref.StringKind, pref.StringKind),
  561. mustMakeMapEntry(7, pref.StringKind, pref.BoolKind),
  562. mustMakeMapEntry(8, pref.StringKind, pref.Int32Kind),
  563. mustMakeMapEntry(9, pref.StringKind, pref.Int64Kind),
  564. mustMakeMapEntry(10, pref.StringKind, pref.Uint32Kind),
  565. mustMakeMapEntry(11, pref.StringKind, pref.Uint64Kind),
  566. mustMakeMapEntry(12, pref.StringKind, pref.FloatKind),
  567. mustMakeMapEntry(13, pref.StringKind, pref.DoubleKind),
  568. mustMakeMapEntry(14, pref.StringKind, pref.StringKind),
  569. mustMakeMapEntry(15, pref.StringKind, pref.StringKind),
  570. mustMakeMapEntry(16, pref.StringKind, pref.BytesKind),
  571. mustMakeMapEntry(17, pref.StringKind, pref.BytesKind),
  572. mustMakeMapEntry(18, pref.StringKind, pref.StringKind),
  573. mustMakeMapEntry(19, pref.StringKind, pref.StringKind),
  574. mustMakeMapEntry(20, pref.StringKind, pref.BytesKind),
  575. mustMakeMapEntry(21, pref.StringKind, pref.BytesKind),
  576. mustMakeMapEntry(22, pref.StringKind, pref.StringKind),
  577. mustMakeMapEntry(23, pref.StringKind, pref.StringKind),
  578. mustMakeMapEntry(24, pref.StringKind, pref.BytesKind),
  579. mustMakeMapEntry(25, pref.StringKind, pref.BytesKind),
  580. },
  581. }),
  582. func(pref.MessageType) pref.ProtoMessage {
  583. return new(MapScalars)
  584. },
  585. )}
  586. func (m *MapScalars) Type() pref.MessageType { return mapScalarsType.Type }
  587. func (m *MapScalars) KnownFields() pref.KnownFields { return mapScalarsType.KnownFieldsOf(m) }
  588. func (m *MapScalars) UnknownFields() pref.UnknownFields { return mapScalarsType.UnknownFieldsOf(m) }
  589. func (m *MapScalars) Interface() pref.ProtoMessage { return m }
  590. func (m *MapScalars) ProtoReflect() pref.Message { return m }
  591. func (m *MapScalars) ProtoMutable() {}
  592. func TestMapScalars(t *testing.T) {
  593. empty := &MapScalars{}
  594. emptyFS := empty.KnownFields()
  595. want := &MapScalars{
  596. KeyBools: map[bool]string{true: "true", false: "false"},
  597. KeyInt32s: map[int32]string{0: "zero", -1: "one", 2: "two"},
  598. KeyInt64s: map[int64]string{0: "zero", -10: "ten", 20: "twenty"},
  599. KeyUint32s: map[uint32]string{0: "zero", 1: "one", 2: "two"},
  600. KeyUint64s: map[uint64]string{0: "zero", 10: "ten", 20: "twenty"},
  601. KeyStrings: map[string]string{"": "", "foo": "bar"},
  602. ValBools: map[string]bool{"true": true, "false": false},
  603. ValInt32s: map[string]int32{"one": 1, "two": 2, "three": 3},
  604. ValInt64s: map[string]int64{"ten": 10, "twenty": -20, "thirty": 30},
  605. ValUint32s: map[string]uint32{"0x00": 0x00, "0xff": 0xff, "0xdead": 0xdead},
  606. ValUint64s: map[string]uint64{"0x00": 0x00, "0xff": 0xff, "0xdead": 0xdead},
  607. ValFloat32s: map[string]float32{"nan": float32(math.NaN()), "pi": float32(math.Pi)},
  608. ValFloat64s: map[string]float64{"nan": float64(math.NaN()), "pi": float64(math.Pi)},
  609. ValStrings: map[string]string{"s1": "s1", "s2": "s2"},
  610. ValStringsA: map[string][]byte{"s1": []byte("s1"), "s2": []byte("s2")},
  611. ValBytes: map[string][]byte{"s1": []byte("s1"), "s2": []byte("s2")},
  612. ValBytesA: map[string]string{"s1": "s1", "s2": "s2"},
  613. MyStrings1: map[MyString]MyString{"s1": "s1", "s2": "s2"},
  614. MyStrings2: map[MyString]MyBytes{"s1": []byte("s1"), "s2": []byte("s2")},
  615. MyBytes1: map[MyString]MyBytes{"s1": []byte("s1"), "s2": []byte("s2")},
  616. MyBytes2: map[MyString]MyString{"s1": "s1", "s2": "s2"},
  617. MyStrings3: MapStrings{"s1": "s1", "s2": "s2"},
  618. MyStrings4: MapBytes{"s1": []byte("s1"), "s2": []byte("s2")},
  619. MyBytes3: MapBytes{"s1": []byte("s1"), "s2": []byte("s2")},
  620. MyBytes4: MapStrings{"s1": "s1", "s2": "s2"},
  621. }
  622. wantFS := want.KnownFields()
  623. testMessage(t, nil, &MapScalars{}, messageOps{
  624. 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},
  625. 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)},
  626. 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)},
  627. mapFields{
  628. 2: {
  629. lenMap(0),
  630. hasMap{int32(0): false, int32(-1): false, int32(2): false},
  631. setMap{int32(0): V("zero")},
  632. lenMap(1),
  633. hasMap{int32(0): true, int32(-1): false, int32(2): false},
  634. setMap{int32(-1): V("one")},
  635. lenMap(2),
  636. hasMap{int32(0): true, int32(-1): true, int32(2): false},
  637. setMap{int32(2): V("two")},
  638. lenMap(3),
  639. hasMap{int32(0): true, int32(-1): true, int32(2): true},
  640. },
  641. 4: {
  642. setMap{uint32(0): V("zero"), uint32(1): V("one"), uint32(2): V("two")},
  643. equalMap{wantFS.Get(4).Map()},
  644. },
  645. 6: {
  646. clearMap{"noexist"},
  647. setMap{"foo": V("bar")},
  648. setMap{"": V("empty")},
  649. getMap{"": V("empty"), "foo": V("bar"), "noexist": V(nil)},
  650. setMap{"": V(""), "extra": V("extra")},
  651. clearMap{"extra", "noexist"},
  652. },
  653. 8: {
  654. equalMap{emptyFS.Get(8).Map()},
  655. setMap{"one": V(int32(1)), "two": V(int32(2)), "three": V(int32(3))},
  656. },
  657. 10: {
  658. setMap{"0x00": V(uint32(0x00)), "0xff": V(uint32(0xff)), "0xdead": V(uint32(0xdead))},
  659. lenMap(3),
  660. equalMap{wantFS.Get(10).Map()},
  661. getMap{"0x00": V(uint32(0x00)), "0xff": V(uint32(0xff)), "0xdead": V(uint32(0xdead)), "0xdeadbeef": V(nil)},
  662. },
  663. 12: {
  664. setMap{"nan": V(float32(math.NaN())), "pi": V(float32(math.Pi)), "e": V(float32(math.E))},
  665. clearMap{"e", "phi"},
  666. rangeMap{"nan": V(float32(math.NaN())), "pi": V(float32(math.Pi))},
  667. },
  668. 14: {
  669. equalMap{emptyFS.Get(14).Map()},
  670. setMap{"s1": V("s1"), "s2": V("s2")},
  671. },
  672. 16: {
  673. setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))},
  674. equalMap{wantFS.Get(16).Map()},
  675. },
  676. 18: {
  677. hasMap{"s1": false, "s2": false, "s3": false},
  678. setMap{"s1": V("s1"), "s2": V("s2")},
  679. hasMap{"s1": true, "s2": true, "s3": false},
  680. },
  681. 20: {
  682. equalMap{emptyFS.Get(20).Map()},
  683. setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))},
  684. },
  685. 22: {
  686. rangeMap{},
  687. setMap{"s1": V("s1"), "s2": V("s2")},
  688. rangeMap{"s1": V("s1"), "s2": V("s2")},
  689. lenMap(2),
  690. },
  691. 24: {
  692. setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))},
  693. equalMap{wantFS.Get(24).Map()},
  694. },
  695. },
  696. 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},
  697. equalMessage{want},
  698. clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25},
  699. equalMessage{empty},
  700. })
  701. // Test read-only operations on nil message.
  702. testMessage(t, nil, (*MapScalars)(nil), messageOps{
  703. 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},
  704. mapFields{2: {lenMap(0)}, 4: {lenMap(0)}, 6: {lenMap(0)}, 8: {lenMap(0)}, 10: {lenMap(0)}, 12: {lenMap(0)}, 14: {lenMap(0)}, 16: {lenMap(0)}, 18: {lenMap(0)}, 20: {lenMap(0)}, 22: {lenMap(0)}, 24: {lenMap(0)}},
  705. })
  706. }
  707. type OneofScalars struct {
  708. Union isOneofScalars_Union `protobuf_oneof:"union"`
  709. }
  710. var oneofScalarsType = pimpl.MessageType{Type: ptype.GoMessage(
  711. mustMakeMessageDesc(ptype.StandaloneMessage{
  712. Syntax: pref.Proto2,
  713. FullName: "OneofScalars",
  714. Fields: []ptype.Field{
  715. {Name: "f1", Number: 1, Cardinality: pref.Optional, Kind: pref.BoolKind, Default: V(bool(true)), OneofName: "union"},
  716. {Name: "f2", Number: 2, Cardinality: pref.Optional, Kind: pref.Int32Kind, Default: V(int32(2)), OneofName: "union"},
  717. {Name: "f3", Number: 3, Cardinality: pref.Optional, Kind: pref.Int64Kind, Default: V(int64(3)), OneofName: "union"},
  718. {Name: "f4", Number: 4, Cardinality: pref.Optional, Kind: pref.Uint32Kind, Default: V(uint32(4)), OneofName: "union"},
  719. {Name: "f5", Number: 5, Cardinality: pref.Optional, Kind: pref.Uint64Kind, Default: V(uint64(5)), OneofName: "union"},
  720. {Name: "f6", Number: 6, Cardinality: pref.Optional, Kind: pref.FloatKind, Default: V(float32(6)), OneofName: "union"},
  721. {Name: "f7", Number: 7, Cardinality: pref.Optional, Kind: pref.DoubleKind, Default: V(float64(7)), OneofName: "union"},
  722. {Name: "f8", Number: 8, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("8")), OneofName: "union"},
  723. {Name: "f9", Number: 9, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("9")), OneofName: "union"},
  724. {Name: "f10", Number: 10, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("10")), OneofName: "union"},
  725. {Name: "f11", Number: 11, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("11")), OneofName: "union"},
  726. {Name: "f12", Number: 12, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("12")), OneofName: "union"},
  727. {Name: "f13", Number: 13, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("13")), OneofName: "union"},
  728. },
  729. Oneofs: []ptype.Oneof{{Name: "union"}},
  730. }),
  731. func(pref.MessageType) pref.ProtoMessage {
  732. return new(OneofScalars)
  733. },
  734. )}
  735. func (m *OneofScalars) Type() pref.MessageType { return oneofScalarsType.Type }
  736. func (m *OneofScalars) KnownFields() pref.KnownFields { return oneofScalarsType.KnownFieldsOf(m) }
  737. func (m *OneofScalars) UnknownFields() pref.UnknownFields { return oneofScalarsType.UnknownFieldsOf(m) }
  738. func (m *OneofScalars) Interface() pref.ProtoMessage { return m }
  739. func (m *OneofScalars) ProtoReflect() pref.Message { return m }
  740. func (m *OneofScalars) ProtoMutable() {}
  741. func (*OneofScalars) XXX_OneofWrappers() []interface{} {
  742. return []interface{}{
  743. (*OneofScalars_Bool)(nil),
  744. (*OneofScalars_Int32)(nil),
  745. (*OneofScalars_Int64)(nil),
  746. (*OneofScalars_Uint32)(nil),
  747. (*OneofScalars_Uint64)(nil),
  748. (*OneofScalars_Float32)(nil),
  749. (*OneofScalars_Float64)(nil),
  750. (*OneofScalars_String)(nil),
  751. (*OneofScalars_StringA)(nil),
  752. (*OneofScalars_StringB)(nil),
  753. (*OneofScalars_Bytes)(nil),
  754. (*OneofScalars_BytesA)(nil),
  755. (*OneofScalars_BytesB)(nil),
  756. }
  757. }
  758. type (
  759. isOneofScalars_Union interface {
  760. isOneofScalars_Union()
  761. }
  762. OneofScalars_Bool struct {
  763. Bool bool `protobuf:"1"`
  764. }
  765. OneofScalars_Int32 struct {
  766. Int32 MyInt32 `protobuf:"2"`
  767. }
  768. OneofScalars_Int64 struct {
  769. Int64 int64 `protobuf:"3"`
  770. }
  771. OneofScalars_Uint32 struct {
  772. Uint32 MyUint32 `protobuf:"4"`
  773. }
  774. OneofScalars_Uint64 struct {
  775. Uint64 uint64 `protobuf:"5"`
  776. }
  777. OneofScalars_Float32 struct {
  778. Float32 MyFloat32 `protobuf:"6"`
  779. }
  780. OneofScalars_Float64 struct {
  781. Float64 float64 `protobuf:"7"`
  782. }
  783. OneofScalars_String struct {
  784. String string `protobuf:"8"`
  785. }
  786. OneofScalars_StringA struct {
  787. StringA []byte `protobuf:"9"`
  788. }
  789. OneofScalars_StringB struct {
  790. StringB MyString `protobuf:"10"`
  791. }
  792. OneofScalars_Bytes struct {
  793. Bytes []byte `protobuf:"11"`
  794. }
  795. OneofScalars_BytesA struct {
  796. BytesA string `protobuf:"12"`
  797. }
  798. OneofScalars_BytesB struct {
  799. BytesB MyBytes `protobuf:"13"`
  800. }
  801. )
  802. func (*OneofScalars_Bool) isOneofScalars_Union() {}
  803. func (*OneofScalars_Int32) isOneofScalars_Union() {}
  804. func (*OneofScalars_Int64) isOneofScalars_Union() {}
  805. func (*OneofScalars_Uint32) isOneofScalars_Union() {}
  806. func (*OneofScalars_Uint64) isOneofScalars_Union() {}
  807. func (*OneofScalars_Float32) isOneofScalars_Union() {}
  808. func (*OneofScalars_Float64) isOneofScalars_Union() {}
  809. func (*OneofScalars_String) isOneofScalars_Union() {}
  810. func (*OneofScalars_StringA) isOneofScalars_Union() {}
  811. func (*OneofScalars_StringB) isOneofScalars_Union() {}
  812. func (*OneofScalars_Bytes) isOneofScalars_Union() {}
  813. func (*OneofScalars_BytesA) isOneofScalars_Union() {}
  814. func (*OneofScalars_BytesB) isOneofScalars_Union() {}
  815. func TestOneofs(t *testing.T) {
  816. empty := &OneofScalars{}
  817. want1 := &OneofScalars{Union: &OneofScalars_Bool{true}}
  818. want2 := &OneofScalars{Union: &OneofScalars_Int32{20}}
  819. want3 := &OneofScalars{Union: &OneofScalars_Int64{30}}
  820. want4 := &OneofScalars{Union: &OneofScalars_Uint32{40}}
  821. want5 := &OneofScalars{Union: &OneofScalars_Uint64{50}}
  822. want6 := &OneofScalars{Union: &OneofScalars_Float32{60}}
  823. want7 := &OneofScalars{Union: &OneofScalars_Float64{70}}
  824. want8 := &OneofScalars{Union: &OneofScalars_String{string("80")}}
  825. want9 := &OneofScalars{Union: &OneofScalars_StringA{[]byte("90")}}
  826. want10 := &OneofScalars{Union: &OneofScalars_StringB{MyString("100")}}
  827. want11 := &OneofScalars{Union: &OneofScalars_Bytes{[]byte("110")}}
  828. want12 := &OneofScalars{Union: &OneofScalars_BytesA{string("120")}}
  829. want13 := &OneofScalars{Union: &OneofScalars_BytesB{MyBytes("130")}}
  830. testMessage(t, nil, &OneofScalars{}, messageOps{
  831. 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},
  832. 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"))},
  833. setFields{1: V(bool(true))}, hasFields{1: true}, equalMessage{want1},
  834. setFields{2: V(int32(20))}, hasFields{2: true}, equalMessage{want2},
  835. setFields{3: V(int64(30))}, hasFields{3: true}, equalMessage{want3},
  836. setFields{4: V(uint32(40))}, hasFields{4: true}, equalMessage{want4},
  837. setFields{5: V(uint64(50))}, hasFields{5: true}, equalMessage{want5},
  838. setFields{6: V(float32(60))}, hasFields{6: true}, equalMessage{want6},
  839. setFields{7: V(float64(70))}, hasFields{7: true}, equalMessage{want7},
  840. setFields{8: V(string("80"))}, hasFields{8: true}, equalMessage{want8},
  841. setFields{9: V(string("90"))}, hasFields{9: true}, equalMessage{want9},
  842. setFields{10: V(string("100"))}, hasFields{10: true}, equalMessage{want10},
  843. setFields{11: V([]byte("110"))}, hasFields{11: true}, equalMessage{want11},
  844. setFields{12: V([]byte("120"))}, hasFields{12: true}, equalMessage{want12},
  845. setFields{13: V([]byte("130"))}, hasFields{13: true}, equalMessage{want13},
  846. 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},
  847. 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"))},
  848. clearFields{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
  849. equalMessage{want13},
  850. clearFields{13},
  851. equalMessage{empty},
  852. })
  853. // Test read-only operations on nil message.
  854. testMessage(t, nil, (*OneofScalars)(nil), messageOps{
  855. 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},
  856. 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"))},
  857. })
  858. }
  859. type EnumProto2 int32
  860. var enumProto2Type = ptype.GoEnum(
  861. mustMakeEnumDesc(ptype.StandaloneEnum{
  862. Syntax: pref.Proto2,
  863. FullName: "EnumProto2",
  864. Values: []ptype.EnumValue{{Name: "DEAD", Number: 0xdead}, {Name: "BEEF", Number: 0xbeef}},
  865. }),
  866. func(_ pref.EnumType, n pref.EnumNumber) pref.ProtoEnum {
  867. return EnumProto2(n)
  868. },
  869. )
  870. func (e EnumProto2) Enum() *EnumProto2 { return &e }
  871. func (e EnumProto2) Type() pref.EnumType { return enumProto2Type }
  872. func (e EnumProto2) Number() pref.EnumNumber { return pref.EnumNumber(e) }
  873. func (e EnumProto2) ProtoReflect() pref.Enum { return e }
  874. type EnumProto3 int32
  875. var enumProto3Type = ptype.GoEnum(
  876. mustMakeEnumDesc(ptype.StandaloneEnum{
  877. Syntax: pref.Proto3,
  878. FullName: "EnumProto3",
  879. Values: []ptype.EnumValue{{Name: "ALPHA", Number: 0}, {Name: "BRAVO", Number: 1}},
  880. }),
  881. func(_ pref.EnumType, n pref.EnumNumber) pref.ProtoEnum {
  882. return EnumProto3(n)
  883. },
  884. )
  885. func (e EnumProto3) Enum() *EnumProto3 { return &e }
  886. func (e EnumProto3) Type() pref.EnumType { return enumProto3Type }
  887. func (e EnumProto3) Number() pref.EnumNumber { return pref.EnumNumber(e) }
  888. func (e EnumProto3) ProtoReflect() pref.Enum { return e }
  889. type EnumMessages struct {
  890. EnumP2 *EnumProto2 `protobuf:"1"`
  891. EnumP3 *EnumProto3 `protobuf:"2"`
  892. MessageLegacy *proto2_20180125.Message `protobuf:"3"`
  893. MessageCycle *EnumMessages `protobuf:"4"`
  894. EnumList []EnumProto2 `protobuf:"5"`
  895. MessageList []*ScalarProto2 `protobuf:"6"`
  896. EnumMap map[string]EnumProto3 `protobuf:"7"`
  897. MessageMap map[string]*ScalarProto3 `protobuf:"8"`
  898. Union isEnumMessages_Union `protobuf_oneof:"union"`
  899. }
  900. var enumMessagesType = pimpl.MessageType{Type: ptype.GoMessage(
  901. mustMakeMessageDesc(ptype.StandaloneMessage{
  902. Syntax: pref.Proto2,
  903. FullName: "EnumMessages",
  904. Fields: []ptype.Field{
  905. {Name: "f1", Number: 1, Cardinality: pref.Optional, Kind: pref.EnumKind, Default: V("BEEF"), EnumType: enumProto2Type},
  906. {Name: "f2", Number: 2, Cardinality: pref.Optional, Kind: pref.EnumKind, Default: V("BRAVO"), EnumType: enumProto3Type},
  907. {Name: "f3", Number: 3, Cardinality: pref.Optional, Kind: pref.MessageKind, MessageType: pimpl.Export{}.MessageOf(new(proto2_20180125.Message)).Type()},
  908. {Name: "f4", Number: 4, Cardinality: pref.Optional, Kind: pref.MessageKind, MessageType: ptype.PlaceholderMessage("EnumMessages")},
  909. {Name: "f5", Number: 5, Cardinality: pref.Repeated, Kind: pref.EnumKind, EnumType: enumProto2Type},
  910. {Name: "f6", Number: 6, Cardinality: pref.Repeated, Kind: pref.MessageKind, MessageType: scalarProto2Type.Type},
  911. {Name: "f7", Number: 7, Cardinality: pref.Repeated, Kind: pref.MessageKind, MessageType: enumMapDesc},
  912. {Name: "f8", Number: 8, Cardinality: pref.Repeated, Kind: pref.MessageKind, MessageType: messageMapDesc},
  913. {Name: "f9", Number: 9, Cardinality: pref.Optional, Kind: pref.EnumKind, Default: V("BEEF"), OneofName: "union", EnumType: enumProto2Type},
  914. {Name: "f10", Number: 10, Cardinality: pref.Optional, Kind: pref.EnumKind, Default: V("BRAVO"), OneofName: "union", EnumType: enumProto3Type},
  915. {Name: "f11", Number: 11, Cardinality: pref.Optional, Kind: pref.MessageKind, OneofName: "union", MessageType: scalarProto2Type.Type},
  916. {Name: "f12", Number: 12, Cardinality: pref.Optional, Kind: pref.MessageKind, OneofName: "union", MessageType: scalarProto3Type.Type},
  917. },
  918. Oneofs: []ptype.Oneof{{Name: "union"}},
  919. }),
  920. func(pref.MessageType) pref.ProtoMessage {
  921. return new(EnumMessages)
  922. },
  923. )}
  924. var enumMapDesc = mustMakeMessageDesc(ptype.StandaloneMessage{
  925. Syntax: pref.Proto2,
  926. FullName: "EnumMessages.F7Entry",
  927. Fields: []ptype.Field{
  928. {Name: "key", Number: 1, Cardinality: pref.Optional, Kind: pref.StringKind},
  929. {Name: "value", Number: 2, Cardinality: pref.Optional, Kind: pref.EnumKind, EnumType: enumProto3Type},
  930. },
  931. Options: &descriptorpb.MessageOptions{MapEntry: scalar.Bool(true)},
  932. })
  933. var messageMapDesc = mustMakeMessageDesc(ptype.StandaloneMessage{
  934. Syntax: pref.Proto2,
  935. FullName: "EnumMessages.F8Entry",
  936. Fields: []ptype.Field{
  937. {Name: "key", Number: 1, Cardinality: pref.Optional, Kind: pref.StringKind},
  938. {Name: "value", Number: 2, Cardinality: pref.Optional, Kind: pref.MessageKind, MessageType: scalarProto3Type.Type},
  939. },
  940. Options: &descriptorpb.MessageOptions{MapEntry: scalar.Bool(true)},
  941. })
  942. func (m *EnumMessages) Type() pref.MessageType { return enumMessagesType.Type }
  943. func (m *EnumMessages) KnownFields() pref.KnownFields { return enumMessagesType.KnownFieldsOf(m) }
  944. func (m *EnumMessages) UnknownFields() pref.UnknownFields { return enumMessagesType.UnknownFieldsOf(m) }
  945. func (m *EnumMessages) Interface() pref.ProtoMessage { return m }
  946. func (m *EnumMessages) ProtoReflect() pref.Message { return m }
  947. func (m *EnumMessages) ProtoMutable() {}
  948. func (*EnumMessages) XXX_OneofWrappers() []interface{} {
  949. return []interface{}{
  950. (*EnumMessages_OneofE2)(nil),
  951. (*EnumMessages_OneofE3)(nil),
  952. (*EnumMessages_OneofM2)(nil),
  953. (*EnumMessages_OneofM3)(nil),
  954. }
  955. }
  956. type (
  957. isEnumMessages_Union interface {
  958. isEnumMessages_Union()
  959. }
  960. EnumMessages_OneofE2 struct {
  961. OneofE2 EnumProto2 `protobuf:"9"`
  962. }
  963. EnumMessages_OneofE3 struct {
  964. OneofE3 EnumProto3 `protobuf:"10"`
  965. }
  966. EnumMessages_OneofM2 struct {
  967. OneofM2 *ScalarProto2 `protobuf:"11"`
  968. }
  969. EnumMessages_OneofM3 struct {
  970. OneofM3 *ScalarProto3 `protobuf:"12"`
  971. }
  972. )
  973. func (*EnumMessages_OneofE2) isEnumMessages_Union() {}
  974. func (*EnumMessages_OneofE3) isEnumMessages_Union() {}
  975. func (*EnumMessages_OneofM2) isEnumMessages_Union() {}
  976. func (*EnumMessages_OneofM3) isEnumMessages_Union() {}
  977. func TestEnumMessages(t *testing.T) {
  978. wantL := pimpl.Export{}.MessageOf(&proto2_20180125.Message{OptionalFloat: scalar.Float32(math.E)})
  979. wantM := &EnumMessages{EnumP2: EnumProto2(1234).Enum()}
  980. wantM2a := &ScalarProto2{Float32: scalar.Float32(math.Pi)}
  981. wantM2b := &ScalarProto2{Float32: scalar.Float32(math.Phi)}
  982. wantM3a := &ScalarProto3{Float32: math.Pi}
  983. wantM3b := &ScalarProto3{Float32: math.Ln2}
  984. wantList5 := (&EnumMessages{EnumList: []EnumProto2{333, 222}}).KnownFields().Get(5)
  985. wantList6 := (&EnumMessages{MessageList: []*ScalarProto2{wantM2a, wantM2b}}).KnownFields().Get(6)
  986. wantMap7 := (&EnumMessages{EnumMap: map[string]EnumProto3{"one": 1, "two": 2}}).KnownFields().Get(7)
  987. wantMap8 := (&EnumMessages{MessageMap: map[string]*ScalarProto3{"pi": wantM3a, "ln2": wantM3b}}).KnownFields().Get(8)
  988. testMessage(t, nil, &EnumMessages{}, messageOps{
  989. 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},
  990. getFields{1: VE(0xbeef), 2: VE(1), 3: V(nil), 4: V(nil), 9: VE(0xbeef), 10: VE(1)},
  991. // Test singular enums.
  992. setFields{1: VE(0xdead), 2: VE(0)},
  993. getFields{1: VE(0xdead), 2: VE(0)},
  994. hasFields{1: true, 2: true},
  995. // Test singular messages.
  996. messageFields{3: messageOps{setFields{109: V(float32(math.E))}}},
  997. messageFields{4: messageOps{setFields{1: VE(1234)}}},
  998. getFields{3: V(wantL), 4: V(wantM)},
  999. clearFields{3, 4},
  1000. hasFields{3: false, 4: false},
  1001. setFields{3: V(wantL), 4: V(wantM)},
  1002. hasFields{3: true, 4: true},
  1003. // Test list of enums and messages.
  1004. listFields{
  1005. 5: listOps{
  1006. appendList{VE(111), VE(222)},
  1007. setList{0: VE(333)},
  1008. getList{0: VE(333), 1: VE(222)},
  1009. lenList(2),
  1010. },
  1011. 6: listOps{
  1012. appendMessageList{setFields{4: V(uint32(1e6))}},
  1013. appendMessageList{setFields{6: V(float32(math.Phi))}},
  1014. setList{0: V(wantM2a)},
  1015. getList{0: V(wantM2a), 1: V(wantM2b)},
  1016. },
  1017. },
  1018. getFields{5: wantList5, 6: wantList6},
  1019. hasFields{5: true, 6: true},
  1020. listFields{5: listOps{truncList(0)}},
  1021. hasFields{5: false, 6: true},
  1022. // Test maps of enums and messages.
  1023. mapFields{
  1024. 7: mapOps{
  1025. setMap{"one": VE(1), "two": VE(2)},
  1026. hasMap{"one": true, "two": true, "three": false},
  1027. lenMap(2),
  1028. },
  1029. 8: mapOps{
  1030. messageMap{"pi": messageOps{setFields{6: V(float32(math.Pi))}}},
  1031. setMap{"ln2": V(wantM3b)},
  1032. getMap{"pi": V(wantM3a), "ln2": V(wantM3b), "none": V(nil)},
  1033. lenMap(2),
  1034. },
  1035. },
  1036. getFields{7: wantMap7, 8: wantMap8},
  1037. hasFields{7: true, 8: true},
  1038. mapFields{8: mapOps{clearMap{"pi", "ln2", "none"}}},
  1039. hasFields{7: true, 8: false},
  1040. // Test oneofs of enums and messages.
  1041. setFields{9: VE(0xdead)},
  1042. hasFields{1: true, 2: true, 9: true, 10: false, 11: false, 12: false},
  1043. setFields{10: VE(0)},
  1044. hasFields{1: true, 2: true, 9: false, 10: true, 11: false, 12: false},
  1045. messageFields{11: messageOps{setFields{6: V(float32(math.Pi))}}},
  1046. getFields{11: V(wantM2a)},
  1047. hasFields{1: true, 2: true, 9: false, 10: false, 11: true, 12: false},
  1048. messageFields{12: messageOps{setFields{6: V(float32(math.Pi))}}},
  1049. getFields{12: V(wantM3a)},
  1050. hasFields{1: true, 2: true, 9: false, 10: false, 11: false, 12: true},
  1051. // Check entire message.
  1052. rangeFields{1: VE(0xdead), 2: VE(0), 3: V(wantL), 4: V(wantM), 6: wantList6, 7: wantMap7, 12: V(wantM3a)},
  1053. equalMessage{&EnumMessages{
  1054. EnumP2: EnumProto2(0xdead).Enum(),
  1055. EnumP3: EnumProto3(0).Enum(),
  1056. MessageLegacy: &proto2_20180125.Message{OptionalFloat: scalar.Float32(math.E)},
  1057. MessageCycle: wantM,
  1058. MessageList: []*ScalarProto2{wantM2a, wantM2b},
  1059. EnumMap: map[string]EnumProto3{"one": 1, "two": 2},
  1060. Union: &EnumMessages_OneofM3{wantM3a},
  1061. }},
  1062. clearFields{1, 2, 3, 4, 6, 7, 12},
  1063. equalMessage{&EnumMessages{}},
  1064. })
  1065. // Test read-only operations on nil message.
  1066. testMessage(t, nil, (*EnumMessages)(nil), messageOps{
  1067. 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},
  1068. getFields{1: VE(0xbeef), 2: VE(1), 3: V(nil), 4: V(nil), 9: VE(0xbeef), 10: VE(1), 11: V(nil), 12: V(nil)},
  1069. listFields{5: {lenList(0)}, 6: {lenList(0)}},
  1070. mapFields{7: {lenMap(0)}, 8: {lenMap(0)}},
  1071. })
  1072. }
  1073. var cmpOpts = cmp.Options{
  1074. cmp.Comparer(func(x, y *proto2_20180125.Message) bool {
  1075. return protoV1.Equal(x, y)
  1076. }),
  1077. cmp.Transformer("UnwrapValue", func(pv pref.Value) interface{} {
  1078. return pv.Interface()
  1079. }),
  1080. cmp.Transformer("UnwrapGeneric", func(x pvalue.Unwrapper) interface{} {
  1081. return x.ProtoUnwrap()
  1082. }),
  1083. cmpopts.EquateNaNs(),
  1084. cmpopts.EquateEmpty(),
  1085. }
  1086. func testMessage(t *testing.T, p path, m pref.Message, tt messageOps) {
  1087. fs := m.KnownFields()
  1088. for i, op := range tt {
  1089. p.Push(i)
  1090. switch op := op.(type) {
  1091. case equalMessage:
  1092. if diff := cmp.Diff(op.Message, m, cmpOpts); diff != "" {
  1093. t.Errorf("operation %v, message mismatch (-want, +got):\n%s", p, diff)
  1094. }
  1095. case hasFields:
  1096. got := map[pref.FieldNumber]bool{}
  1097. want := map[pref.FieldNumber]bool(op)
  1098. for n := range want {
  1099. got[n] = fs.Has(n)
  1100. }
  1101. if diff := cmp.Diff(want, got); diff != "" {
  1102. t.Errorf("operation %v, KnownFields.Has mismatch (-want, +got):\n%s", p, diff)
  1103. }
  1104. case getFields:
  1105. got := map[pref.FieldNumber]pref.Value{}
  1106. want := map[pref.FieldNumber]pref.Value(op)
  1107. for n := range want {
  1108. got[n] = fs.Get(n)
  1109. }
  1110. if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
  1111. t.Errorf("operation %v, KnownFields.Get mismatch (-want, +got):\n%s", p, diff)
  1112. }
  1113. case setFields:
  1114. for n, v := range op {
  1115. fs.Set(n, v)
  1116. }
  1117. case clearFields:
  1118. for _, n := range op {
  1119. fs.Clear(n)
  1120. }
  1121. case messageFields:
  1122. for n, tt := range op {
  1123. p.Push(int(n))
  1124. testMessage(t, p, fs.Mutable(n).(pref.Message), tt)
  1125. p.Pop()
  1126. }
  1127. case listFields:
  1128. for n, tt := range op {
  1129. p.Push(int(n))
  1130. testLists(t, p, fs.Get(n).List(), tt)
  1131. p.Pop()
  1132. }
  1133. case mapFields:
  1134. for n, tt := range op {
  1135. p.Push(int(n))
  1136. testMaps(t, p, fs.Get(n).Map(), tt)
  1137. p.Pop()
  1138. }
  1139. case rangeFields:
  1140. got := map[pref.FieldNumber]pref.Value{}
  1141. want := map[pref.FieldNumber]pref.Value(op)
  1142. fs.Range(func(n pref.FieldNumber, v pref.Value) bool {
  1143. got[n] = v
  1144. return true
  1145. })
  1146. if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
  1147. t.Errorf("operation %v, KnownFields.Range mismatch (-want, +got):\n%s", p, diff)
  1148. }
  1149. default:
  1150. t.Fatalf("operation %v, invalid operation: %T", p, op)
  1151. }
  1152. p.Pop()
  1153. }
  1154. }
  1155. func testLists(t *testing.T, p path, v pref.List, tt listOps) {
  1156. for i, op := range tt {
  1157. p.Push(i)
  1158. switch op := op.(type) {
  1159. case equalList:
  1160. if diff := cmp.Diff(op.List, v, cmpOpts); diff != "" {
  1161. t.Errorf("operation %v, list mismatch (-want, +got):\n%s", p, diff)
  1162. }
  1163. case lenList:
  1164. if got, want := v.Len(), int(op); got != want {
  1165. t.Errorf("operation %v, List.Len = %d, want %d", p, got, want)
  1166. }
  1167. case getList:
  1168. got := map[int]pref.Value{}
  1169. want := map[int]pref.Value(op)
  1170. for n := range want {
  1171. got[n] = v.Get(n)
  1172. }
  1173. if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
  1174. t.Errorf("operation %v, List.Get mismatch (-want, +got):\n%s", p, diff)
  1175. }
  1176. case setList:
  1177. for n, e := range op {
  1178. v.Set(n, e)
  1179. }
  1180. case appendList:
  1181. for _, e := range op {
  1182. v.Append(e)
  1183. }
  1184. case appendMessageList:
  1185. testMessage(t, p, v.MutableAppend().(pref.Message), messageOps(op))
  1186. case truncList:
  1187. v.Truncate(int(op))
  1188. default:
  1189. t.Fatalf("operation %v, invalid operation: %T", p, op)
  1190. }
  1191. p.Pop()
  1192. }
  1193. }
  1194. func testMaps(t *testing.T, p path, m pref.Map, tt mapOps) {
  1195. for i, op := range tt {
  1196. p.Push(i)
  1197. switch op := op.(type) {
  1198. case equalMap:
  1199. if diff := cmp.Diff(op.Map, m, cmpOpts); diff != "" {
  1200. t.Errorf("operation %v, map mismatch (-want, +got):\n%s", p, diff)
  1201. }
  1202. case lenMap:
  1203. if got, want := m.Len(), int(op); got != want {
  1204. t.Errorf("operation %v, Map.Len = %d, want %d", p, got, want)
  1205. }
  1206. case hasMap:
  1207. got := map[interface{}]bool{}
  1208. want := map[interface{}]bool(op)
  1209. for k := range want {
  1210. got[k] = m.Has(V(k).MapKey())
  1211. }
  1212. if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
  1213. t.Errorf("operation %v, Map.Has mismatch (-want, +got):\n%s", p, diff)
  1214. }
  1215. case getMap:
  1216. got := map[interface{}]pref.Value{}
  1217. want := map[interface{}]pref.Value(op)
  1218. for k := range want {
  1219. got[k] = m.Get(V(k).MapKey())
  1220. }
  1221. if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
  1222. t.Errorf("operation %v, Map.Get mismatch (-want, +got):\n%s", p, diff)
  1223. }
  1224. case setMap:
  1225. for k, v := range op {
  1226. m.Set(V(k).MapKey(), v)
  1227. }
  1228. case clearMap:
  1229. for _, k := range op {
  1230. m.Clear(V(k).MapKey())
  1231. }
  1232. case messageMap:
  1233. for k, tt := range op {
  1234. testMessage(t, p, m.Mutable(V(k).MapKey()).(pref.Message), tt)
  1235. }
  1236. case rangeMap:
  1237. got := map[interface{}]pref.Value{}
  1238. want := map[interface{}]pref.Value(op)
  1239. m.Range(func(k pref.MapKey, v pref.Value) bool {
  1240. got[k.Interface()] = v
  1241. return true
  1242. })
  1243. if diff := cmp.Diff(want, got, cmpOpts); diff != "" {
  1244. t.Errorf("operation %v, Map.Range mismatch (-want, +got):\n%s", p, diff)
  1245. }
  1246. default:
  1247. t.Fatalf("operation %v, invalid operation: %T", p, op)
  1248. }
  1249. p.Pop()
  1250. }
  1251. }
  1252. type path []int
  1253. func (p *path) Push(i int) { *p = append(*p, i) }
  1254. func (p *path) Pop() { *p = (*p)[:len(*p)-1] }
  1255. func (p path) String() string {
  1256. var ss []string
  1257. for _, i := range p {
  1258. ss = append(ss, fmt.Sprint(i))
  1259. }
  1260. return strings.Join(ss, ".")
  1261. }