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