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