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