GPBMessageTests+Merge.m 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2015 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #import "GPBTestUtilities.h"
  31. #import <objc/runtime.h>
  32. #import "GPBMessage.h"
  33. #import "google/protobuf/MapUnittest.pbobjc.h"
  34. #import "google/protobuf/Unittest.pbobjc.h"
  35. #import "google/protobuf/UnittestPreserveUnknownEnum.pbobjc.h"
  36. #import "google/protobuf/UnittestRuntimeProto2.pbobjc.h"
  37. #import "google/protobuf/UnittestRuntimeProto3.pbobjc.h"
  38. @interface MessageMergeTests : GPBTestCase
  39. @end
  40. @implementation MessageMergeTests
  41. // TODO(thomasvl): Pull tests over from GPBMessageTests that are merge specific.
  42. - (void)testProto3MergingAndZeroValues {
  43. // Proto2 covered in other tests.
  44. Message3 *src = [[Message3 alloc] init];
  45. Message3 *dst = [[Message3 alloc] init];
  46. NSData *testData1 = [@"abc" dataUsingEncoding:NSUTF8StringEncoding];
  47. NSData *testData2 = [@"def" dataUsingEncoding:NSUTF8StringEncoding];
  48. dst.optionalInt32 = 1;
  49. dst.optionalInt64 = 1;
  50. dst.optionalUint32 = 1;
  51. dst.optionalUint64 = 1;
  52. dst.optionalSint32 = 1;
  53. dst.optionalSint64 = 1;
  54. dst.optionalFixed32 = 1;
  55. dst.optionalFixed64 = 1;
  56. dst.optionalSfixed32 = 1;
  57. dst.optionalSfixed64 = 1;
  58. dst.optionalFloat = 1.0f;
  59. dst.optionalDouble = 1.0;
  60. dst.optionalBool = YES;
  61. dst.optionalString = @"bar";
  62. dst.optionalBytes = testData1;
  63. dst.optionalEnum = Message3_Enum_Bar;
  64. // All zeros, nothing should overright.
  65. src.optionalInt32 = 0;
  66. src.optionalInt64 = 0;
  67. src.optionalUint32 = 0;
  68. src.optionalUint64 = 0;
  69. src.optionalSint32 = 0;
  70. src.optionalSint64 = 0;
  71. src.optionalFixed32 = 0;
  72. src.optionalFixed64 = 0;
  73. src.optionalSfixed32 = 0;
  74. src.optionalSfixed64 = 0;
  75. src.optionalFloat = 0.0f;
  76. src.optionalDouble = 0.0;
  77. src.optionalBool = NO;
  78. src.optionalString = @"";
  79. src.optionalBytes = [NSData data];
  80. src.optionalEnum = Message3_Enum_Foo; // first value
  81. [dst mergeFrom:src];
  82. XCTAssertEqual(dst.optionalInt32, 1);
  83. XCTAssertEqual(dst.optionalInt64, 1);
  84. XCTAssertEqual(dst.optionalUint32, 1U);
  85. XCTAssertEqual(dst.optionalUint64, 1U);
  86. XCTAssertEqual(dst.optionalSint32, 1);
  87. XCTAssertEqual(dst.optionalSint64, 1);
  88. XCTAssertEqual(dst.optionalFixed32, 1U);
  89. XCTAssertEqual(dst.optionalFixed64, 1U);
  90. XCTAssertEqual(dst.optionalSfixed32, 1);
  91. XCTAssertEqual(dst.optionalSfixed64, 1);
  92. XCTAssertEqual(dst.optionalFloat, 1.0f);
  93. XCTAssertEqual(dst.optionalDouble, 1.0);
  94. XCTAssertEqual(dst.optionalBool, YES);
  95. XCTAssertEqualObjects(dst.optionalString, @"bar");
  96. XCTAssertEqualObjects(dst.optionalBytes, testData1);
  97. XCTAssertEqual(dst.optionalEnum, Message3_Enum_Bar);
  98. // Half the values that will replace.
  99. src.optionalInt32 = 0;
  100. src.optionalInt64 = 2;
  101. src.optionalUint32 = 0;
  102. src.optionalUint64 = 2;
  103. src.optionalSint32 = 0;
  104. src.optionalSint64 = 2;
  105. src.optionalFixed32 = 0;
  106. src.optionalFixed64 = 2;
  107. src.optionalSfixed32 = 0;
  108. src.optionalSfixed64 = 2;
  109. src.optionalFloat = 0.0f;
  110. src.optionalDouble = 2.0;
  111. src.optionalBool = YES; // No other value to use. :(
  112. src.optionalString = @"baz";
  113. src.optionalBytes = nil;
  114. src.optionalEnum = Message3_Enum_Baz;
  115. [dst mergeFrom:src];
  116. XCTAssertEqual(dst.optionalInt32, 1);
  117. XCTAssertEqual(dst.optionalInt64, 2);
  118. XCTAssertEqual(dst.optionalUint32, 1U);
  119. XCTAssertEqual(dst.optionalUint64, 2U);
  120. XCTAssertEqual(dst.optionalSint32, 1);
  121. XCTAssertEqual(dst.optionalSint64, 2);
  122. XCTAssertEqual(dst.optionalFixed32, 1U);
  123. XCTAssertEqual(dst.optionalFixed64, 2U);
  124. XCTAssertEqual(dst.optionalSfixed32, 1);
  125. XCTAssertEqual(dst.optionalSfixed64, 2);
  126. XCTAssertEqual(dst.optionalFloat, 1.0f);
  127. XCTAssertEqual(dst.optionalDouble, 2.0);
  128. XCTAssertEqual(dst.optionalBool, YES);
  129. XCTAssertEqualObjects(dst.optionalString, @"baz");
  130. XCTAssertEqualObjects(dst.optionalBytes, testData1);
  131. XCTAssertEqual(dst.optionalEnum, Message3_Enum_Baz);
  132. // Other half the values that will replace.
  133. src.optionalInt32 = 3;
  134. src.optionalInt64 = 0;
  135. src.optionalUint32 = 3;
  136. src.optionalUint64 = 0;
  137. src.optionalSint32 = 3;
  138. src.optionalSint64 = 0;
  139. src.optionalFixed32 = 3;
  140. src.optionalFixed64 = 0;
  141. src.optionalSfixed32 = 3;
  142. src.optionalSfixed64 = 0;
  143. src.optionalFloat = 3.0f;
  144. src.optionalDouble = 0.0;
  145. src.optionalBool = YES; // No other value to use. :(
  146. src.optionalString = nil;
  147. src.optionalBytes = testData2;
  148. src.optionalEnum = Message3_Enum_Foo;
  149. [dst mergeFrom:src];
  150. XCTAssertEqual(dst.optionalInt32, 3);
  151. XCTAssertEqual(dst.optionalInt64, 2);
  152. XCTAssertEqual(dst.optionalUint32, 3U);
  153. XCTAssertEqual(dst.optionalUint64, 2U);
  154. XCTAssertEqual(dst.optionalSint32, 3);
  155. XCTAssertEqual(dst.optionalSint64, 2);
  156. XCTAssertEqual(dst.optionalFixed32, 3U);
  157. XCTAssertEqual(dst.optionalFixed64, 2U);
  158. XCTAssertEqual(dst.optionalSfixed32, 3);
  159. XCTAssertEqual(dst.optionalSfixed64, 2);
  160. XCTAssertEqual(dst.optionalFloat, 3.0f);
  161. XCTAssertEqual(dst.optionalDouble, 2.0);
  162. XCTAssertEqual(dst.optionalBool, YES);
  163. XCTAssertEqualObjects(dst.optionalString, @"baz");
  164. XCTAssertEqualObjects(dst.optionalBytes, testData2);
  165. XCTAssertEqual(dst.optionalEnum, Message3_Enum_Baz);
  166. [src release];
  167. [dst release];
  168. }
  169. - (void)testProto3MergingEnums {
  170. UnknownEnumsMyMessage *src = [UnknownEnumsMyMessage message];
  171. UnknownEnumsMyMessage *dst = [UnknownEnumsMyMessage message];
  172. // Known value.
  173. src.e = UnknownEnumsMyEnum_Bar;
  174. src.repeatedEArray =
  175. [GPBEnumArray arrayWithValidationFunction:UnknownEnumsMyEnum_IsValidValue
  176. rawValue:UnknownEnumsMyEnum_Bar];
  177. src.repeatedPackedEArray =
  178. [GPBEnumArray arrayWithValidationFunction:UnknownEnumsMyEnum_IsValidValue
  179. rawValue:UnknownEnumsMyEnum_Bar];
  180. src.oneofE1 = UnknownEnumsMyEnum_Bar;
  181. [dst mergeFrom:src];
  182. XCTAssertEqual(dst.e, UnknownEnumsMyEnum_Bar);
  183. XCTAssertEqual(dst.repeatedEArray.count, 1U);
  184. XCTAssertEqual([dst.repeatedEArray valueAtIndex:0], UnknownEnumsMyEnum_Bar);
  185. XCTAssertEqual(dst.repeatedPackedEArray.count, 1U);
  186. XCTAssertEqual([dst.repeatedPackedEArray valueAtIndex:0],
  187. UnknownEnumsMyEnum_Bar);
  188. XCTAssertEqual(dst.oneofE1, UnknownEnumsMyEnum_Bar);
  189. // Unknown value.
  190. const int32_t kUnknownValue = 666;
  191. SetUnknownEnumsMyMessage_E_RawValue(src, kUnknownValue);
  192. src.repeatedEArray =
  193. [GPBEnumArray arrayWithValidationFunction:UnknownEnumsMyEnum_IsValidValue
  194. rawValue:kUnknownValue];
  195. src.repeatedPackedEArray =
  196. [GPBEnumArray arrayWithValidationFunction:UnknownEnumsMyEnum_IsValidValue
  197. rawValue:kUnknownValue];
  198. SetUnknownEnumsMyMessage_OneofE1_RawValue(src, kUnknownValue);
  199. [dst mergeFrom:src];
  200. XCTAssertEqual(dst.e, UnknownEnumsMyEnum_GPBUnrecognizedEnumeratorValue);
  201. XCTAssertEqual(UnknownEnumsMyMessage_E_RawValue(dst), kUnknownValue);
  202. XCTAssertEqual(dst.repeatedEArray.count, 2U);
  203. XCTAssertEqual([dst.repeatedEArray valueAtIndex:0], UnknownEnumsMyEnum_Bar);
  204. XCTAssertEqual([dst.repeatedEArray valueAtIndex:1],
  205. UnknownEnumsMyEnum_GPBUnrecognizedEnumeratorValue);
  206. XCTAssertEqual([dst.repeatedEArray rawValueAtIndex:1], kUnknownValue);
  207. XCTAssertEqual(dst.repeatedPackedEArray.count, 2U);
  208. XCTAssertEqual([dst.repeatedPackedEArray valueAtIndex:0],
  209. UnknownEnumsMyEnum_Bar);
  210. XCTAssertEqual([dst.repeatedPackedEArray valueAtIndex:1],
  211. UnknownEnumsMyEnum_GPBUnrecognizedEnumeratorValue);
  212. XCTAssertEqual([dst.repeatedPackedEArray rawValueAtIndex:1], kUnknownValue);
  213. XCTAssertEqual(dst.oneofE1,
  214. UnknownEnumsMyEnum_GPBUnrecognizedEnumeratorValue);
  215. XCTAssertEqual(UnknownEnumsMyMessage_OneofE1_RawValue(dst), kUnknownValue);
  216. }
  217. - (void)testProto2MergeOneof {
  218. Message2 *src = [Message2 message];
  219. Message2 *dst = [Message2 message];
  220. //
  221. // Make sure whatever is in dst gets cleared out be merging in something else.
  222. //
  223. dst.oneofEnum = Message2_Enum_Bar;
  224. //%PDDM-DEFINE MERGE2_TEST(SET_NAME, SET_VALUE, CLEARED_NAME, CLEARED_DEFAULT)
  225. //% src.oneof##SET_NAME = SET_VALUE;
  226. //% [dst mergeFrom:src];
  227. //% XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_Oneof##SET_NAME);
  228. //% XCTAssertEqual(dst.oneof##SET_NAME, SET_VALUE);
  229. //% XCTAssertEqual(dst.oneof##CLEARED_NAME, CLEARED_DEFAULT);
  230. //%
  231. //%PDDM-EXPAND MERGE2_TEST(Int32, 10, Enum, Message2_Enum_Baz)
  232. // This block of code is generated, do not edit it directly.
  233. src.oneofInt32 = 10;
  234. [dst mergeFrom:src];
  235. XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofInt32);
  236. XCTAssertEqual(dst.oneofInt32, 10);
  237. XCTAssertEqual(dst.oneofEnum, Message2_Enum_Baz);
  238. //%PDDM-EXPAND MERGE2_TEST(Int64, 11, Int32, 100)
  239. // This block of code is generated, do not edit it directly.
  240. src.oneofInt64 = 11;
  241. [dst mergeFrom:src];
  242. XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofInt64);
  243. XCTAssertEqual(dst.oneofInt64, 11);
  244. XCTAssertEqual(dst.oneofInt32, 100);
  245. //%PDDM-EXPAND MERGE2_TEST(Uint32, 12U, Int64, 101)
  246. // This block of code is generated, do not edit it directly.
  247. src.oneofUint32 = 12U;
  248. [dst mergeFrom:src];
  249. XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofUint32);
  250. XCTAssertEqual(dst.oneofUint32, 12U);
  251. XCTAssertEqual(dst.oneofInt64, 101);
  252. //%PDDM-EXPAND MERGE2_TEST(Uint64, 13U, Uint32, 102U)
  253. // This block of code is generated, do not edit it directly.
  254. src.oneofUint64 = 13U;
  255. [dst mergeFrom:src];
  256. XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofUint64);
  257. XCTAssertEqual(dst.oneofUint64, 13U);
  258. XCTAssertEqual(dst.oneofUint32, 102U);
  259. //%PDDM-EXPAND MERGE2_TEST(Sint32, 14, Uint64, 103U)
  260. // This block of code is generated, do not edit it directly.
  261. src.oneofSint32 = 14;
  262. [dst mergeFrom:src];
  263. XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofSint32);
  264. XCTAssertEqual(dst.oneofSint32, 14);
  265. XCTAssertEqual(dst.oneofUint64, 103U);
  266. //%PDDM-EXPAND MERGE2_TEST(Sint64, 15, Sint32, 104)
  267. // This block of code is generated, do not edit it directly.
  268. src.oneofSint64 = 15;
  269. [dst mergeFrom:src];
  270. XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofSint64);
  271. XCTAssertEqual(dst.oneofSint64, 15);
  272. XCTAssertEqual(dst.oneofSint32, 104);
  273. //%PDDM-EXPAND MERGE2_TEST(Fixed32, 16U, Sint64, 105)
  274. // This block of code is generated, do not edit it directly.
  275. src.oneofFixed32 = 16U;
  276. [dst mergeFrom:src];
  277. XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofFixed32);
  278. XCTAssertEqual(dst.oneofFixed32, 16U);
  279. XCTAssertEqual(dst.oneofSint64, 105);
  280. //%PDDM-EXPAND MERGE2_TEST(Fixed64, 17U, Fixed32, 106U)
  281. // This block of code is generated, do not edit it directly.
  282. src.oneofFixed64 = 17U;
  283. [dst mergeFrom:src];
  284. XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofFixed64);
  285. XCTAssertEqual(dst.oneofFixed64, 17U);
  286. XCTAssertEqual(dst.oneofFixed32, 106U);
  287. //%PDDM-EXPAND MERGE2_TEST(Sfixed32, 18, Fixed64, 107U)
  288. // This block of code is generated, do not edit it directly.
  289. src.oneofSfixed32 = 18;
  290. [dst mergeFrom:src];
  291. XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofSfixed32);
  292. XCTAssertEqual(dst.oneofSfixed32, 18);
  293. XCTAssertEqual(dst.oneofFixed64, 107U);
  294. //%PDDM-EXPAND MERGE2_TEST(Sfixed64, 19, Sfixed32, 108)
  295. // This block of code is generated, do not edit it directly.
  296. src.oneofSfixed64 = 19;
  297. [dst mergeFrom:src];
  298. XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofSfixed64);
  299. XCTAssertEqual(dst.oneofSfixed64, 19);
  300. XCTAssertEqual(dst.oneofSfixed32, 108);
  301. //%PDDM-EXPAND MERGE2_TEST(Float, 20.0f, Sfixed64, 109)
  302. // This block of code is generated, do not edit it directly.
  303. src.oneofFloat = 20.0f;
  304. [dst mergeFrom:src];
  305. XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofFloat);
  306. XCTAssertEqual(dst.oneofFloat, 20.0f);
  307. XCTAssertEqual(dst.oneofSfixed64, 109);
  308. //%PDDM-EXPAND MERGE2_TEST(Double, 21.0, Float, 110.0f)
  309. // This block of code is generated, do not edit it directly.
  310. src.oneofDouble = 21.0;
  311. [dst mergeFrom:src];
  312. XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofDouble);
  313. XCTAssertEqual(dst.oneofDouble, 21.0);
  314. XCTAssertEqual(dst.oneofFloat, 110.0f);
  315. //%PDDM-EXPAND MERGE2_TEST(Bool, NO, Double, 111.0)
  316. // This block of code is generated, do not edit it directly.
  317. src.oneofBool = NO;
  318. [dst mergeFrom:src];
  319. XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofBool);
  320. XCTAssertEqual(dst.oneofBool, NO);
  321. XCTAssertEqual(dst.oneofDouble, 111.0);
  322. //%PDDM-EXPAND MERGE2_TEST(Enum, Message2_Enum_Bar, Bool, YES)
  323. // This block of code is generated, do not edit it directly.
  324. src.oneofEnum = Message2_Enum_Bar;
  325. [dst mergeFrom:src];
  326. XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofEnum);
  327. XCTAssertEqual(dst.oneofEnum, Message2_Enum_Bar);
  328. XCTAssertEqual(dst.oneofBool, YES);
  329. //%PDDM-EXPAND-END (14 expansions)
  330. NSString *oneofStringDefault = @"string";
  331. NSData *oneofBytesDefault = [@"data" dataUsingEncoding:NSUTF8StringEncoding];
  332. src.oneofString = @"foo";
  333. [dst mergeFrom:src];
  334. XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofString);
  335. XCTAssertEqualObjects(dst.oneofString, @"foo");
  336. XCTAssertEqual(dst.oneofEnum, Message2_Enum_Baz);
  337. src.oneofBytes = [@"bar" dataUsingEncoding:NSUTF8StringEncoding];
  338. [dst mergeFrom:src];
  339. XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofBytes);
  340. XCTAssertEqualObjects(dst.oneofBytes,
  341. [@"bar" dataUsingEncoding:NSUTF8StringEncoding]);
  342. XCTAssertEqualObjects(dst.oneofString, oneofStringDefault);
  343. Message2_OneofGroup *group = [Message2_OneofGroup message];
  344. group.a = 666;
  345. src.oneofGroup = group;
  346. [dst mergeFrom:src];
  347. XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofGroup);
  348. Message2_OneofGroup *mergedGroup = [[dst.oneofGroup retain] autorelease];
  349. XCTAssertNotNil(mergedGroup);
  350. XCTAssertNotEqual(mergedGroup, group); // Pointer comparision.
  351. XCTAssertEqualObjects(mergedGroup, group);
  352. XCTAssertEqualObjects(dst.oneofBytes, oneofBytesDefault);
  353. Message2 *subMessage = [Message2 message];
  354. subMessage.optionalInt32 = 777;
  355. src.oneofMessage = subMessage;
  356. [dst mergeFrom:src];
  357. XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofMessage);
  358. Message2 *mergedSubMessage = [[dst.oneofMessage retain] autorelease];
  359. XCTAssertNotNil(mergedSubMessage);
  360. XCTAssertNotEqual(mergedSubMessage, subMessage); // Pointer comparision.
  361. XCTAssertEqualObjects(mergedSubMessage, subMessage);
  362. XCTAssertNotNil(dst.oneofGroup);
  363. XCTAssertNotEqual(dst.oneofGroup, mergedGroup); // Pointer comparision.
  364. // Back to something else to make sure message clears out ok.
  365. src.oneofInt32 = 10;
  366. [dst mergeFrom:src];
  367. XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofInt32);
  368. XCTAssertNotNil(dst.oneofMessage);
  369. XCTAssertNotEqual(dst.oneofMessage,
  370. mergedSubMessage); // Pointer comparision.
  371. //
  372. // Test merging in to message/group when they already had something.
  373. //
  374. src.oneofGroup = group;
  375. mergedGroup = [Message2_OneofGroup message];
  376. mergedGroup.b = 888;
  377. dst.oneofGroup = mergedGroup;
  378. [dst mergeFrom:src];
  379. XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofGroup);
  380. // Shouldn't have been a new object.
  381. XCTAssertEqual(dst.oneofGroup, mergedGroup); // Pointer comparision.
  382. XCTAssertEqual(dst.oneofGroup.a, 666); // Pointer comparision.
  383. XCTAssertEqual(dst.oneofGroup.b, 888); // Pointer comparision.
  384. src.oneofMessage = subMessage;
  385. mergedSubMessage = [Message2 message];
  386. mergedSubMessage.optionalInt64 = 999;
  387. dst.oneofMessage = mergedSubMessage;
  388. [dst mergeFrom:src];
  389. XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofMessage);
  390. // Shouldn't have been a new object.
  391. XCTAssertEqual(dst.oneofMessage, mergedSubMessage); // Pointer comparision.
  392. XCTAssertEqual(dst.oneofMessage.optionalInt32, 777); // Pointer comparision.
  393. XCTAssertEqual(dst.oneofMessage.optionalInt64, 999); // Pointer comparision.
  394. }
  395. - (void)testProto3MergeOneof {
  396. Message3 *src = [Message3 message];
  397. Message3 *dst = [Message3 message];
  398. //
  399. // Make sure whatever is in dst gets cleared out be merging in something else.
  400. //
  401. dst.oneofEnum = Message3_Enum_Bar;
  402. //%PDDM-DEFINE MERGE3_TEST(SET_NAME, SET_VALUE, CLEARED_NAME, CLEARED_DEFAULT)
  403. //% src.oneof##SET_NAME = SET_VALUE;
  404. //% [dst mergeFrom:src];
  405. //% XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_Oneof##SET_NAME);
  406. //% XCTAssertEqual(dst.oneof##SET_NAME, SET_VALUE);
  407. //% XCTAssertEqual(dst.oneof##CLEARED_NAME, CLEARED_DEFAULT);
  408. //%
  409. //%PDDM-EXPAND MERGE3_TEST(Int32, 10, Enum, Message3_Enum_Foo)
  410. // This block of code is generated, do not edit it directly.
  411. src.oneofInt32 = 10;
  412. [dst mergeFrom:src];
  413. XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofInt32);
  414. XCTAssertEqual(dst.oneofInt32, 10);
  415. XCTAssertEqual(dst.oneofEnum, Message3_Enum_Foo);
  416. //%PDDM-EXPAND MERGE3_TEST(Int64, 11, Int32, 0)
  417. // This block of code is generated, do not edit it directly.
  418. src.oneofInt64 = 11;
  419. [dst mergeFrom:src];
  420. XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofInt64);
  421. XCTAssertEqual(dst.oneofInt64, 11);
  422. XCTAssertEqual(dst.oneofInt32, 0);
  423. //%PDDM-EXPAND MERGE3_TEST(Uint32, 12U, Int64, 0)
  424. // This block of code is generated, do not edit it directly.
  425. src.oneofUint32 = 12U;
  426. [dst mergeFrom:src];
  427. XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofUint32);
  428. XCTAssertEqual(dst.oneofUint32, 12U);
  429. XCTAssertEqual(dst.oneofInt64, 0);
  430. //%PDDM-EXPAND MERGE3_TEST(Uint64, 13U, Uint32, 0U)
  431. // This block of code is generated, do not edit it directly.
  432. src.oneofUint64 = 13U;
  433. [dst mergeFrom:src];
  434. XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofUint64);
  435. XCTAssertEqual(dst.oneofUint64, 13U);
  436. XCTAssertEqual(dst.oneofUint32, 0U);
  437. //%PDDM-EXPAND MERGE3_TEST(Sint32, 14, Uint64, 0U)
  438. // This block of code is generated, do not edit it directly.
  439. src.oneofSint32 = 14;
  440. [dst mergeFrom:src];
  441. XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofSint32);
  442. XCTAssertEqual(dst.oneofSint32, 14);
  443. XCTAssertEqual(dst.oneofUint64, 0U);
  444. //%PDDM-EXPAND MERGE3_TEST(Sint64, 15, Sint32, 0)
  445. // This block of code is generated, do not edit it directly.
  446. src.oneofSint64 = 15;
  447. [dst mergeFrom:src];
  448. XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofSint64);
  449. XCTAssertEqual(dst.oneofSint64, 15);
  450. XCTAssertEqual(dst.oneofSint32, 0);
  451. //%PDDM-EXPAND MERGE3_TEST(Fixed32, 16U, Sint64, 0)
  452. // This block of code is generated, do not edit it directly.
  453. src.oneofFixed32 = 16U;
  454. [dst mergeFrom:src];
  455. XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofFixed32);
  456. XCTAssertEqual(dst.oneofFixed32, 16U);
  457. XCTAssertEqual(dst.oneofSint64, 0);
  458. //%PDDM-EXPAND MERGE3_TEST(Fixed64, 17U, Fixed32, 0U)
  459. // This block of code is generated, do not edit it directly.
  460. src.oneofFixed64 = 17U;
  461. [dst mergeFrom:src];
  462. XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofFixed64);
  463. XCTAssertEqual(dst.oneofFixed64, 17U);
  464. XCTAssertEqual(dst.oneofFixed32, 0U);
  465. //%PDDM-EXPAND MERGE3_TEST(Sfixed32, 18, Fixed64, 0U)
  466. // This block of code is generated, do not edit it directly.
  467. src.oneofSfixed32 = 18;
  468. [dst mergeFrom:src];
  469. XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofSfixed32);
  470. XCTAssertEqual(dst.oneofSfixed32, 18);
  471. XCTAssertEqual(dst.oneofFixed64, 0U);
  472. //%PDDM-EXPAND MERGE3_TEST(Sfixed64, 19, Sfixed32, 0)
  473. // This block of code is generated, do not edit it directly.
  474. src.oneofSfixed64 = 19;
  475. [dst mergeFrom:src];
  476. XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofSfixed64);
  477. XCTAssertEqual(dst.oneofSfixed64, 19);
  478. XCTAssertEqual(dst.oneofSfixed32, 0);
  479. //%PDDM-EXPAND MERGE3_TEST(Float, 20.0f, Sfixed64, 0)
  480. // This block of code is generated, do not edit it directly.
  481. src.oneofFloat = 20.0f;
  482. [dst mergeFrom:src];
  483. XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofFloat);
  484. XCTAssertEqual(dst.oneofFloat, 20.0f);
  485. XCTAssertEqual(dst.oneofSfixed64, 0);
  486. //%PDDM-EXPAND MERGE3_TEST(Double, 21.0, Float, 0.0f)
  487. // This block of code is generated, do not edit it directly.
  488. src.oneofDouble = 21.0;
  489. [dst mergeFrom:src];
  490. XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofDouble);
  491. XCTAssertEqual(dst.oneofDouble, 21.0);
  492. XCTAssertEqual(dst.oneofFloat, 0.0f);
  493. //%PDDM-EXPAND MERGE3_TEST(Bool, YES, Double, 0.0)
  494. // This block of code is generated, do not edit it directly.
  495. src.oneofBool = YES;
  496. [dst mergeFrom:src];
  497. XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofBool);
  498. XCTAssertEqual(dst.oneofBool, YES);
  499. XCTAssertEqual(dst.oneofDouble, 0.0);
  500. //%PDDM-EXPAND MERGE3_TEST(Enum, Message3_Enum_Bar, Bool, NO)
  501. // This block of code is generated, do not edit it directly.
  502. src.oneofEnum = Message3_Enum_Bar;
  503. [dst mergeFrom:src];
  504. XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofEnum);
  505. XCTAssertEqual(dst.oneofEnum, Message3_Enum_Bar);
  506. XCTAssertEqual(dst.oneofBool, NO);
  507. //%PDDM-EXPAND-END (14 expansions)
  508. NSString *oneofStringDefault = @"";
  509. NSData *oneofBytesDefault = [NSData data];
  510. src.oneofString = @"foo";
  511. [dst mergeFrom:src];
  512. XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofString);
  513. XCTAssertEqualObjects(dst.oneofString, @"foo");
  514. XCTAssertEqual(dst.oneofEnum, Message3_Enum_Foo);
  515. src.oneofBytes = [@"bar" dataUsingEncoding:NSUTF8StringEncoding];
  516. [dst mergeFrom:src];
  517. XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofBytes);
  518. XCTAssertEqualObjects(dst.oneofBytes,
  519. [@"bar" dataUsingEncoding:NSUTF8StringEncoding]);
  520. XCTAssertEqualObjects(dst.oneofString, oneofStringDefault);
  521. Message3 *subMessage = [Message3 message];
  522. subMessage.optionalInt32 = 777;
  523. src.oneofMessage = subMessage;
  524. [dst mergeFrom:src];
  525. XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofMessage);
  526. Message3 *mergedSubMessage = [[dst.oneofMessage retain] autorelease];
  527. XCTAssertNotNil(mergedSubMessage);
  528. XCTAssertNotEqual(mergedSubMessage, subMessage); // Pointer comparision.
  529. XCTAssertEqualObjects(mergedSubMessage, subMessage);
  530. XCTAssertEqualObjects(dst.oneofBytes, oneofBytesDefault);
  531. // Back to something else to make sure message clears out ok.
  532. src.oneofInt32 = 10;
  533. [dst mergeFrom:src];
  534. XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofInt32);
  535. XCTAssertNotNil(dst.oneofMessage);
  536. XCTAssertNotEqual(dst.oneofMessage,
  537. mergedSubMessage); // Pointer comparision.
  538. //
  539. // Test merging in to message when they already had something.
  540. //
  541. src.oneofMessage = subMessage;
  542. mergedSubMessage = [Message3 message];
  543. mergedSubMessage.optionalInt64 = 999;
  544. dst.oneofMessage = mergedSubMessage;
  545. [dst mergeFrom:src];
  546. XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofMessage);
  547. // Shouldn't have been a new object.
  548. XCTAssertEqual(dst.oneofMessage, mergedSubMessage); // Pointer comparision.
  549. XCTAssertEqual(dst.oneofMessage.optionalInt32, 777); // Pointer comparision.
  550. XCTAssertEqual(dst.oneofMessage.optionalInt64, 999); // Pointer comparision.
  551. }
  552. #pragma mark - Subset from from map_tests.cc
  553. // TEST(GeneratedMapFieldTest, CopyFromMessageMap)
  554. - (void)testMap_CopyFromMessageMap {
  555. TestMessageMap *msg1 = [[TestMessageMap alloc] init];
  556. TestMessageMap *msg2 = [[TestMessageMap alloc] init];
  557. TestAllTypes *subMsg = [TestAllTypes message];
  558. subMsg.repeatedInt32Array = [GPBInt32Array arrayWithValue:100];
  559. [msg1.mapInt32Message setObject:subMsg forKey:0];
  560. subMsg = nil;
  561. subMsg = [TestAllTypes message];
  562. subMsg.repeatedInt32Array = [GPBInt32Array arrayWithValue:101];
  563. [msg2.mapInt32Message setObject:subMsg forKey:0];
  564. subMsg = nil;
  565. [msg1 mergeFrom:msg2];
  566. // Checks repeated field is overwritten.
  567. XCTAssertEqual(msg1.mapInt32Message.count, 1U);
  568. subMsg = [msg1.mapInt32Message objectForKey:0];
  569. XCTAssertNotNil(subMsg);
  570. XCTAssertEqual(subMsg.repeatedInt32Array.count, 1U);
  571. XCTAssertEqual([subMsg.repeatedInt32Array valueAtIndex:0], 101);
  572. [msg2 release];
  573. [msg1 release];
  574. }
  575. @end