message_test.go 53 KB

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