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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
|
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#import "GPBTestUtilities.h"
#import <objc/runtime.h>
#import "GPBArray_PackagePrivate.h"
#import "GPBDescriptor.h"
#import "GPBDictionary_PackagePrivate.h"
#import "GPBMessage_PackagePrivate.h"
#import "GPBUnknownField_PackagePrivate.h"
#import "GPBUnknownFieldSet_PackagePrivate.h"
#import "google/protobuf/Unittest.pbobjc.h"
#import "google/protobuf/UnittestObjc.pbobjc.h"
@interface MessageTests : GPBTestCase
@end
@implementation MessageTests
// TODO(thomasvl): this should get split into a few files of logic junks, it is
// a jumble of things at the moment (and the testutils have a bunch of the real
// assertions).
- (TestAllTypes *)mergeSource {
TestAllTypes *message = [TestAllTypes message];
[message setOptionalInt32:1];
[message setOptionalString:@"foo"];
[message setOptionalForeignMessage:[ForeignMessage message]];
message.repeatedStringArray = [NSMutableArray array];
[message.repeatedStringArray addObject:@"bar"];
return message;
}
- (TestAllTypes *)mergeDestination {
TestAllTypes *message = [TestAllTypes message];
[message setOptionalInt64:2];
[message setOptionalString:@"baz"];
ForeignMessage *foreignMessage = [ForeignMessage message];
[foreignMessage setC:3];
[message setOptionalForeignMessage:foreignMessage];
message.repeatedStringArray = [NSMutableArray array];
[message.repeatedStringArray addObject:@"qux"];
return message;
}
- (TestAllTypes *)mergeDestinationWithoutForeignMessageIvar {
TestAllTypes *message = [TestAllTypes message];
[message setOptionalInt64:2];
[message setOptionalString:@"baz"];
message.repeatedStringArray = [NSMutableArray array];
[message.repeatedStringArray addObject:@"qux"];
return message;
}
- (TestAllTypes *)mergeResult {
TestAllTypes *message = [TestAllTypes message];
[message setOptionalInt32:1];
[message setOptionalInt64:2];
[message setOptionalString:@"foo"];
ForeignMessage *foreignMessage = [ForeignMessage message];
[foreignMessage setC:3];
[message setOptionalForeignMessage:foreignMessage];
message.repeatedStringArray = [NSMutableArray array];
[message.repeatedStringArray addObject:@"qux"];
[message.repeatedStringArray addObject:@"bar"];
return message;
}
- (TestAllTypes *)mergeResultForDestinationWithoutForeignMessageIvar {
TestAllTypes *message = [TestAllTypes message];
[message setOptionalInt32:1];
[message setOptionalInt64:2];
[message setOptionalString:@"foo"];
ForeignMessage *foreignMessage = [ForeignMessage message];
[message setOptionalForeignMessage:foreignMessage];
message.repeatedStringArray = [NSMutableArray array];
[message.repeatedStringArray addObject:@"qux"];
[message.repeatedStringArray addObject:@"bar"];
return message;
}
- (TestAllExtensions *)mergeExtensionsDestination {
TestAllExtensions *message = [TestAllExtensions message];
[message setExtension:[UnittestRoot optionalInt32Extension] value:@5];
[message setExtension:[UnittestRoot optionalStringExtension] value:@"foo"];
ForeignMessage *foreignMessage = [ForeignMessage message];
foreignMessage.c = 4;
[message setExtension:[UnittestRoot optionalForeignMessageExtension]
value:foreignMessage];
TestAllTypes_NestedMessage *nestedMessage =
[TestAllTypes_NestedMessage message];
[message setExtension:[UnittestRoot optionalNestedMessageExtension]
value:nestedMessage];
return message;
}
- (TestAllExtensions *)mergeExtensionsSource {
TestAllExtensions *message = [TestAllExtensions message];
[message setExtension:[UnittestRoot optionalInt64Extension] value:@6];
[message setExtension:[UnittestRoot optionalStringExtension] value:@"bar"];
ForeignMessage *foreignMessage = [ForeignMessage message];
[message setExtension:[UnittestRoot optionalForeignMessageExtension]
value:foreignMessage];
TestAllTypes_NestedMessage *nestedMessage =
[TestAllTypes_NestedMessage message];
nestedMessage.bb = 7;
[message setExtension:[UnittestRoot optionalNestedMessageExtension]
value:nestedMessage];
return message;
}
- (TestAllExtensions *)mergeExtensionsResult {
TestAllExtensions *message = [TestAllExtensions message];
[message setExtension:[UnittestRoot optionalInt32Extension] value:@5];
[message setExtension:[UnittestRoot optionalInt64Extension] value:@6];
[message setExtension:[UnittestRoot optionalStringExtension] value:@"bar"];
ForeignMessage *foreignMessage = [ForeignMessage message];
foreignMessage.c = 4;
[message setExtension:[UnittestRoot optionalForeignMessageExtension]
value:foreignMessage];
TestAllTypes_NestedMessage *nestedMessage =
[TestAllTypes_NestedMessage message];
nestedMessage.bb = 7;
[message setExtension:[UnittestRoot optionalNestedMessageExtension]
value:nestedMessage];
return message;
}
- (void)testMergeFrom {
TestAllTypes *result = [[self.mergeDestination copy] autorelease];
[result mergeFrom:self.mergeSource];
NSData *resultData = [result data];
NSData *mergeResultData = [self.mergeResult data];
XCTAssertEqualObjects(resultData, mergeResultData);
XCTAssertEqualObjects(result, self.mergeResult);
// Test when destination does not have an Ivar (type is an object) but source
// has such Ivar.
// The result must has the Ivar which is same as the one in source.
result = [[self.mergeDestinationWithoutForeignMessageIvar copy] autorelease];
[result mergeFrom:self.mergeSource];
resultData = [result data];
mergeResultData =
[self.mergeResultForDestinationWithoutForeignMessageIvar data];
XCTAssertEqualObjects(resultData, mergeResultData);
XCTAssertEqualObjects(
result, self.mergeResultForDestinationWithoutForeignMessageIvar);
// Test when destination is empty.
// The result must is same as the source.
result = [TestAllTypes message];
[result mergeFrom:self.mergeSource];
resultData = [result data];
mergeResultData = [self.mergeSource data];
XCTAssertEqualObjects(resultData, mergeResultData);
XCTAssertEqualObjects(result, self.mergeSource);
}
- (void)testMergeFromWithExtensions {
TestAllExtensions *result = [self mergeExtensionsDestination];
[result mergeFrom:[self mergeExtensionsSource]];
NSData *resultData = [result data];
NSData *mergeResultData = [[self mergeExtensionsResult] data];
XCTAssertEqualObjects(resultData, mergeResultData);
XCTAssertEqualObjects(result, [self mergeExtensionsResult]);
// Test merging from data.
result = [self mergeExtensionsDestination];
NSData *data = [[self mergeExtensionsSource] data];
XCTAssertNotNil(data);
[result mergeFromData:data
extensionRegistry:[UnittestRoot extensionRegistry]];
resultData = [result data];
XCTAssertEqualObjects(resultData, mergeResultData);
XCTAssertEqualObjects(result, [self mergeExtensionsResult]);
}
- (void)testIsEquals {
TestAllTypes *result = [[self.mergeDestination copy] autorelease];
[result mergeFrom:self.mergeSource];
XCTAssertEqualObjects(result.data, self.mergeResult.data);
XCTAssertEqualObjects(result, self.mergeResult);
TestAllTypes *result2 = [[self.mergeDestination copy] autorelease];
XCTAssertNotEqualObjects(result2.data, self.mergeResult.data);
XCTAssertNotEqualObjects(result2, self.mergeResult);
}
// =================================================================
// Required-field-related tests.
- (TestRequired *)testRequiredInitialized {
TestRequired *message = [TestRequired message];
[message setA:1];
[message setB:2];
[message setC:3];
return message;
}
- (void)testRequired {
TestRequired *message = [TestRequired message];
XCTAssertFalse(message.initialized);
[message setA:1];
XCTAssertFalse(message.initialized);
[message setB:1];
XCTAssertFalse(message.initialized);
[message setC:1];
XCTAssertTrue(message.initialized);
}
- (void)testRequiredForeign {
TestRequiredForeign *message = [TestRequiredForeign message];
XCTAssertTrue(message.initialized);
[message setOptionalMessage:[TestRequired message]];
XCTAssertFalse(message.initialized);
[message setOptionalMessage:self.testRequiredInitialized];
XCTAssertTrue(message.initialized);
message.repeatedMessageArray = [NSMutableArray array];
[message.repeatedMessageArray addObject:[TestRequired message]];
XCTAssertFalse(message.initialized);
[message.repeatedMessageArray removeAllObjects];
[message.repeatedMessageArray addObject:self.testRequiredInitialized];
XCTAssertTrue(message.initialized);
}
- (void)testRequiredExtension {
TestAllExtensions *message = [TestAllExtensions message];
XCTAssertTrue(message.initialized);
[message setExtension:[TestRequired single] value:[TestRequired message]];
XCTAssertFalse(message.initialized);
[message setExtension:[TestRequired single]
value:self.testRequiredInitialized];
XCTAssertTrue(message.initialized);
[message addExtension:[TestRequired multi] value:[TestRequired message]];
XCTAssertFalse(message.initialized);
[message setExtension:[TestRequired multi]
index:0
value:self.testRequiredInitialized];
XCTAssertTrue(message.initialized);
}
- (void)testDataFromUninitialized {
TestRequired *message = [TestRequired message];
NSData *data = [message data];
// In DEBUG, the data generation will fail, but in non DEBUG, it passes
// because the check isn't done (for speed).
#ifdef DEBUG
XCTAssertNil(data);
#else
XCTAssertNotNil(data);
XCTAssertFalse(message.initialized);
#endif // DEBUG
}
- (void)testInitialized {
// We're mostly testing that no exception is thrown.
TestRequired *message = [TestRequired message];
XCTAssertFalse(message.initialized);
}
- (void)testDataFromNestedUninitialized {
TestRequiredForeign *message = [TestRequiredForeign message];
[message setOptionalMessage:[TestRequired message]];
message.repeatedMessageArray = [NSMutableArray array];
[message.repeatedMessageArray addObject:[TestRequired message]];
[message.repeatedMessageArray addObject:[TestRequired message]];
NSData *data = [message data];
// In DEBUG, the data generation will fail, but in non DEBUG, it passes
// because the check isn't done (for speed).
#ifdef DEBUG
XCTAssertNil(data);
#else
XCTAssertNotNil(data);
XCTAssertFalse(message.initialized);
#endif // DEBUG
}
- (void)testNestedInitialized {
// We're mostly testing that no exception is thrown.
TestRequiredForeign *message = [TestRequiredForeign message];
[message setOptionalMessage:[TestRequired message]];
message.repeatedMessageArray = [NSMutableArray array];
[message.repeatedMessageArray addObject:[TestRequired message]];
[message.repeatedMessageArray addObject:[TestRequired message]];
XCTAssertFalse(message.initialized);
}
- (void)testParseUninitialized {
NSError *error = nil;
TestRequired *msg =
[TestRequired parseFromData:GPBEmptyNSData() error:&error];
// In DEBUG, the parse will fail, but in non DEBUG, it passes because
// the check isn't done (for speed).
#ifdef DEBUG
XCTAssertNil(msg);
XCTAssertNotNil(error);
XCTAssertEqualObjects(error.domain, GPBMessageErrorDomain);
XCTAssertEqual(error.code, GPBMessageErrorCodeMissingRequiredField);
#else
XCTAssertNotNil(msg);
XCTAssertNil(error);
XCTAssertFalse(msg.initialized);
#endif // DEBUG
}
- (void)testCoding {
NSData *data =
[NSKeyedArchiver archivedDataWithRootObject:[self mergeResult]];
id unarchivedObject = [NSKeyedUnarchiver unarchiveObjectWithData:data];
XCTAssertEqualObjects(unarchivedObject, [self mergeResult]);
// Intentionally doing a pointer comparison.
XCTAssertNotEqual(unarchivedObject, [self mergeResult]);
}
- (void)testObjectReset {
// Tests a failure where clearing out defaults values caused an over release.
TestAllTypes *message = [TestAllTypes message];
message.hasOptionalNestedMessage = NO;
[message setOptionalNestedMessage:[TestAllTypes_NestedMessage message]];
message.hasOptionalNestedMessage = NO;
[message setOptionalNestedMessage:[TestAllTypes_NestedMessage message]];
[message setOptionalNestedMessage:nil];
message.hasOptionalNestedMessage = NO;
}
- (void)testSettingHasToYes {
TestAllTypes *message = [TestAllTypes message];
XCTAssertThrows([message setHasOptionalNestedMessage:YES]);
}
- (void)testRoot {
XCTAssertNotNil([UnittestRoot extensionRegistry]);
}
- (void)testGPBMessageSize {
// See the note in GPBMessage_PackagePrivate.h about why we want to keep the
// base instance size pointer size aligned.
size_t messageSize = class_getInstanceSize([GPBMessage class]);
XCTAssertEqual((messageSize % sizeof(void *)), (size_t)0,
@"Base size isn't pointer size aligned");
// Since we add storage ourselves (see +allocWithZone: in GPBMessage), confirm
// that the size of some generated classes is still the same as the base for
// that logic to work as desired.
size_t testMessageSize = class_getInstanceSize([TestAllTypes class]);
XCTAssertEqual(testMessageSize, messageSize);
}
- (void)testInit {
TestAllTypes *message = [TestAllTypes message];
[self assertClear:message];
}
- (void)testAccessors {
TestAllTypes *message = [TestAllTypes message];
[self setAllFields:message repeatedCount:kGPBDefaultRepeatCount];
[self assertAllFieldsSet:message repeatedCount:kGPBDefaultRepeatCount];
}
- (void)testKVC_ValueForKey {
TestAllTypes *message = [TestAllTypes message];
[self setAllFields:message repeatedCount:kGPBDefaultRepeatCount];
[self assertAllFieldsKVCMatch:message];
}
- (void)testKVC_SetValue_ForKey {
TestAllTypes *message = [TestAllTypes message];
[self setAllFieldsViaKVC:message repeatedCount:kGPBDefaultRepeatCount];
[self assertAllFieldsKVCMatch:message];
[self assertAllFieldsSet:message repeatedCount:kGPBDefaultRepeatCount];
[self assertAllFieldsKVCMatch:message];
}
- (void)testDescription {
// No real test, just exercise code
TestAllTypes *message = [TestAllTypes message];
[self setAllFields:message repeatedCount:kGPBDefaultRepeatCount];
GPBUnknownFieldSet *unknownFields =
[[[GPBUnknownFieldSet alloc] init] autorelease];
GPBUnknownField *field =
[[[GPBUnknownField alloc] initWithNumber:2] autorelease];
[field addVarint:2];
[unknownFields addField:field];
field = [[[GPBUnknownField alloc] initWithNumber:3] autorelease];
[field addVarint:4];
[unknownFields addField:field];
[message setUnknownFields:unknownFields];
NSString *description = [message description];
XCTAssertGreaterThan([description length], 0U);
GPBMessage *message2 = [TestAllExtensions message];
[message2 setExtension:[UnittestRoot optionalInt32Extension] value:@1];
[message2 addExtension:[UnittestRoot repeatedInt32Extension] value:@2];
description = [message2 description];
XCTAssertGreaterThan([description length], 0U);
}
- (void)testSetter {
// Test to make sure that if we set a value that has a default value
// with the default, that the has is set, and the value gets put into the
// message correctly.
TestAllTypes *message = [TestAllTypes message];
GPBDescriptor *descriptor = [[message class] descriptor];
XCTAssertNotNil(descriptor);
GPBFieldDescriptor *fieldDescriptor =
[descriptor fieldWithName:@"defaultInt32"];
XCTAssertNotNil(fieldDescriptor);
GPBGenericValue defaultValue = [fieldDescriptor defaultValue];
[message setDefaultInt32:defaultValue.valueInt32];
XCTAssertTrue(message.hasDefaultInt32);
XCTAssertEqual(message.defaultInt32, defaultValue.valueInt32);
// Do the same thing with an object type.
message = [TestAllTypes message];
fieldDescriptor = [descriptor fieldWithName:@"defaultString"];
XCTAssertNotNil(fieldDescriptor);
defaultValue = [fieldDescriptor defaultValue];
[message setDefaultString:defaultValue.valueString];
XCTAssertTrue(message.hasDefaultString);
XCTAssertEqualObjects(message.defaultString, defaultValue.valueString);
// Test default string type.
message = [TestAllTypes message];
XCTAssertEqualObjects(message.defaultString, @"hello");
XCTAssertFalse(message.hasDefaultString);
fieldDescriptor = [descriptor fieldWithName:@"defaultString"];
XCTAssertNotNil(fieldDescriptor);
defaultValue = [fieldDescriptor defaultValue];
[message setDefaultString:defaultValue.valueString];
XCTAssertEqualObjects(message.defaultString, @"hello");
XCTAssertTrue(message.hasDefaultString);
[message setDefaultString:nil];
XCTAssertEqualObjects(message.defaultString, @"hello");
XCTAssertFalse(message.hasDefaultString);
message.hasDefaultString = NO;
XCTAssertFalse(message.hasDefaultString);
XCTAssertEqualObjects(message.defaultString, @"hello");
// Test default bytes type.
NSData *defaultBytes = [@"world" dataUsingEncoding:NSUTF8StringEncoding];
XCTAssertEqualObjects(message.defaultBytes, defaultBytes);
XCTAssertFalse(message.hasDefaultString);
fieldDescriptor = [descriptor fieldWithName:@"defaultBytes"];
XCTAssertNotNil(fieldDescriptor);
defaultValue = [fieldDescriptor defaultValue];
[message setDefaultBytes:defaultValue.valueData];
XCTAssertEqualObjects(message.defaultBytes, defaultBytes);
XCTAssertTrue(message.hasDefaultBytes);
[message setDefaultBytes:nil];
XCTAssertEqualObjects(message.defaultBytes, defaultBytes);
XCTAssertFalse(message.hasDefaultBytes);
message.hasDefaultBytes = NO;
XCTAssertFalse(message.hasDefaultBytes);
XCTAssertEqualObjects(message.defaultBytes, defaultBytes);
// Test optional string.
XCTAssertFalse(message.hasOptionalString);
XCTAssertEqualObjects(message.optionalString, @"");
XCTAssertFalse(message.hasOptionalString);
message.optionalString = nil;
XCTAssertFalse(message.hasOptionalString);
XCTAssertEqualObjects(message.optionalString, @"");
NSString *string = @"string";
message.optionalString = string;
XCTAssertEqualObjects(message.optionalString, string);
XCTAssertTrue(message.hasOptionalString);
message.optionalString = nil;
XCTAssertFalse(message.hasOptionalString);
XCTAssertEqualObjects(message.optionalString, @"");
// Test optional data.
XCTAssertFalse(message.hasOptionalBytes);
XCTAssertEqualObjects(message.optionalBytes, GPBEmptyNSData());
XCTAssertFalse(message.hasOptionalBytes);
message.optionalBytes = nil;
XCTAssertFalse(message.hasOptionalBytes);
XCTAssertEqualObjects(message.optionalBytes, GPBEmptyNSData());
NSData *data = [@"bytes" dataUsingEncoding:NSUTF8StringEncoding];
message.optionalBytes = data;
XCTAssertEqualObjects(message.optionalBytes, data);
XCTAssertTrue(message.hasOptionalBytes);
message.optionalBytes = nil;
XCTAssertFalse(message.hasOptionalBytes);
XCTAssertEqualObjects(message.optionalBytes, GPBEmptyNSData());
// Test lazy message setting
XCTAssertFalse(message.hasOptionalLazyMessage);
XCTAssertNotNil(message.optionalLazyMessage);
XCTAssertFalse(message.hasOptionalLazyMessage);
message.hasOptionalLazyMessage = NO;
XCTAssertFalse(message.hasOptionalLazyMessage);
XCTAssertNotNil(message.optionalLazyMessage);
XCTAssertFalse(message.hasOptionalLazyMessage);
message.optionalLazyMessage = nil;
XCTAssertFalse(message.hasOptionalLazyMessage);
// Test nested messages
XCTAssertFalse(message.hasOptionalLazyMessage);
message.optionalLazyMessage.bb = 1;
XCTAssertTrue(message.hasOptionalLazyMessage);
XCTAssertEqual(message.optionalLazyMessage.bb, 1);
XCTAssertNotNil(message.optionalLazyMessage);
message.optionalLazyMessage = nil;
XCTAssertFalse(message.hasOptionalLazyMessage);
XCTAssertEqual(message.optionalLazyMessage.bb, 0);
XCTAssertFalse(message.hasOptionalLazyMessage);
XCTAssertNotNil(message.optionalLazyMessage);
// -testDefaultSubMessages tests the "defaulting" handling of fields
// containing messages.
}
- (void)testRepeatedSetters {
TestAllTypes *message = [TestAllTypes message];
[self setAllFields:message repeatedCount:kGPBDefaultRepeatCount];
[self modifyRepeatedFields:message];
[self assertRepeatedFieldsModified:message
repeatedCount:kGPBDefaultRepeatCount];
}
- (void)testClear {
TestAllTypes *message = [TestAllTypes message];
[self setAllFields:message repeatedCount:kGPBDefaultRepeatCount];
[self clearAllFields:message];
[self assertClear:message];
TestAllTypes *message2 = [TestAllTypes message];
XCTAssertEqualObjects(message, message2);
}
- (void)testClearKVC {
TestAllTypes *message = [TestAllTypes message];
[self setAllFields:message repeatedCount:kGPBDefaultRepeatCount];
[self clearAllFields:message];
[self assertClear:message];
[self assertClearKVC:message];
}
- (void)testClearExtension {
// clearExtension() is not actually used in TestUtil, so try it manually.
GPBMessage *message1 = [TestAllExtensions message];
[message1 setExtension:[UnittestRoot optionalInt32Extension] value:@1];
XCTAssertTrue([message1 hasExtension:[UnittestRoot optionalInt32Extension]]);
[message1 clearExtension:[UnittestRoot optionalInt32Extension]];
XCTAssertFalse([message1 hasExtension:[UnittestRoot optionalInt32Extension]]);
GPBMessage *message2 = [TestAllExtensions message];
[message2 addExtension:[UnittestRoot repeatedInt32Extension] value:@1];
XCTAssertEqual(
[[message2 getExtension:[UnittestRoot repeatedInt32Extension]] count],
(NSUInteger)1);
[message2 clearExtension:[UnittestRoot repeatedInt32Extension]];
XCTAssertEqual(
[[message2 getExtension:[UnittestRoot repeatedInt32Extension]] count],
(NSUInteger)0);
// Clearing an unset extension field shouldn't make the target message
// visible.
GPBMessage *message3 = [TestAllExtensions message];
GPBMessage *extension_msg =
[message3 getExtension:[UnittestObjcRoot recursiveExtension]];
XCTAssertFalse([message3 hasExtension:[UnittestObjcRoot recursiveExtension]]);
[extension_msg clearExtension:[UnittestRoot optionalInt32Extension]];
XCTAssertFalse([message3 hasExtension:[UnittestObjcRoot recursiveExtension]]);
}
- (void)testDefaultingSubMessages {
TestAllTypes *message = [TestAllTypes message];
// Initially they should all not have values.
XCTAssertFalse(message.hasOptionalGroup);
XCTAssertFalse(message.hasOptionalNestedMessage);
XCTAssertFalse(message.hasOptionalForeignMessage);
XCTAssertFalse(message.hasOptionalImportMessage);
XCTAssertFalse(message.hasOptionalPublicImportMessage);
XCTAssertFalse(message.hasOptionalLazyMessage);
// They should auto create something when fetched.
TestAllTypes_OptionalGroup *optionalGroup = [message.optionalGroup retain];
TestAllTypes_NestedMessage *optionalNestedMessage =
[message.optionalNestedMessage retain];
ForeignMessage *optionalForeignMessage =
[message.optionalForeignMessage retain];
ImportMessage *optionalImportMessage = [message.optionalImportMessage retain];
PublicImportMessage *optionalPublicImportMessage =
[message.optionalPublicImportMessage retain];
TestAllTypes_NestedMessage *optionalLazyMessage =
[message.optionalLazyMessage retain];
XCTAssertNotNil(optionalGroup);
XCTAssertNotNil(optionalNestedMessage);
XCTAssertNotNil(optionalForeignMessage);
XCTAssertNotNil(optionalImportMessage);
XCTAssertNotNil(optionalPublicImportMessage);
XCTAssertNotNil(optionalLazyMessage);
// Although they were created, they should not respond to hasValue until that
// submessage is mutated.
XCTAssertFalse(message.hasOptionalGroup);
XCTAssertFalse(message.hasOptionalNestedMessage);
XCTAssertFalse(message.hasOptionalForeignMessage);
XCTAssertFalse(message.hasOptionalImportMessage);
XCTAssertFalse(message.hasOptionalPublicImportMessage);
XCTAssertFalse(message.hasOptionalLazyMessage);
// And they set that value back in to the message since the value created was
// mutable (so a second fetch should give the same object).
XCTAssertEqual(message.optionalGroup, optionalGroup);
XCTAssertEqual(message.optionalNestedMessage, optionalNestedMessage);
XCTAssertEqual(message.optionalForeignMessage, optionalForeignMessage);
XCTAssertEqual(message.optionalImportMessage, optionalImportMessage);
XCTAssertEqual(message.optionalPublicImportMessage,
optionalPublicImportMessage);
XCTAssertEqual(message.optionalLazyMessage, optionalLazyMessage);
// And the default objects for a second message should be distinct (again,
// since they are mutable, each needs their own copy).
TestAllTypes *message2 = [TestAllTypes message];
// Intentionally doing a pointer comparison.
XCTAssertNotEqual(message2.optionalGroup, optionalGroup);
XCTAssertNotEqual(message2.optionalNestedMessage, optionalNestedMessage);
XCTAssertNotEqual(message2.optionalForeignMessage, optionalForeignMessage);
XCTAssertNotEqual(message2.optionalImportMessage, optionalImportMessage);
XCTAssertNotEqual(message2.optionalPublicImportMessage,
optionalPublicImportMessage);
XCTAssertNotEqual(message2.optionalLazyMessage, optionalLazyMessage);
// Setting the values to nil will clear the has flag, and on next access you
// get back new submessages.
message.optionalGroup = nil;
message.optionalNestedMessage = nil;
message.optionalForeignMessage = nil;
message.optionalImportMessage = nil;
message.optionalPublicImportMessage = nil;
message.optionalLazyMessage = nil;
XCTAssertFalse(message.hasOptionalGroup);
XCTAssertFalse(message.hasOptionalNestedMessage);
XCTAssertFalse(message.hasOptionalForeignMessage);
XCTAssertFalse(message.hasOptionalImportMessage);
XCTAssertFalse(message.hasOptionalPublicImportMessage);
XCTAssertFalse(message.hasOptionalLazyMessage);
// Intentionally doing a pointer comparison.
XCTAssertNotEqual(message.optionalGroup, optionalGroup);
XCTAssertNotEqual(message.optionalNestedMessage, optionalNestedMessage);
XCTAssertNotEqual(message.optionalForeignMessage, optionalForeignMessage);
XCTAssertNotEqual(message.optionalImportMessage, optionalImportMessage);
XCTAssertNotEqual(message.optionalPublicImportMessage,
optionalPublicImportMessage);
XCTAssertNotEqual(message.optionalLazyMessage, optionalLazyMessage);
[optionalGroup release];
[optionalNestedMessage release];
[optionalForeignMessage release];
[optionalImportMessage release];
[optionalPublicImportMessage release];
[optionalLazyMessage release];
}
- (void)testMultiplePointersToAutocreatedMessage {
// Multiple objects pointing to the same autocreated message.
TestAllTypes *message = [TestAllTypes message];
TestAllTypes *message2 = [TestAllTypes message];
message2.optionalGroup = message.optionalGroup;
XCTAssertTrue([message2 hasOptionalGroup]);
XCTAssertFalse([message hasOptionalGroup]);
message2.optionalGroup.a = 42;
XCTAssertTrue([message hasOptionalGroup]);
XCTAssertTrue([message2 hasOptionalGroup]);
}
- (void)testCopyWithAutocreatedMessage {
// Mutable copy should not copy autocreated messages.
TestAllTypes *message = [TestAllTypes message];
message.optionalGroup.a = 42;
XCTAssertNotNil(message.optionalNestedMessage);
TestAllTypes *message2 = [[message copy] autorelease];
XCTAssertTrue([message2 hasOptionalGroup]);
XCTAssertFalse([message2 hasOptionalNestedMessage]);
// Intentionally doing a pointer comparison.
XCTAssertNotEqual(message.optionalNestedMessage,
message2.optionalNestedMessage);
}
- (void)testClearAutocreatedSubmessage {
// Call clear on an intermediate submessage should cause it to get recreated
// on the next call.
TestRecursiveMessage *message = [TestRecursiveMessage message];
TestRecursiveMessage *message_inner = [message.a.a.a retain];
XCTAssertNotNil(message_inner);
XCTAssertTrue(GPBWasMessageAutocreatedBy(message_inner, message.a.a));
[message.a.a clear];
XCTAssertFalse(GPBWasMessageAutocreatedBy(message_inner, message.a.a));
// Intentionally doing a pointer comparison.
XCTAssertNotEqual(message.a.a.a, message_inner);
[message_inner release];
}
- (void)testRetainAutocreatedSubmessage {
// Should be able to retain autocreated submessage while the creator is
// dealloced.
TestAllTypes *message = [TestAllTypes message];
ForeignMessage *subMessage;
@autoreleasepool {
TestAllTypes *message2 = [TestAllTypes message];
subMessage = message2.optionalForeignMessage; // Autocreated
message.optionalForeignMessage = subMessage;
XCTAssertTrue(GPBWasMessageAutocreatedBy(message.optionalForeignMessage,
message2));
}
// Should be the same object, and should still be live.
XCTAssertEqual(message.optionalForeignMessage, subMessage);
XCTAssertNotNil([subMessage description]);
}
- (void)testSetNilAutocreatedSubmessage {
TestRecursiveMessage *message = [TestRecursiveMessage message];
TestRecursiveMessage *message_inner = [message.a.a retain];
XCTAssertFalse([message hasA]);
XCTAssertFalse([message.a hasA]);
message.a.a = nil;
// |message.a| has to be made visible, but |message.a.a| was set to nil so
// shouldn't be.
XCTAssertTrue([message hasA]);
XCTAssertFalse([message.a hasA]);
// Setting submessage to nil should cause it to lose its creator.
XCTAssertFalse(GPBWasMessageAutocreatedBy(message_inner, message.a));
// After setting to nil, getting it again should create a new autocreated
// message.
// Intentionally doing a pointer comparison.
XCTAssertNotEqual(message.a.a, message_inner);
[message_inner release];
}
- (void)testSetDoesntHaveAutocreatedSubmessage {
// Clearing submessage (set has == NO) should NOT cause it to lose its
// creator.
TestAllTypes *message = [TestAllTypes message];
TestAllTypes_NestedMessage *nestedMessage = message.optionalNestedMessage;
XCTAssertFalse([message hasOptionalNestedMessage]);
[message setHasOptionalNestedMessage:NO];
XCTAssertFalse([message hasOptionalNestedMessage]);
XCTAssertEqual(message.optionalNestedMessage, nestedMessage);
}
- (void)testSetAutocreatedMessageBecomesVisible {
// Setting a value should cause the submessage to appear to its creator.
// Test this several levels deep.
TestRecursiveMessage *message = [TestRecursiveMessage message];
message.a.a.a.a.i = 42;
XCTAssertTrue([message hasA]);
XCTAssertTrue([message.a hasA]);
XCTAssertTrue([message.a.a hasA]);
XCTAssertTrue([message.a.a.a hasA]);
XCTAssertFalse([message.a.a.a.a hasA]);
XCTAssertEqual(message.a.a.a.a.i, 42);
}
- (void)testClearUnsetFieldOfAutocreatedMessage {
// Clearing an unset field should not cause the submessage to appear to its
// creator.
TestRecursiveMessage *message = [TestRecursiveMessage message];
message.a.a.a.a.hasI = NO;
XCTAssertFalse([message hasA]);
XCTAssertFalse([message.a hasA]);
XCTAssertFalse([message.a.a hasA]);
XCTAssertFalse([message.a.a.a hasA]);
}
- (void)testAutocreatedSubmessageAssignSkip {
TestRecursiveMessage *message = [TestRecursiveMessage message];
TestRecursiveMessage *messageLevel1 = [message.a retain];
TestRecursiveMessage *messageLevel2 = [message.a.a retain];
TestRecursiveMessage *messageLevel3 = [message.a.a.a retain];
TestRecursiveMessage *messageLevel4 = [message.a.a.a.a retain];
XCTAssertTrue(GPBWasMessageAutocreatedBy(messageLevel1, message));
XCTAssertTrue(GPBWasMessageAutocreatedBy(messageLevel2, messageLevel1));
XCTAssertTrue(GPBWasMessageAutocreatedBy(messageLevel3, messageLevel2));
XCTAssertTrue(GPBWasMessageAutocreatedBy(messageLevel4, messageLevel3));
// Test skipping over an autocreated submessage and ensure it gets unset.
message.a = message.a.a;
XCTAssertEqual(message.a, messageLevel2);
XCTAssertTrue([message hasA]);
XCTAssertEqual(message.a.a, messageLevel3);
XCTAssertFalse([message.a hasA]);
XCTAssertEqual(message.a.a.a, messageLevel4);
XCTAssertFalse(GPBWasMessageAutocreatedBy(messageLevel1,
message)); // Because it was orphaned.
XCTAssertTrue(GPBWasMessageAutocreatedBy(messageLevel2, messageLevel1));
XCTAssertTrue(GPBWasMessageAutocreatedBy(messageLevel3, messageLevel2));
[messageLevel1 release];
[messageLevel2 release];
[messageLevel3 release];
[messageLevel4 release];
}
- (void)testAutocreatedSubmessageAssignLoop {
TestRecursiveMessage *message = [TestRecursiveMessage message];
TestRecursiveMessage *messageLevel1 = [message.a retain];
TestRecursiveMessage *messageLevel2 = [message.a.a retain];
TestRecursiveMessage *messageLevel3 = [message.a.a.a retain];
TestRecursiveMessage *messageLevel4 = [message.a.a.a.a retain];
XCTAssertTrue(GPBWasMessageAutocreatedBy(messageLevel1, message));
XCTAssertTrue(GPBWasMessageAutocreatedBy(messageLevel2, messageLevel1));
XCTAssertTrue(GPBWasMessageAutocreatedBy(messageLevel3, messageLevel2));
XCTAssertTrue(GPBWasMessageAutocreatedBy(messageLevel4, messageLevel3));
// Test a property with a loop. You'd never do this but at least ensure the
// autocreated submessages behave sanely.
message.a.a = message.a;
XCTAssertTrue([message hasA]);
XCTAssertEqual(message.a, messageLevel1);
XCTAssertTrue([message.a hasA]);
XCTAssertEqual(message.a.a, messageLevel1);
XCTAssertTrue([message.a.a hasA]);
XCTAssertEqual(message.a.a.a, messageLevel1);
XCTAssertFalse(GPBWasMessageAutocreatedBy(messageLevel1,
message)); // Because it was assigned.
XCTAssertFalse(GPBWasMessageAutocreatedBy(messageLevel2,
messageLevel1)); // Because it was orphaned.
XCTAssertFalse([messageLevel2 hasA]);
// Break the retain loop.
message.a.a = nil;
XCTAssertTrue([message hasA]);
XCTAssertFalse([message.a hasA]);
[messageLevel1 release];
[messageLevel2 release];
[messageLevel3 release];
[messageLevel4 release];
}
- (void)testSetAutocreatedSubmessage {
// Setting autocreated submessage to another value should cause the old one to
// lose its creator.
TestAllTypes *message = [TestAllTypes message];
TestAllTypes_NestedMessage *nestedMessage =
[message.optionalNestedMessage retain];
message.optionalNestedMessage = [TestAllTypes_NestedMessage message];
XCTAssertTrue([message hasOptionalNestedMessage]);
XCTAssertTrue(message.optionalNestedMessage != nestedMessage);
XCTAssertFalse(GPBWasMessageAutocreatedBy(nestedMessage, message));
[nestedMessage release];
}
- (void)testAutocreatedUnknownFields {
// Doing anything with (except reading) unknown fields should cause the
// submessage to become visible.
TestAllTypes *message = [TestAllTypes message];
XCTAssertNotNil(message.optionalNestedMessage);
XCTAssertFalse([message hasOptionalNestedMessage]);
XCTAssertNil(message.optionalNestedMessage.unknownFields);
XCTAssertFalse([message hasOptionalNestedMessage]);
GPBUnknownFieldSet *unknownFields =
[[[GPBUnknownFieldSet alloc] init] autorelease];
message.optionalNestedMessage.unknownFields = unknownFields;
XCTAssertTrue([message hasOptionalNestedMessage]);
message.optionalNestedMessage = nil;
XCTAssertFalse([message hasOptionalNestedMessage]);
[message.optionalNestedMessage setUnknownFields:unknownFields];
XCTAssertTrue([message hasOptionalNestedMessage]);
}
- (void)testSetAutocreatedSubmessageToSelf {
// Setting submessage to itself should cause it to become visible.
TestAllTypes *message = [TestAllTypes message];
XCTAssertNotNil(message.optionalNestedMessage);
XCTAssertFalse([message hasOptionalNestedMessage]);
message.optionalNestedMessage = message.optionalNestedMessage;
XCTAssertTrue([message hasOptionalNestedMessage]);
}
- (void)testAutocreatedSubmessageMemoryLeaks {
// Test for memory leaks with autocreated submessages.
TestRecursiveMessage *message;
TestRecursiveMessage *messageLevel1;
TestRecursiveMessage *messageLevel2;
TestRecursiveMessage *messageLevel3;
TestRecursiveMessage *messageLevel4;
@autoreleasepool {
message = [[TestRecursiveMessage alloc] init];
messageLevel1 = [message.a retain];
messageLevel2 = [message.a.a retain];
messageLevel3 = [message.a.a.a retain];
messageLevel4 = [message.a.a.a.a retain];
message.a.i = 1;
}
XCTAssertEqual(message.retainCount, (NSUInteger)1);
[message release];
XCTAssertEqual(messageLevel1.retainCount, (NSUInteger)1);
[messageLevel1 release];
XCTAssertEqual(messageLevel2.retainCount, (NSUInteger)1);
[messageLevel2 release];
XCTAssertEqual(messageLevel3.retainCount, (NSUInteger)1);
[messageLevel3 release];
XCTAssertEqual(messageLevel4.retainCount, (NSUInteger)1);
[messageLevel4 release];
}
- (void)testDefaultingArrays {
// Basic tests for default creation of arrays in a message.
TestRecursiveMessageWithRepeatedField *message =
[TestRecursiveMessageWithRepeatedField message];
TestRecursiveMessageWithRepeatedField *message2 =
[TestRecursiveMessageWithRepeatedField message];
// Simply accessing the array should not make any fields visible.
XCTAssertNotNil(message.a.a.iArray);
XCTAssertFalse([message hasA]);
XCTAssertFalse([message.a hasA]);
XCTAssertNotNil(message2.a.a.strArray);
XCTAssertFalse([message2 hasA]);
XCTAssertFalse([message2.a hasA]);
// But adding an element to the array should.
[message.a.a.iArray addValue:42];
XCTAssertTrue([message hasA]);
XCTAssertTrue([message.a hasA]);
XCTAssertEqual([message.a.a.iArray count], (NSUInteger)1);
[message2.a.a.strArray addObject:@"foo"];
XCTAssertTrue([message2 hasA]);
XCTAssertTrue([message2.a hasA]);
XCTAssertEqual([message2.a.a.strArray count], (NSUInteger)1);
}
- (void)testAutocreatedArrayShared {
// Multiple objects pointing to the same array.
TestRecursiveMessageWithRepeatedField *message1a =
[TestRecursiveMessageWithRepeatedField message];
TestRecursiveMessageWithRepeatedField *message1b =
[TestRecursiveMessageWithRepeatedField message];
message1a.a.iArray = message1b.a.iArray;
XCTAssertTrue([message1a hasA]);
XCTAssertFalse([message1b hasA]);
[message1a.a.iArray addValue:1];
XCTAssertTrue([message1a hasA]);
XCTAssertTrue([message1b hasA]);
XCTAssertEqual(message1a.a.iArray, message1b.a.iArray);
TestRecursiveMessageWithRepeatedField *message2a =
[TestRecursiveMessageWithRepeatedField message];
TestRecursiveMessageWithRepeatedField *message2b =
[TestRecursiveMessageWithRepeatedField message];
message2a.a.strArray = message2b.a.strArray;
XCTAssertTrue([message2a hasA]);
XCTAssertFalse([message2b hasA]);
[message2a.a.strArray addObject:@"bar"];
XCTAssertTrue([message2a hasA]);
XCTAssertTrue([message2b hasA]);
XCTAssertEqual(message2a.a.strArray, message2b.a.strArray);
}
- (void)testAutocreatedArrayCopy {
// Copy should not copy autocreated arrays.
TestAllTypes *message = [TestAllTypes message];
XCTAssertNotNil(message.repeatedStringArray);
XCTAssertNotNil(message.repeatedInt32Array);
TestAllTypes *message2 = [[message copy] autorelease];
// Pointer conparisions.
XCTAssertNotEqual(message.repeatedStringArray, message2.repeatedStringArray);
XCTAssertNotEqual(message.repeatedInt32Array, message2.repeatedInt32Array);
// Mutable copy should copy empty arrays that were explicitly set (end up
// with different objects that are equal).
TestAllTypes *message3 = [TestAllTypes message];
message3.repeatedInt32Array = [GPBInt32Array arrayWithValue:42];
message3.repeatedStringArray = [NSMutableArray arrayWithObject:@"wee"];
XCTAssertNotNil(message.repeatedInt32Array);
XCTAssertNotNil(message.repeatedStringArray);
TestAllTypes *message4 = [[message3 copy] autorelease];
XCTAssertNotEqual(message3.repeatedInt32Array, message4.repeatedInt32Array);
XCTAssertEqualObjects(message3.repeatedInt32Array,
message4.repeatedInt32Array);
XCTAssertNotEqual(message3.repeatedStringArray, message4.repeatedStringArray);
XCTAssertEqualObjects(message3.repeatedStringArray,
message4.repeatedStringArray);
}
- (void)testAutocreatedArrayRetain {
// Should be able to retain autocreated array while the creator is dealloced.
TestAllTypes *message = [TestAllTypes message];
@autoreleasepool {
TestAllTypes *message2 = [TestAllTypes message];
message.repeatedInt32Array = message2.repeatedInt32Array;
message.repeatedStringArray = message2.repeatedStringArray;
// Pointer conparision
XCTAssertEqual(message.repeatedInt32Array->_autocreator, message2);
XCTAssertTrue([message.repeatedStringArray
isKindOfClass:[GPBAutocreatedArray class]]);
XCTAssertEqual(
((GPBAutocreatedArray *)message.repeatedStringArray)->_autocreator,
message2);
}
XCTAssertNil(message.repeatedInt32Array->_autocreator);
XCTAssertTrue(
[message.repeatedStringArray isKindOfClass:[GPBAutocreatedArray class]]);
XCTAssertNil(
((GPBAutocreatedArray *)message.repeatedStringArray)->_autocreator);
}
- (void)testSetNilAutocreatedArray {
// Setting array to nil should cause it to lose its delegate.
TestAllTypes *message = [TestAllTypes message];
GPBInt32Array *repeatedInt32Array = [message.repeatedInt32Array retain];
GPBAutocreatedArray *repeatedStringArray =
(GPBAutocreatedArray *)[message.repeatedStringArray retain];
XCTAssertTrue([repeatedStringArray isKindOfClass:[GPBAutocreatedArray class]]);
XCTAssertEqual(repeatedInt32Array->_autocreator, message);
XCTAssertEqual(repeatedStringArray->_autocreator, message);
message.repeatedInt32Array = nil;
message.repeatedStringArray = nil;
XCTAssertNil(repeatedInt32Array->_autocreator);
XCTAssertNil(repeatedStringArray->_autocreator);
[repeatedInt32Array release];
[repeatedStringArray release];
}
- (void)testReplaceAutocreatedArray {
// Replacing array should orphan the old one and cause its creator to become
// visible.
{
TestRecursiveMessageWithRepeatedField *message =
[TestRecursiveMessageWithRepeatedField message];
XCTAssertNotNil(message.a);
XCTAssertNotNil(message.a.iArray);
XCTAssertFalse([message hasA]);
GPBInt32Array *iArray = [message.a.iArray retain];
XCTAssertEqual(iArray->_autocreator, message.a); // Pointer comparision
message.a.iArray = [GPBInt32Array arrayWithValue:1];
XCTAssertTrue([message hasA]);
XCTAssertNotEqual(message.a.iArray, iArray); // Pointer comparision
XCTAssertNil(iArray->_autocreator);
[iArray release];
}
{
TestRecursiveMessageWithRepeatedField *message =
[TestRecursiveMessageWithRepeatedField message];
XCTAssertNotNil(message.a);
XCTAssertNotNil(message.a.strArray);
XCTAssertFalse([message hasA]);
GPBAutocreatedArray *strArray =
(GPBAutocreatedArray *)[message.a.strArray retain];
XCTAssertTrue([strArray isKindOfClass:[GPBAutocreatedArray class]]);
XCTAssertEqual(strArray->_autocreator, message.a); // Pointer comparision
message.a.strArray = [NSMutableArray arrayWithObject:@"foo"];
XCTAssertTrue([message hasA]);
XCTAssertNotEqual(message.a.strArray, strArray); // Pointer comparision
XCTAssertNil(strArray->_autocreator);
[strArray release];
}
}
- (void)testSetAutocreatedArrayToSelf {
// Setting array to itself should cause it to become visible.
{
TestRecursiveMessageWithRepeatedField *message =
[TestRecursiveMessageWithRepeatedField message];
XCTAssertNotNil(message.a);
XCTAssertNotNil(message.a.iArray);
XCTAssertFalse([message hasA]);
message.a.iArray = message.a.iArray;
XCTAssertTrue([message hasA]);
XCTAssertNil(message.a.iArray->_autocreator);
}
{
TestRecursiveMessageWithRepeatedField *message =
[TestRecursiveMessageWithRepeatedField message];
XCTAssertNotNil(message.a);
XCTAssertNotNil(message.a.strArray);
XCTAssertFalse([message hasA]);
message.a.strArray = message.a.strArray;
XCTAssertTrue([message hasA]);
XCTAssertTrue([message.a.strArray isKindOfClass:[GPBAutocreatedArray class]]);
XCTAssertNil(((GPBAutocreatedArray *)message.a.strArray)->_autocreator);
}
}
- (void)testAutocreatedArrayRemoveAllValues {
// Calling removeAllValues on autocreated array should not cause it to be
// visible.
TestRecursiveMessageWithRepeatedField *message =
[TestRecursiveMessageWithRepeatedField message];
[message.a.iArray removeAll];
XCTAssertFalse([message hasA]);
[message.a.strArray removeAllObjects];
XCTAssertFalse([message hasA]);
}
- (void)testDefaultingMaps {
// Basic tests for default creation of maps in a message.
TestRecursiveMessageWithRepeatedField *message =
[TestRecursiveMessageWithRepeatedField message];
TestRecursiveMessageWithRepeatedField *message2 =
[TestRecursiveMessageWithRepeatedField message];
// Simply accessing the map should not make any fields visible.
XCTAssertNotNil(message.a.a.iToI);
XCTAssertFalse([message hasA]);
XCTAssertFalse([message.a hasA]);
XCTAssertNotNil(message2.a.a.strToStr);
XCTAssertFalse([message2 hasA]);
XCTAssertFalse([message2.a hasA]);
// But adding an element to the map should.
[message.a.a.iToI setValue:100 forKey:200];
XCTAssertTrue([message hasA]);
XCTAssertTrue([message.a hasA]);
XCTAssertEqual([message.a.a.iToI count], (NSUInteger)1);
[message2.a.a.strToStr setObject:@"foo" forKey:@"bar"];
XCTAssertTrue([message2 hasA]);
XCTAssertTrue([message2.a hasA]);
XCTAssertEqual([message2.a.a.strToStr count], (NSUInteger)1);
}
- (void)testAutocreatedMapShared {
// Multiple objects pointing to the same map.
TestRecursiveMessageWithRepeatedField *message1a =
[TestRecursiveMessageWithRepeatedField message];
TestRecursiveMessageWithRepeatedField *message1b =
[TestRecursiveMessageWithRepeatedField message];
message1a.a.iToI = message1b.a.iToI;
XCTAssertTrue([message1a hasA]);
XCTAssertFalse([message1b hasA]);
[message1a.a.iToI setValue:1 forKey:2];
XCTAssertTrue([message1a hasA]);
XCTAssertTrue([message1b hasA]);
XCTAssertEqual(message1a.a.iToI, message1b.a.iToI);
TestRecursiveMessageWithRepeatedField *message2a =
[TestRecursiveMessageWithRepeatedField message];
TestRecursiveMessageWithRepeatedField *message2b =
[TestRecursiveMessageWithRepeatedField message];
message2a.a.strToStr = message2b.a.strToStr;
XCTAssertTrue([message2a hasA]);
XCTAssertFalse([message2b hasA]);
[message2a.a.strToStr setObject:@"bar" forKey:@"foo"];
XCTAssertTrue([message2a hasA]);
XCTAssertTrue([message2b hasA]);
XCTAssertEqual(message2a.a.strToStr, message2b.a.strToStr);
}
- (void)testAutocreatedMapCopy {
// Copy should not copy autocreated maps.
TestRecursiveMessageWithRepeatedField *message =
[TestRecursiveMessageWithRepeatedField message];
XCTAssertNotNil(message.strToStr);
XCTAssertNotNil(message.iToI);
TestRecursiveMessageWithRepeatedField *message2 =
[[message copy] autorelease];
// Pointer conparisions.
XCTAssertNotEqual(message.strToStr, message2.strToStr);
XCTAssertNotEqual(message.iToI, message2.iToI);
// Mutable copy should copy empty arrays that were explicitly set (end up
// with different objects that are equal).
TestRecursiveMessageWithRepeatedField *message3 =
[TestRecursiveMessageWithRepeatedField message];
message3.iToI = [GPBInt32Int32Dictionary dictionaryWithValue:10 forKey:20];
message3.strToStr =
[NSMutableDictionary dictionaryWithObject:@"abc" forKey:@"123"];
XCTAssertNotNil(message.iToI);
XCTAssertNotNil(message.iToI);
TestRecursiveMessageWithRepeatedField *message4 =
[[message3 copy] autorelease];
XCTAssertNotEqual(message3.iToI, message4.iToI);
XCTAssertEqualObjects(message3.iToI, message4.iToI);
XCTAssertNotEqual(message3.strToStr, message4.strToStr);
XCTAssertEqualObjects(message3.strToStr, message4.strToStr);
}
- (void)testAutocreatedMapRetain {
// Should be able to retain autocreated map while the creator is dealloced.
TestRecursiveMessageWithRepeatedField *message =
[TestRecursiveMessageWithRepeatedField message];
@autoreleasepool {
TestRecursiveMessageWithRepeatedField *message2 =
[TestRecursiveMessageWithRepeatedField message];
message.iToI = message2.iToI;
message.strToStr = message2.strToStr;
// Pointer conparision
XCTAssertEqual(message.iToI->_autocreator, message2);
XCTAssertTrue([message.strToStr
isKindOfClass:[GPBAutocreatedDictionary class]]);
XCTAssertEqual(
((GPBAutocreatedDictionary *)message.strToStr)->_autocreator,
message2);
}
XCTAssertNil(message.iToI->_autocreator);
XCTAssertTrue(
[message.strToStr isKindOfClass:[GPBAutocreatedDictionary class]]);
XCTAssertNil(
((GPBAutocreatedDictionary *)message.strToStr)->_autocreator);
}
- (void)testSetNilAutocreatedMap {
// Setting map to nil should cause it to lose its delegate.
TestRecursiveMessageWithRepeatedField *message =
[TestRecursiveMessageWithRepeatedField message];
GPBInt32Int32Dictionary *iToI = [message.iToI retain];
GPBAutocreatedDictionary *strToStr =
(GPBAutocreatedDictionary *)[message.strToStr retain];
XCTAssertTrue([strToStr isKindOfClass:[GPBAutocreatedDictionary class]]);
XCTAssertEqual(iToI->_autocreator, message);
XCTAssertEqual(strToStr->_autocreator, message);
message.iToI = nil;
message.strToStr = nil;
XCTAssertNil(iToI->_autocreator);
XCTAssertNil(strToStr->_autocreator);
[iToI release];
[strToStr release];
}
- (void)testReplaceAutocreatedMap {
// Replacing map should orphan the old one and cause its creator to become
// visible.
{
TestRecursiveMessageWithRepeatedField *message =
[TestRecursiveMessageWithRepeatedField message];
XCTAssertNotNil(message.a);
XCTAssertNotNil(message.a.iToI);
XCTAssertFalse([message hasA]);
GPBInt32Int32Dictionary *iToI = [message.a.iToI retain];
XCTAssertEqual(iToI->_autocreator, message.a); // Pointer comparision
message.a.iToI = [GPBInt32Int32Dictionary dictionaryWithValue:6 forKey:7];
XCTAssertTrue([message hasA]);
XCTAssertNotEqual(message.a.iToI, iToI); // Pointer comparision
XCTAssertNil(iToI->_autocreator);
[iToI release];
}
{
TestRecursiveMessageWithRepeatedField *message =
[TestRecursiveMessageWithRepeatedField message];
XCTAssertNotNil(message.a);
XCTAssertNotNil(message.a.strToStr);
XCTAssertFalse([message hasA]);
GPBAutocreatedDictionary *strToStr =
(GPBAutocreatedDictionary *)[message.a.strToStr retain];
XCTAssertTrue([strToStr isKindOfClass:[GPBAutocreatedDictionary class]]);
XCTAssertEqual(strToStr->_autocreator, message.a); // Pointer comparision
message.a.strToStr =
[NSMutableDictionary dictionaryWithObject:@"abc" forKey:@"def"];
XCTAssertTrue([message hasA]);
XCTAssertNotEqual(message.a.strToStr, strToStr); // Pointer comparision
XCTAssertNil(strToStr->_autocreator);
[strToStr release];
}
}
- (void)testSetAutocreatedMapToSelf {
// Setting map to itself should cause it to become visible.
{
TestRecursiveMessageWithRepeatedField *message =
[TestRecursiveMessageWithRepeatedField message];
XCTAssertNotNil(message.a);
XCTAssertNotNil(message.a.iToI);
XCTAssertFalse([message hasA]);
message.a.iToI = message.a.iToI;
XCTAssertTrue([message hasA]);
XCTAssertNil(message.a.iToI->_autocreator);
}
{
TestRecursiveMessageWithRepeatedField *message =
[TestRecursiveMessageWithRepeatedField message];
XCTAssertNotNil(message.a);
XCTAssertNotNil(message.a.strToStr);
XCTAssertFalse([message hasA]);
message.a.strToStr = message.a.strToStr;
XCTAssertTrue([message hasA]);
XCTAssertTrue([message.a.strToStr isKindOfClass:[GPBAutocreatedDictionary class]]);
XCTAssertNil(((GPBAutocreatedDictionary *)message.a.strToStr)->_autocreator);
}
}
- (void)testAutocreatedMapRemoveAllValues {
// Calling removeAll on autocreated map should not cause it to be visible.
TestRecursiveMessageWithRepeatedField *message =
[TestRecursiveMessageWithRepeatedField message];
[message.a.iToI removeAll];
XCTAssertFalse([message hasA]);
[message.a.strToStr removeAllObjects];
XCTAssertFalse([message hasA]);
}
- (void)testExtensionAccessors {
TestAllExtensions *message = [TestAllExtensions message];
[self setAllExtensions:message repeatedCount:kGPBDefaultRepeatCount];
[self assertAllExtensionsSet:message repeatedCount:kGPBDefaultRepeatCount];
}
- (void)testExtensionRepeatedSetters {
TestAllExtensions *message = [TestAllExtensions message];
[self setAllExtensions:message repeatedCount:kGPBDefaultRepeatCount];
[self modifyRepeatedExtensions:message];
[self assertRepeatedExtensionsModified:message
repeatedCount:kGPBDefaultRepeatCount];
}
- (void)testExtensionDefaults {
[self assertExtensionsClear:[TestAllExtensions message]];
}
- (void)testExtensionIsEquals {
TestAllExtensions *message = [TestAllExtensions message];
[self setAllExtensions:message repeatedCount:kGPBDefaultRepeatCount];
[self modifyRepeatedExtensions:message];
TestAllExtensions *message2 = [TestAllExtensions message];
[self setAllExtensions:message2 repeatedCount:kGPBDefaultRepeatCount];
XCTAssertFalse([message isEqual:message2]);
message2 = [TestAllExtensions message];
[self setAllExtensions:message2 repeatedCount:kGPBDefaultRepeatCount];
[self modifyRepeatedExtensions:message2];
XCTAssertEqualObjects(message, message2);
}
- (void)testExtensionsMergeFrom {
TestAllExtensions *message = [TestAllExtensions message];
[self setAllExtensions:message repeatedCount:kGPBDefaultRepeatCount];
[self modifyRepeatedExtensions:message];
message = [TestAllExtensions message];
[self setAllExtensions:message repeatedCount:kGPBDefaultRepeatCount];
TestAllExtensions *message2 = [TestAllExtensions message];
[self modifyRepeatedExtensions:message2];
[message2 mergeFrom:message];
XCTAssertEqualObjects(message, message2);
}
- (void)testDefaultingExtensionMessages {
TestAllExtensions *message = [TestAllExtensions message];
// Initially they should all not have values.
XCTAssertFalse([message hasExtension:[UnittestRoot optionalGroupExtension]]);
XCTAssertFalse([message hasExtension:[UnittestRoot optionalGroupExtension]]);
XCTAssertFalse(
[message hasExtension:[UnittestRoot optionalNestedMessageExtension]]);
XCTAssertFalse(
[message hasExtension:[UnittestRoot optionalForeignMessageExtension]]);
XCTAssertFalse(
[message hasExtension:[UnittestRoot optionalImportMessageExtension]]);
XCTAssertFalse([message
hasExtension:[UnittestRoot optionalPublicImportMessageExtension]]);
XCTAssertFalse(
[message hasExtension:[UnittestRoot optionalLazyMessageExtension]]);
// They should auto create something when fetched.
TestAllTypes_OptionalGroup *optionalGroup =
[message getExtension:[UnittestRoot optionalGroupExtension]];
TestAllTypes_NestedMessage *optionalNestedMessage =
[message getExtension:[UnittestRoot optionalNestedMessageExtension]];
ForeignMessage *optionalForeignMessage =
[message getExtension:[UnittestRoot optionalForeignMessageExtension]];
ImportMessage *optionalImportMessage =
[message getExtension:[UnittestRoot optionalImportMessageExtension]];
PublicImportMessage *optionalPublicImportMessage = [message
getExtension:[UnittestRoot optionalPublicImportMessageExtension]];
TestAllTypes_NestedMessage *optionalLazyMessage =
[message getExtension:[UnittestRoot optionalLazyMessageExtension]];
XCTAssertNotNil(optionalGroup);
XCTAssertNotNil(optionalNestedMessage);
XCTAssertNotNil(optionalForeignMessage);
XCTAssertNotNil(optionalImportMessage);
XCTAssertNotNil(optionalPublicImportMessage);
XCTAssertNotNil(optionalLazyMessage);
// Although it auto-created empty messages, it should not show that it has
// them.
XCTAssertFalse([message hasExtension:[UnittestRoot optionalGroupExtension]]);
XCTAssertFalse([message hasExtension:[UnittestRoot optionalGroupExtension]]);
XCTAssertFalse([message hasExtension:[UnittestRoot optionalNestedMessageExtension]]);
XCTAssertFalse([message hasExtension:[UnittestRoot optionalForeignMessageExtension]]);
XCTAssertFalse([message hasExtension:[UnittestRoot optionalImportMessageExtension]]);
XCTAssertFalse([message hasExtension:[UnittestRoot optionalPublicImportMessageExtension]]);
XCTAssertFalse([message hasExtension:[UnittestRoot optionalLazyMessageExtension]]);
// And they set that value back in to the message since the value created was
// mutable (so a second fetch should give the same object).
XCTAssertEqual([message getExtension:[UnittestRoot optionalGroupExtension]],
optionalGroup);
XCTAssertEqual(
[message getExtension:[UnittestRoot optionalNestedMessageExtension]],
optionalNestedMessage);
XCTAssertEqual(
[message getExtension:[UnittestRoot optionalForeignMessageExtension]],
optionalForeignMessage);
XCTAssertEqual(
[message getExtension:[UnittestRoot optionalImportMessageExtension]],
optionalImportMessage);
XCTAssertEqual(
[message getExtension:[UnittestRoot optionalPublicImportMessageExtension]],
optionalPublicImportMessage);
XCTAssertEqual(
[message getExtension:[UnittestRoot optionalLazyMessageExtension]],
optionalLazyMessage);
// And the default objects for a second message should be distinct (again,
// since they are mutable, each needs their own copy).
TestAllExtensions *message2 = [TestAllExtensions message];
// Intentionally doing a pointer comparison.
XCTAssertNotEqual(
[message2 getExtension:[UnittestRoot optionalGroupExtension]],
optionalGroup);
XCTAssertNotEqual(
[message2 getExtension:[UnittestRoot optionalNestedMessageExtension]],
optionalNestedMessage);
XCTAssertNotEqual(
[message2 getExtension:[UnittestRoot optionalForeignMessageExtension]],
optionalForeignMessage);
XCTAssertNotEqual(
[message2 getExtension:[UnittestRoot optionalImportMessageExtension]],
optionalImportMessage);
XCTAssertNotEqual(
[message2 getExtension:[UnittestRoot optionalPublicImportMessageExtension]],
optionalPublicImportMessage);
XCTAssertNotEqual(
[message2 getExtension:[UnittestRoot optionalLazyMessageExtension]],
optionalLazyMessage);
// Clear values, and on next access you get back new submessages.
[message setExtension:[UnittestRoot optionalGroupExtension] value:nil];
[message setExtension:[UnittestRoot optionalGroupExtension] value:nil];
[message setExtension:[UnittestRoot optionalNestedMessageExtension]
value:nil];
[message setExtension:[UnittestRoot optionalForeignMessageExtension]
value:nil];
[message setExtension:[UnittestRoot optionalImportMessageExtension]
value:nil];
[message setExtension:[UnittestRoot optionalPublicImportMessageExtension]
value:nil];
[message setExtension:[UnittestRoot optionalLazyMessageExtension] value:nil];
XCTAssertFalse([message hasExtension:[UnittestRoot optionalGroupExtension]]);
XCTAssertFalse([message hasExtension:[UnittestRoot optionalGroupExtension]]);
XCTAssertFalse(
[message hasExtension:[UnittestRoot optionalNestedMessageExtension]]);
XCTAssertFalse(
[message hasExtension:[UnittestRoot optionalForeignMessageExtension]]);
XCTAssertFalse(
[message hasExtension:[UnittestRoot optionalImportMessageExtension]]);
XCTAssertFalse([message
hasExtension:[UnittestRoot optionalPublicImportMessageExtension]]);
XCTAssertFalse(
[message hasExtension:[UnittestRoot optionalLazyMessageExtension]]);
XCTAssertEqual([message getExtension:[UnittestRoot optionalGroupExtension]],
optionalGroup);
XCTAssertEqual(
[message getExtension:[UnittestRoot optionalNestedMessageExtension]],
optionalNestedMessage);
XCTAssertEqual(
[message getExtension:[UnittestRoot optionalForeignMessageExtension]],
optionalForeignMessage);
XCTAssertEqual(
[message getExtension:[UnittestRoot optionalImportMessageExtension]],
optionalImportMessage);
XCTAssertEqual(
[message
getExtension:[UnittestRoot optionalPublicImportMessageExtension]],
optionalPublicImportMessage);
XCTAssertEqual(
[message getExtension:[UnittestRoot optionalLazyMessageExtension]],
optionalLazyMessage);
}
- (void)testMultiplePointersToAutocreatedExtension {
// 2 objects point to the same auto-created extension. One should "has" it.
// The other should not.
TestAllExtensions *message = [TestAllExtensions message];
TestAllExtensions *message2 = [TestAllExtensions message];
GPBExtensionDescriptor *extension = [UnittestRoot optionalGroupExtension];
[message setExtension:extension value:[message2 getExtension:extension]];
XCTAssertEqual([message getExtension:extension],
[message2 getExtension:extension]);
XCTAssertFalse([message2 hasExtension:extension]);
XCTAssertTrue([message hasExtension:extension]);
TestAllTypes_OptionalGroup *extensionValue =
[message2 getExtension:extension];
extensionValue.a = 1;
XCTAssertTrue([message2 hasExtension:extension]);
XCTAssertTrue([message hasExtension:extension]);
}
- (void)testCopyWithAutocreatedExtension {
// Mutable copy shouldn't copy autocreated extensions.
TestAllExtensions *message = [TestAllExtensions message];
GPBExtensionDescriptor *optionalGroupExtension =
[UnittestRoot optionalGroupExtension];
GPBExtensionDescriptor *optionalNestedMessageExtesion =
[UnittestRoot optionalNestedMessageExtension];
TestAllTypes_OptionalGroup *optionalGroup =
[message getExtension:optionalGroupExtension];
optionalGroup.a = 42;
XCTAssertNotNil(optionalGroup);
XCTAssertNotNil([message getExtension:optionalNestedMessageExtesion]);
XCTAssertTrue([message hasExtension:optionalGroupExtension]);
XCTAssertFalse([message hasExtension:optionalNestedMessageExtesion]);
TestAllExtensions *message2 = [[message copy] autorelease];
// message2 should end up with its own copy of the optional group.
XCTAssertTrue([message2 hasExtension:optionalGroupExtension]);
XCTAssertEqualObjects([message getExtension:optionalGroupExtension],
[message2 getExtension:optionalGroupExtension]);
// Intentionally doing a pointer comparison.
XCTAssertNotEqual([message getExtension:optionalGroupExtension],
[message2 getExtension:optionalGroupExtension]);
XCTAssertFalse([message2 hasExtension:optionalNestedMessageExtesion]);
// Intentionally doing a pointer comparison (auto creation should be
// different)
XCTAssertNotEqual([message getExtension:optionalNestedMessageExtesion],
[message2 getExtension:optionalNestedMessageExtesion]);
}
- (void)testClearMessageAutocreatedExtension {
// Call clear should cause it to recreate its autocreated extensions.
TestAllExtensions *message = [TestAllExtensions message];
GPBExtensionDescriptor *optionalGroupExtension =
[UnittestRoot optionalGroupExtension];
TestAllTypes_OptionalGroup *optionalGroup =
[[message getExtension:optionalGroupExtension] retain];
[message clear];
TestAllTypes_OptionalGroup *optionalGroupNew =
[message getExtension:optionalGroupExtension];
// Intentionally doing a pointer comparison.
XCTAssertNotEqual(optionalGroup, optionalGroupNew);
[optionalGroup release];
}
- (void)testRetainAutocreatedExtension {
// Should be able to retain autocreated extension while the creator is
// dealloced.
TestAllExtensions *message = [TestAllExtensions message];
GPBExtensionDescriptor *optionalGroupExtension =
[UnittestRoot optionalGroupExtension];
@autoreleasepool {
TestAllExtensions *message2 = [TestAllExtensions message];
[message setExtension:optionalGroupExtension
value:[message2 getExtension:optionalGroupExtension]];
XCTAssertTrue(GPBWasMessageAutocreatedBy(
[message getExtension:optionalGroupExtension], message2));
}
XCTAssertFalse(GPBWasMessageAutocreatedBy(
[message getExtension:optionalGroupExtension], message));
}
- (void)testClearAutocreatedExtension {
// Clearing autocreated extension should NOT cause it to lose its creator.
TestAllExtensions *message = [TestAllExtensions message];
GPBExtensionDescriptor *optionalGroupExtension =
[UnittestRoot optionalGroupExtension];
TestAllTypes_OptionalGroup *optionalGroup =
[[message getExtension:optionalGroupExtension] retain];
[message clearExtension:optionalGroupExtension];
TestAllTypes_OptionalGroup *optionalGroupNew =
[message getExtension:optionalGroupExtension];
XCTAssertEqual(optionalGroup, optionalGroupNew);
XCTAssertFalse([message hasExtension:optionalGroupExtension]);
[optionalGroup release];
// Clearing autocreated extension should not cause its creator to become
// visible
GPBExtensionDescriptor *recursiveExtension =
[UnittestObjcRoot recursiveExtension];
TestAllExtensions *message_lvl2 = [message getExtension:recursiveExtension];
TestAllExtensions *message_lvl3 =
[message_lvl2 getExtension:recursiveExtension];
[message_lvl3 clearExtension:recursiveExtension];
XCTAssertFalse([message hasExtension:recursiveExtension]);
}
- (void)testSetAutocreatedExtensionBecomesVisible {
// Setting an extension should cause the extension to appear to its creator.
// Test this several levels deep.
TestAllExtensions *message = [TestAllExtensions message];
GPBExtensionDescriptor *recursiveExtension =
[UnittestObjcRoot recursiveExtension];
TestAllExtensions *message_lvl2 = [message getExtension:recursiveExtension];
TestAllExtensions *message_lvl3 =
[message_lvl2 getExtension:recursiveExtension];
TestAllExtensions *message_lvl4 =
[message_lvl3 getExtension:recursiveExtension];
XCTAssertFalse([message hasExtension:recursiveExtension]);
XCTAssertFalse([message_lvl2 hasExtension:recursiveExtension]);
XCTAssertFalse([message_lvl3 hasExtension:recursiveExtension]);
XCTAssertFalse([message_lvl4 hasExtension:recursiveExtension]);
[message_lvl4 setExtension:[UnittestRoot optionalInt32Extension] value:@(1)];
XCTAssertTrue([message hasExtension:recursiveExtension]);
XCTAssertTrue([message_lvl2 hasExtension:recursiveExtension]);
XCTAssertTrue([message_lvl3 hasExtension:recursiveExtension]);
XCTAssertFalse([message_lvl4 hasExtension:recursiveExtension]);
XCTAssertFalse(GPBWasMessageAutocreatedBy(message_lvl4, message_lvl3));
XCTAssertFalse(GPBWasMessageAutocreatedBy(message_lvl3, message_lvl2));
XCTAssertFalse(GPBWasMessageAutocreatedBy(message_lvl2, message));
}
- (void)testSetAutocreatedExtensionToSelf {
// Setting extension to itself should cause it to become visible.
TestAllExtensions *message = [TestAllExtensions message];
GPBExtensionDescriptor *optionalGroupExtension =
[UnittestRoot optionalGroupExtension];
XCTAssertNotNil([message getExtension:optionalGroupExtension]);
XCTAssertFalse([message hasExtension:optionalGroupExtension]);
[message setExtension:optionalGroupExtension
value:[message getExtension:optionalGroupExtension]];
XCTAssertTrue([message hasExtension:optionalGroupExtension]);
}
- (void)testAutocreatedExtensionMemoryLeaks {
GPBExtensionDescriptor *recursiveExtension =
[UnittestObjcRoot recursiveExtension];
// Test for memory leaks with autocreated extensions.
TestAllExtensions *message;
TestAllExtensions *message_lvl2;
TestAllExtensions *message_lvl3;
TestAllExtensions *message_lvl4;
@autoreleasepool {
message = [[TestAllExtensions alloc] init];
message_lvl2 = [[message getExtension:recursiveExtension] retain];
message_lvl3 = [[message_lvl2 getExtension:recursiveExtension] retain];
message_lvl4 = [[message_lvl3 getExtension:recursiveExtension] retain];
[message_lvl2 setExtension:[UnittestRoot optionalInt32Extension]
value:@(1)];
}
XCTAssertEqual(message.retainCount, (NSUInteger)1);
@autoreleasepool {
[message release];
}
XCTAssertEqual(message_lvl2.retainCount, (NSUInteger)1);
@autoreleasepool {
[message_lvl2 release];
}
XCTAssertEqual(message_lvl3.retainCount, (NSUInteger)1);
@autoreleasepool {
[message_lvl3 release];
}
XCTAssertEqual(message_lvl4.retainCount, (NSUInteger)1);
[message_lvl4 release];
}
- (void)testSetExtensionWithAutocreatedValue {
GPBExtensionDescriptor *recursiveExtension =
[UnittestObjcRoot recursiveExtension];
TestAllExtensions *message;
@autoreleasepool {
message = [[TestAllExtensions alloc] init];
[message getExtension:recursiveExtension];
}
// This statements checks that the extension value isn't accidentally
// dealloced when removing it from the autocreated map.
[message setExtension:recursiveExtension
value:[message getExtension:recursiveExtension]];
XCTAssertTrue([message hasExtension:recursiveExtension]);
[message release];
}
- (void)testRecursion {
TestRecursiveMessage *message = [TestRecursiveMessage message];
XCTAssertNotNil(message.a);
XCTAssertNotNil(message.a.a);
XCTAssertEqual(message.a.a.i, 0);
}
- (void)testGenerateAndParseUnknownMessage {
GPBUnknownFieldSet *unknowns =
[[[GPBUnknownFieldSet alloc] init] autorelease];
[unknowns mergeVarintField:123 value:456];
GPBMessage *message = [GPBMessage message];
[message setUnknownFields:unknowns];
NSData *data = [message data];
GPBMessage *message2 =
[GPBMessage parseFromData:data extensionRegistry:nil error:NULL];
XCTAssertEqualObjects(message, message2);
}
- (void)testDelimitedWriteAndParseMultipleMessages {
GPBUnknownFieldSet *unknowns1 =
[[[GPBUnknownFieldSet alloc] init] autorelease];
[unknowns1 mergeVarintField:123 value:456];
GPBMessage *message1 = [GPBMessage message];
[message1 setUnknownFields:unknowns1];
GPBUnknownFieldSet *unknowns2 =
[[[GPBUnknownFieldSet alloc] init] autorelease];
[unknowns2 mergeVarintField:789 value:987];
[unknowns2 mergeVarintField:654 value:321];
GPBMessage *message2 = [GPBMessage message];
[message2 setUnknownFields:unknowns2];
NSMutableData *delimitedData = [NSMutableData data];
[delimitedData appendData:[message1 delimitedData]];
[delimitedData appendData:[message2 delimitedData]];
GPBCodedInputStream *input =
[GPBCodedInputStream streamWithData:delimitedData];
GPBMessage *message3 = [GPBMessage parseDelimitedFromCodedInputStream:input
extensionRegistry:nil
error:NULL];
GPBMessage *message4 = [GPBMessage parseDelimitedFromCodedInputStream:input
extensionRegistry:nil
error:NULL];
XCTAssertEqualObjects(message1, message3);
XCTAssertEqualObjects(message2, message4);
}
- (void)testDuplicateEnums {
XCTAssertEqual(TestEnumWithDupValue_Foo1, TestEnumWithDupValue_Foo2);
}
- (void)testWeirdDefaults {
ObjcWeirdDefaults *message = [ObjcWeirdDefaults message];
GPBDescriptor *descriptor = [[message class] descriptor];
GPBFieldDescriptor *fieldDesc = [descriptor fieldWithName:@"foo"];
XCTAssertNotNil(fieldDesc);
XCTAssertTrue(fieldDesc.hasDefaultValue);
XCTAssertFalse(message.hasFoo);
XCTAssertEqualObjects(message.foo, @"");
fieldDesc = [descriptor fieldWithName:@"bar"];
XCTAssertNotNil(fieldDesc);
XCTAssertTrue(fieldDesc.hasDefaultValue);
XCTAssertFalse(message.hasBar);
XCTAssertEqualObjects(message.bar, GPBEmptyNSData());
}
- (void)testEnumDescriptorFromExtensionDescriptor {
GPBExtensionDescriptor *extDescriptor =
[UnittestRoot optionalForeignEnumExtension];
XCTAssertEqual(extDescriptor.dataType, GPBDataTypeEnum);
GPBEnumDescriptor *enumDescriptor = extDescriptor.enumDescriptor;
GPBEnumDescriptor *expectedDescriptor = ForeignEnum_EnumDescriptor();
XCTAssertEqualObjects(enumDescriptor, expectedDescriptor);
}
- (void)testEnumNaming {
// objectivec_helpers.cc has some interesting cases to deal with in
// EnumValueName/EnumValueShortName. Confirm that things generated as
// expected.
// This block just has to compile to confirm we got the expected types/names.
// The *_IsValidValue() calls are just there to keep the projects warnings
// flags happy by providing use of the variables/values.
Foo aFoo = Foo_SerializedSize;
Foo_IsValidValue(aFoo);
aFoo = Foo_Size;
Foo_IsValidValue(aFoo);
Category_Enum aCat = Category_Enum_Red;
Category_Enum_IsValidValue(aCat);
Time aTime = Time_Base;
Time_IsValidValue(aTime);
aTime = Time_SomethingElse;
Time_IsValidValue(aTime);
// This block confirms the names in the decriptors is what we wanted.
GPBEnumDescriptor *descriptor;
NSString *valueName;
descriptor = Foo_EnumDescriptor();
XCTAssertNotNil(descriptor);
XCTAssertEqualObjects(@"Foo", descriptor.name);
valueName = [descriptor enumNameForValue:Foo_SerializedSize];
XCTAssertEqualObjects(@"Foo_SerializedSize", valueName);
valueName = [descriptor enumNameForValue:Foo_Size];
XCTAssertEqualObjects(@"Foo_Size", valueName);
descriptor = Category_Enum_EnumDescriptor();
XCTAssertNotNil(descriptor);
XCTAssertEqualObjects(@"Category_Enum", descriptor.name);
valueName = [descriptor enumNameForValue:Category_Enum_Red];
XCTAssertEqualObjects(@"Category_Enum_Red", valueName);
descriptor = Time_EnumDescriptor();
XCTAssertNotNil(descriptor);
XCTAssertEqualObjects(@"Time", descriptor.name);
valueName = [descriptor enumNameForValue:Time_Base];
XCTAssertEqualObjects(@"Time_Base", valueName);
valueName = [descriptor enumNameForValue:Time_SomethingElse];
XCTAssertEqualObjects(@"Time_SomethingElse", valueName);
}
- (void)testNegativeEnums {
EnumTestMsg *msg = [EnumTestMsg message];
// Defaults
XCTAssertEqual(msg.foo, EnumTestMsg_MyEnum_Zero);
XCTAssertEqual(msg.bar, EnumTestMsg_MyEnum_One);
XCTAssertEqual(msg.baz, EnumTestMsg_MyEnum_NegOne);
// Bounce to wire and back.
NSData *data = [msg data];
XCTAssertNotNil(data);
EnumTestMsg *msgPrime = [EnumTestMsg parseFromData:data error:NULL];
XCTAssertEqualObjects(msgPrime, msg);
XCTAssertEqual(msgPrime.foo, EnumTestMsg_MyEnum_Zero);
XCTAssertEqual(msgPrime.bar, EnumTestMsg_MyEnum_One);
XCTAssertEqual(msgPrime.baz, EnumTestMsg_MyEnum_NegOne);
// Other values
msg.bar = EnumTestMsg_MyEnum_Two;
msg.baz = EnumTestMsg_MyEnum_NegTwo;
XCTAssertEqual(msg.bar, EnumTestMsg_MyEnum_Two);
XCTAssertEqual(msg.baz, EnumTestMsg_MyEnum_NegTwo);
// Bounce to wire and back.
data = [msg data];
XCTAssertNotNil(data);
msgPrime = [EnumTestMsg parseFromData:data error:NULL];
XCTAssertEqualObjects(msgPrime, msg);
XCTAssertEqual(msgPrime.foo, EnumTestMsg_MyEnum_Zero);
XCTAssertEqual(msgPrime.bar, EnumTestMsg_MyEnum_Two);
XCTAssertEqual(msgPrime.baz, EnumTestMsg_MyEnum_NegTwo);
// Repeated field (shouldn't ever be an issue since developer has to use the
// right GPBArray methods themselves).
msg.mumbleArray = [GPBEnumArray
arrayWithValidationFunction:EnumTestMsg_MyEnum_IsValidValue];
[msg.mumbleArray addValue:EnumTestMsg_MyEnum_Zero];
[msg.mumbleArray addValue:EnumTestMsg_MyEnum_One];
[msg.mumbleArray addValue:EnumTestMsg_MyEnum_Two];
[msg.mumbleArray addValue:EnumTestMsg_MyEnum_NegOne];
[msg.mumbleArray addValue:EnumTestMsg_MyEnum_NegTwo];
XCTAssertEqual([msg.mumbleArray valueAtIndex:0], EnumTestMsg_MyEnum_Zero);
XCTAssertEqual([msg.mumbleArray valueAtIndex:1], EnumTestMsg_MyEnum_One);
XCTAssertEqual([msg.mumbleArray valueAtIndex:2], EnumTestMsg_MyEnum_Two);
XCTAssertEqual([msg.mumbleArray valueAtIndex:3], EnumTestMsg_MyEnum_NegOne);
XCTAssertEqual([msg.mumbleArray valueAtIndex:4], EnumTestMsg_MyEnum_NegTwo);
// Bounce to wire and back.
data = [msg data];
XCTAssertNotNil(data);
msgPrime = [EnumTestMsg parseFromData:data error:NULL];
XCTAssertEqualObjects(msgPrime, msg);
XCTAssertEqual([msgPrime.mumbleArray valueAtIndex:0],
EnumTestMsg_MyEnum_Zero);
XCTAssertEqual([msgPrime.mumbleArray valueAtIndex:1], EnumTestMsg_MyEnum_One);
XCTAssertEqual([msgPrime.mumbleArray valueAtIndex:2], EnumTestMsg_MyEnum_Two);
XCTAssertEqual([msgPrime.mumbleArray valueAtIndex:3],
EnumTestMsg_MyEnum_NegOne);
XCTAssertEqual([msgPrime.mumbleArray valueAtIndex:4],
EnumTestMsg_MyEnum_NegTwo);
}
@end
|