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