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 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/Unittest.pbobjc.h" 39#import "google/protobuf/UnittestPreserveUnknownEnum.pbobjc.h" 40#import "google/protobuf/UnittestRuntimeProto2.pbobjc.h" 41#import "google/protobuf/UnittestRuntimeProto3.pbobjc.h" 42 43@interface MessageMergeTests : GPBTestCase 44@end 45 46@implementation MessageMergeTests 47 48// TODO(thomasvl): Pull tests over from GPBMessageTests that are merge specific. 49 50- (void)testProto3MergingAndZeroValues { 51 // Proto2 covered in other tests. 52 53 Message3 *src = [[Message3 alloc] init]; 54 Message3 *dst = [[Message3 alloc] init]; 55 NSData *testData1 = [@"abc" dataUsingEncoding:NSUTF8StringEncoding]; 56 NSData *testData2 = [@"def" dataUsingEncoding:NSUTF8StringEncoding]; 57 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; 74 75 // All zeros, nothing should overright. 76 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 93 94 [dst mergeFrom:src]; 95 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); 112 113 // Half the values that will replace. 114 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; 131 132 [dst mergeFrom:src]; 133 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); 150 151 // Other half the values that will replace. 152 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; 169 170 [dst mergeFrom:src]; 171 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); 188 189 [src release]; 190 [dst release]; 191} 192 193- (void)testProto3MergingEnums { 194 UnknownEnumsMyMessage *src = [UnknownEnumsMyMessage message]; 195 UnknownEnumsMyMessage *dst = [UnknownEnumsMyMessage message]; 196 197 // Known value. 198 199 src.e = UnknownEnumsMyEnum_Bar; 200 src.repeatedEArray = 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; 207 208 [dst mergeFrom:src]; 209 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); 217 218 // Unknown value. 219 220 const int32_t kUnknownValue = 666; 221 222 SetUnknownEnumsMyMessage_E_RawValue(src, kUnknownValue); 223 src.repeatedEArray = 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); 230 231 [dst mergeFrom:src]; 232 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); 249} 250 251- (void)testProto2MergeOneof { 252 Message2 *src = [Message2 message]; 253 Message2 *dst = [Message2 message]; 254 255 // 256 // Make sure whatever is in dst gets cleared out be merging in something else. 257 // 258 259 dst.oneofEnum = Message2_Enum_Bar; 260 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); 267//% 268//%PDDM-EXPAND MERGE2_TEST(Int32, 10, Enum, Message2_Enum_Baz) 269// This block of code is generated, do not edit it directly. 270 271 src.oneofInt32 = 10; 272 [dst mergeFrom:src]; 273 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofInt32); 274 XCTAssertEqual(dst.oneofInt32, 10); 275 XCTAssertEqual(dst.oneofEnum, Message2_Enum_Baz); 276 277//%PDDM-EXPAND MERGE2_TEST(Int64, 11, Int32, 100) 278// This block of code is generated, do not edit it directly. 279 280 src.oneofInt64 = 11; 281 [dst mergeFrom:src]; 282 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofInt64); 283 XCTAssertEqual(dst.oneofInt64, 11); 284 XCTAssertEqual(dst.oneofInt32, 100); 285 286//%PDDM-EXPAND MERGE2_TEST(Uint32, 12U, Int64, 101) 287// This block of code is generated, do not edit it directly. 288 289 src.oneofUint32 = 12U; 290 [dst mergeFrom:src]; 291 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofUint32); 292 XCTAssertEqual(dst.oneofUint32, 12U); 293 XCTAssertEqual(dst.oneofInt64, 101); 294 295//%PDDM-EXPAND MERGE2_TEST(Uint64, 13U, Uint32, 102U) 296// This block of code is generated, do not edit it directly. 297 298 src.oneofUint64 = 13U; 299 [dst mergeFrom:src]; 300 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofUint64); 301 XCTAssertEqual(dst.oneofUint64, 13U); 302 XCTAssertEqual(dst.oneofUint32, 102U); 303 304//%PDDM-EXPAND MERGE2_TEST(Sint32, 14, Uint64, 103U) 305// This block of code is generated, do not edit it directly. 306 307 src.oneofSint32 = 14; 308 [dst mergeFrom:src]; 309 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofSint32); 310 XCTAssertEqual(dst.oneofSint32, 14); 311 XCTAssertEqual(dst.oneofUint64, 103U); 312 313//%PDDM-EXPAND MERGE2_TEST(Sint64, 15, Sint32, 104) 314// This block of code is generated, do not edit it directly. 315 316 src.oneofSint64 = 15; 317 [dst mergeFrom:src]; 318 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofSint64); 319 XCTAssertEqual(dst.oneofSint64, 15); 320 XCTAssertEqual(dst.oneofSint32, 104); 321 322//%PDDM-EXPAND MERGE2_TEST(Fixed32, 16U, Sint64, 105) 323// This block of code is generated, do not edit it directly. 324 325 src.oneofFixed32 = 16U; 326 [dst mergeFrom:src]; 327 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofFixed32); 328 XCTAssertEqual(dst.oneofFixed32, 16U); 329 XCTAssertEqual(dst.oneofSint64, 105); 330 331//%PDDM-EXPAND MERGE2_TEST(Fixed64, 17U, Fixed32, 106U) 332// This block of code is generated, do not edit it directly. 333 334 src.oneofFixed64 = 17U; 335 [dst mergeFrom:src]; 336 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofFixed64); 337 XCTAssertEqual(dst.oneofFixed64, 17U); 338 XCTAssertEqual(dst.oneofFixed32, 106U); 339 340//%PDDM-EXPAND MERGE2_TEST(Sfixed32, 18, Fixed64, 107U) 341// This block of code is generated, do not edit it directly. 342 343 src.oneofSfixed32 = 18; 344 [dst mergeFrom:src]; 345 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofSfixed32); 346 XCTAssertEqual(dst.oneofSfixed32, 18); 347 XCTAssertEqual(dst.oneofFixed64, 107U); 348 349//%PDDM-EXPAND MERGE2_TEST(Sfixed64, 19, Sfixed32, 108) 350// This block of code is generated, do not edit it directly. 351 352 src.oneofSfixed64 = 19; 353 [dst mergeFrom:src]; 354 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofSfixed64); 355 XCTAssertEqual(dst.oneofSfixed64, 19); 356 XCTAssertEqual(dst.oneofSfixed32, 108); 357 358//%PDDM-EXPAND MERGE2_TEST(Float, 20.0f, Sfixed64, 109) 359// This block of code is generated, do not edit it directly. 360 361 src.oneofFloat = 20.0f; 362 [dst mergeFrom:src]; 363 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofFloat); 364 XCTAssertEqual(dst.oneofFloat, 20.0f); 365 XCTAssertEqual(dst.oneofSfixed64, 109); 366 367//%PDDM-EXPAND MERGE2_TEST(Double, 21.0, Float, 110.0f) 368// This block of code is generated, do not edit it directly. 369 370 src.oneofDouble = 21.0; 371 [dst mergeFrom:src]; 372 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofDouble); 373 XCTAssertEqual(dst.oneofDouble, 21.0); 374 XCTAssertEqual(dst.oneofFloat, 110.0f); 375 376//%PDDM-EXPAND MERGE2_TEST(Bool, NO, Double, 111.0) 377// This block of code is generated, do not edit it directly. 378 379 src.oneofBool = NO; 380 [dst mergeFrom:src]; 381 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofBool); 382 XCTAssertEqual(dst.oneofBool, NO); 383 XCTAssertEqual(dst.oneofDouble, 111.0); 384 385//%PDDM-EXPAND MERGE2_TEST(Enum, Message2_Enum_Bar, Bool, YES) 386// This block of code is generated, do not edit it directly. 387 388 src.oneofEnum = Message2_Enum_Bar; 389 [dst mergeFrom:src]; 390 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofEnum); 391 XCTAssertEqual(dst.oneofEnum, Message2_Enum_Bar); 392 XCTAssertEqual(dst.oneofBool, YES); 393 394//%PDDM-EXPAND-END (14 expansions) 395 396 NSString *oneofStringDefault = @"string"; 397 NSData *oneofBytesDefault = [@"data" dataUsingEncoding:NSUTF8StringEncoding]; 398 399 src.oneofString = @"foo"; 400 [dst mergeFrom:src]; 401 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofString); 402 XCTAssertEqualObjects(dst.oneofString, @"foo"); 403 XCTAssertEqual(dst.oneofEnum, Message2_Enum_Baz); 404 405 src.oneofBytes = [@"bar" dataUsingEncoding:NSUTF8StringEncoding]; 406 [dst mergeFrom:src]; 407 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofBytes); 408 XCTAssertEqualObjects(dst.oneofBytes, 409 [@"bar" dataUsingEncoding:NSUTF8StringEncoding]); 410 XCTAssertEqualObjects(dst.oneofString, oneofStringDefault); 411 412 Message2_OneofGroup *group = [Message2_OneofGroup message]; 413 group.a = 666; 414 src.oneofGroup = group; 415 [dst mergeFrom:src]; 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); 422 423 Message2 *subMessage = [Message2 message]; 424 subMessage.optionalInt32 = 777; 425 src.oneofMessage = subMessage; 426 [dst mergeFrom:src]; 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. 434 435 // Back to something else to make sure message clears out ok. 436 437 src.oneofInt32 = 10; 438 [dst mergeFrom:src]; 439 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofInt32); 440 XCTAssertNotNil(dst.oneofMessage); 441 XCTAssertNotEqual(dst.oneofMessage, 442 mergedSubMessage); // Pointer comparision. 443 444 // 445 // Test merging in to message/group when they already had something. 446 // 447 448 src.oneofGroup = group; 449 mergedGroup = [Message2_OneofGroup message]; 450 mergedGroup.b = 888; 451 dst.oneofGroup = mergedGroup; 452 [dst mergeFrom:src]; 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. 458 459 src.oneofMessage = subMessage; 460 mergedSubMessage = [Message2 message]; 461 mergedSubMessage.optionalInt64 = 999; 462 dst.oneofMessage = mergedSubMessage; 463 [dst mergeFrom:src]; 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. 469} 470 471- (void)testProto3MergeOneof { 472 Message3 *src = [Message3 message]; 473 Message3 *dst = [Message3 message]; 474 475 // 476 // Make sure whatever is in dst gets cleared out be merging in something else. 477 // 478 479 dst.oneofEnum = Message3_Enum_Bar; 480 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); 487//% 488//%PDDM-EXPAND MERGE3_TEST(Int32, 10, Enum, Message3_Enum_Foo) 489// This block of code is generated, do not edit it directly. 490 491 src.oneofInt32 = 10; 492 [dst mergeFrom:src]; 493 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofInt32); 494 XCTAssertEqual(dst.oneofInt32, 10); 495 XCTAssertEqual(dst.oneofEnum, Message3_Enum_Foo); 496 497//%PDDM-EXPAND MERGE3_TEST(Int64, 11, Int32, 0) 498// This block of code is generated, do not edit it directly. 499 500 src.oneofInt64 = 11; 501 [dst mergeFrom:src]; 502 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofInt64); 503 XCTAssertEqual(dst.oneofInt64, 11); 504 XCTAssertEqual(dst.oneofInt32, 0); 505 506//%PDDM-EXPAND MERGE3_TEST(Uint32, 12U, Int64, 0) 507// This block of code is generated, do not edit it directly. 508 509 src.oneofUint32 = 12U; 510 [dst mergeFrom:src]; 511 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofUint32); 512 XCTAssertEqual(dst.oneofUint32, 12U); 513 XCTAssertEqual(dst.oneofInt64, 0); 514 515//%PDDM-EXPAND MERGE3_TEST(Uint64, 13U, Uint32, 0U) 516// This block of code is generated, do not edit it directly. 517 518 src.oneofUint64 = 13U; 519 [dst mergeFrom:src]; 520 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofUint64); 521 XCTAssertEqual(dst.oneofUint64, 13U); 522 XCTAssertEqual(dst.oneofUint32, 0U); 523 524//%PDDM-EXPAND MERGE3_TEST(Sint32, 14, Uint64, 0U) 525// This block of code is generated, do not edit it directly. 526 527 src.oneofSint32 = 14; 528 [dst mergeFrom:src]; 529 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofSint32); 530 XCTAssertEqual(dst.oneofSint32, 14); 531 XCTAssertEqual(dst.oneofUint64, 0U); 532 533//%PDDM-EXPAND MERGE3_TEST(Sint64, 15, Sint32, 0) 534// This block of code is generated, do not edit it directly. 535 536 src.oneofSint64 = 15; 537 [dst mergeFrom:src]; 538 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofSint64); 539 XCTAssertEqual(dst.oneofSint64, 15); 540 XCTAssertEqual(dst.oneofSint32, 0); 541 542//%PDDM-EXPAND MERGE3_TEST(Fixed32, 16U, Sint64, 0) 543// This block of code is generated, do not edit it directly. 544 545 src.oneofFixed32 = 16U; 546 [dst mergeFrom:src]; 547 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofFixed32); 548 XCTAssertEqual(dst.oneofFixed32, 16U); 549 XCTAssertEqual(dst.oneofSint64, 0); 550 551//%PDDM-EXPAND MERGE3_TEST(Fixed64, 17U, Fixed32, 0U) 552// This block of code is generated, do not edit it directly. 553 554 src.oneofFixed64 = 17U; 555 [dst mergeFrom:src]; 556 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofFixed64); 557 XCTAssertEqual(dst.oneofFixed64, 17U); 558 XCTAssertEqual(dst.oneofFixed32, 0U); 559 560//%PDDM-EXPAND MERGE3_TEST(Sfixed32, 18, Fixed64, 0U) 561// This block of code is generated, do not edit it directly. 562 563 src.oneofSfixed32 = 18; 564 [dst mergeFrom:src]; 565 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofSfixed32); 566 XCTAssertEqual(dst.oneofSfixed32, 18); 567 XCTAssertEqual(dst.oneofFixed64, 0U); 568 569//%PDDM-EXPAND MERGE3_TEST(Sfixed64, 19, Sfixed32, 0) 570// This block of code is generated, do not edit it directly. 571 572 src.oneofSfixed64 = 19; 573 [dst mergeFrom:src]; 574 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofSfixed64); 575 XCTAssertEqual(dst.oneofSfixed64, 19); 576 XCTAssertEqual(dst.oneofSfixed32, 0); 577 578//%PDDM-EXPAND MERGE3_TEST(Float, 20.0f, Sfixed64, 0) 579// This block of code is generated, do not edit it directly. 580 581 src.oneofFloat = 20.0f; 582 [dst mergeFrom:src]; 583 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofFloat); 584 XCTAssertEqual(dst.oneofFloat, 20.0f); 585 XCTAssertEqual(dst.oneofSfixed64, 0); 586 587//%PDDM-EXPAND MERGE3_TEST(Double, 21.0, Float, 0.0f) 588// This block of code is generated, do not edit it directly. 589 590 src.oneofDouble = 21.0; 591 [dst mergeFrom:src]; 592 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofDouble); 593 XCTAssertEqual(dst.oneofDouble, 21.0); 594 XCTAssertEqual(dst.oneofFloat, 0.0f); 595 596//%PDDM-EXPAND MERGE3_TEST(Bool, YES, Double, 0.0) 597// This block of code is generated, do not edit it directly. 598 599 src.oneofBool = YES; 600 [dst mergeFrom:src]; 601 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofBool); 602 XCTAssertEqual(dst.oneofBool, YES); 603 XCTAssertEqual(dst.oneofDouble, 0.0); 604 605//%PDDM-EXPAND MERGE3_TEST(Enum, Message3_Enum_Bar, Bool, NO) 606// This block of code is generated, do not edit it directly. 607 608 src.oneofEnum = Message3_Enum_Bar; 609 [dst mergeFrom:src]; 610 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofEnum); 611 XCTAssertEqual(dst.oneofEnum, Message3_Enum_Bar); 612 XCTAssertEqual(dst.oneofBool, NO); 613 614//%PDDM-EXPAND-END (14 expansions) 615 616 NSString *oneofStringDefault = @""; 617 NSData *oneofBytesDefault = [NSData data]; 618 619 src.oneofString = @"foo"; 620 [dst mergeFrom:src]; 621 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofString); 622 XCTAssertEqualObjects(dst.oneofString, @"foo"); 623 XCTAssertEqual(dst.oneofEnum, Message3_Enum_Foo); 624 625 src.oneofBytes = [@"bar" dataUsingEncoding:NSUTF8StringEncoding]; 626 [dst mergeFrom:src]; 627 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofBytes); 628 XCTAssertEqualObjects(dst.oneofBytes, 629 [@"bar" dataUsingEncoding:NSUTF8StringEncoding]); 630 XCTAssertEqualObjects(dst.oneofString, oneofStringDefault); 631 632 633 Message3 *subMessage = [Message3 message]; 634 subMessage.optionalInt32 = 777; 635 src.oneofMessage = subMessage; 636 [dst mergeFrom:src]; 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); 643 644 // Back to something else to make sure message clears out ok. 645 646 src.oneofInt32 = 10; 647 [dst mergeFrom:src]; 648 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofInt32); 649 XCTAssertNotNil(dst.oneofMessage); 650 XCTAssertNotEqual(dst.oneofMessage, 651 mergedSubMessage); // Pointer comparision. 652 653 // 654 // Test merging in to message when they already had something. 655 // 656 657 src.oneofMessage = subMessage; 658 mergedSubMessage = [Message3 message]; 659 mergedSubMessage.optionalInt64 = 999; 660 dst.oneofMessage = mergedSubMessage; 661 [dst mergeFrom:src]; 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. 667} 668 669#pragma mark - Subset from from map_tests.cc 670 671// TEST(GeneratedMapFieldTest, CopyFromMessageMap) 672- (void)testMap_CopyFromMessageMap { 673 TestMessageMap *msg1 = [[TestMessageMap alloc] init]; 674 TestMessageMap *msg2 = [[TestMessageMap alloc] init]; 675 676 TestAllTypes *subMsg = [TestAllTypes message]; 677 subMsg.repeatedInt32Array = [GPBInt32Array arrayWithValue:100]; 678 [msg1.mapInt32Message setObject:subMsg forKey:0]; 679 subMsg = nil; 680 681 subMsg = [TestAllTypes message]; 682 subMsg.repeatedInt32Array = [GPBInt32Array arrayWithValue:101]; 683 684 [msg2.mapInt32Message setObject: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 objectForKey: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 701