-
Notifications
You must be signed in to change notification settings - Fork 186
/
Copy pathmap_field_test.dart
457 lines (386 loc) · 15.2 KB
/
map_field_test.dart
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:convert';
import 'package:protobuf/protobuf.dart';
import 'package:test/test.dart';
import '../out/protos/map_field.pb.dart';
void main() {
void setValues(TestMap testMap) {
testMap
..int32ToInt32Field[1] = 11
..int32ToInt32Field[2] = 22
..int32ToInt32Field[3] = 33
..int32ToStringField[1] = '11'
..int32ToStringField[2] = '22'
..int32ToStringField[3] = '33'
..int32ToBytesField[1] = utf8.encode('11')
..int32ToBytesField[2] = utf8.encode('22')
..int32ToBytesField[3] = utf8.encode('33')
..int32ToEnumField[1] = TestMap_EnumValue.DEFAULT
..int32ToEnumField[2] = TestMap_EnumValue.BAR
..int32ToEnumField[3] = TestMap_EnumValue.BAZ
..int32ToMessageField[1] = (TestMap_MessageValue()..value = 11)
..int32ToMessageField[2] = (TestMap_MessageValue()..value = 22)
..int32ToMessageField[3] = (TestMap_MessageValue()..value = 33)
..stringToInt32Field['1'] = 11
..stringToInt32Field['2'] = 22
..stringToInt32Field['3'] = 33;
}
void updateValues(TestMap testMap) {
testMap
..int32ToInt32Field[1] = 111
..int32ToInt32Field.remove(2)
..int32ToInt32Field[4] = 44
..int32ToStringField[1] = '111'
..int32ToStringField.remove(2)
..int32ToStringField[4] = '44'
..int32ToBytesField[1] = utf8.encode('111')
..int32ToBytesField.remove(2)
..int32ToBytesField[4] = utf8.encode('44')
..int32ToEnumField[1] = TestMap_EnumValue.BAR
..int32ToEnumField.remove(2)
..int32ToEnumField[4] = TestMap_EnumValue.ZOP
..int32ToMessageField[1] = (TestMap_MessageValue()..value = 111)
..int32ToMessageField.remove(2)
..int32ToMessageField[4] = (TestMap_MessageValue()..value = 44)
..stringToInt32Field['1'] = 111
..stringToInt32Field.remove('2')
..stringToInt32Field['4'] = 44;
}
void expectEmpty(TestMap testMap) {
expect(testMap.int32ToInt32Field, isEmpty);
expect(testMap.int32ToStringField, isEmpty);
expect(testMap.int32ToBytesField, isEmpty);
expect(testMap.int32ToEnumField, isEmpty);
expect(testMap.int32ToMessageField, isEmpty);
expect(testMap.stringToInt32Field, isEmpty);
}
void expectMapValuesSet(TestMap testMap) {
expect(testMap.int32ToInt32Field[1], 11);
expect(testMap.int32ToInt32Field[2], 22);
expect(testMap.int32ToInt32Field[3], 33);
expect(testMap.int32ToStringField[1], '11');
expect(testMap.int32ToStringField[2], '22');
expect(testMap.int32ToStringField[3], '33');
expect(testMap.int32ToBytesField[1], utf8.encode('11'));
expect(testMap.int32ToBytesField[2], utf8.encode('22'));
expect(testMap.int32ToBytesField[3], utf8.encode('33'));
expect(testMap.int32ToEnumField[1], TestMap_EnumValue.DEFAULT);
expect(testMap.int32ToEnumField[2], TestMap_EnumValue.BAR);
expect(testMap.int32ToEnumField[3], TestMap_EnumValue.BAZ);
expect(testMap.int32ToMessageField[1]!.value, 11);
expect(testMap.int32ToMessageField[2]!.value, 22);
expect(testMap.int32ToMessageField[3]!.value, 33);
expect(testMap.stringToInt32Field['1'], 11);
expect(testMap.stringToInt32Field['2'], 22);
expect(testMap.stringToInt32Field['3'], 33);
}
void expectMapValuesUpdated(TestMap testMap) {
expect(testMap.int32ToInt32Field.length, 3);
expect(testMap.int32ToInt32Field[1], 111);
expect(testMap.int32ToInt32Field[3], 33);
expect(testMap.int32ToInt32Field[4], 44);
expect(testMap.int32ToStringField.length, 3);
expect(testMap.int32ToStringField[1], '111');
expect(testMap.int32ToStringField[3], '33');
expect(testMap.int32ToStringField[4], '44');
expect(testMap.int32ToBytesField.length, 3);
expect(testMap.int32ToBytesField[1], utf8.encode('111'));
expect(testMap.int32ToBytesField[3], utf8.encode('33'));
expect(testMap.int32ToBytesField[4], utf8.encode('44'));
expect(testMap.int32ToEnumField.length, 3);
expect(testMap.int32ToEnumField[1], TestMap_EnumValue.BAR);
expect(testMap.int32ToEnumField[3], TestMap_EnumValue.BAZ);
expect(testMap.int32ToEnumField[4], TestMap_EnumValue.ZOP);
expect(testMap.int32ToMessageField.length, 3);
expect(testMap.int32ToMessageField[1]!.value, 111);
expect(testMap.int32ToMessageField[3]!.value, 33);
expect(testMap.int32ToMessageField[4]!.value, 44);
expect(testMap.stringToInt32Field.length, 3);
expect(testMap.stringToInt32Field['1'], 111);
expect(testMap.stringToInt32Field['3'], 33);
expect(testMap.stringToInt32Field['4'], 44);
}
test('set and clear values', () {
final testMap = TestMap();
expectEmpty(testMap);
setValues(testMap);
expectMapValuesSet(testMap);
testMap.clear();
expectEmpty(testMap);
});
test('update map values', () {
final testMap = TestMap();
setValues(testMap);
updateValues(testMap);
expectMapValuesUpdated(testMap);
});
test('Serialize and parse map', () {
var testMap = TestMap();
setValues(testMap);
testMap = TestMap.fromBuffer(testMap.writeToBuffer());
expectMapValuesSet(testMap);
updateValues(testMap);
testMap = TestMap.fromBuffer(testMap.writeToBuffer());
expectMapValuesUpdated(testMap);
testMap.clear();
testMap = TestMap.fromBuffer(testMap.writeToBuffer());
expectEmpty(testMap);
});
test('json serialize map', () {
var testMap = TestMap();
setValues(testMap);
testMap = TestMap.fromJson(testMap.writeToJson());
expectMapValuesSet(testMap);
updateValues(testMap);
testMap = TestMap.fromJson(testMap.writeToJson());
expectMapValuesUpdated(testMap);
testMap.clear();
testMap = TestMap.fromJson(testMap.writeToJson());
expectEmpty(testMap);
});
test(
'PbMap` is equal to another PbMap with equal key/value pairs in any order',
() {
final t = TestMap()
..int32ToStringField[2] = 'test2'
..int32ToStringField[1] = 'test';
final t2 = TestMap()
..int32ToStringField[1] = 'test'
..int32ToStringField[2] = 'test2';
final t3 = TestMap()..int32ToStringField[1] = 'test';
final m = t.int32ToStringField;
final m2 = t2.int32ToStringField;
final m3 = t3.int32ToStringField;
expect(t, t2);
expect(t.hashCode, t2.hashCode);
expect(m, m2);
expect(m == m2, isTrue);
expect(m.hashCode, m2.hashCode);
expect(m, isNot(m3));
expect(m == m3, isFalse);
expect(m.hashCode, isNot(m3.hashCode));
});
test('Unitialized map field is equal to initialized', () {
final testMap1 = TestMap();
final testMap2 = TestMap();
// Do a trivial operation to initialize the map field of testMap2.
testMap2.int32ToStringField.clear();
expect(testMap1, equals(testMap2));
expect(testMap2, equals(testMap1));
expect(testMap1.hashCode, equals(testMap2.hashCode));
});
test('merge from other message', () {
var testMap = TestMap();
setValues(testMap);
var other = TestMap();
other.mergeFromMessage(testMap);
expectMapValuesSet(other);
testMap = TestMap()
..int32ToMessageField[1] = (TestMap_MessageValue()..value = 42)
..int32ToMessageField[2] = (TestMap_MessageValue()..value = 44);
other = TestMap()
..int32ToMessageField[1] = (TestMap_MessageValue()..secondValue = 43);
testMap.mergeFromMessage(other);
expect(testMap.int32ToMessageField[1]!.value, 0);
expect(testMap.int32ToMessageField[1]!.secondValue, 43);
expect(testMap.int32ToMessageField[2]!.value, 44);
});
test('parse duplicate keys', () {
final testMap = TestMap()..int32ToStringField[1] = 'foo';
final testMap2 = TestMap()..int32ToStringField[1] = 'bar';
final merge = TestMap.fromBuffer(
[...testMap.writeToBuffer(), ...testMap2.writeToBuffer()]);
// When parsing from the wire, if there are duplicate map keys the last key
// seen should be used.
expect(merge.int32ToStringField.length, 1);
expect(merge.int32ToStringField[1], 'bar');
});
test('Deep merge from other message', () {
final i1 = Inner()..innerMap['a'] = 'a';
final i2 = Inner()..innerMap['b'] = 'b';
final o1 = Outer()..i = i1;
final o2 = Outer()..i = i2;
o1.mergeFromMessage(o2);
expect(o1.i.innerMap.length, 2);
});
test('retain explicit default values of sub-messages', () {
final testMap = TestMap()..int32ToMessageField[1] = TestMap_MessageValue();
expect(testMap.int32ToMessageField[1]!.secondValue, 42);
final testMap2 = TestMap()..int32ToMessageField[2] = TestMap_MessageValue();
testMap.mergeFromBuffer(testMap2.writeToBuffer());
expect(testMap.int32ToMessageField[2]!.secondValue, 42);
});
test('Freeze message with map field', () {
final testMap = TestMap();
setValues(testMap);
testMap.freeze();
expect(() => updateValues(testMap),
throwsA(const TypeMatcher<UnsupportedError>()));
expect(() => testMap.int32ToMessageField[1]!.value = 42,
throwsA(const TypeMatcher<UnsupportedError>()));
expect(() => testMap.int32ToStringField.remove(1),
throwsA(const TypeMatcher<UnsupportedError>()));
expect(() => testMap.int32ToStringField.clear(),
throwsA(const TypeMatcher<UnsupportedError>()));
});
test('Values for different keys are not merged together when decoding', () {
final testMap = TestMap();
testMap.int32ToMessageField[1] = (TestMap_MessageValue()..value = 11);
testMap.int32ToMessageField[2] = (TestMap_MessageValue()..secondValue = 12);
void testValues(TestMap candidate) {
final message1 = candidate.int32ToMessageField[1]!;
final message2 = candidate.int32ToMessageField[2]!;
expect(message1.hasValue(), true);
expect(message1.value, 11);
expect(message1.hasSecondValue(), false);
expect(message1.secondValue, 42);
expect(message2.hasValue(), false);
expect(message2.value, 0);
expect(message2.hasSecondValue(), true);
expect(message2.secondValue, 12);
}
testValues(TestMap.fromBuffer(testMap.writeToBuffer()));
testValues(TestMap.fromJson(testMap.writeToJson()));
});
test('Calling getField on map fields using reflective API works.', () {
final testMap = TestMap();
final mapFieldInfo = testMap.info_.fieldInfo.values
.where((fieldInfo) =>
fieldInfo is MapFieldInfo && fieldInfo.name == 'int32ToBytesField')
.single;
final value = testMap.getField(mapFieldInfo.tagNumber);
expect(value is Map<int, List<int>>, true);
});
test('Parses null keys and values', () {
// Use a desugared version of the message to create missing
// values in the serialized form.
final d = Desugared()
..int32ToStringField.add(Desugared_Int32ToString()
..clearKey()
..value = 'abc')
..int32ToStringField.add(Desugared_Int32ToString()
..key = 42
..clearValue())
..int32ToStringField.add(Desugared_Int32ToString()
..key = 11
..value = 'def')
..stringToInt32Field.add(Desugared_StringToInt32()
..clearKey()
..value = 11)
..stringToInt32Field.add(Desugared_StringToInt32()
..key = 'abc'
..clearValue())
..stringToInt32Field.add(Desugared_StringToInt32()
..key = 'def'
..value = 42);
final m = TestMap.fromBuffer(d.writeToBuffer());
expect(m.int32ToStringField[0], 'abc');
expect(m.int32ToStringField[42], '');
expect(m.int32ToStringField[11], 'def');
expect(m.stringToInt32Field['abc'], 0);
expect(m.stringToInt32Field[''], 11);
expect(m.stringToInt32Field['def'], 42);
});
test('Map field reads should not affect equality or hash of message', () {
final m1 = TestMap.create();
final m2 = TestMap.create();
expect(m1, equals(m2));
expect(m1.hashCode, equals(m2.hashCode));
m1.int32ToStringField; // read a map field
expect(m1, equals(m2));
expect(m1.hashCode, equals(m2.hashCode));
});
test('getField and \$_getMap are in sync', () {
final msg1 = TestMap();
expect(msg1.hasField(1), false);
final map1 = msg1.getField(1) as Map<int, int>;
expect(msg1.hasField(1), true);
map1[1] = 2;
expect(msg1.int32ToInt32Field[1], 2);
final msg2 = TestMap();
expect(msg2.hasField(1), false);
final map2 = msg2.$_getMap(0) as Map<int, int>;
expect(msg2.hasField(1), true);
map2[1] = 2;
expect(msg2.int32ToInt32Field[1], 2);
});
test('Parses empty map fields', () {
// Map fields are encoded as messages (as length-delimited fields). Check
// that we handle 0 length fields. (#719)
{
final messageBytes = <int>[
(5 << 3) | 2, // tag = 5, wire type = 2 (length delimited)
0, // length = 0
];
final message = TestMap.fromBuffer(messageBytes);
expect(
message, TestMap()..int32ToMessageField[0] = TestMap_MessageValue());
}
{
final messageBytes = <int>[
(4 << 3) | 2, // tag = 4, wire type = 2 (length delimited)
0, // length = 0
];
final message = TestMap.fromBuffer(messageBytes);
expect(
message, TestMap()..int32ToEnumField[0] = TestMap_EnumValue.DEFAULT);
}
});
test('Parses map field with just key', () {
// Similar to the case above, but the field just has key (no value)
{
final messageBytes = <int>[
(5 << 3) | 2, // tag = 5, wire type = 2 (length delimited)
2, // length = 2
(1 << 3) | 0, // tag = 1 (map key), wire type = 0 (varint)
1, // key = 1
];
final message = TestMap.fromBuffer(messageBytes);
expect(
message, TestMap()..int32ToMessageField[1] = TestMap_MessageValue());
}
{
final messageBytes = <int>[
(4 << 3) | 2, // tag = 4, wire type = 2 (length delimited)
2, // length = 2
(1 << 3) | 0, // tag = 1 (map key), wire type = 0 (varint)
1, // key = 1
];
final message = TestMap.fromBuffer(messageBytes);
expect(
message, TestMap()..int32ToEnumField[1] = TestMap_EnumValue.DEFAULT);
}
});
test('Parses map field with just key', () {
// Similar to the case above, but the field just has value (no key)
{
final messageBytes = <int>[
(5 << 3) | 2, // tag = 5, wire type = 2 (length delimited)
2, // length = 2
(2 << 3) | 2, // tag = 2 (map value), wire type = 2 (length delimited)
0, // length = 0 (empty message)
];
final message = TestMap.fromBuffer(messageBytes);
expect(
message, TestMap()..int32ToMessageField[0] = TestMap_MessageValue());
}
{
final messageBytes = <int>[
(4 << 3) | 2, // tag = 4, wire type = 2 (length delimited)
2, // length = 2
(2 << 3) | 2, // tag = 2 (map value), wire type = 2 (length delimited)
1, // enum value = 1
];
final message = TestMap.fromBuffer(messageBytes);
expect(message, TestMap()..int32ToEnumField[0] = TestMap_EnumValue.BAR);
}
});
test('Read-only message uninitialized map field value is read-only', () {
final msg = TestMap()..freeze();
expect(() {
msg.int32ToInt32Field[0] = 1;
}, throwsA(const TypeMatcher<UnsupportedError>()));
});
}