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