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// clang-format off 271 272 src.oneofInt32 = 10; 273 [dst mergeFrom:src]; 274 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofInt32); 275 XCTAssertEqual(dst.oneofInt32, 10); 276 XCTAssertEqual(dst.oneofEnum, Message2_Enum_Baz); 277 278// clang-format on 279//%PDDM-EXPAND MERGE2_TEST(Int64, 11, Int32, 100) 280// This block of code is generated, do not edit it directly. 281// clang-format off 282 283 src.oneofInt64 = 11; 284 [dst mergeFrom:src]; 285 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofInt64); 286 XCTAssertEqual(dst.oneofInt64, 11); 287 XCTAssertEqual(dst.oneofInt32, 100); 288 289// clang-format on 290//%PDDM-EXPAND MERGE2_TEST(Uint32, 12U, Int64, 101) 291// This block of code is generated, do not edit it directly. 292// clang-format off 293 294 src.oneofUint32 = 12U; 295 [dst mergeFrom:src]; 296 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofUint32); 297 XCTAssertEqual(dst.oneofUint32, 12U); 298 XCTAssertEqual(dst.oneofInt64, 101); 299 300// clang-format on 301//%PDDM-EXPAND MERGE2_TEST(Uint64, 13U, Uint32, 102U) 302// This block of code is generated, do not edit it directly. 303// clang-format off 304 305 src.oneofUint64 = 13U; 306 [dst mergeFrom:src]; 307 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofUint64); 308 XCTAssertEqual(dst.oneofUint64, 13U); 309 XCTAssertEqual(dst.oneofUint32, 102U); 310 311// clang-format on 312//%PDDM-EXPAND MERGE2_TEST(Sint32, 14, Uint64, 103U) 313// This block of code is generated, do not edit it directly. 314// clang-format off 315 316 src.oneofSint32 = 14; 317 [dst mergeFrom:src]; 318 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofSint32); 319 XCTAssertEqual(dst.oneofSint32, 14); 320 XCTAssertEqual(dst.oneofUint64, 103U); 321 322// clang-format on 323//%PDDM-EXPAND MERGE2_TEST(Sint64, 15, Sint32, 104) 324// This block of code is generated, do not edit it directly. 325// clang-format off 326 327 src.oneofSint64 = 15; 328 [dst mergeFrom:src]; 329 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofSint64); 330 XCTAssertEqual(dst.oneofSint64, 15); 331 XCTAssertEqual(dst.oneofSint32, 104); 332 333// clang-format on 334//%PDDM-EXPAND MERGE2_TEST(Fixed32, 16U, Sint64, 105) 335// This block of code is generated, do not edit it directly. 336// clang-format off 337 338 src.oneofFixed32 = 16U; 339 [dst mergeFrom:src]; 340 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofFixed32); 341 XCTAssertEqual(dst.oneofFixed32, 16U); 342 XCTAssertEqual(dst.oneofSint64, 105); 343 344// clang-format on 345//%PDDM-EXPAND MERGE2_TEST(Fixed64, 17U, Fixed32, 106U) 346// This block of code is generated, do not edit it directly. 347// clang-format off 348 349 src.oneofFixed64 = 17U; 350 [dst mergeFrom:src]; 351 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofFixed64); 352 XCTAssertEqual(dst.oneofFixed64, 17U); 353 XCTAssertEqual(dst.oneofFixed32, 106U); 354 355// clang-format on 356//%PDDM-EXPAND MERGE2_TEST(Sfixed32, 18, Fixed64, 107U) 357// This block of code is generated, do not edit it directly. 358// clang-format off 359 360 src.oneofSfixed32 = 18; 361 [dst mergeFrom:src]; 362 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofSfixed32); 363 XCTAssertEqual(dst.oneofSfixed32, 18); 364 XCTAssertEqual(dst.oneofFixed64, 107U); 365 366// clang-format on 367//%PDDM-EXPAND MERGE2_TEST(Sfixed64, 19, Sfixed32, 108) 368// This block of code is generated, do not edit it directly. 369// clang-format off 370 371 src.oneofSfixed64 = 19; 372 [dst mergeFrom:src]; 373 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofSfixed64); 374 XCTAssertEqual(dst.oneofSfixed64, 19); 375 XCTAssertEqual(dst.oneofSfixed32, 108); 376 377// clang-format on 378//%PDDM-EXPAND MERGE2_TEST(Float, 20.0f, Sfixed64, 109) 379// This block of code is generated, do not edit it directly. 380// clang-format off 381 382 src.oneofFloat = 20.0f; 383 [dst mergeFrom:src]; 384 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofFloat); 385 XCTAssertEqual(dst.oneofFloat, 20.0f); 386 XCTAssertEqual(dst.oneofSfixed64, 109); 387 388// clang-format on 389//%PDDM-EXPAND MERGE2_TEST(Double, 21.0, Float, 110.0f) 390// This block of code is generated, do not edit it directly. 391// clang-format off 392 393 src.oneofDouble = 21.0; 394 [dst mergeFrom:src]; 395 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofDouble); 396 XCTAssertEqual(dst.oneofDouble, 21.0); 397 XCTAssertEqual(dst.oneofFloat, 110.0f); 398 399// clang-format on 400//%PDDM-EXPAND MERGE2_TEST(Bool, NO, Double, 111.0) 401// This block of code is generated, do not edit it directly. 402// clang-format off 403 404 src.oneofBool = NO; 405 [dst mergeFrom:src]; 406 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofBool); 407 XCTAssertEqual(dst.oneofBool, NO); 408 XCTAssertEqual(dst.oneofDouble, 111.0); 409 410// clang-format on 411//%PDDM-EXPAND MERGE2_TEST(Enum, Message2_Enum_Bar, Bool, YES) 412// This block of code is generated, do not edit it directly. 413// clang-format off 414 415 src.oneofEnum = Message2_Enum_Bar; 416 [dst mergeFrom:src]; 417 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofEnum); 418 XCTAssertEqual(dst.oneofEnum, Message2_Enum_Bar); 419 XCTAssertEqual(dst.oneofBool, YES); 420 421// clang-format on 422//%PDDM-EXPAND-END (14 expansions) 423 424 NSString *oneofStringDefault = @"string"; 425 NSData *oneofBytesDefault = [@"data" dataUsingEncoding:NSUTF8StringEncoding]; 426 427 src.oneofString = @"foo"; 428 [dst mergeFrom:src]; 429 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofString); 430 XCTAssertEqualObjects(dst.oneofString, @"foo"); 431 XCTAssertEqual(dst.oneofEnum, Message2_Enum_Baz); 432 433 src.oneofBytes = [@"bar" dataUsingEncoding:NSUTF8StringEncoding]; 434 [dst mergeFrom:src]; 435 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofBytes); 436 XCTAssertEqualObjects(dst.oneofBytes, 437 [@"bar" dataUsingEncoding:NSUTF8StringEncoding]); 438 XCTAssertEqualObjects(dst.oneofString, oneofStringDefault); 439 440 Message2_OneofGroup *group = [Message2_OneofGroup message]; 441 group.a = 666; 442 src.oneofGroup = group; 443 [dst mergeFrom:src]; 444 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofGroup); 445 Message2_OneofGroup *mergedGroup = [[dst.oneofGroup retain] autorelease]; 446 XCTAssertNotNil(mergedGroup); 447 XCTAssertNotEqual(mergedGroup, group); // Pointer comparison. 448 XCTAssertEqualObjects(mergedGroup, group); 449 XCTAssertEqualObjects(dst.oneofBytes, oneofBytesDefault); 450 451 Message2 *subMessage = [Message2 message]; 452 subMessage.optionalInt32 = 777; 453 src.oneofMessage = subMessage; 454 [dst mergeFrom:src]; 455 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofMessage); 456 Message2 *mergedSubMessage = [[dst.oneofMessage retain] autorelease]; 457 XCTAssertNotNil(mergedSubMessage); 458 XCTAssertNotEqual(mergedSubMessage, subMessage); // Pointer comparison. 459 XCTAssertEqualObjects(mergedSubMessage, subMessage); 460 XCTAssertNotNil(dst.oneofGroup); 461 XCTAssertNotEqual(dst.oneofGroup, mergedGroup); // Pointer comparison. 462 463 // Back to something else to make sure message clears out ok. 464 465 src.oneofInt32 = 10; 466 [dst mergeFrom:src]; 467 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofInt32); 468 XCTAssertNotNil(dst.oneofMessage); 469 XCTAssertNotEqual(dst.oneofMessage, 470 mergedSubMessage); // Pointer comparison. 471 472 // 473 // Test merging in to message/group when they already had something. 474 // 475 476 src.oneofGroup = group; 477 mergedGroup = [Message2_OneofGroup message]; 478 mergedGroup.b = 888; 479 dst.oneofGroup = mergedGroup; 480 [dst mergeFrom:src]; 481 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofGroup); 482 // Shouldn't have been a new object. 483 XCTAssertEqual(dst.oneofGroup, mergedGroup); // Pointer comparison. 484 XCTAssertEqual(dst.oneofGroup.a, 666); // Pointer comparison. 485 XCTAssertEqual(dst.oneofGroup.b, 888); // Pointer comparison. 486 487 src.oneofMessage = subMessage; 488 mergedSubMessage = [Message2 message]; 489 mergedSubMessage.optionalInt64 = 999; 490 dst.oneofMessage = mergedSubMessage; 491 [dst mergeFrom:src]; 492 XCTAssertEqual(dst.oOneOfCase, Message2_O_OneOfCase_OneofMessage); 493 // Shouldn't have been a new object. 494 XCTAssertEqual(dst.oneofMessage, mergedSubMessage); // Pointer comparison. 495 XCTAssertEqual(dst.oneofMessage.optionalInt32, 777); // Pointer comparison. 496 XCTAssertEqual(dst.oneofMessage.optionalInt64, 999); // Pointer comparison. 497} 498 499- (void)testProto3MergeOneof { 500 Message3 *src = [Message3 message]; 501 Message3 *dst = [Message3 message]; 502 503 // 504 // Make sure whatever is in dst gets cleared out be merging in something else. 505 // 506 507 dst.oneofEnum = Message3_Enum_Bar; 508 509//%PDDM-DEFINE MERGE3_TEST(SET_NAME, SET_VALUE, CLEARED_NAME, CLEARED_DEFAULT) 510//% src.oneof##SET_NAME = SET_VALUE; 511//% [dst mergeFrom:src]; 512//% XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_Oneof##SET_NAME); 513//% XCTAssertEqual(dst.oneof##SET_NAME, SET_VALUE); 514//% XCTAssertEqual(dst.oneof##CLEARED_NAME, CLEARED_DEFAULT); 515//% 516//%PDDM-EXPAND MERGE3_TEST(Int32, 10, Enum, Message3_Enum_Foo) 517// This block of code is generated, do not edit it directly. 518// clang-format off 519 520 src.oneofInt32 = 10; 521 [dst mergeFrom:src]; 522 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofInt32); 523 XCTAssertEqual(dst.oneofInt32, 10); 524 XCTAssertEqual(dst.oneofEnum, Message3_Enum_Foo); 525 526// clang-format on 527//%PDDM-EXPAND MERGE3_TEST(Int64, 11, Int32, 0) 528// This block of code is generated, do not edit it directly. 529// clang-format off 530 531 src.oneofInt64 = 11; 532 [dst mergeFrom:src]; 533 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofInt64); 534 XCTAssertEqual(dst.oneofInt64, 11); 535 XCTAssertEqual(dst.oneofInt32, 0); 536 537// clang-format on 538//%PDDM-EXPAND MERGE3_TEST(Uint32, 12U, Int64, 0) 539// This block of code is generated, do not edit it directly. 540// clang-format off 541 542 src.oneofUint32 = 12U; 543 [dst mergeFrom:src]; 544 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofUint32); 545 XCTAssertEqual(dst.oneofUint32, 12U); 546 XCTAssertEqual(dst.oneofInt64, 0); 547 548// clang-format on 549//%PDDM-EXPAND MERGE3_TEST(Uint64, 13U, Uint32, 0U) 550// This block of code is generated, do not edit it directly. 551// clang-format off 552 553 src.oneofUint64 = 13U; 554 [dst mergeFrom:src]; 555 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofUint64); 556 XCTAssertEqual(dst.oneofUint64, 13U); 557 XCTAssertEqual(dst.oneofUint32, 0U); 558 559// clang-format on 560//%PDDM-EXPAND MERGE3_TEST(Sint32, 14, Uint64, 0U) 561// This block of code is generated, do not edit it directly. 562// clang-format off 563 564 src.oneofSint32 = 14; 565 [dst mergeFrom:src]; 566 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofSint32); 567 XCTAssertEqual(dst.oneofSint32, 14); 568 XCTAssertEqual(dst.oneofUint64, 0U); 569 570// clang-format on 571//%PDDM-EXPAND MERGE3_TEST(Sint64, 15, Sint32, 0) 572// This block of code is generated, do not edit it directly. 573// clang-format off 574 575 src.oneofSint64 = 15; 576 [dst mergeFrom:src]; 577 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofSint64); 578 XCTAssertEqual(dst.oneofSint64, 15); 579 XCTAssertEqual(dst.oneofSint32, 0); 580 581// clang-format on 582//%PDDM-EXPAND MERGE3_TEST(Fixed32, 16U, Sint64, 0) 583// This block of code is generated, do not edit it directly. 584// clang-format off 585 586 src.oneofFixed32 = 16U; 587 [dst mergeFrom:src]; 588 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofFixed32); 589 XCTAssertEqual(dst.oneofFixed32, 16U); 590 XCTAssertEqual(dst.oneofSint64, 0); 591 592// clang-format on 593//%PDDM-EXPAND MERGE3_TEST(Fixed64, 17U, Fixed32, 0U) 594// This block of code is generated, do not edit it directly. 595// clang-format off 596 597 src.oneofFixed64 = 17U; 598 [dst mergeFrom:src]; 599 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofFixed64); 600 XCTAssertEqual(dst.oneofFixed64, 17U); 601 XCTAssertEqual(dst.oneofFixed32, 0U); 602 603// clang-format on 604//%PDDM-EXPAND MERGE3_TEST(Sfixed32, 18, Fixed64, 0U) 605// This block of code is generated, do not edit it directly. 606// clang-format off 607 608 src.oneofSfixed32 = 18; 609 [dst mergeFrom:src]; 610 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofSfixed32); 611 XCTAssertEqual(dst.oneofSfixed32, 18); 612 XCTAssertEqual(dst.oneofFixed64, 0U); 613 614// clang-format on 615//%PDDM-EXPAND MERGE3_TEST(Sfixed64, 19, Sfixed32, 0) 616// This block of code is generated, do not edit it directly. 617// clang-format off 618 619 src.oneofSfixed64 = 19; 620 [dst mergeFrom:src]; 621 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofSfixed64); 622 XCTAssertEqual(dst.oneofSfixed64, 19); 623 XCTAssertEqual(dst.oneofSfixed32, 0); 624 625// clang-format on 626//%PDDM-EXPAND MERGE3_TEST(Float, 20.0f, Sfixed64, 0) 627// This block of code is generated, do not edit it directly. 628// clang-format off 629 630 src.oneofFloat = 20.0f; 631 [dst mergeFrom:src]; 632 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofFloat); 633 XCTAssertEqual(dst.oneofFloat, 20.0f); 634 XCTAssertEqual(dst.oneofSfixed64, 0); 635 636// clang-format on 637//%PDDM-EXPAND MERGE3_TEST(Double, 21.0, Float, 0.0f) 638// This block of code is generated, do not edit it directly. 639// clang-format off 640 641 src.oneofDouble = 21.0; 642 [dst mergeFrom:src]; 643 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofDouble); 644 XCTAssertEqual(dst.oneofDouble, 21.0); 645 XCTAssertEqual(dst.oneofFloat, 0.0f); 646 647// clang-format on 648//%PDDM-EXPAND MERGE3_TEST(Bool, YES, Double, 0.0) 649// This block of code is generated, do not edit it directly. 650// clang-format off 651 652 src.oneofBool = YES; 653 [dst mergeFrom:src]; 654 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofBool); 655 XCTAssertEqual(dst.oneofBool, YES); 656 XCTAssertEqual(dst.oneofDouble, 0.0); 657 658// clang-format on 659//%PDDM-EXPAND MERGE3_TEST(Enum, Message3_Enum_Bar, Bool, NO) 660// This block of code is generated, do not edit it directly. 661// clang-format off 662 663 src.oneofEnum = Message3_Enum_Bar; 664 [dst mergeFrom:src]; 665 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofEnum); 666 XCTAssertEqual(dst.oneofEnum, Message3_Enum_Bar); 667 XCTAssertEqual(dst.oneofBool, NO); 668 669// clang-format on 670//%PDDM-EXPAND-END (14 expansions) 671 672 NSString *oneofStringDefault = @""; 673 NSData *oneofBytesDefault = [NSData data]; 674 675 src.oneofString = @"foo"; 676 [dst mergeFrom:src]; 677 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofString); 678 XCTAssertEqualObjects(dst.oneofString, @"foo"); 679 XCTAssertEqual(dst.oneofEnum, Message3_Enum_Foo); 680 681 src.oneofBytes = [@"bar" dataUsingEncoding:NSUTF8StringEncoding]; 682 [dst mergeFrom:src]; 683 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofBytes); 684 XCTAssertEqualObjects(dst.oneofBytes, 685 [@"bar" dataUsingEncoding:NSUTF8StringEncoding]); 686 XCTAssertEqualObjects(dst.oneofString, oneofStringDefault); 687 688 689 Message3 *subMessage = [Message3 message]; 690 subMessage.optionalInt32 = 777; 691 src.oneofMessage = subMessage; 692 [dst mergeFrom:src]; 693 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofMessage); 694 Message3 *mergedSubMessage = [[dst.oneofMessage retain] autorelease]; 695 XCTAssertNotNil(mergedSubMessage); 696 XCTAssertNotEqual(mergedSubMessage, subMessage); // Pointer comparison. 697 XCTAssertEqualObjects(mergedSubMessage, subMessage); 698 XCTAssertEqualObjects(dst.oneofBytes, oneofBytesDefault); 699 700 // Back to something else to make sure message clears out ok. 701 702 src.oneofInt32 = 10; 703 [dst mergeFrom:src]; 704 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofInt32); 705 XCTAssertNotNil(dst.oneofMessage); 706 XCTAssertNotEqual(dst.oneofMessage, 707 mergedSubMessage); // Pointer comparison. 708 709 // 710 // Test merging in to message when they already had something. 711 // 712 713 src.oneofMessage = subMessage; 714 mergedSubMessage = [Message3 message]; 715 mergedSubMessage.optionalInt64 = 999; 716 dst.oneofMessage = mergedSubMessage; 717 [dst mergeFrom:src]; 718 XCTAssertEqual(dst.oOneOfCase, Message3_O_OneOfCase_OneofMessage); 719 // Shouldn't have been a new object. 720 XCTAssertEqual(dst.oneofMessage, mergedSubMessage); // Pointer comparison. 721 XCTAssertEqual(dst.oneofMessage.optionalInt32, 777); // Pointer comparison. 722 XCTAssertEqual(dst.oneofMessage.optionalInt64, 999); // Pointer comparison. 723} 724 725#pragma mark - Subset from from map_tests.cc 726 727// TEST(GeneratedMapFieldTest, CopyFromMessageMap) 728- (void)testMap_CopyFromMessageMap { 729 TestMessageMap *msg1 = [[TestMessageMap alloc] init]; 730 TestMessageMap *msg2 = [[TestMessageMap alloc] init]; 731 732 TestAllTypes *subMsg = [TestAllTypes message]; 733 subMsg.repeatedInt32Array = [GPBInt32Array arrayWithValue:100]; 734 [msg1.mapInt32Message setObject:subMsg forKey:0]; 735 subMsg = nil; 736 737 subMsg = [TestAllTypes message]; 738 subMsg.repeatedInt32Array = [GPBInt32Array arrayWithValue:101]; 739 740 [msg2.mapInt32Message setObject:subMsg forKey:0]; 741 subMsg = nil; 742 743 [msg1 mergeFrom:msg2]; 744 745 // Checks repeated field is overwritten. 746 XCTAssertEqual(msg1.mapInt32Message.count, 1U); 747 subMsg = [msg1.mapInt32Message objectForKey:0]; 748 XCTAssertNotNil(subMsg); 749 XCTAssertEqual(subMsg.repeatedInt32Array.count, 1U); 750 XCTAssertEqual([subMsg.repeatedInt32Array valueAtIndex:0], 101); 751 752 [msg2 release]; 753 [msg1 release]; 754} 755 756@end 757