message_test.go 53 KB

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