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 | |
| 5 | package impl |
| 6 | |
| 7 | import ( |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 8 | "fmt" |
| 9 | "math" |
| 10 | "strings" |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 11 | "testing" |
| 12 | |
| 13 | "github.com/google/go-cmp/cmp" |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 14 | "github.com/google/go-cmp/cmp/cmpopts" |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 15 | |
Damien Neil | 204f1c0 | 2018-10-23 15:03:38 -0700 | [diff] [blame] | 16 | protoV1 "github.com/golang/protobuf/proto" |
| 17 | descriptorV1 "github.com/golang/protobuf/protoc-gen-go/descriptor" |
Joe Tsai | 01ab296 | 2018-09-21 17:44:00 -0700 | [diff] [blame] | 18 | pref "github.com/golang/protobuf/v2/reflect/protoreflect" |
| 19 | ptype "github.com/golang/protobuf/v2/reflect/prototype" |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 20 | ) |
| 21 | |
Joe Tsai | c6b7561 | 2018-09-13 14:24:37 -0700 | [diff] [blame] | 22 | func mustMakeMessageDesc(t ptype.StandaloneMessage) pref.MessageDescriptor { |
| 23 | md, err := ptype.NewMessage(&t) |
| 24 | if err != nil { |
| 25 | panic(err) |
| 26 | } |
| 27 | return md |
| 28 | } |
| 29 | |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 30 | var V = pref.ValueOf |
| 31 | |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 32 | type ( |
| 33 | MyBool bool |
| 34 | MyInt32 int32 |
| 35 | MyInt64 int64 |
| 36 | MyUint32 uint32 |
| 37 | MyUint64 uint64 |
| 38 | MyFloat32 float32 |
| 39 | MyFloat64 float64 |
| 40 | MyString string |
| 41 | MyBytes []byte |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 42 | |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame^] | 43 | ListStrings []MyString |
| 44 | ListBytes []MyBytes |
Joe Tsai | bbfaeb7 | 2018-10-17 00:27:21 +0000 | [diff] [blame] | 45 | |
| 46 | MapStrings map[MyString]MyString |
| 47 | MapBytes map[MyString]MyBytes |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 48 | ) |
| 49 | |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame^] | 50 | // List of test operations to perform on messages, lists, or maps. |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 51 | type ( |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame^] | 52 | messageOp interface{} // equalMessage | hasFields | getFields | setFields | clearFields | listFields | mapFields |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 53 | messageOps []messageOp |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 54 | |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame^] | 55 | listOp interface{} // equalList | lenList | getList | setList | appendList | truncList |
| 56 | listOps []listOp |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 57 | |
Joe Tsai | bbfaeb7 | 2018-10-17 00:27:21 +0000 | [diff] [blame] | 58 | mapOp interface{} // equalMap | lenMap | hasMap | getMap | setMap | clearMap | rangeMap |
| 59 | mapOps []mapOp |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 60 | ) |
| 61 | |
| 62 | // Test operations performed on a message. |
| 63 | type ( |
| 64 | equalMessage pref.Message |
| 65 | hasFields map[pref.FieldNumber]bool |
| 66 | getFields map[pref.FieldNumber]pref.Value |
| 67 | setFields map[pref.FieldNumber]pref.Value |
| 68 | clearFields map[pref.FieldNumber]bool |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame^] | 69 | listFields map[pref.FieldNumber]listOps |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 70 | mapFields map[pref.FieldNumber]mapOps |
| 71 | messageFields map[pref.FieldNumber]messageOps |
| 72 | // TODO: Mutable, Range, ExtensionTypes |
| 73 | ) |
| 74 | |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame^] | 75 | // Test operations performed on a list. |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 76 | type ( |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame^] | 77 | equalList pref.List |
| 78 | lenList int |
| 79 | getList map[int]pref.Value |
| 80 | setList map[int]pref.Value |
| 81 | appendList []pref.Value |
| 82 | truncList int |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 83 | // TODO: Mutable, MutableAppend |
| 84 | ) |
| 85 | |
Joe Tsai | bbfaeb7 | 2018-10-17 00:27:21 +0000 | [diff] [blame] | 86 | // Test operations performed on a map. |
| 87 | type ( |
| 88 | equalMap pref.Map |
| 89 | lenMap int |
| 90 | hasMap map[interface{}]bool |
| 91 | getMap map[interface{}]pref.Value |
| 92 | setMap map[interface{}]pref.Value |
| 93 | clearMap map[interface{}]bool |
| 94 | rangeMap map[interface{}]pref.Value |
Joe Tsai | 3903b21 | 2018-10-17 11:54:32 -0700 | [diff] [blame] | 95 | // TODO: Mutable |
Joe Tsai | bbfaeb7 | 2018-10-17 00:27:21 +0000 | [diff] [blame] | 96 | ) |
| 97 | |
Joe Tsai | ce6edd3 | 2018-10-19 16:27:46 -0700 | [diff] [blame] | 98 | type ScalarProto2 struct { |
| 99 | Bool *bool `protobuf:"1"` |
| 100 | Int32 *int32 `protobuf:"2"` |
| 101 | Int64 *int64 `protobuf:"3"` |
| 102 | Uint32 *uint32 `protobuf:"4"` |
| 103 | Uint64 *uint64 `protobuf:"5"` |
| 104 | Float32 *float32 `protobuf:"6"` |
| 105 | Float64 *float64 `protobuf:"7"` |
| 106 | String *string `protobuf:"8"` |
| 107 | StringA []byte `protobuf:"9"` |
| 108 | Bytes []byte `protobuf:"10"` |
| 109 | BytesA *string `protobuf:"11"` |
| 110 | |
| 111 | MyBool *MyBool `protobuf:"12"` |
| 112 | MyInt32 *MyInt32 `protobuf:"13"` |
| 113 | MyInt64 *MyInt64 `protobuf:"14"` |
| 114 | MyUint32 *MyUint32 `protobuf:"15"` |
| 115 | MyUint64 *MyUint64 `protobuf:"16"` |
| 116 | MyFloat32 *MyFloat32 `protobuf:"17"` |
| 117 | MyFloat64 *MyFloat64 `protobuf:"18"` |
| 118 | MyString *MyString `protobuf:"19"` |
| 119 | MyStringA MyBytes `protobuf:"20"` |
| 120 | MyBytes MyBytes `protobuf:"21"` |
| 121 | MyBytesA *MyString `protobuf:"22"` |
| 122 | } |
| 123 | |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 124 | var scalarProto2Type = MessageType{Type: ptype.GoMessage( |
| 125 | mustMakeMessageDesc(ptype.StandaloneMessage{ |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 126 | Syntax: pref.Proto2, |
| 127 | FullName: "ScalarProto2", |
| 128 | Fields: []ptype.Field{ |
| 129 | {Name: "f1", Number: 1, Cardinality: pref.Optional, Kind: pref.BoolKind, Default: V(bool(true))}, |
| 130 | {Name: "f2", Number: 2, Cardinality: pref.Optional, Kind: pref.Int32Kind, Default: V(int32(2))}, |
| 131 | {Name: "f3", Number: 3, Cardinality: pref.Optional, Kind: pref.Int64Kind, Default: V(int64(3))}, |
| 132 | {Name: "f4", Number: 4, Cardinality: pref.Optional, Kind: pref.Uint32Kind, Default: V(uint32(4))}, |
| 133 | {Name: "f5", Number: 5, Cardinality: pref.Optional, Kind: pref.Uint64Kind, Default: V(uint64(5))}, |
| 134 | {Name: "f6", Number: 6, Cardinality: pref.Optional, Kind: pref.FloatKind, Default: V(float32(6))}, |
| 135 | {Name: "f7", Number: 7, Cardinality: pref.Optional, Kind: pref.DoubleKind, Default: V(float64(7))}, |
| 136 | {Name: "f8", Number: 8, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("8"))}, |
| 137 | {Name: "f9", Number: 9, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("9"))}, |
| 138 | {Name: "f10", Number: 10, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("10"))}, |
| 139 | {Name: "f11", Number: 11, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("11"))}, |
| 140 | |
| 141 | {Name: "f12", Number: 12, Cardinality: pref.Optional, Kind: pref.BoolKind, Default: V(bool(true))}, |
| 142 | {Name: "f13", Number: 13, Cardinality: pref.Optional, Kind: pref.Int32Kind, Default: V(int32(13))}, |
| 143 | {Name: "f14", Number: 14, Cardinality: pref.Optional, Kind: pref.Int64Kind, Default: V(int64(14))}, |
| 144 | {Name: "f15", Number: 15, Cardinality: pref.Optional, Kind: pref.Uint32Kind, Default: V(uint32(15))}, |
| 145 | {Name: "f16", Number: 16, Cardinality: pref.Optional, Kind: pref.Uint64Kind, Default: V(uint64(16))}, |
| 146 | {Name: "f17", Number: 17, Cardinality: pref.Optional, Kind: pref.FloatKind, Default: V(float32(17))}, |
| 147 | {Name: "f18", Number: 18, Cardinality: pref.Optional, Kind: pref.DoubleKind, Default: V(float64(18))}, |
| 148 | {Name: "f19", Number: 19, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("19"))}, |
| 149 | {Name: "f20", Number: 20, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("20"))}, |
| 150 | {Name: "f21", Number: 21, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("21"))}, |
| 151 | {Name: "f22", Number: 22, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("22"))}, |
| 152 | }, |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 153 | }), |
| 154 | func(pref.MessageType) pref.ProtoMessage { |
| 155 | return new(ScalarProto2) |
| 156 | }, |
| 157 | )} |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 158 | |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 159 | func (m *ScalarProto2) Type() pref.MessageType { return scalarProto2Type.Type } |
| 160 | func (m *ScalarProto2) KnownFields() pref.KnownFields { return scalarProto2Type.KnownFieldsOf(m) } |
| 161 | func (m *ScalarProto2) UnknownFields() pref.UnknownFields { return scalarProto2Type.UnknownFieldsOf(m) } |
| 162 | func (m *ScalarProto2) Interface() pref.ProtoMessage { return m } |
| 163 | func (m *ScalarProto2) ProtoReflect() pref.Message { return m } |
| 164 | func (m *ScalarProto2) ProtoMutable() {} |
| 165 | |
| 166 | func TestScalarProto2(t *testing.T) { |
| 167 | testMessage(t, nil, &ScalarProto2{}, messageOps{ |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 168 | hasFields{ |
| 169 | 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false, |
| 170 | 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false, |
| 171 | }, |
| 172 | getFields{ |
| 173 | 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")), |
| 174 | 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")), |
| 175 | }, |
| 176 | setFields{ |
| 177 | 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)), |
| 178 | 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)), |
| 179 | }, |
| 180 | hasFields{ |
| 181 | 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true, |
| 182 | 12: true, 13: true, 14: true, 15: true, 16: true, 17: true, 18: true, 19: true, 20: true, 21: true, 22: true, |
| 183 | }, |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 184 | equalMessage(&ScalarProto2{ |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 185 | new(bool), new(int32), new(int64), new(uint32), new(uint64), new(float32), new(float64), new(string), []byte{}, []byte{}, new(string), |
| 186 | new(MyBool), new(MyInt32), new(MyInt64), new(MyUint32), new(MyUint64), new(MyFloat32), new(MyFloat64), new(MyString), MyBytes{}, MyBytes{}, new(MyString), |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 187 | }), |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 188 | clearFields{ |
| 189 | 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true, |
| 190 | 12: true, 13: true, 14: true, 15: true, 16: true, 17: true, 18: true, 19: true, 20: true, 21: true, 22: true, |
| 191 | }, |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 192 | equalMessage(&ScalarProto2{}), |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 193 | }) |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Joe Tsai | ce6edd3 | 2018-10-19 16:27:46 -0700 | [diff] [blame] | 196 | type ScalarProto3 struct { |
| 197 | Bool bool `protobuf:"1"` |
| 198 | Int32 int32 `protobuf:"2"` |
| 199 | Int64 int64 `protobuf:"3"` |
| 200 | Uint32 uint32 `protobuf:"4"` |
| 201 | Uint64 uint64 `protobuf:"5"` |
| 202 | Float32 float32 `protobuf:"6"` |
| 203 | Float64 float64 `protobuf:"7"` |
| 204 | String string `protobuf:"8"` |
| 205 | StringA []byte `protobuf:"9"` |
| 206 | Bytes []byte `protobuf:"10"` |
| 207 | BytesA string `protobuf:"11"` |
| 208 | |
| 209 | MyBool MyBool `protobuf:"12"` |
| 210 | MyInt32 MyInt32 `protobuf:"13"` |
| 211 | MyInt64 MyInt64 `protobuf:"14"` |
| 212 | MyUint32 MyUint32 `protobuf:"15"` |
| 213 | MyUint64 MyUint64 `protobuf:"16"` |
| 214 | MyFloat32 MyFloat32 `protobuf:"17"` |
| 215 | MyFloat64 MyFloat64 `protobuf:"18"` |
| 216 | MyString MyString `protobuf:"19"` |
| 217 | MyStringA MyBytes `protobuf:"20"` |
| 218 | MyBytes MyBytes `protobuf:"21"` |
| 219 | MyBytesA MyString `protobuf:"22"` |
| 220 | } |
| 221 | |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 222 | var scalarProto3Type = MessageType{Type: ptype.GoMessage( |
| 223 | mustMakeMessageDesc(ptype.StandaloneMessage{ |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 224 | Syntax: pref.Proto3, |
| 225 | FullName: "ScalarProto3", |
| 226 | Fields: []ptype.Field{ |
| 227 | {Name: "f1", Number: 1, Cardinality: pref.Optional, Kind: pref.BoolKind}, |
| 228 | {Name: "f2", Number: 2, Cardinality: pref.Optional, Kind: pref.Int32Kind}, |
| 229 | {Name: "f3", Number: 3, Cardinality: pref.Optional, Kind: pref.Int64Kind}, |
| 230 | {Name: "f4", Number: 4, Cardinality: pref.Optional, Kind: pref.Uint32Kind}, |
| 231 | {Name: "f5", Number: 5, Cardinality: pref.Optional, Kind: pref.Uint64Kind}, |
| 232 | {Name: "f6", Number: 6, Cardinality: pref.Optional, Kind: pref.FloatKind}, |
| 233 | {Name: "f7", Number: 7, Cardinality: pref.Optional, Kind: pref.DoubleKind}, |
| 234 | {Name: "f8", Number: 8, Cardinality: pref.Optional, Kind: pref.StringKind}, |
| 235 | {Name: "f9", Number: 9, Cardinality: pref.Optional, Kind: pref.StringKind}, |
| 236 | {Name: "f10", Number: 10, Cardinality: pref.Optional, Kind: pref.BytesKind}, |
| 237 | {Name: "f11", Number: 11, Cardinality: pref.Optional, Kind: pref.BytesKind}, |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 238 | |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 239 | {Name: "f12", Number: 12, Cardinality: pref.Optional, Kind: pref.BoolKind}, |
| 240 | {Name: "f13", Number: 13, Cardinality: pref.Optional, Kind: pref.Int32Kind}, |
| 241 | {Name: "f14", Number: 14, Cardinality: pref.Optional, Kind: pref.Int64Kind}, |
| 242 | {Name: "f15", Number: 15, Cardinality: pref.Optional, Kind: pref.Uint32Kind}, |
| 243 | {Name: "f16", Number: 16, Cardinality: pref.Optional, Kind: pref.Uint64Kind}, |
| 244 | {Name: "f17", Number: 17, Cardinality: pref.Optional, Kind: pref.FloatKind}, |
| 245 | {Name: "f18", Number: 18, Cardinality: pref.Optional, Kind: pref.DoubleKind}, |
| 246 | {Name: "f19", Number: 19, Cardinality: pref.Optional, Kind: pref.StringKind}, |
| 247 | {Name: "f20", Number: 20, Cardinality: pref.Optional, Kind: pref.StringKind}, |
| 248 | {Name: "f21", Number: 21, Cardinality: pref.Optional, Kind: pref.BytesKind}, |
| 249 | {Name: "f22", Number: 22, Cardinality: pref.Optional, Kind: pref.BytesKind}, |
| 250 | }, |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 251 | }), |
| 252 | func(pref.MessageType) pref.ProtoMessage { |
| 253 | return new(ScalarProto3) |
| 254 | }, |
| 255 | )} |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 256 | |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 257 | func (m *ScalarProto3) Type() pref.MessageType { return scalarProto3Type.Type } |
| 258 | func (m *ScalarProto3) KnownFields() pref.KnownFields { return scalarProto3Type.KnownFieldsOf(m) } |
| 259 | func (m *ScalarProto3) UnknownFields() pref.UnknownFields { return scalarProto3Type.UnknownFieldsOf(m) } |
| 260 | func (m *ScalarProto3) Interface() pref.ProtoMessage { return m } |
| 261 | func (m *ScalarProto3) ProtoReflect() pref.Message { return m } |
| 262 | func (m *ScalarProto3) ProtoMutable() {} |
| 263 | |
| 264 | func TestScalarProto3(t *testing.T) { |
| 265 | testMessage(t, nil, &ScalarProto3{}, messageOps{ |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 266 | hasFields{ |
| 267 | 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false, |
| 268 | 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false, |
| 269 | }, |
| 270 | getFields{ |
| 271 | 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)), |
| 272 | 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)), |
| 273 | }, |
| 274 | setFields{ |
| 275 | 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)), |
| 276 | 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)), |
| 277 | }, |
| 278 | hasFields{ |
| 279 | 1: false, 2: false, 3: false, 4: false, 5: false, 6: false, 7: false, 8: false, 9: false, 10: false, 11: false, |
| 280 | 12: false, 13: false, 14: false, 15: false, 16: false, 17: false, 18: false, 19: false, 20: false, 21: false, 22: false, |
| 281 | }, |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 282 | equalMessage(&ScalarProto3{}), |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 283 | setFields{ |
| 284 | 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")), |
| 285 | 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")), |
| 286 | }, |
| 287 | hasFields{ |
| 288 | 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true, |
| 289 | 12: true, 13: true, 14: true, 15: true, 16: true, 17: true, 18: true, 19: true, 20: true, 21: true, 22: true, |
| 290 | }, |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 291 | equalMessage(&ScalarProto3{ |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 292 | true, 2, 3, 4, 5, 6, 7, "8", []byte("9"), []byte("10"), "11", |
| 293 | true, 13, 14, 15, 16, 17, 18, "19", []byte("20"), []byte("21"), "22", |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 294 | }), |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 295 | clearFields{ |
| 296 | 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true, |
| 297 | 12: true, 13: true, 14: true, 15: true, 16: true, 17: true, 18: true, 19: true, 20: true, 21: true, 22: true, |
| 298 | }, |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 299 | equalMessage(&ScalarProto3{}), |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 300 | }) |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 301 | } |
| 302 | |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 303 | type ListScalars struct { |
Joe Tsai | ce6edd3 | 2018-10-19 16:27:46 -0700 | [diff] [blame] | 304 | Bools []bool `protobuf:"1"` |
| 305 | Int32s []int32 `protobuf:"2"` |
| 306 | Int64s []int64 `protobuf:"3"` |
| 307 | Uint32s []uint32 `protobuf:"4"` |
| 308 | Uint64s []uint64 `protobuf:"5"` |
| 309 | Float32s []float32 `protobuf:"6"` |
| 310 | Float64s []float64 `protobuf:"7"` |
| 311 | Strings []string `protobuf:"8"` |
| 312 | StringsA [][]byte `protobuf:"9"` |
| 313 | Bytes [][]byte `protobuf:"10"` |
| 314 | BytesA []string `protobuf:"11"` |
| 315 | |
| 316 | MyStrings1 []MyString `protobuf:"12"` |
| 317 | MyStrings2 []MyBytes `protobuf:"13"` |
| 318 | MyBytes1 []MyBytes `protobuf:"14"` |
| 319 | MyBytes2 []MyString `protobuf:"15"` |
| 320 | |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame^] | 321 | MyStrings3 ListStrings `protobuf:"16"` |
| 322 | MyStrings4 ListBytes `protobuf:"17"` |
| 323 | MyBytes3 ListBytes `protobuf:"18"` |
| 324 | MyBytes4 ListStrings `protobuf:"19"` |
Joe Tsai | ce6edd3 | 2018-10-19 16:27:46 -0700 | [diff] [blame] | 325 | } |
| 326 | |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 327 | var listScalarsType = MessageType{Type: ptype.GoMessage( |
| 328 | mustMakeMessageDesc(ptype.StandaloneMessage{ |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 329 | Syntax: pref.Proto2, |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 330 | FullName: "ListScalars", |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 331 | Fields: []ptype.Field{ |
| 332 | {Name: "f1", Number: 1, Cardinality: pref.Repeated, Kind: pref.BoolKind}, |
| 333 | {Name: "f2", Number: 2, Cardinality: pref.Repeated, Kind: pref.Int32Kind}, |
| 334 | {Name: "f3", Number: 3, Cardinality: pref.Repeated, Kind: pref.Int64Kind}, |
| 335 | {Name: "f4", Number: 4, Cardinality: pref.Repeated, Kind: pref.Uint32Kind}, |
| 336 | {Name: "f5", Number: 5, Cardinality: pref.Repeated, Kind: pref.Uint64Kind}, |
| 337 | {Name: "f6", Number: 6, Cardinality: pref.Repeated, Kind: pref.FloatKind}, |
| 338 | {Name: "f7", Number: 7, Cardinality: pref.Repeated, Kind: pref.DoubleKind}, |
| 339 | {Name: "f8", Number: 8, Cardinality: pref.Repeated, Kind: pref.StringKind}, |
| 340 | {Name: "f9", Number: 9, Cardinality: pref.Repeated, Kind: pref.StringKind}, |
| 341 | {Name: "f10", Number: 10, Cardinality: pref.Repeated, Kind: pref.BytesKind}, |
| 342 | {Name: "f11", Number: 11, Cardinality: pref.Repeated, Kind: pref.BytesKind}, |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 343 | |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 344 | {Name: "f12", Number: 12, Cardinality: pref.Repeated, Kind: pref.StringKind}, |
| 345 | {Name: "f13", Number: 13, Cardinality: pref.Repeated, Kind: pref.StringKind}, |
| 346 | {Name: "f14", Number: 14, Cardinality: pref.Repeated, Kind: pref.BytesKind}, |
| 347 | {Name: "f15", Number: 15, Cardinality: pref.Repeated, Kind: pref.BytesKind}, |
| 348 | |
| 349 | {Name: "f16", Number: 16, Cardinality: pref.Repeated, Kind: pref.StringKind}, |
| 350 | {Name: "f17", Number: 17, Cardinality: pref.Repeated, Kind: pref.StringKind}, |
| 351 | {Name: "f18", Number: 18, Cardinality: pref.Repeated, Kind: pref.BytesKind}, |
| 352 | {Name: "f19", Number: 19, Cardinality: pref.Repeated, Kind: pref.BytesKind}, |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 353 | }, |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 354 | }), |
| 355 | func(pref.MessageType) pref.ProtoMessage { |
| 356 | return new(ListScalars) |
| 357 | }, |
| 358 | )} |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 359 | |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 360 | func (m *ListScalars) Type() pref.MessageType { return listScalarsType.Type } |
| 361 | func (m *ListScalars) KnownFields() pref.KnownFields { return listScalarsType.KnownFieldsOf(m) } |
| 362 | func (m *ListScalars) UnknownFields() pref.UnknownFields { return listScalarsType.UnknownFieldsOf(m) } |
| 363 | func (m *ListScalars) Interface() pref.ProtoMessage { return m } |
| 364 | func (m *ListScalars) ProtoReflect() pref.Message { return m } |
| 365 | func (m *ListScalars) ProtoMutable() {} |
| 366 | |
| 367 | func TestListScalars(t *testing.T) { |
| 368 | empty := &ListScalars{} |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 369 | emptyFS := empty.KnownFields() |
| 370 | |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 371 | want := &ListScalars{ |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 372 | Bools: []bool{true, false, true}, |
| 373 | Int32s: []int32{2, math.MinInt32, math.MaxInt32}, |
| 374 | Int64s: []int64{3, math.MinInt64, math.MaxInt64}, |
| 375 | Uint32s: []uint32{4, math.MaxUint32 / 2, math.MaxUint32}, |
| 376 | Uint64s: []uint64{5, math.MaxUint64 / 2, math.MaxUint64}, |
| 377 | Float32s: []float32{6, math.SmallestNonzeroFloat32, float32(math.NaN()), math.MaxFloat32}, |
| 378 | Float64s: []float64{7, math.SmallestNonzeroFloat64, float64(math.NaN()), math.MaxFloat64}, |
| 379 | Strings: []string{"8", "", "eight"}, |
| 380 | StringsA: [][]byte{[]byte("9"), nil, []byte("nine")}, |
| 381 | Bytes: [][]byte{[]byte("10"), nil, []byte("ten")}, |
| 382 | BytesA: []string{"11", "", "eleven"}, |
| 383 | |
| 384 | MyStrings1: []MyString{"12", "", "twelve"}, |
| 385 | MyStrings2: []MyBytes{[]byte("13"), nil, []byte("thirteen")}, |
| 386 | MyBytes1: []MyBytes{[]byte("14"), nil, []byte("fourteen")}, |
| 387 | MyBytes2: []MyString{"15", "", "fifteen"}, |
| 388 | |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame^] | 389 | MyStrings3: ListStrings{"16", "", "sixteen"}, |
| 390 | MyStrings4: ListBytes{[]byte("17"), nil, []byte("seventeen")}, |
| 391 | MyBytes3: ListBytes{[]byte("18"), nil, []byte("eighteen")}, |
| 392 | MyBytes4: ListStrings{"19", "", "nineteen"}, |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 393 | } |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 394 | wantFS := want.KnownFields() |
| 395 | |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 396 | testMessage(t, nil, &ListScalars{}, messageOps{ |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 397 | 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}, |
| 398 | 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)}, |
| 399 | 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^] | 400 | listFields{ |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 401 | 2: { |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame^] | 402 | lenList(0), |
| 403 | appendList{V(int32(2)), V(int32(math.MinInt32)), V(int32(math.MaxInt32))}, |
| 404 | getList{0: V(int32(2)), 1: V(int32(math.MinInt32)), 2: V(int32(math.MaxInt32))}, |
| 405 | equalList(wantFS.Get(2).List()), |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 406 | }, |
| 407 | 4: { |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame^] | 408 | appendList{V(uint32(0)), V(uint32(0)), V(uint32(0))}, |
| 409 | setList{0: V(uint32(4)), 1: V(uint32(math.MaxUint32 / 2)), 2: V(uint32(math.MaxUint32))}, |
| 410 | lenList(3), |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 411 | }, |
| 412 | 6: { |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame^] | 413 | appendList{V(float32(6)), V(float32(math.SmallestNonzeroFloat32)), V(float32(math.NaN())), V(float32(math.MaxFloat32))}, |
| 414 | equalList(wantFS.Get(6).List()), |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 415 | }, |
| 416 | 8: { |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame^] | 417 | appendList{V(""), V(""), V(""), V(""), V(""), V("")}, |
| 418 | lenList(6), |
| 419 | setList{0: V("8"), 2: V("eight")}, |
| 420 | truncList(3), |
| 421 | equalList(wantFS.Get(8).List()), |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 422 | }, |
| 423 | 10: { |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame^] | 424 | appendList{V([]byte(nil)), V([]byte(nil))}, |
| 425 | setList{0: V([]byte("10"))}, |
| 426 | appendList{V([]byte("wrong"))}, |
| 427 | setList{2: V([]byte("ten"))}, |
| 428 | equalList(wantFS.Get(10).List()), |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 429 | }, |
| 430 | 12: { |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame^] | 431 | appendList{V("12"), V("wrong"), V("twelve")}, |
| 432 | setList{1: V("")}, |
| 433 | equalList(wantFS.Get(12).List()), |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 434 | }, |
| 435 | 14: { |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame^] | 436 | appendList{V([]byte("14")), V([]byte(nil)), V([]byte("fourteen"))}, |
| 437 | equalList(wantFS.Get(14).List()), |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 438 | }, |
| 439 | 16: { |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame^] | 440 | appendList{V("16"), V(""), V("sixteen"), V("extra")}, |
| 441 | truncList(3), |
| 442 | equalList(wantFS.Get(16).List()), |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 443 | }, |
| 444 | 18: { |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame^] | 445 | appendList{V([]byte("18")), V([]byte(nil)), V([]byte("eighteen"))}, |
| 446 | equalList(wantFS.Get(18).List()), |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 447 | }, |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 448 | }, |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 449 | 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}, |
| 450 | equalMessage(want), |
| 451 | clearFields{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 | bbfaeb7 | 2018-10-17 00:27:21 +0000 | [diff] [blame] | 452 | equalMessage(empty), |
| 453 | }) |
| 454 | } |
| 455 | |
Joe Tsai | ce6edd3 | 2018-10-19 16:27:46 -0700 | [diff] [blame] | 456 | type MapScalars struct { |
| 457 | KeyBools map[bool]string `protobuf:"1"` |
| 458 | KeyInt32s map[int32]string `protobuf:"2"` |
| 459 | KeyInt64s map[int64]string `protobuf:"3"` |
| 460 | KeyUint32s map[uint32]string `protobuf:"4"` |
| 461 | KeyUint64s map[uint64]string `protobuf:"5"` |
| 462 | KeyStrings map[string]string `protobuf:"6"` |
| 463 | |
| 464 | ValBools map[string]bool `protobuf:"7"` |
| 465 | ValInt32s map[string]int32 `protobuf:"8"` |
| 466 | ValInt64s map[string]int64 `protobuf:"9"` |
| 467 | ValUint32s map[string]uint32 `protobuf:"10"` |
| 468 | ValUint64s map[string]uint64 `protobuf:"11"` |
| 469 | ValFloat32s map[string]float32 `protobuf:"12"` |
| 470 | ValFloat64s map[string]float64 `protobuf:"13"` |
| 471 | ValStrings map[string]string `protobuf:"14"` |
| 472 | ValStringsA map[string][]byte `protobuf:"15"` |
| 473 | ValBytes map[string][]byte `protobuf:"16"` |
| 474 | ValBytesA map[string]string `protobuf:"17"` |
| 475 | |
| 476 | MyStrings1 map[MyString]MyString `protobuf:"18"` |
| 477 | MyStrings2 map[MyString]MyBytes `protobuf:"19"` |
| 478 | MyBytes1 map[MyString]MyBytes `protobuf:"20"` |
| 479 | MyBytes2 map[MyString]MyString `protobuf:"21"` |
| 480 | |
| 481 | MyStrings3 MapStrings `protobuf:"22"` |
| 482 | MyStrings4 MapBytes `protobuf:"23"` |
| 483 | MyBytes3 MapBytes `protobuf:"24"` |
| 484 | MyBytes4 MapStrings `protobuf:"25"` |
| 485 | } |
| 486 | |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 487 | func mustMakeMapEntry(n pref.FieldNumber, keyKind, valKind pref.Kind) ptype.Field { |
| 488 | return ptype.Field{ |
| 489 | Name: pref.Name(fmt.Sprintf("f%d", n)), |
| 490 | Number: n, |
| 491 | Cardinality: pref.Repeated, |
| 492 | Kind: pref.MessageKind, |
| 493 | MessageType: mustMakeMessageDesc(ptype.StandaloneMessage{ |
| 494 | Syntax: pref.Proto2, |
| 495 | FullName: pref.FullName(fmt.Sprintf("MapScalars.F%dEntry", n)), |
| 496 | Fields: []ptype.Field{ |
| 497 | {Name: "key", Number: 1, Cardinality: pref.Optional, Kind: keyKind}, |
| 498 | {Name: "value", Number: 2, Cardinality: pref.Optional, Kind: valKind}, |
| 499 | }, |
| 500 | Options: &descriptorV1.MessageOptions{MapEntry: protoV1.Bool(true)}, |
| 501 | }), |
Joe Tsai | bbfaeb7 | 2018-10-17 00:27:21 +0000 | [diff] [blame] | 502 | } |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | var mapScalarsType = MessageType{Type: ptype.GoMessage( |
| 506 | mustMakeMessageDesc(ptype.StandaloneMessage{ |
Joe Tsai | bbfaeb7 | 2018-10-17 00:27:21 +0000 | [diff] [blame] | 507 | Syntax: pref.Proto2, |
| 508 | FullName: "MapScalars", |
| 509 | Fields: []ptype.Field{ |
| 510 | mustMakeMapEntry(1, pref.BoolKind, pref.StringKind), |
| 511 | mustMakeMapEntry(2, pref.Int32Kind, pref.StringKind), |
| 512 | mustMakeMapEntry(3, pref.Int64Kind, pref.StringKind), |
| 513 | mustMakeMapEntry(4, pref.Uint32Kind, pref.StringKind), |
| 514 | mustMakeMapEntry(5, pref.Uint64Kind, pref.StringKind), |
| 515 | mustMakeMapEntry(6, pref.StringKind, pref.StringKind), |
| 516 | |
| 517 | mustMakeMapEntry(7, pref.StringKind, pref.BoolKind), |
| 518 | mustMakeMapEntry(8, pref.StringKind, pref.Int32Kind), |
| 519 | mustMakeMapEntry(9, pref.StringKind, pref.Int64Kind), |
| 520 | mustMakeMapEntry(10, pref.StringKind, pref.Uint32Kind), |
| 521 | mustMakeMapEntry(11, pref.StringKind, pref.Uint64Kind), |
| 522 | mustMakeMapEntry(12, pref.StringKind, pref.FloatKind), |
| 523 | mustMakeMapEntry(13, pref.StringKind, pref.DoubleKind), |
| 524 | mustMakeMapEntry(14, pref.StringKind, pref.StringKind), |
| 525 | mustMakeMapEntry(15, pref.StringKind, pref.StringKind), |
| 526 | mustMakeMapEntry(16, pref.StringKind, pref.BytesKind), |
| 527 | mustMakeMapEntry(17, pref.StringKind, pref.BytesKind), |
| 528 | |
| 529 | mustMakeMapEntry(18, pref.StringKind, pref.StringKind), |
| 530 | mustMakeMapEntry(19, pref.StringKind, pref.StringKind), |
| 531 | mustMakeMapEntry(20, pref.StringKind, pref.BytesKind), |
| 532 | mustMakeMapEntry(21, pref.StringKind, pref.BytesKind), |
| 533 | |
| 534 | mustMakeMapEntry(22, pref.StringKind, pref.StringKind), |
| 535 | mustMakeMapEntry(23, pref.StringKind, pref.StringKind), |
| 536 | mustMakeMapEntry(24, pref.StringKind, pref.BytesKind), |
| 537 | mustMakeMapEntry(25, pref.StringKind, pref.BytesKind), |
| 538 | }, |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 539 | }), |
| 540 | func(pref.MessageType) pref.ProtoMessage { |
| 541 | return new(MapScalars) |
| 542 | }, |
| 543 | )} |
Joe Tsai | bbfaeb7 | 2018-10-17 00:27:21 +0000 | [diff] [blame] | 544 | |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 545 | func (m *MapScalars) Type() pref.MessageType { return mapScalarsType.Type } |
| 546 | func (m *MapScalars) KnownFields() pref.KnownFields { return mapScalarsType.KnownFieldsOf(m) } |
| 547 | func (m *MapScalars) UnknownFields() pref.UnknownFields { return mapScalarsType.UnknownFieldsOf(m) } |
| 548 | func (m *MapScalars) Interface() pref.ProtoMessage { return m } |
| 549 | func (m *MapScalars) ProtoReflect() pref.Message { return m } |
| 550 | func (m *MapScalars) ProtoMutable() {} |
| 551 | |
| 552 | func TestMapScalars(t *testing.T) { |
| 553 | empty := &MapScalars{} |
Joe Tsai | bbfaeb7 | 2018-10-17 00:27:21 +0000 | [diff] [blame] | 554 | emptyFS := empty.KnownFields() |
| 555 | |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 556 | want := &MapScalars{ |
Joe Tsai | bbfaeb7 | 2018-10-17 00:27:21 +0000 | [diff] [blame] | 557 | KeyBools: map[bool]string{true: "true", false: "false"}, |
| 558 | KeyInt32s: map[int32]string{0: "zero", -1: "one", 2: "two"}, |
| 559 | KeyInt64s: map[int64]string{0: "zero", -10: "ten", 20: "twenty"}, |
| 560 | KeyUint32s: map[uint32]string{0: "zero", 1: "one", 2: "two"}, |
| 561 | KeyUint64s: map[uint64]string{0: "zero", 10: "ten", 20: "twenty"}, |
| 562 | KeyStrings: map[string]string{"": "", "foo": "bar"}, |
| 563 | |
| 564 | ValBools: map[string]bool{"true": true, "false": false}, |
| 565 | ValInt32s: map[string]int32{"one": 1, "two": 2, "three": 3}, |
| 566 | ValInt64s: map[string]int64{"ten": 10, "twenty": -20, "thirty": 30}, |
| 567 | ValUint32s: map[string]uint32{"0x00": 0x00, "0xff": 0xff, "0xdead": 0xdead}, |
| 568 | ValUint64s: map[string]uint64{"0x00": 0x00, "0xff": 0xff, "0xdead": 0xdead}, |
| 569 | ValFloat32s: map[string]float32{"nan": float32(math.NaN()), "pi": float32(math.Pi)}, |
| 570 | ValFloat64s: map[string]float64{"nan": float64(math.NaN()), "pi": float64(math.Pi)}, |
| 571 | ValStrings: map[string]string{"s1": "s1", "s2": "s2"}, |
| 572 | ValStringsA: map[string][]byte{"s1": []byte("s1"), "s2": []byte("s2")}, |
| 573 | ValBytes: map[string][]byte{"s1": []byte("s1"), "s2": []byte("s2")}, |
| 574 | ValBytesA: map[string]string{"s1": "s1", "s2": "s2"}, |
| 575 | |
| 576 | MyStrings1: map[MyString]MyString{"s1": "s1", "s2": "s2"}, |
| 577 | MyStrings2: map[MyString]MyBytes{"s1": []byte("s1"), "s2": []byte("s2")}, |
| 578 | MyBytes1: map[MyString]MyBytes{"s1": []byte("s1"), "s2": []byte("s2")}, |
| 579 | MyBytes2: map[MyString]MyString{"s1": "s1", "s2": "s2"}, |
| 580 | |
| 581 | MyStrings3: MapStrings{"s1": "s1", "s2": "s2"}, |
| 582 | MyStrings4: MapBytes{"s1": []byte("s1"), "s2": []byte("s2")}, |
| 583 | MyBytes3: MapBytes{"s1": []byte("s1"), "s2": []byte("s2")}, |
| 584 | MyBytes4: MapStrings{"s1": "s1", "s2": "s2"}, |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 585 | } |
Joe Tsai | bbfaeb7 | 2018-10-17 00:27:21 +0000 | [diff] [blame] | 586 | wantFS := want.KnownFields() |
| 587 | |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 588 | testMessage(t, nil, &MapScalars{}, messageOps{ |
Joe Tsai | bbfaeb7 | 2018-10-17 00:27:21 +0000 | [diff] [blame] | 589 | 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}, |
| 590 | 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)}, |
| 591 | 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)}, |
| 592 | mapFields{ |
| 593 | 2: { |
| 594 | lenMap(0), |
| 595 | hasMap{int32(0): false, int32(-1): false, int32(2): false}, |
| 596 | setMap{int32(0): V("zero")}, |
| 597 | lenMap(1), |
| 598 | hasMap{int32(0): true, int32(-1): false, int32(2): false}, |
| 599 | setMap{int32(-1): V("one")}, |
| 600 | lenMap(2), |
| 601 | hasMap{int32(0): true, int32(-1): true, int32(2): false}, |
| 602 | setMap{int32(2): V("two")}, |
| 603 | lenMap(3), |
| 604 | hasMap{int32(0): true, int32(-1): true, int32(2): true}, |
| 605 | }, |
| 606 | 4: { |
| 607 | setMap{uint32(0): V("zero"), uint32(1): V("one"), uint32(2): V("two")}, |
| 608 | equalMap(wantFS.Get(4).Map()), |
| 609 | }, |
| 610 | 6: { |
| 611 | clearMap{"noexist": true}, |
| 612 | setMap{"foo": V("bar")}, |
| 613 | setMap{"": V("empty")}, |
| 614 | getMap{"": V("empty"), "foo": V("bar"), "noexist": V(nil)}, |
| 615 | setMap{"": V(""), "extra": V("extra")}, |
| 616 | clearMap{"extra": true, "noexist": true}, |
| 617 | }, |
| 618 | 8: { |
| 619 | equalMap(emptyFS.Get(8).Map()), |
| 620 | setMap{"one": V(int32(1)), "two": V(int32(2)), "three": V(int32(3))}, |
| 621 | }, |
| 622 | 10: { |
| 623 | setMap{"0x00": V(uint32(0x00)), "0xff": V(uint32(0xff)), "0xdead": V(uint32(0xdead))}, |
| 624 | lenMap(3), |
| 625 | equalMap(wantFS.Get(10).Map()), |
| 626 | getMap{"0x00": V(uint32(0x00)), "0xff": V(uint32(0xff)), "0xdead": V(uint32(0xdead)), "0xdeadbeef": V(nil)}, |
| 627 | }, |
| 628 | 12: { |
| 629 | setMap{"nan": V(float32(math.NaN())), "pi": V(float32(math.Pi)), "e": V(float32(math.E))}, |
| 630 | clearMap{"e": true, "phi": true}, |
| 631 | rangeMap{"nan": V(float32(math.NaN())), "pi": V(float32(math.Pi))}, |
| 632 | }, |
| 633 | 14: { |
| 634 | equalMap(emptyFS.Get(14).Map()), |
| 635 | setMap{"s1": V("s1"), "s2": V("s2")}, |
| 636 | }, |
| 637 | 16: { |
| 638 | setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))}, |
| 639 | equalMap(wantFS.Get(16).Map()), |
| 640 | }, |
| 641 | 18: { |
| 642 | hasMap{"s1": false, "s2": false, "s3": false}, |
| 643 | setMap{"s1": V("s1"), "s2": V("s2")}, |
| 644 | hasMap{"s1": true, "s2": true, "s3": false}, |
| 645 | }, |
| 646 | 20: { |
| 647 | equalMap(emptyFS.Get(20).Map()), |
| 648 | setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))}, |
| 649 | }, |
| 650 | 22: { |
| 651 | rangeMap{}, |
| 652 | setMap{"s1": V("s1"), "s2": V("s2")}, |
| 653 | rangeMap{"s1": V("s1"), "s2": V("s2")}, |
| 654 | lenMap(2), |
| 655 | }, |
| 656 | 24: { |
| 657 | setMap{"s1": V([]byte("s1")), "s2": V([]byte("s2"))}, |
| 658 | equalMap(wantFS.Get(24).Map()), |
| 659 | }, |
| 660 | }, |
| 661 | 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}, |
| 662 | equalMessage(want), |
| 663 | clearFields{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}, |
| 664 | equalMessage(empty), |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 665 | }) |
| 666 | } |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 667 | |
Joe Tsai | 2c870bb | 2018-10-17 11:46:52 -0700 | [diff] [blame] | 668 | type ( |
| 669 | OneofScalars struct { |
| 670 | Union isOneofScalars_Union `protobuf_oneof:"union"` |
| 671 | } |
| 672 | isOneofScalars_Union interface { |
| 673 | isOneofScalars_Union() |
| 674 | } |
| 675 | |
| 676 | OneofScalars_Bool struct { |
| 677 | Bool bool `protobuf:"1"` |
| 678 | } |
| 679 | OneofScalars_Int32 struct { |
| 680 | Int32 MyInt32 `protobuf:"2"` |
| 681 | } |
| 682 | OneofScalars_Int64 struct { |
| 683 | Int64 int64 `protobuf:"3"` |
| 684 | } |
| 685 | OneofScalars_Uint32 struct { |
| 686 | Uint32 MyUint32 `protobuf:"4"` |
| 687 | } |
| 688 | OneofScalars_Uint64 struct { |
| 689 | Uint64 uint64 `protobuf:"5"` |
| 690 | } |
| 691 | OneofScalars_Float32 struct { |
| 692 | Float32 MyFloat32 `protobuf:"6"` |
| 693 | } |
| 694 | OneofScalars_Float64 struct { |
| 695 | Float64 float64 `protobuf:"7"` |
| 696 | } |
| 697 | OneofScalars_String struct { |
| 698 | String string `protobuf:"8"` |
| 699 | } |
| 700 | OneofScalars_StringA struct { |
| 701 | StringA []byte `protobuf:"9"` |
| 702 | } |
| 703 | OneofScalars_StringB struct { |
| 704 | StringB MyString `protobuf:"10"` |
| 705 | } |
| 706 | OneofScalars_Bytes struct { |
| 707 | Bytes []byte `protobuf:"11"` |
| 708 | } |
| 709 | OneofScalars_BytesA struct { |
| 710 | BytesA string `protobuf:"12"` |
| 711 | } |
| 712 | OneofScalars_BytesB struct { |
| 713 | BytesB MyBytes `protobuf:"13"` |
| 714 | } |
| 715 | ) |
| 716 | |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 717 | var oneofScalarsType = MessageType{Type: ptype.GoMessage( |
| 718 | mustMakeMessageDesc(ptype.StandaloneMessage{ |
| 719 | Syntax: pref.Proto2, |
| 720 | FullName: "ScalarProto2", |
| 721 | Fields: []ptype.Field{ |
| 722 | {Name: "f1", Number: 1, Cardinality: pref.Optional, Kind: pref.BoolKind, Default: V(bool(true)), OneofName: "union"}, |
| 723 | {Name: "f2", Number: 2, Cardinality: pref.Optional, Kind: pref.Int32Kind, Default: V(int32(2)), OneofName: "union"}, |
| 724 | {Name: "f3", Number: 3, Cardinality: pref.Optional, Kind: pref.Int64Kind, Default: V(int64(3)), OneofName: "union"}, |
| 725 | {Name: "f4", Number: 4, Cardinality: pref.Optional, Kind: pref.Uint32Kind, Default: V(uint32(4)), OneofName: "union"}, |
| 726 | {Name: "f5", Number: 5, Cardinality: pref.Optional, Kind: pref.Uint64Kind, Default: V(uint64(5)), OneofName: "union"}, |
| 727 | {Name: "f6", Number: 6, Cardinality: pref.Optional, Kind: pref.FloatKind, Default: V(float32(6)), OneofName: "union"}, |
| 728 | {Name: "f7", Number: 7, Cardinality: pref.Optional, Kind: pref.DoubleKind, Default: V(float64(7)), OneofName: "union"}, |
| 729 | {Name: "f8", Number: 8, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("8")), OneofName: "union"}, |
| 730 | {Name: "f9", Number: 9, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("9")), OneofName: "union"}, |
| 731 | {Name: "f10", Number: 10, Cardinality: pref.Optional, Kind: pref.StringKind, Default: V(string("10")), OneofName: "union"}, |
| 732 | {Name: "f11", Number: 11, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("11")), OneofName: "union"}, |
| 733 | {Name: "f12", Number: 12, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("12")), OneofName: "union"}, |
| 734 | {Name: "f13", Number: 13, Cardinality: pref.Optional, Kind: pref.BytesKind, Default: V([]byte("13")), OneofName: "union"}, |
| 735 | }, |
| 736 | Oneofs: []ptype.Oneof{{Name: "union"}}, |
| 737 | }), |
| 738 | func(pref.MessageType) pref.ProtoMessage { |
| 739 | return new(OneofScalars) |
| 740 | }, |
| 741 | )} |
| 742 | |
| 743 | func (m *OneofScalars) Type() pref.MessageType { return oneofScalarsType.Type } |
| 744 | func (m *OneofScalars) KnownFields() pref.KnownFields { return oneofScalarsType.KnownFieldsOf(m) } |
| 745 | func (m *OneofScalars) UnknownFields() pref.UnknownFields { return oneofScalarsType.UnknownFieldsOf(m) } |
| 746 | func (m *OneofScalars) Interface() pref.ProtoMessage { return m } |
| 747 | func (m *OneofScalars) ProtoReflect() pref.Message { return m } |
| 748 | func (m *OneofScalars) ProtoMutable() {} |
| 749 | |
| 750 | func (*OneofScalars) XXX_OneofFuncs() (func(protoV1.Message, *protoV1.Buffer) error, func(protoV1.Message, int, int, *protoV1.Buffer) (bool, error), func(protoV1.Message) int, []interface{}) { |
Joe Tsai | 2c870bb | 2018-10-17 11:46:52 -0700 | [diff] [blame] | 751 | return nil, nil, nil, []interface{}{ |
| 752 | (*OneofScalars_Bool)(nil), |
| 753 | (*OneofScalars_Int32)(nil), |
| 754 | (*OneofScalars_Int64)(nil), |
| 755 | (*OneofScalars_Uint32)(nil), |
| 756 | (*OneofScalars_Uint64)(nil), |
| 757 | (*OneofScalars_Float32)(nil), |
| 758 | (*OneofScalars_Float64)(nil), |
| 759 | (*OneofScalars_String)(nil), |
| 760 | (*OneofScalars_StringA)(nil), |
| 761 | (*OneofScalars_StringB)(nil), |
| 762 | (*OneofScalars_Bytes)(nil), |
| 763 | (*OneofScalars_BytesA)(nil), |
| 764 | (*OneofScalars_BytesB)(nil), |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | func (*OneofScalars_Bool) isOneofScalars_Union() {} |
| 769 | func (*OneofScalars_Int32) isOneofScalars_Union() {} |
| 770 | func (*OneofScalars_Int64) isOneofScalars_Union() {} |
| 771 | func (*OneofScalars_Uint32) isOneofScalars_Union() {} |
| 772 | func (*OneofScalars_Uint64) isOneofScalars_Union() {} |
| 773 | func (*OneofScalars_Float32) isOneofScalars_Union() {} |
| 774 | func (*OneofScalars_Float64) isOneofScalars_Union() {} |
| 775 | func (*OneofScalars_String) isOneofScalars_Union() {} |
| 776 | func (*OneofScalars_StringA) isOneofScalars_Union() {} |
| 777 | func (*OneofScalars_StringB) isOneofScalars_Union() {} |
| 778 | func (*OneofScalars_Bytes) isOneofScalars_Union() {} |
| 779 | func (*OneofScalars_BytesA) isOneofScalars_Union() {} |
| 780 | func (*OneofScalars_BytesB) isOneofScalars_Union() {} |
| 781 | |
| 782 | func TestOneofs(t *testing.T) { |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 783 | empty := &OneofScalars{} |
| 784 | want1 := &OneofScalars{Union: &OneofScalars_Bool{true}} |
| 785 | want2 := &OneofScalars{Union: &OneofScalars_Int32{20}} |
| 786 | want3 := &OneofScalars{Union: &OneofScalars_Int64{30}} |
| 787 | want4 := &OneofScalars{Union: &OneofScalars_Uint32{40}} |
| 788 | want5 := &OneofScalars{Union: &OneofScalars_Uint64{50}} |
| 789 | want6 := &OneofScalars{Union: &OneofScalars_Float32{60}} |
| 790 | want7 := &OneofScalars{Union: &OneofScalars_Float64{70}} |
| 791 | want8 := &OneofScalars{Union: &OneofScalars_String{string("80")}} |
| 792 | want9 := &OneofScalars{Union: &OneofScalars_StringA{[]byte("90")}} |
| 793 | want10 := &OneofScalars{Union: &OneofScalars_StringB{MyString("100")}} |
| 794 | want11 := &OneofScalars{Union: &OneofScalars_Bytes{[]byte("110")}} |
| 795 | want12 := &OneofScalars{Union: &OneofScalars_BytesA{string("120")}} |
| 796 | want13 := &OneofScalars{Union: &OneofScalars_BytesB{MyBytes("130")}} |
Joe Tsai | 2c870bb | 2018-10-17 11:46:52 -0700 | [diff] [blame] | 797 | |
Joe Tsai | f0c01e4 | 2018-11-06 13:05:20 -0800 | [diff] [blame] | 798 | testMessage(t, nil, &OneofScalars{}, messageOps{ |
Joe Tsai | 2c870bb | 2018-10-17 11:46:52 -0700 | [diff] [blame] | 799 | 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}, |
| 800 | 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"))}, |
| 801 | |
| 802 | setFields{1: V(bool(true))}, hasFields{1: true}, equalMessage(want1), |
| 803 | setFields{2: V(int32(20))}, hasFields{2: true}, equalMessage(want2), |
| 804 | setFields{3: V(int64(30))}, hasFields{3: true}, equalMessage(want3), |
| 805 | setFields{4: V(uint32(40))}, hasFields{4: true}, equalMessage(want4), |
| 806 | setFields{5: V(uint64(50))}, hasFields{5: true}, equalMessage(want5), |
| 807 | setFields{6: V(float32(60))}, hasFields{6: true}, equalMessage(want6), |
| 808 | setFields{7: V(float64(70))}, hasFields{7: true}, equalMessage(want7), |
| 809 | setFields{8: V(string("80"))}, hasFields{8: true}, equalMessage(want8), |
| 810 | setFields{9: V(string("90"))}, hasFields{9: true}, equalMessage(want9), |
| 811 | setFields{10: V(string("100"))}, hasFields{10: true}, equalMessage(want10), |
| 812 | setFields{11: V([]byte("110"))}, hasFields{11: true}, equalMessage(want11), |
| 813 | setFields{12: V([]byte("120"))}, hasFields{12: true}, equalMessage(want12), |
| 814 | setFields{13: V([]byte("130"))}, hasFields{13: true}, equalMessage(want13), |
| 815 | |
| 816 | 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}, |
| 817 | 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"))}, |
| 818 | clearFields{1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true, 12: true}, |
| 819 | equalMessage(want13), |
| 820 | clearFields{13: true}, |
| 821 | equalMessage(empty), |
| 822 | }) |
| 823 | } |
| 824 | |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 825 | // TODO: Need to test singular and repeated messages |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 826 | |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 827 | var cmpOpts = cmp.Options{ |
| 828 | cmp.Transformer("UnwrapValue", func(v pref.Value) interface{} { |
| 829 | return v.Interface() |
| 830 | }), |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame^] | 831 | cmp.Transformer("UnwrapList", func(v pref.List) interface{} { |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 832 | return v.(interface{ Unwrap() interface{} }).Unwrap() |
| 833 | }), |
| 834 | cmp.Transformer("UnwrapMap", func(m pref.Map) interface{} { |
| 835 | return m.(interface{ Unwrap() interface{} }).Unwrap() |
| 836 | }), |
| 837 | cmpopts.EquateNaNs(), |
| 838 | } |
| 839 | |
| 840 | func testMessage(t *testing.T, p path, m pref.Message, tt messageOps) { |
| 841 | fs := m.KnownFields() |
| 842 | for i, op := range tt { |
| 843 | p.Push(i) |
| 844 | switch op := op.(type) { |
| 845 | case equalMessage: |
| 846 | if diff := cmp.Diff(op, m, cmpOpts); diff != "" { |
| 847 | t.Errorf("operation %v, message mismatch (-want, +got):\n%s", p, diff) |
| 848 | } |
| 849 | case hasFields: |
| 850 | got := map[pref.FieldNumber]bool{} |
| 851 | want := map[pref.FieldNumber]bool(op) |
| 852 | for n := range want { |
| 853 | got[n] = fs.Has(n) |
| 854 | } |
| 855 | if diff := cmp.Diff(want, got); diff != "" { |
| 856 | t.Errorf("operation %v, KnownFields.Has mismatch (-want, +got):\n%s", p, diff) |
| 857 | } |
| 858 | case getFields: |
| 859 | got := map[pref.FieldNumber]pref.Value{} |
| 860 | want := map[pref.FieldNumber]pref.Value(op) |
| 861 | for n := range want { |
| 862 | got[n] = fs.Get(n) |
| 863 | } |
| 864 | if diff := cmp.Diff(want, got, cmpOpts); diff != "" { |
| 865 | t.Errorf("operation %v, KnownFields.Get mismatch (-want, +got):\n%s", p, diff) |
| 866 | } |
| 867 | case setFields: |
| 868 | for n, v := range op { |
| 869 | fs.Set(n, v) |
| 870 | } |
| 871 | case clearFields: |
| 872 | for n, ok := range op { |
| 873 | if ok { |
| 874 | fs.Clear(n) |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 875 | } |
| 876 | } |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame^] | 877 | case listFields: |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 878 | for n, tt := range op { |
| 879 | p.Push(int(n)) |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame^] | 880 | testLists(t, p, fs.Mutable(n).(pref.List), tt) |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 881 | p.Pop() |
| 882 | } |
Joe Tsai | bbfaeb7 | 2018-10-17 00:27:21 +0000 | [diff] [blame] | 883 | case mapFields: |
| 884 | for n, tt := range op { |
| 885 | p.Push(int(n)) |
| 886 | testMaps(t, p, fs.Mutable(n).(pref.Map), tt) |
| 887 | p.Pop() |
| 888 | } |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 889 | default: |
| 890 | t.Fatalf("operation %v, invalid operation: %T", p, op) |
| 891 | } |
| 892 | p.Pop() |
Joe Tsai | fa02f4e | 2018-09-12 16:20:37 -0700 | [diff] [blame] | 893 | } |
| 894 | } |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 895 | |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame^] | 896 | func testLists(t *testing.T, p path, v pref.List, tt listOps) { |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 897 | for i, op := range tt { |
| 898 | p.Push(i) |
| 899 | switch op := op.(type) { |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame^] | 900 | case equalList: |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 901 | if diff := cmp.Diff(op, v, cmpOpts); diff != "" { |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame^] | 902 | t.Errorf("operation %v, list mismatch (-want, +got):\n%s", p, diff) |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 903 | } |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame^] | 904 | case lenList: |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 905 | if got, want := v.Len(), int(op); got != want { |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame^] | 906 | t.Errorf("operation %v, List.Len = %d, want %d", p, got, want) |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 907 | } |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame^] | 908 | case getList: |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 909 | got := map[int]pref.Value{} |
| 910 | want := map[int]pref.Value(op) |
| 911 | for n := range want { |
| 912 | got[n] = v.Get(n) |
| 913 | } |
| 914 | if diff := cmp.Diff(want, got, cmpOpts); diff != "" { |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame^] | 915 | 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] | 916 | } |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame^] | 917 | case setList: |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 918 | for n, e := range op { |
| 919 | v.Set(n, e) |
| 920 | } |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame^] | 921 | case appendList: |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 922 | for _, e := range op { |
| 923 | v.Append(e) |
| 924 | } |
Joe Tsai | 4b7aff6 | 2018-11-14 14:05:19 -0800 | [diff] [blame^] | 925 | case truncList: |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 926 | v.Truncate(int(op)) |
| 927 | default: |
| 928 | t.Fatalf("operation %v, invalid operation: %T", p, op) |
| 929 | } |
| 930 | p.Pop() |
| 931 | } |
| 932 | } |
| 933 | |
Joe Tsai | bbfaeb7 | 2018-10-17 00:27:21 +0000 | [diff] [blame] | 934 | func testMaps(t *testing.T, p path, m pref.Map, tt mapOps) { |
| 935 | for i, op := range tt { |
| 936 | p.Push(i) |
| 937 | switch op := op.(type) { |
| 938 | case equalMap: |
| 939 | if diff := cmp.Diff(op, m, cmpOpts); diff != "" { |
| 940 | t.Errorf("operation %v, map mismatch (-want, +got):\n%s", p, diff) |
| 941 | } |
| 942 | case lenMap: |
| 943 | if got, want := m.Len(), int(op); got != want { |
| 944 | t.Errorf("operation %v, Map.Len = %d, want %d", p, got, want) |
| 945 | } |
| 946 | case hasMap: |
| 947 | got := map[interface{}]bool{} |
| 948 | want := map[interface{}]bool(op) |
| 949 | for k := range want { |
| 950 | got[k] = m.Has(V(k).MapKey()) |
| 951 | } |
| 952 | if diff := cmp.Diff(want, got, cmpOpts); diff != "" { |
| 953 | t.Errorf("operation %v, Map.Has mismatch (-want, +got):\n%s", p, diff) |
| 954 | } |
| 955 | case getMap: |
| 956 | got := map[interface{}]pref.Value{} |
| 957 | want := map[interface{}]pref.Value(op) |
| 958 | for k := range want { |
| 959 | got[k] = m.Get(V(k).MapKey()) |
| 960 | } |
| 961 | if diff := cmp.Diff(want, got, cmpOpts); diff != "" { |
| 962 | t.Errorf("operation %v, Map.Get mismatch (-want, +got):\n%s", p, diff) |
| 963 | } |
| 964 | case setMap: |
| 965 | for k, v := range op { |
| 966 | m.Set(V(k).MapKey(), v) |
| 967 | } |
| 968 | case clearMap: |
| 969 | for v, ok := range op { |
| 970 | if ok { |
| 971 | m.Clear(V(v).MapKey()) |
| 972 | } |
| 973 | } |
| 974 | case rangeMap: |
| 975 | got := map[interface{}]pref.Value{} |
| 976 | want := map[interface{}]pref.Value(op) |
| 977 | m.Range(func(k pref.MapKey, v pref.Value) bool { |
| 978 | got[k.Interface()] = v |
| 979 | return true |
| 980 | }) |
| 981 | if diff := cmp.Diff(want, got, cmpOpts); diff != "" { |
| 982 | t.Errorf("operation %v, Map.Range mismatch (-want, +got):\n%s", p, diff) |
| 983 | } |
| 984 | default: |
| 985 | t.Fatalf("operation %v, invalid operation: %T", p, op) |
| 986 | } |
| 987 | p.Pop() |
| 988 | } |
| 989 | } |
| 990 | |
Joe Tsai | 91e1466 | 2018-09-13 13:24:35 -0700 | [diff] [blame] | 991 | type path []int |
| 992 | |
| 993 | func (p *path) Push(i int) { *p = append(*p, i) } |
| 994 | func (p *path) Pop() { *p = (*p)[:len(*p)-1] } |
| 995 | func (p path) String() string { |
| 996 | var ss []string |
| 997 | for _, i := range p { |
| 998 | ss = append(ss, fmt.Sprint(i)) |
| 999 | } |
| 1000 | return strings.Join(ss, ".") |
| 1001 | } |