1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2015 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
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
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.
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.
31 #import "GPBTestUtilities.h"
33 #import <objc/runtime.h>
35 #import "GPBMessage.h"
37 #import "google/protobuf/MapUnittest.pbobjc.h"
38 #import "google/protobuf/Unittest.pbobjc.h"
39 #import "google/protobuf/UnittestPreserveUnknownEnum.pbobjc.h"
40 #import "google/protobuf/UnittestRuntimeProto2.pbobjc.h"
41 #import "google/protobuf/UnittestRuntimeProto3.pbobjc.h"
43 @interface MessageMergeTests : GPBTestCase
46 @implementation MessageMergeTests
48 // TODO(thomasvl): Pull tests over from GPBMessageTests that are merge specific.
50 - (void)testProto3MergingAndZeroValues {
51 // Proto2 covered in other tests.
53 Message3 *src = [[Message3 alloc] init];
54 Message3 *dst = [[Message3 alloc] init];
55 NSData *testData1 = [@"abc" dataUsingEncoding:NSUTF8StringEncoding];
56 NSData *testData2 = [@"def" dataUsingEncoding:NSUTF8StringEncoding];
58 dst.optionalInt32 = 1;
59 dst.optionalInt64 = 1;
60 dst.optionalUint32 = 1;
61 dst.optionalUint64 = 1;
62 dst.optionalSint32 = 1;
63 dst.optionalSint64 = 1;
64 dst.optionalFixed32 = 1;
65 dst.optionalFixed64 = 1;
66 dst.optionalSfixed32 = 1;
67 dst.optionalSfixed64 = 1;
68 dst.optionalFloat = 1.0f;
69 dst.optionalDouble = 1.0;
70 dst.optionalBool = YES;
71 dst.optionalString = @"bar";
72 dst.optionalBytes = testData1;
73 dst.optionalEnum = Message3_Enum_Bar;
75 // All zeros, nothing should overright.
77 src.optionalInt32 = 0;
78 src.optionalInt64 = 0;
79 src.optionalUint32 = 0;
80 src.optionalUint64 = 0;
81 src.optionalSint32 = 0;
82 src.optionalSint64 = 0;
83 src.optionalFixed32 = 0;
84 src.optionalFixed64 = 0;
85 src.optionalSfixed32 = 0;
86 src.optionalSfixed64 = 0;
87 src.optionalFloat = 0.0f;
88 src.optionalDouble = 0.0;
89 src.optionalBool = NO;
90 src.optionalString = @"";
91 src.optionalBytes = [NSData data];
92 src.optionalEnum = Message3_Enum_Foo; // first value
96 XCTAssertEqual(dst.optionalInt32, 1);
97 XCTAssertEqual(dst.optionalInt64, 1);
98 XCTAssertEqual(dst.optionalUint32, 1U);
99 XCTAssertEqual(dst.optionalUint64, 1U);
100 XCTAssertEqual(dst.optionalSint32, 1);
101 XCTAssertEqual(dst.optionalSint64, 1);
102 XCTAssertEqual(dst.optionalFixed32, 1U);
103 XCTAssertEqual(dst.optionalFixed64, 1U);
104 XCTAssertEqual(dst.optionalSfixed32, 1);
105 XCTAssertEqual(dst.optionalSfixed64, 1);
106 XCTAssertEqual(dst.optionalFloat, 1.0f);
107 XCTAssertEqual(dst.optionalDouble, 1.0);
108 XCTAssertEqual(dst.optionalBool, YES);
109 XCTAssertEqualObjects(dst.optionalString, @"bar");
110 XCTAssertEqualObjects(dst.optionalBytes, testData1);
111 XCTAssertEqual(dst.optionalEnum, Message3_Enum_Bar);
113 // Half the values that will replace.
115 src.optionalInt32 = 0;
116 src.optionalInt64 = 2;
117 src.optionalUint32 = 0;
118 src.optionalUint64 = 2;
119 src.optionalSint32 = 0;
120 src.optionalSint64 = 2;
121 src.optionalFixed32 = 0;
122 src.optionalFixed64 = 2;
123 src.optionalSfixed32 = 0;
124 src.optionalSfixed64 = 2;
125 src.optionalFloat = 0.0f;
126 src.optionalDouble = 2.0;
127 src.optionalBool = YES; // No other value to use. :(
128 src.optionalString = @"baz";
129 src.optionalBytes = nil;
130 src.optionalEnum = Message3_Enum_Baz;
134 XCTAssertEqual(dst.optionalInt32, 1);
135 XCTAssertEqual(dst.optionalInt64, 2);
136 XCTAssertEqual(dst.optionalUint32, 1U);
137 XCTAssertEqual(dst.optionalUint64, 2U);
138 XCTAssertEqual(dst.optionalSint32, 1);
139 XCTAssertEqual(dst.optionalSint64, 2);
140 XCTAssertEqual(dst.optionalFixed32, 1U);
141 XCTAssertEqual(dst.optionalFixed64, 2U);
142 XCTAssertEqual(dst.optionalSfixed32, 1);
143 XCTAssertEqual(dst.optionalSfixed64, 2);
144 XCTAssertEqual(dst.optionalFloat, 1.0f);
145 XCTAssertEqual(dst.optionalDouble, 2.0);
146 XCTAssertEqual(dst.optionalBool, YES);
147 XCTAssertEqualObjects(dst.optionalString, @"baz");
148 XCTAssertEqualObjects(dst.optionalBytes, testData1);
149 XCTAssertEqual(dst.optionalEnum, Message3_Enum_Baz);
151 // Other half the values that will replace.
153 src.optionalInt32 = 3;
154 src.optionalInt64 = 0;
155 src.optionalUint32 = 3;
156 src.optionalUint64 = 0;
157 src.optionalSint32 = 3;
158 src.optionalSint64 = 0;
159 src.optionalFixed32 = 3;
160 src.optionalFixed64 = 0;
161 src.optionalSfixed32 = 3;
162 src.optionalSfixed64 = 0;
163 src.optionalFloat = 3.0f;
164 src.optionalDouble = 0.0;
165 src.optionalBool = YES; // No other value to use. :(
166 src.optionalString = nil;
167 src.optionalBytes = testData2;
168 src.optionalEnum = Message3_Enum_Foo;
172 XCTAssertEqual(dst.optionalInt32, 3);
173 XCTAssertEqual(dst.optionalInt64, 2);
174 XCTAssertEqual(dst.optionalUint32, 3U);
175 XCTAssertEqual(dst.optionalUint64, 2U);
176 XCTAssertEqual(dst.optionalSint32, 3);
177 XCTAssertEqual(dst.optionalSint64, 2);
178 XCTAssertEqual(dst.optionalFixed32, 3U);
179 XCTAssertEqual(dst.optionalFixed64, 2U);
180 XCTAssertEqual(dst.optionalSfixed32, 3);
181 XCTAssertEqual(dst.optionalSfixed64, 2);
182 XCTAssertEqual(dst.optionalFloat, 3.0f);
183 XCTAssertEqual(dst.optionalDouble, 2.0);
184 XCTAssertEqual(dst.optionalBool, YES);
185 XCTAssertEqualObjects(dst.optionalString, @"baz");
186 XCTAssertEqualObjects(dst.optionalBytes, testData2);
187 XCTAssertEqual(dst.optionalEnum, Message3_Enum_Baz);
193 - (void)testProto3MergingEnums {
194 UnknownEnumsMyMessage *src = [UnknownEnumsMyMessage message];
195 UnknownEnumsMyMessage *dst = [UnknownEnumsMyMessage message];
199 src.e = UnknownEnumsMyEnum_Bar;
201 [GPBEnumArray arrayWithValidationFunction:UnknownEnumsMyEnum_IsValidValue
202 rawValue:UnknownEnumsMyEnum_Bar];
203 src.repeatedPackedEArray =
204 [GPBEnumArray arrayWithValidationFunction:UnknownEnumsMyEnum_IsValidValue
205 rawValue:UnknownEnumsMyEnum_Bar];
206 src.oneofE1 = UnknownEnumsMyEnum_Bar;
210 XCTAssertEqual(dst.e, UnknownEnumsMyEnum_Bar);
211 XCTAssertEqual(dst.repeatedEArray.count, 1U);
212 XCTAssertEqual([dst.repeatedEArray valueAtIndex:0], UnknownEnumsMyEnum_Bar);
213 XCTAssertEqual(dst.repeatedPackedEArray.count, 1U);
214 XCTAssertEqual([dst.repeatedPackedEArray valueAtIndex:0],
215 UnknownEnumsMyEnum_Bar);
216 XCTAssertEqual(dst.oneofE1, UnknownEnumsMyEnum_Bar);
220 const int32_t kUnknownValue = 666;
222 SetUnknownEnumsMyMessage_E_RawValue(src, kUnknownValue);
224 [GPBEnumArray arrayWithValidationFunction:UnknownEnumsMyEnum_IsValidValue
225 rawValue:kUnknownValue];
226 src.repeatedPackedEArray =
227 [GPBEnumArray arrayWithValidationFunction:UnknownEnumsMyEnum_IsValidValue
228 rawValue:kUnknownValue];
229 SetUnknownEnumsMyMessage_OneofE1_RawValue(src, kUnknownValue);
233 XCTAssertEqual(dst.e, UnknownEnumsMyEnum_GPBUnrecognizedEnumeratorValue);
234 XCTAssertEqual(UnknownEnumsMyMessage_E_RawValue(dst), kUnknownValue);
235 XCTAssertEqual(dst.repeatedEArray.count, 2U);
236 XCTAssertEqual([dst.repeatedEArray valueAtIndex:0], UnknownEnumsMyEnum_Bar);
237 XCTAssertEqual([dst.repeatedEArray valueAtIndex:1],
238 UnknownEnumsMyEnum_GPBUnrecognizedEnumeratorValue);
239 XCTAssertEqual([dst.repeatedEArray rawValueAtIndex:1], kUnknownValue);
240 XCTAssertEqual(dst.repeatedPackedEArray.count, 2U);
241 XCTAssertEqual([dst.repeatedPackedEArray valueAtIndex:0],
242 UnknownEnumsMyEnum_Bar);
243 XCTAssertEqual([dst.repeatedPackedEArray valueAtIndex:1],
244 UnknownEnumsMyEnum_GPBUnrecognizedEnumeratorValue);
245 XCTAssertEqual([dst.repeatedPackedEArray rawValueAtIndex:1], kUnknownValue);
246 XCTAssertEqual(dst.oneofE1,
247 UnknownEnumsMyEnum_GPBUnrecognizedEnumeratorValue);
248 XCTAssertEqual(UnknownEnumsMyMessage_OneofE1_RawValue(dst), kUnknownValue);
251 - (void)testProto2MergeOneof {
252 Message2 *src = [Message2 message];
253 Message2 *dst = [Message2 message];
256 // Make sure whatever is in dst gets cleared out be merging in something else.
259 dst.oneofEnum = Message2_Enum_Bar;
261 //%PDDM-DEFINE MERGE2_TEST(SET_NAME, SET_VALUE, CLEARED_NAME, CLEARED_DEFAULT)
262 //% src.oneof##SET_NAME = SET_VALUE;
263 //% [dst mergeFrom:src];
264 //% XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_Oneof##SET_NAME);
265 //% XCTAssertEqual(dst.oneof##SET_NAME, SET_VALUE);
266 //% XCTAssertEqual(dst.oneof##CLEARED_NAME, CLEARED_DEFAULT);
268 //%PDDM-EXPAND MERGE2_TEST(Int32, 10, Enum, Message2_Enum_Baz)
269 // This block of code is generated, do not edit it directly.
273 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofInt32);
274 XCTAssertEqual(dst.oneofInt32, 10);
275 XCTAssertEqual(dst.oneofEnum, Message2_Enum_Baz);
277 //%PDDM-EXPAND MERGE2_TEST(Int64, 11, Int32, 100)
278 // This block of code is generated, do not edit it directly.
282 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofInt64);
283 XCTAssertEqual(dst.oneofInt64, 11);
284 XCTAssertEqual(dst.oneofInt32, 100);
286 //%PDDM-EXPAND MERGE2_TEST(Uint32, 12U, Int64, 101)
287 // This block of code is generated, do not edit it directly.
289 src.oneofUint32 = 12U;
291 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofUint32);
292 XCTAssertEqual(dst.oneofUint32, 12U);
293 XCTAssertEqual(dst.oneofInt64, 101);
295 //%PDDM-EXPAND MERGE2_TEST(Uint64, 13U, Uint32, 102U)
296 // This block of code is generated, do not edit it directly.
298 src.oneofUint64 = 13U;
300 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofUint64);
301 XCTAssertEqual(dst.oneofUint64, 13U);
302 XCTAssertEqual(dst.oneofUint32, 102U);
304 //%PDDM-EXPAND MERGE2_TEST(Sint32, 14, Uint64, 103U)
305 // This block of code is generated, do not edit it directly.
307 src.oneofSint32 = 14;
309 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofSint32);
310 XCTAssertEqual(dst.oneofSint32, 14);
311 XCTAssertEqual(dst.oneofUint64, 103U);
313 //%PDDM-EXPAND MERGE2_TEST(Sint64, 15, Sint32, 104)
314 // This block of code is generated, do not edit it directly.
316 src.oneofSint64 = 15;
318 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofSint64);
319 XCTAssertEqual(dst.oneofSint64, 15);
320 XCTAssertEqual(dst.oneofSint32, 104);
322 //%PDDM-EXPAND MERGE2_TEST(Fixed32, 16U, Sint64, 105)
323 // This block of code is generated, do not edit it directly.
325 src.oneofFixed32 = 16U;
327 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofFixed32);
328 XCTAssertEqual(dst.oneofFixed32, 16U);
329 XCTAssertEqual(dst.oneofSint64, 105);
331 //%PDDM-EXPAND MERGE2_TEST(Fixed64, 17U, Fixed32, 106U)
332 // This block of code is generated, do not edit it directly.
334 src.oneofFixed64 = 17U;
336 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofFixed64);
337 XCTAssertEqual(dst.oneofFixed64, 17U);
338 XCTAssertEqual(dst.oneofFixed32, 106U);
340 //%PDDM-EXPAND MERGE2_TEST(Sfixed32, 18, Fixed64, 107U)
341 // This block of code is generated, do not edit it directly.
343 src.oneofSfixed32 = 18;
345 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofSfixed32);
346 XCTAssertEqual(dst.oneofSfixed32, 18);
347 XCTAssertEqual(dst.oneofFixed64, 107U);
349 //%PDDM-EXPAND MERGE2_TEST(Sfixed64, 19, Sfixed32, 108)
350 // This block of code is generated, do not edit it directly.
352 src.oneofSfixed64 = 19;
354 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofSfixed64);
355 XCTAssertEqual(dst.oneofSfixed64, 19);
356 XCTAssertEqual(dst.oneofSfixed32, 108);
358 //%PDDM-EXPAND MERGE2_TEST(Float, 20.0f, Sfixed64, 109)
359 // This block of code is generated, do not edit it directly.
361 src.oneofFloat = 20.0f;
363 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofFloat);
364 XCTAssertEqual(dst.oneofFloat, 20.0f);
365 XCTAssertEqual(dst.oneofSfixed64, 109);
367 //%PDDM-EXPAND MERGE2_TEST(Double, 21.0, Float, 110.0f)
368 // This block of code is generated, do not edit it directly.
370 src.oneofDouble = 21.0;
372 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofDouble);
373 XCTAssertEqual(dst.oneofDouble, 21.0);
374 XCTAssertEqual(dst.oneofFloat, 110.0f);
376 //%PDDM-EXPAND MERGE2_TEST(Bool, NO, Double, 111.0)
377 // This block of code is generated, do not edit it directly.
381 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofBool);
382 XCTAssertEqual(dst.oneofBool, NO);
383 XCTAssertEqual(dst.oneofDouble, 111.0);
385 //%PDDM-EXPAND MERGE2_TEST(Enum, Message2_Enum_Bar, Bool, YES)
386 // This block of code is generated, do not edit it directly.
388 src.oneofEnum = Message2_Enum_Bar;
390 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofEnum);
391 XCTAssertEqual(dst.oneofEnum, Message2_Enum_Bar);
392 XCTAssertEqual(dst.oneofBool, YES);
394 //%PDDM-EXPAND-END (14 expansions)
396 NSString *oneofStringDefault = @"string";
397 NSData *oneofBytesDefault = [@"data" dataUsingEncoding:NSUTF8StringEncoding];
399 src.oneofString = @"foo";
401 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofString);
402 XCTAssertEqualObjects(dst.oneofString, @"foo");
403 XCTAssertEqual(dst.oneofEnum, Message2_Enum_Baz);
405 src.oneofBytes = [@"bar" dataUsingEncoding:NSUTF8StringEncoding];
407 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofBytes);
408 XCTAssertEqualObjects(dst.oneofBytes,
409 [@"bar" dataUsingEncoding:NSUTF8StringEncoding]);
410 XCTAssertEqualObjects(dst.oneofString, oneofStringDefault);
412 Message2_OneofGroup *group = [Message2_OneofGroup message];
414 src.oneofGroup = group;
416 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofGroup);
417 Message2_OneofGroup *mergedGroup = [[dst.oneofGroup retain] autorelease];
418 XCTAssertNotNil(mergedGroup);
419 XCTAssertNotEqual(mergedGroup, group); // Pointer comparision.
420 XCTAssertEqualObjects(mergedGroup, group);
421 XCTAssertEqualObjects(dst.oneofBytes, oneofBytesDefault);
423 Message2 *subMessage = [Message2 message];
424 subMessage.optionalInt32 = 777;
425 src.oneofMessage = subMessage;
427 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofMessage);
428 Message2 *mergedSubMessage = [[dst.oneofMessage retain] autorelease];
429 XCTAssertNotNil(mergedSubMessage);
430 XCTAssertNotEqual(mergedSubMessage, subMessage); // Pointer comparision.
431 XCTAssertEqualObjects(mergedSubMessage, subMessage);
432 XCTAssertNotNil(dst.oneofGroup);
433 XCTAssertNotEqual(dst.oneofGroup, mergedGroup); // Pointer comparision.
435 // Back to something else to make sure message clears out ok.
439 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofInt32);
440 XCTAssertNotNil(dst.oneofMessage);
441 XCTAssertNotEqual(dst.oneofMessage,
442 mergedSubMessage); // Pointer comparision.
445 // Test merging in to message/group when they already had something.
448 src.oneofGroup = group;
449 mergedGroup = [Message2_OneofGroup message];
451 dst.oneofGroup = mergedGroup;
453 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofGroup);
454 // Shouldn't have been a new object.
455 XCTAssertEqual(dst.oneofGroup, mergedGroup); // Pointer comparision.
456 XCTAssertEqual(dst.oneofGroup.a, 666); // Pointer comparision.
457 XCTAssertEqual(dst.oneofGroup.b, 888); // Pointer comparision.
459 src.oneofMessage = subMessage;
460 mergedSubMessage = [Message2 message];
461 mergedSubMessage.optionalInt64 = 999;
462 dst.oneofMessage = mergedSubMessage;
464 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofMessage);
465 // Shouldn't have been a new object.
466 XCTAssertEqual(dst.oneofMessage, mergedSubMessage); // Pointer comparision.
467 XCTAssertEqual(dst.oneofMessage.optionalInt32, 777); // Pointer comparision.
468 XCTAssertEqual(dst.oneofMessage.optionalInt64, 999); // Pointer comparision.
471 - (void)testProto3MergeOneof {
472 Message3 *src = [Message3 message];
473 Message3 *dst = [Message3 message];
476 // Make sure whatever is in dst gets cleared out be merging in something else.
479 dst.oneofEnum = Message3_Enum_Bar;
481 //%PDDM-DEFINE MERGE3_TEST(SET_NAME, SET_VALUE, CLEARED_NAME, CLEARED_DEFAULT)
482 //% src.oneof##SET_NAME = SET_VALUE;
483 //% [dst mergeFrom:src];
484 //% XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_Oneof##SET_NAME);
485 //% XCTAssertEqual(dst.oneof##SET_NAME, SET_VALUE);
486 //% XCTAssertEqual(dst.oneof##CLEARED_NAME, CLEARED_DEFAULT);
488 //%PDDM-EXPAND MERGE3_TEST(Int32, 10, Enum, Message3_Enum_Foo)
489 // This block of code is generated, do not edit it directly.
493 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofInt32);
494 XCTAssertEqual(dst.oneofInt32, 10);
495 XCTAssertEqual(dst.oneofEnum, Message3_Enum_Foo);
497 //%PDDM-EXPAND MERGE3_TEST(Int64, 11, Int32, 0)
498 // This block of code is generated, do not edit it directly.
502 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofInt64);
503 XCTAssertEqual(dst.oneofInt64, 11);
504 XCTAssertEqual(dst.oneofInt32, 0);
506 //%PDDM-EXPAND MERGE3_TEST(Uint32, 12U, Int64, 0)
507 // This block of code is generated, do not edit it directly.
509 src.oneofUint32 = 12U;
511 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofUint32);
512 XCTAssertEqual(dst.oneofUint32, 12U);
513 XCTAssertEqual(dst.oneofInt64, 0);
515 //%PDDM-EXPAND MERGE3_TEST(Uint64, 13U, Uint32, 0U)
516 // This block of code is generated, do not edit it directly.
518 src.oneofUint64 = 13U;
520 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofUint64);
521 XCTAssertEqual(dst.oneofUint64, 13U);
522 XCTAssertEqual(dst.oneofUint32, 0U);
524 //%PDDM-EXPAND MERGE3_TEST(Sint32, 14, Uint64, 0U)
525 // This block of code is generated, do not edit it directly.
527 src.oneofSint32 = 14;
529 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofSint32);
530 XCTAssertEqual(dst.oneofSint32, 14);
531 XCTAssertEqual(dst.oneofUint64, 0U);
533 //%PDDM-EXPAND MERGE3_TEST(Sint64, 15, Sint32, 0)
534 // This block of code is generated, do not edit it directly.
536 src.oneofSint64 = 15;
538 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofSint64);
539 XCTAssertEqual(dst.oneofSint64, 15);
540 XCTAssertEqual(dst.oneofSint32, 0);
542 //%PDDM-EXPAND MERGE3_TEST(Fixed32, 16U, Sint64, 0)
543 // This block of code is generated, do not edit it directly.
545 src.oneofFixed32 = 16U;
547 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofFixed32);
548 XCTAssertEqual(dst.oneofFixed32, 16U);
549 XCTAssertEqual(dst.oneofSint64, 0);
551 //%PDDM-EXPAND MERGE3_TEST(Fixed64, 17U, Fixed32, 0U)
552 // This block of code is generated, do not edit it directly.
554 src.oneofFixed64 = 17U;
556 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofFixed64);
557 XCTAssertEqual(dst.oneofFixed64, 17U);
558 XCTAssertEqual(dst.oneofFixed32, 0U);
560 //%PDDM-EXPAND MERGE3_TEST(Sfixed32, 18, Fixed64, 0U)
561 // This block of code is generated, do not edit it directly.
563 src.oneofSfixed32 = 18;
565 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofSfixed32);
566 XCTAssertEqual(dst.oneofSfixed32, 18);
567 XCTAssertEqual(dst.oneofFixed64, 0U);
569 //%PDDM-EXPAND MERGE3_TEST(Sfixed64, 19, Sfixed32, 0)
570 // This block of code is generated, do not edit it directly.
572 src.oneofSfixed64 = 19;
574 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofSfixed64);
575 XCTAssertEqual(dst.oneofSfixed64, 19);
576 XCTAssertEqual(dst.oneofSfixed32, 0);
578 //%PDDM-EXPAND MERGE3_TEST(Float, 20.0f, Sfixed64, 0)
579 // This block of code is generated, do not edit it directly.
581 src.oneofFloat = 20.0f;
583 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofFloat);
584 XCTAssertEqual(dst.oneofFloat, 20.0f);
585 XCTAssertEqual(dst.oneofSfixed64, 0);
587 //%PDDM-EXPAND MERGE3_TEST(Double, 21.0, Float, 0.0f)
588 // This block of code is generated, do not edit it directly.
590 src.oneofDouble = 21.0;
592 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofDouble);
593 XCTAssertEqual(dst.oneofDouble, 21.0);
594 XCTAssertEqual(dst.oneofFloat, 0.0f);
596 //%PDDM-EXPAND MERGE3_TEST(Bool, YES, Double, 0.0)
597 // This block of code is generated, do not edit it directly.
601 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofBool);
602 XCTAssertEqual(dst.oneofBool, YES);
603 XCTAssertEqual(dst.oneofDouble, 0.0);
605 //%PDDM-EXPAND MERGE3_TEST(Enum, Message3_Enum_Bar, Bool, NO)
606 // This block of code is generated, do not edit it directly.
608 src.oneofEnum = Message3_Enum_Bar;
610 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofEnum);
611 XCTAssertEqual(dst.oneofEnum, Message3_Enum_Bar);
612 XCTAssertEqual(dst.oneofBool, NO);
614 //%PDDM-EXPAND-END (14 expansions)
616 NSString *oneofStringDefault = @"";
617 NSData *oneofBytesDefault = [NSData data];
619 src.oneofString = @"foo";
621 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofString);
622 XCTAssertEqualObjects(dst.oneofString, @"foo");
623 XCTAssertEqual(dst.oneofEnum, Message3_Enum_Foo);
625 src.oneofBytes = [@"bar" dataUsingEncoding:NSUTF8StringEncoding];
627 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofBytes);
628 XCTAssertEqualObjects(dst.oneofBytes,
629 [@"bar" dataUsingEncoding:NSUTF8StringEncoding]);
630 XCTAssertEqualObjects(dst.oneofString, oneofStringDefault);
633 Message3 *subMessage = [Message3 message];
634 subMessage.optionalInt32 = 777;
635 src.oneofMessage = subMessage;
637 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofMessage);
638 Message3 *mergedSubMessage = [[dst.oneofMessage retain] autorelease];
639 XCTAssertNotNil(mergedSubMessage);
640 XCTAssertNotEqual(mergedSubMessage, subMessage); // Pointer comparision.
641 XCTAssertEqualObjects(mergedSubMessage, subMessage);
642 XCTAssertEqualObjects(dst.oneofBytes, oneofBytesDefault);
644 // Back to something else to make sure message clears out ok.
648 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofInt32);
649 XCTAssertNotNil(dst.oneofMessage);
650 XCTAssertNotEqual(dst.oneofMessage,
651 mergedSubMessage); // Pointer comparision.
654 // Test merging in to message when they already had something.
657 src.oneofMessage = subMessage;
658 mergedSubMessage = [Message3 message];
659 mergedSubMessage.optionalInt64 = 999;
660 dst.oneofMessage = mergedSubMessage;
662 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofMessage);
663 // Shouldn't have been a new object.
664 XCTAssertEqual(dst.oneofMessage, mergedSubMessage); // Pointer comparision.
665 XCTAssertEqual(dst.oneofMessage.optionalInt32, 777); // Pointer comparision.
666 XCTAssertEqual(dst.oneofMessage.optionalInt64, 999); // Pointer comparision.
669 #pragma mark - Subset from from map_tests.cc
671 // TEST(GeneratedMapFieldTest, CopyFromMessageMap)
672 - (void)testMap_CopyFromMessageMap {
673 TestMessageMap *msg1 = [[TestMessageMap alloc] init];
674 TestMessageMap *msg2 = [[TestMessageMap alloc] init];
676 TestAllTypes *subMsg = [TestAllTypes message];
677 subMsg.repeatedInt32Array = [GPBInt32Array arrayWithValue:100];
678 msg1.mapInt32Message = [GPBInt32ObjectDictionary dictionary];
679 [msg1.mapInt32Message setValue:subMsg forKey:0];
682 subMsg = [TestAllTypes message];
683 subMsg.repeatedInt32Array = [GPBInt32Array arrayWithValue:101];
684 msg2.mapInt32Message = [GPBInt32ObjectDictionary dictionary];
685 [msg2.mapInt32Message setValue:subMsg forKey:0];
688 [msg1 mergeFrom:msg2];
690 // Checks repeated field is overwritten.
691 XCTAssertEqual(msg1.mapInt32Message.count, 1U);
692 subMsg = [msg1.mapInt32Message valueForKey:0];
693 XCTAssertNotNil(subMsg);
694 XCTAssertEqual(subMsg.repeatedInt32Array.count, 1U);
695 XCTAssertEqual([subMsg.repeatedInt32Array valueAtIndex:0], 101);