OLD | NEW |
(Empty) | |
| 1 // Protocol Buffers - Google's data interchange format |
| 2 // Copyright 2008 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 "GPBArray_PackagePrivate.h" |
| 36 #import "GPBDescriptor.h" |
| 37 #import "GPBDictionary_PackagePrivate.h" |
| 38 #import "GPBMessage_PackagePrivate.h" |
| 39 #import "GPBUnknownField_PackagePrivate.h" |
| 40 #import "GPBUnknownFieldSet_PackagePrivate.h" |
| 41 #import "google/protobuf/Unittest.pbobjc.h" |
| 42 #import "google/protobuf/UnittestObjc.pbobjc.h" |
| 43 |
| 44 @interface MessageTests : GPBTestCase |
| 45 @end |
| 46 |
| 47 @implementation MessageTests |
| 48 |
| 49 // TODO(thomasvl): this should get split into a few files of logic junks, it is |
| 50 // a jumble of things at the moment (and the testutils have a bunch of the real |
| 51 // assertions). |
| 52 |
| 53 - (TestAllTypes *)mergeSource { |
| 54 TestAllTypes *message = [TestAllTypes message]; |
| 55 [message setOptionalInt32:1]; |
| 56 [message setOptionalString:@"foo"]; |
| 57 [message setOptionalForeignMessage:[ForeignMessage message]]; |
| 58 message.repeatedStringArray = [NSMutableArray array]; |
| 59 [message.repeatedStringArray addObject:@"bar"]; |
| 60 return message; |
| 61 } |
| 62 |
| 63 - (TestAllTypes *)mergeDestination { |
| 64 TestAllTypes *message = [TestAllTypes message]; |
| 65 [message setOptionalInt64:2]; |
| 66 [message setOptionalString:@"baz"]; |
| 67 ForeignMessage *foreignMessage = [ForeignMessage message]; |
| 68 [foreignMessage setC:3]; |
| 69 [message setOptionalForeignMessage:foreignMessage]; |
| 70 message.repeatedStringArray = [NSMutableArray array]; |
| 71 [message.repeatedStringArray addObject:@"qux"]; |
| 72 return message; |
| 73 } |
| 74 |
| 75 - (TestAllTypes *)mergeDestinationWithoutForeignMessageIvar { |
| 76 TestAllTypes *message = [TestAllTypes message]; |
| 77 [message setOptionalInt64:2]; |
| 78 [message setOptionalString:@"baz"]; |
| 79 message.repeatedStringArray = [NSMutableArray array]; |
| 80 [message.repeatedStringArray addObject:@"qux"]; |
| 81 return message; |
| 82 } |
| 83 |
| 84 - (TestAllTypes *)mergeResult { |
| 85 TestAllTypes *message = [TestAllTypes message]; |
| 86 [message setOptionalInt32:1]; |
| 87 [message setOptionalInt64:2]; |
| 88 [message setOptionalString:@"foo"]; |
| 89 ForeignMessage *foreignMessage = [ForeignMessage message]; |
| 90 [foreignMessage setC:3]; |
| 91 [message setOptionalForeignMessage:foreignMessage]; |
| 92 message.repeatedStringArray = [NSMutableArray array]; |
| 93 [message.repeatedStringArray addObject:@"qux"]; |
| 94 [message.repeatedStringArray addObject:@"bar"]; |
| 95 return message; |
| 96 } |
| 97 |
| 98 - (TestAllTypes *)mergeResultForDestinationWithoutForeignMessageIvar { |
| 99 TestAllTypes *message = [TestAllTypes message]; |
| 100 [message setOptionalInt32:1]; |
| 101 [message setOptionalInt64:2]; |
| 102 [message setOptionalString:@"foo"]; |
| 103 ForeignMessage *foreignMessage = [ForeignMessage message]; |
| 104 [message setOptionalForeignMessage:foreignMessage]; |
| 105 message.repeatedStringArray = [NSMutableArray array]; |
| 106 [message.repeatedStringArray addObject:@"qux"]; |
| 107 [message.repeatedStringArray addObject:@"bar"]; |
| 108 return message; |
| 109 } |
| 110 |
| 111 - (TestAllExtensions *)mergeExtensionsDestination { |
| 112 TestAllExtensions *message = [TestAllExtensions message]; |
| 113 [message setExtension:[UnittestRoot optionalInt32Extension] value:@5]; |
| 114 [message setExtension:[UnittestRoot optionalStringExtension] value:@"foo"]; |
| 115 ForeignMessage *foreignMessage = [ForeignMessage message]; |
| 116 foreignMessage.c = 4; |
| 117 [message setExtension:[UnittestRoot optionalForeignMessageExtension] |
| 118 value:foreignMessage]; |
| 119 TestAllTypes_NestedMessage *nestedMessage = |
| 120 [TestAllTypes_NestedMessage message]; |
| 121 [message setExtension:[UnittestRoot optionalNestedMessageExtension] |
| 122 value:nestedMessage]; |
| 123 return message; |
| 124 } |
| 125 |
| 126 - (TestAllExtensions *)mergeExtensionsSource { |
| 127 TestAllExtensions *message = [TestAllExtensions message]; |
| 128 [message setExtension:[UnittestRoot optionalInt64Extension] value:@6]; |
| 129 [message setExtension:[UnittestRoot optionalStringExtension] value:@"bar"]; |
| 130 ForeignMessage *foreignMessage = [ForeignMessage message]; |
| 131 [message setExtension:[UnittestRoot optionalForeignMessageExtension] |
| 132 value:foreignMessage]; |
| 133 TestAllTypes_NestedMessage *nestedMessage = |
| 134 [TestAllTypes_NestedMessage message]; |
| 135 nestedMessage.bb = 7; |
| 136 [message setExtension:[UnittestRoot optionalNestedMessageExtension] |
| 137 value:nestedMessage]; |
| 138 return message; |
| 139 } |
| 140 |
| 141 - (TestAllExtensions *)mergeExtensionsResult { |
| 142 TestAllExtensions *message = [TestAllExtensions message]; |
| 143 [message setExtension:[UnittestRoot optionalInt32Extension] value:@5]; |
| 144 [message setExtension:[UnittestRoot optionalInt64Extension] value:@6]; |
| 145 [message setExtension:[UnittestRoot optionalStringExtension] value:@"bar"]; |
| 146 ForeignMessage *foreignMessage = [ForeignMessage message]; |
| 147 foreignMessage.c = 4; |
| 148 [message setExtension:[UnittestRoot optionalForeignMessageExtension] |
| 149 value:foreignMessage]; |
| 150 TestAllTypes_NestedMessage *nestedMessage = |
| 151 [TestAllTypes_NestedMessage message]; |
| 152 nestedMessage.bb = 7; |
| 153 [message setExtension:[UnittestRoot optionalNestedMessageExtension] |
| 154 value:nestedMessage]; |
| 155 return message; |
| 156 } |
| 157 |
| 158 - (void)testMergeFrom { |
| 159 TestAllTypes *result = [[self.mergeDestination copy] autorelease]; |
| 160 [result mergeFrom:self.mergeSource]; |
| 161 NSData *resultData = [result data]; |
| 162 NSData *mergeResultData = [self.mergeResult data]; |
| 163 XCTAssertEqualObjects(resultData, mergeResultData); |
| 164 XCTAssertEqualObjects(result, self.mergeResult); |
| 165 |
| 166 // Test when destination does not have an Ivar (type is an object) but source |
| 167 // has such Ivar. |
| 168 // The result must has the Ivar which is same as the one in source. |
| 169 result = [[self.mergeDestinationWithoutForeignMessageIvar copy] autorelease]; |
| 170 [result mergeFrom:self.mergeSource]; |
| 171 resultData = [result data]; |
| 172 mergeResultData = |
| 173 [self.mergeResultForDestinationWithoutForeignMessageIvar data]; |
| 174 XCTAssertEqualObjects(resultData, mergeResultData); |
| 175 XCTAssertEqualObjects( |
| 176 result, self.mergeResultForDestinationWithoutForeignMessageIvar); |
| 177 |
| 178 // Test when destination is empty. |
| 179 // The result must is same as the source. |
| 180 result = [TestAllTypes message]; |
| 181 [result mergeFrom:self.mergeSource]; |
| 182 resultData = [result data]; |
| 183 mergeResultData = [self.mergeSource data]; |
| 184 XCTAssertEqualObjects(resultData, mergeResultData); |
| 185 XCTAssertEqualObjects(result, self.mergeSource); |
| 186 } |
| 187 |
| 188 - (void)testMergeFromWithExtensions { |
| 189 TestAllExtensions *result = [self mergeExtensionsDestination]; |
| 190 [result mergeFrom:[self mergeExtensionsSource]]; |
| 191 NSData *resultData = [result data]; |
| 192 NSData *mergeResultData = [[self mergeExtensionsResult] data]; |
| 193 XCTAssertEqualObjects(resultData, mergeResultData); |
| 194 XCTAssertEqualObjects(result, [self mergeExtensionsResult]); |
| 195 |
| 196 // Test merging from data. |
| 197 result = [self mergeExtensionsDestination]; |
| 198 NSData *data = [[self mergeExtensionsSource] data]; |
| 199 XCTAssertNotNil(data); |
| 200 [result mergeFromData:data |
| 201 extensionRegistry:[UnittestRoot extensionRegistry]]; |
| 202 resultData = [result data]; |
| 203 XCTAssertEqualObjects(resultData, mergeResultData); |
| 204 XCTAssertEqualObjects(result, [self mergeExtensionsResult]); |
| 205 } |
| 206 |
| 207 - (void)testIsEquals { |
| 208 TestAllTypes *result = [[self.mergeDestination copy] autorelease]; |
| 209 [result mergeFrom:self.mergeSource]; |
| 210 XCTAssertEqualObjects(result.data, self.mergeResult.data); |
| 211 XCTAssertEqualObjects(result, self.mergeResult); |
| 212 TestAllTypes *result2 = [[self.mergeDestination copy] autorelease]; |
| 213 XCTAssertNotEqualObjects(result2.data, self.mergeResult.data); |
| 214 XCTAssertNotEqualObjects(result2, self.mergeResult); |
| 215 } |
| 216 |
| 217 // ================================================================= |
| 218 // Required-field-related tests. |
| 219 |
| 220 - (TestRequired *)testRequiredInitialized { |
| 221 TestRequired *message = [TestRequired message]; |
| 222 [message setA:1]; |
| 223 [message setB:2]; |
| 224 [message setC:3]; |
| 225 return message; |
| 226 } |
| 227 |
| 228 - (void)testRequired { |
| 229 TestRequired *message = [TestRequired message]; |
| 230 |
| 231 XCTAssertFalse(message.initialized); |
| 232 [message setA:1]; |
| 233 XCTAssertFalse(message.initialized); |
| 234 [message setB:1]; |
| 235 XCTAssertFalse(message.initialized); |
| 236 [message setC:1]; |
| 237 XCTAssertTrue(message.initialized); |
| 238 } |
| 239 |
| 240 - (void)testRequiredForeign { |
| 241 TestRequiredForeign *message = [TestRequiredForeign message]; |
| 242 |
| 243 XCTAssertTrue(message.initialized); |
| 244 |
| 245 [message setOptionalMessage:[TestRequired message]]; |
| 246 XCTAssertFalse(message.initialized); |
| 247 |
| 248 [message setOptionalMessage:self.testRequiredInitialized]; |
| 249 XCTAssertTrue(message.initialized); |
| 250 |
| 251 message.repeatedMessageArray = [NSMutableArray array]; |
| 252 [message.repeatedMessageArray addObject:[TestRequired message]]; |
| 253 XCTAssertFalse(message.initialized); |
| 254 |
| 255 [message.repeatedMessageArray removeAllObjects]; |
| 256 [message.repeatedMessageArray addObject:self.testRequiredInitialized]; |
| 257 XCTAssertTrue(message.initialized); |
| 258 } |
| 259 |
| 260 - (void)testRequiredExtension { |
| 261 TestAllExtensions *message = [TestAllExtensions message]; |
| 262 |
| 263 XCTAssertTrue(message.initialized); |
| 264 |
| 265 [message setExtension:[TestRequired single] value:[TestRequired message]]; |
| 266 XCTAssertFalse(message.initialized); |
| 267 |
| 268 [message setExtension:[TestRequired single] |
| 269 value:self.testRequiredInitialized]; |
| 270 XCTAssertTrue(message.initialized); |
| 271 |
| 272 [message addExtension:[TestRequired multi] value:[TestRequired message]]; |
| 273 XCTAssertFalse(message.initialized); |
| 274 |
| 275 [message setExtension:[TestRequired multi] |
| 276 index:0 |
| 277 value:self.testRequiredInitialized]; |
| 278 XCTAssertTrue(message.initialized); |
| 279 } |
| 280 |
| 281 - (void)testDataFromUninitialized { |
| 282 TestRequired *message = [TestRequired message]; |
| 283 NSData *data = [message data]; |
| 284 // In DEBUG, the data generation will fail, but in non DEBUG, it passes |
| 285 // because the check isn't done (for speed). |
| 286 #ifdef DEBUG |
| 287 XCTAssertNil(data); |
| 288 #else |
| 289 XCTAssertNotNil(data); |
| 290 XCTAssertFalse(message.initialized); |
| 291 #endif // DEBUG |
| 292 } |
| 293 |
| 294 - (void)testInitialized { |
| 295 // We're mostly testing that no exception is thrown. |
| 296 TestRequired *message = [TestRequired message]; |
| 297 XCTAssertFalse(message.initialized); |
| 298 } |
| 299 |
| 300 - (void)testDataFromNestedUninitialized { |
| 301 TestRequiredForeign *message = [TestRequiredForeign message]; |
| 302 [message setOptionalMessage:[TestRequired message]]; |
| 303 message.repeatedMessageArray = [NSMutableArray array]; |
| 304 [message.repeatedMessageArray addObject:[TestRequired message]]; |
| 305 [message.repeatedMessageArray addObject:[TestRequired message]]; |
| 306 NSData *data = [message data]; |
| 307 // In DEBUG, the data generation will fail, but in non DEBUG, it passes |
| 308 // because the check isn't done (for speed). |
| 309 #ifdef DEBUG |
| 310 XCTAssertNil(data); |
| 311 #else |
| 312 XCTAssertNotNil(data); |
| 313 XCTAssertFalse(message.initialized); |
| 314 #endif // DEBUG |
| 315 } |
| 316 |
| 317 - (void)testNestedInitialized { |
| 318 // We're mostly testing that no exception is thrown. |
| 319 |
| 320 TestRequiredForeign *message = [TestRequiredForeign message]; |
| 321 [message setOptionalMessage:[TestRequired message]]; |
| 322 message.repeatedMessageArray = [NSMutableArray array]; |
| 323 [message.repeatedMessageArray addObject:[TestRequired message]]; |
| 324 [message.repeatedMessageArray addObject:[TestRequired message]]; |
| 325 |
| 326 XCTAssertFalse(message.initialized); |
| 327 } |
| 328 |
| 329 - (void)testParseUninitialized { |
| 330 NSError *error = nil; |
| 331 TestRequired *msg = |
| 332 [TestRequired parseFromData:GPBEmptyNSData() error:&error]; |
| 333 // In DEBUG, the parse will fail, but in non DEBUG, it passes because |
| 334 // the check isn't done (for speed). |
| 335 #ifdef DEBUG |
| 336 XCTAssertNil(msg); |
| 337 XCTAssertNotNil(error); |
| 338 XCTAssertEqualObjects(error.domain, GPBMessageErrorDomain); |
| 339 XCTAssertEqual(error.code, GPBMessageErrorCodeMissingRequiredField); |
| 340 #else |
| 341 XCTAssertNotNil(msg); |
| 342 XCTAssertNil(error); |
| 343 XCTAssertFalse(msg.initialized); |
| 344 #endif // DEBUG |
| 345 } |
| 346 |
| 347 - (void)testCoding { |
| 348 NSData *data = |
| 349 [NSKeyedArchiver archivedDataWithRootObject:[self mergeResult]]; |
| 350 id unarchivedObject = [NSKeyedUnarchiver unarchiveObjectWithData:data]; |
| 351 |
| 352 XCTAssertEqualObjects(unarchivedObject, [self mergeResult]); |
| 353 |
| 354 // Intentionally doing a pointer comparison. |
| 355 XCTAssertNotEqual(unarchivedObject, [self mergeResult]); |
| 356 } |
| 357 |
| 358 - (void)testObjectReset { |
| 359 // Tests a failure where clearing out defaults values caused an over release. |
| 360 TestAllTypes *message = [TestAllTypes message]; |
| 361 message.hasOptionalNestedMessage = NO; |
| 362 [message setOptionalNestedMessage:[TestAllTypes_NestedMessage message]]; |
| 363 message.hasOptionalNestedMessage = NO; |
| 364 [message setOptionalNestedMessage:[TestAllTypes_NestedMessage message]]; |
| 365 [message setOptionalNestedMessage:nil]; |
| 366 message.hasOptionalNestedMessage = NO; |
| 367 } |
| 368 |
| 369 - (void)testSettingHasToYes { |
| 370 TestAllTypes *message = [TestAllTypes message]; |
| 371 XCTAssertThrows([message setHasOptionalNestedMessage:YES]); |
| 372 } |
| 373 |
| 374 - (void)testRoot { |
| 375 XCTAssertNotNil([UnittestRoot extensionRegistry]); |
| 376 } |
| 377 |
| 378 - (void)testGPBMessageSize { |
| 379 // See the note in GPBMessage_PackagePrivate.h about why we want to keep the |
| 380 // base instance size pointer size aligned. |
| 381 size_t messageSize = class_getInstanceSize([GPBMessage class]); |
| 382 XCTAssertEqual((messageSize % sizeof(void *)), (size_t)0, |
| 383 @"Base size isn't pointer size aligned"); |
| 384 |
| 385 // Since we add storage ourselves (see +allocWithZone: in GPBMessage), confirm |
| 386 // that the size of some generated classes is still the same as the base for |
| 387 // that logic to work as desired. |
| 388 size_t testMessageSize = class_getInstanceSize([TestAllTypes class]); |
| 389 XCTAssertEqual(testMessageSize, messageSize); |
| 390 } |
| 391 |
| 392 - (void)testInit { |
| 393 TestAllTypes *message = [TestAllTypes message]; |
| 394 [self assertClear:message]; |
| 395 } |
| 396 |
| 397 - (void)testAccessors { |
| 398 TestAllTypes *message = [TestAllTypes message]; |
| 399 [self setAllFields:message repeatedCount:kGPBDefaultRepeatCount]; |
| 400 [self assertAllFieldsSet:message repeatedCount:kGPBDefaultRepeatCount]; |
| 401 } |
| 402 |
| 403 - (void)testKVC_ValueForKey { |
| 404 TestAllTypes *message = [TestAllTypes message]; |
| 405 [self setAllFields:message repeatedCount:kGPBDefaultRepeatCount]; |
| 406 [self assertAllFieldsKVCMatch:message]; |
| 407 } |
| 408 |
| 409 - (void)testKVC_SetValue_ForKey { |
| 410 TestAllTypes *message = [TestAllTypes message]; |
| 411 [self setAllFieldsViaKVC:message repeatedCount:kGPBDefaultRepeatCount]; |
| 412 [self assertAllFieldsKVCMatch:message]; |
| 413 [self assertAllFieldsSet:message repeatedCount:kGPBDefaultRepeatCount]; |
| 414 [self assertAllFieldsKVCMatch:message]; |
| 415 } |
| 416 |
| 417 - (void)testDescription { |
| 418 // No real test, just exercise code |
| 419 TestAllTypes *message = [TestAllTypes message]; |
| 420 [self setAllFields:message repeatedCount:kGPBDefaultRepeatCount]; |
| 421 |
| 422 GPBUnknownFieldSet *unknownFields = |
| 423 [[[GPBUnknownFieldSet alloc] init] autorelease]; |
| 424 GPBUnknownField *field = |
| 425 [[[GPBUnknownField alloc] initWithNumber:2] autorelease]; |
| 426 [field addVarint:2]; |
| 427 [unknownFields addField:field]; |
| 428 field = [[[GPBUnknownField alloc] initWithNumber:3] autorelease]; |
| 429 [field addVarint:4]; |
| 430 [unknownFields addField:field]; |
| 431 |
| 432 [message setUnknownFields:unknownFields]; |
| 433 |
| 434 NSString *description = [message description]; |
| 435 XCTAssertGreaterThan([description length], 0U); |
| 436 |
| 437 GPBMessage *message2 = [TestAllExtensions message]; |
| 438 [message2 setExtension:[UnittestRoot optionalInt32Extension] value:@1]; |
| 439 |
| 440 [message2 addExtension:[UnittestRoot repeatedInt32Extension] value:@2]; |
| 441 |
| 442 description = [message2 description]; |
| 443 XCTAssertGreaterThan([description length], 0U); |
| 444 } |
| 445 |
| 446 - (void)testSetter { |
| 447 // Test to make sure that if we set a value that has a default value |
| 448 // with the default, that the has is set, and the value gets put into the |
| 449 // message correctly. |
| 450 TestAllTypes *message = [TestAllTypes message]; |
| 451 GPBDescriptor *descriptor = [[message class] descriptor]; |
| 452 XCTAssertNotNil(descriptor); |
| 453 GPBFieldDescriptor *fieldDescriptor = |
| 454 [descriptor fieldWithName:@"defaultInt32"]; |
| 455 XCTAssertNotNil(fieldDescriptor); |
| 456 GPBGenericValue defaultValue = [fieldDescriptor defaultValue]; |
| 457 [message setDefaultInt32:defaultValue.valueInt32]; |
| 458 XCTAssertTrue(message.hasDefaultInt32); |
| 459 XCTAssertEqual(message.defaultInt32, defaultValue.valueInt32); |
| 460 |
| 461 // Do the same thing with an object type. |
| 462 message = [TestAllTypes message]; |
| 463 fieldDescriptor = [descriptor fieldWithName:@"defaultString"]; |
| 464 XCTAssertNotNil(fieldDescriptor); |
| 465 defaultValue = [fieldDescriptor defaultValue]; |
| 466 [message setDefaultString:defaultValue.valueString]; |
| 467 XCTAssertTrue(message.hasDefaultString); |
| 468 XCTAssertEqualObjects(message.defaultString, defaultValue.valueString); |
| 469 |
| 470 // Test default string type. |
| 471 message = [TestAllTypes message]; |
| 472 XCTAssertEqualObjects(message.defaultString, @"hello"); |
| 473 XCTAssertFalse(message.hasDefaultString); |
| 474 fieldDescriptor = [descriptor fieldWithName:@"defaultString"]; |
| 475 XCTAssertNotNil(fieldDescriptor); |
| 476 defaultValue = [fieldDescriptor defaultValue]; |
| 477 [message setDefaultString:defaultValue.valueString]; |
| 478 XCTAssertEqualObjects(message.defaultString, @"hello"); |
| 479 XCTAssertTrue(message.hasDefaultString); |
| 480 [message setDefaultString:nil]; |
| 481 XCTAssertEqualObjects(message.defaultString, @"hello"); |
| 482 XCTAssertFalse(message.hasDefaultString); |
| 483 message.hasDefaultString = NO; |
| 484 XCTAssertFalse(message.hasDefaultString); |
| 485 XCTAssertEqualObjects(message.defaultString, @"hello"); |
| 486 |
| 487 // Test default bytes type. |
| 488 NSData *defaultBytes = [@"world" dataUsingEncoding:NSUTF8StringEncoding]; |
| 489 XCTAssertEqualObjects(message.defaultBytes, defaultBytes); |
| 490 XCTAssertFalse(message.hasDefaultString); |
| 491 fieldDescriptor = [descriptor fieldWithName:@"defaultBytes"]; |
| 492 XCTAssertNotNil(fieldDescriptor); |
| 493 defaultValue = [fieldDescriptor defaultValue]; |
| 494 [message setDefaultBytes:defaultValue.valueData]; |
| 495 XCTAssertEqualObjects(message.defaultBytes, defaultBytes); |
| 496 XCTAssertTrue(message.hasDefaultBytes); |
| 497 [message setDefaultBytes:nil]; |
| 498 XCTAssertEqualObjects(message.defaultBytes, defaultBytes); |
| 499 XCTAssertFalse(message.hasDefaultBytes); |
| 500 message.hasDefaultBytes = NO; |
| 501 XCTAssertFalse(message.hasDefaultBytes); |
| 502 XCTAssertEqualObjects(message.defaultBytes, defaultBytes); |
| 503 |
| 504 // Test optional string. |
| 505 XCTAssertFalse(message.hasOptionalString); |
| 506 XCTAssertEqualObjects(message.optionalString, @""); |
| 507 XCTAssertFalse(message.hasOptionalString); |
| 508 message.optionalString = nil; |
| 509 XCTAssertFalse(message.hasOptionalString); |
| 510 XCTAssertEqualObjects(message.optionalString, @""); |
| 511 NSString *string = @"string"; |
| 512 message.optionalString = string; |
| 513 XCTAssertEqualObjects(message.optionalString, string); |
| 514 XCTAssertTrue(message.hasOptionalString); |
| 515 message.optionalString = nil; |
| 516 XCTAssertFalse(message.hasOptionalString); |
| 517 XCTAssertEqualObjects(message.optionalString, @""); |
| 518 |
| 519 // Test optional data. |
| 520 XCTAssertFalse(message.hasOptionalBytes); |
| 521 XCTAssertEqualObjects(message.optionalBytes, GPBEmptyNSData()); |
| 522 XCTAssertFalse(message.hasOptionalBytes); |
| 523 message.optionalBytes = nil; |
| 524 XCTAssertFalse(message.hasOptionalBytes); |
| 525 XCTAssertEqualObjects(message.optionalBytes, GPBEmptyNSData()); |
| 526 NSData *data = [@"bytes" dataUsingEncoding:NSUTF8StringEncoding]; |
| 527 message.optionalBytes = data; |
| 528 XCTAssertEqualObjects(message.optionalBytes, data); |
| 529 XCTAssertTrue(message.hasOptionalBytes); |
| 530 message.optionalBytes = nil; |
| 531 XCTAssertFalse(message.hasOptionalBytes); |
| 532 XCTAssertEqualObjects(message.optionalBytes, GPBEmptyNSData()); |
| 533 |
| 534 // Test lazy message setting |
| 535 XCTAssertFalse(message.hasOptionalLazyMessage); |
| 536 XCTAssertNotNil(message.optionalLazyMessage); |
| 537 XCTAssertFalse(message.hasOptionalLazyMessage); |
| 538 message.hasOptionalLazyMessage = NO; |
| 539 XCTAssertFalse(message.hasOptionalLazyMessage); |
| 540 XCTAssertNotNil(message.optionalLazyMessage); |
| 541 XCTAssertFalse(message.hasOptionalLazyMessage); |
| 542 message.optionalLazyMessage = nil; |
| 543 XCTAssertFalse(message.hasOptionalLazyMessage); |
| 544 |
| 545 // Test nested messages |
| 546 XCTAssertFalse(message.hasOptionalLazyMessage); |
| 547 message.optionalLazyMessage.bb = 1; |
| 548 XCTAssertTrue(message.hasOptionalLazyMessage); |
| 549 XCTAssertEqual(message.optionalLazyMessage.bb, 1); |
| 550 XCTAssertNotNil(message.optionalLazyMessage); |
| 551 message.optionalLazyMessage = nil; |
| 552 XCTAssertFalse(message.hasOptionalLazyMessage); |
| 553 XCTAssertEqual(message.optionalLazyMessage.bb, 0); |
| 554 XCTAssertFalse(message.hasOptionalLazyMessage); |
| 555 XCTAssertNotNil(message.optionalLazyMessage); |
| 556 |
| 557 // -testDefaultSubMessages tests the "defaulting" handling of fields |
| 558 // containing messages. |
| 559 } |
| 560 |
| 561 - (void)testRepeatedSetters { |
| 562 TestAllTypes *message = [TestAllTypes message]; |
| 563 [self setAllFields:message repeatedCount:kGPBDefaultRepeatCount]; |
| 564 [self modifyRepeatedFields:message]; |
| 565 [self assertRepeatedFieldsModified:message |
| 566 repeatedCount:kGPBDefaultRepeatCount]; |
| 567 } |
| 568 |
| 569 - (void)testClear { |
| 570 TestAllTypes *message = [TestAllTypes message]; |
| 571 [self setAllFields:message repeatedCount:kGPBDefaultRepeatCount]; |
| 572 [self clearAllFields:message]; |
| 573 [self assertClear:message]; |
| 574 TestAllTypes *message2 = [TestAllTypes message]; |
| 575 XCTAssertEqualObjects(message, message2); |
| 576 } |
| 577 |
| 578 - (void)testClearKVC { |
| 579 TestAllTypes *message = [TestAllTypes message]; |
| 580 [self setAllFields:message repeatedCount:kGPBDefaultRepeatCount]; |
| 581 [self clearAllFields:message]; |
| 582 [self assertClear:message]; |
| 583 [self assertClearKVC:message]; |
| 584 } |
| 585 |
| 586 - (void)testClearExtension { |
| 587 // clearExtension() is not actually used in TestUtil, so try it manually. |
| 588 GPBMessage *message1 = [TestAllExtensions message]; |
| 589 [message1 setExtension:[UnittestRoot optionalInt32Extension] value:@1]; |
| 590 |
| 591 XCTAssertTrue([message1 hasExtension:[UnittestRoot optionalInt32Extension]]); |
| 592 [message1 clearExtension:[UnittestRoot optionalInt32Extension]]; |
| 593 XCTAssertFalse([message1 hasExtension:[UnittestRoot optionalInt32Extension]]); |
| 594 |
| 595 GPBMessage *message2 = [TestAllExtensions message]; |
| 596 [message2 addExtension:[UnittestRoot repeatedInt32Extension] value:@1]; |
| 597 |
| 598 XCTAssertEqual( |
| 599 [[message2 getExtension:[UnittestRoot repeatedInt32Extension]] count], |
| 600 (NSUInteger)1); |
| 601 [message2 clearExtension:[UnittestRoot repeatedInt32Extension]]; |
| 602 XCTAssertEqual( |
| 603 [[message2 getExtension:[UnittestRoot repeatedInt32Extension]] count], |
| 604 (NSUInteger)0); |
| 605 |
| 606 // Clearing an unset extension field shouldn't make the target message |
| 607 // visible. |
| 608 GPBMessage *message3 = [TestAllExtensions message]; |
| 609 GPBMessage *extension_msg = |
| 610 [message3 getExtension:[UnittestObjcRoot recursiveExtension]]; |
| 611 XCTAssertFalse([message3 hasExtension:[UnittestObjcRoot recursiveExtension]]); |
| 612 [extension_msg clearExtension:[UnittestRoot optionalInt32Extension]]; |
| 613 XCTAssertFalse([message3 hasExtension:[UnittestObjcRoot recursiveExtension]]); |
| 614 } |
| 615 |
| 616 - (void)testDefaultingSubMessages { |
| 617 TestAllTypes *message = [TestAllTypes message]; |
| 618 |
| 619 // Initially they should all not have values. |
| 620 |
| 621 XCTAssertFalse(message.hasOptionalGroup); |
| 622 XCTAssertFalse(message.hasOptionalNestedMessage); |
| 623 XCTAssertFalse(message.hasOptionalForeignMessage); |
| 624 XCTAssertFalse(message.hasOptionalImportMessage); |
| 625 XCTAssertFalse(message.hasOptionalPublicImportMessage); |
| 626 XCTAssertFalse(message.hasOptionalLazyMessage); |
| 627 |
| 628 // They should auto create something when fetched. |
| 629 |
| 630 TestAllTypes_OptionalGroup *optionalGroup = [message.optionalGroup retain]; |
| 631 TestAllTypes_NestedMessage *optionalNestedMessage = |
| 632 [message.optionalNestedMessage retain]; |
| 633 ForeignMessage *optionalForeignMessage = |
| 634 [message.optionalForeignMessage retain]; |
| 635 ImportMessage *optionalImportMessage = [message.optionalImportMessage retain]; |
| 636 PublicImportMessage *optionalPublicImportMessage = |
| 637 [message.optionalPublicImportMessage retain]; |
| 638 TestAllTypes_NestedMessage *optionalLazyMessage = |
| 639 [message.optionalLazyMessage retain]; |
| 640 |
| 641 XCTAssertNotNil(optionalGroup); |
| 642 XCTAssertNotNil(optionalNestedMessage); |
| 643 XCTAssertNotNil(optionalForeignMessage); |
| 644 XCTAssertNotNil(optionalImportMessage); |
| 645 XCTAssertNotNil(optionalPublicImportMessage); |
| 646 XCTAssertNotNil(optionalLazyMessage); |
| 647 |
| 648 // Although they were created, they should not respond to hasValue until that |
| 649 // submessage is mutated. |
| 650 |
| 651 XCTAssertFalse(message.hasOptionalGroup); |
| 652 XCTAssertFalse(message.hasOptionalNestedMessage); |
| 653 XCTAssertFalse(message.hasOptionalForeignMessage); |
| 654 XCTAssertFalse(message.hasOptionalImportMessage); |
| 655 XCTAssertFalse(message.hasOptionalPublicImportMessage); |
| 656 XCTAssertFalse(message.hasOptionalLazyMessage); |
| 657 |
| 658 // And they set that value back in to the message since the value created was |
| 659 // mutable (so a second fetch should give the same object). |
| 660 |
| 661 XCTAssertEqual(message.optionalGroup, optionalGroup); |
| 662 XCTAssertEqual(message.optionalNestedMessage, optionalNestedMessage); |
| 663 XCTAssertEqual(message.optionalForeignMessage, optionalForeignMessage); |
| 664 XCTAssertEqual(message.optionalImportMessage, optionalImportMessage); |
| 665 XCTAssertEqual(message.optionalPublicImportMessage, |
| 666 optionalPublicImportMessage); |
| 667 XCTAssertEqual(message.optionalLazyMessage, optionalLazyMessage); |
| 668 |
| 669 // And the default objects for a second message should be distinct (again, |
| 670 // since they are mutable, each needs their own copy). |
| 671 |
| 672 TestAllTypes *message2 = [TestAllTypes message]; |
| 673 |
| 674 // Intentionally doing a pointer comparison. |
| 675 XCTAssertNotEqual(message2.optionalGroup, optionalGroup); |
| 676 XCTAssertNotEqual(message2.optionalNestedMessage, optionalNestedMessage); |
| 677 XCTAssertNotEqual(message2.optionalForeignMessage, optionalForeignMessage); |
| 678 XCTAssertNotEqual(message2.optionalImportMessage, optionalImportMessage); |
| 679 XCTAssertNotEqual(message2.optionalPublicImportMessage, |
| 680 optionalPublicImportMessage); |
| 681 XCTAssertNotEqual(message2.optionalLazyMessage, optionalLazyMessage); |
| 682 |
| 683 // Setting the values to nil will clear the has flag, and on next access you |
| 684 // get back new submessages. |
| 685 |
| 686 message.optionalGroup = nil; |
| 687 message.optionalNestedMessage = nil; |
| 688 message.optionalForeignMessage = nil; |
| 689 message.optionalImportMessage = nil; |
| 690 message.optionalPublicImportMessage = nil; |
| 691 message.optionalLazyMessage = nil; |
| 692 |
| 693 XCTAssertFalse(message.hasOptionalGroup); |
| 694 XCTAssertFalse(message.hasOptionalNestedMessage); |
| 695 XCTAssertFalse(message.hasOptionalForeignMessage); |
| 696 XCTAssertFalse(message.hasOptionalImportMessage); |
| 697 XCTAssertFalse(message.hasOptionalPublicImportMessage); |
| 698 XCTAssertFalse(message.hasOptionalLazyMessage); |
| 699 |
| 700 // Intentionally doing a pointer comparison. |
| 701 XCTAssertNotEqual(message.optionalGroup, optionalGroup); |
| 702 XCTAssertNotEqual(message.optionalNestedMessage, optionalNestedMessage); |
| 703 XCTAssertNotEqual(message.optionalForeignMessage, optionalForeignMessage); |
| 704 XCTAssertNotEqual(message.optionalImportMessage, optionalImportMessage); |
| 705 XCTAssertNotEqual(message.optionalPublicImportMessage, |
| 706 optionalPublicImportMessage); |
| 707 XCTAssertNotEqual(message.optionalLazyMessage, optionalLazyMessage); |
| 708 |
| 709 [optionalGroup release]; |
| 710 [optionalNestedMessage release]; |
| 711 [optionalForeignMessage release]; |
| 712 [optionalImportMessage release]; |
| 713 [optionalPublicImportMessage release]; |
| 714 [optionalLazyMessage release]; |
| 715 } |
| 716 |
| 717 - (void)testMultiplePointersToAutocreatedMessage { |
| 718 // Multiple objects pointing to the same autocreated message. |
| 719 TestAllTypes *message = [TestAllTypes message]; |
| 720 TestAllTypes *message2 = [TestAllTypes message]; |
| 721 message2.optionalGroup = message.optionalGroup; |
| 722 XCTAssertTrue([message2 hasOptionalGroup]); |
| 723 XCTAssertFalse([message hasOptionalGroup]); |
| 724 message2.optionalGroup.a = 42; |
| 725 XCTAssertTrue([message hasOptionalGroup]); |
| 726 XCTAssertTrue([message2 hasOptionalGroup]); |
| 727 } |
| 728 |
| 729 - (void)testCopyWithAutocreatedMessage { |
| 730 // Mutable copy should not copy autocreated messages. |
| 731 TestAllTypes *message = [TestAllTypes message]; |
| 732 message.optionalGroup.a = 42; |
| 733 XCTAssertNotNil(message.optionalNestedMessage); |
| 734 TestAllTypes *message2 = [[message copy] autorelease]; |
| 735 XCTAssertTrue([message2 hasOptionalGroup]); |
| 736 XCTAssertFalse([message2 hasOptionalNestedMessage]); |
| 737 |
| 738 // Intentionally doing a pointer comparison. |
| 739 XCTAssertNotEqual(message.optionalNestedMessage, |
| 740 message2.optionalNestedMessage); |
| 741 } |
| 742 |
| 743 - (void)testClearAutocreatedSubmessage { |
| 744 // Call clear on an intermediate submessage should cause it to get recreated |
| 745 // on the next call. |
| 746 TestRecursiveMessage *message = [TestRecursiveMessage message]; |
| 747 TestRecursiveMessage *message_inner = [message.a.a.a retain]; |
| 748 XCTAssertNotNil(message_inner); |
| 749 XCTAssertTrue(GPBWasMessageAutocreatedBy(message_inner, message.a.a)); |
| 750 [message.a.a clear]; |
| 751 XCTAssertFalse(GPBWasMessageAutocreatedBy(message_inner, message.a.a)); |
| 752 |
| 753 // Intentionally doing a pointer comparison. |
| 754 XCTAssertNotEqual(message.a.a.a, message_inner); |
| 755 [message_inner release]; |
| 756 } |
| 757 |
| 758 - (void)testRetainAutocreatedSubmessage { |
| 759 // Should be able to retain autocreated submessage while the creator is |
| 760 // dealloced. |
| 761 TestAllTypes *message = [TestAllTypes message]; |
| 762 |
| 763 ForeignMessage *subMessage; |
| 764 @autoreleasepool { |
| 765 TestAllTypes *message2 = [TestAllTypes message]; |
| 766 subMessage = message2.optionalForeignMessage; // Autocreated |
| 767 message.optionalForeignMessage = subMessage; |
| 768 XCTAssertTrue(GPBWasMessageAutocreatedBy(message.optionalForeignMessage, |
| 769 message2)); |
| 770 } |
| 771 |
| 772 // Should be the same object, and should still be live. |
| 773 XCTAssertEqual(message.optionalForeignMessage, subMessage); |
| 774 XCTAssertNotNil([subMessage description]); |
| 775 } |
| 776 |
| 777 - (void)testSetNilAutocreatedSubmessage { |
| 778 TestRecursiveMessage *message = [TestRecursiveMessage message]; |
| 779 TestRecursiveMessage *message_inner = [message.a.a retain]; |
| 780 XCTAssertFalse([message hasA]); |
| 781 XCTAssertFalse([message.a hasA]); |
| 782 message.a.a = nil; |
| 783 |
| 784 // |message.a| has to be made visible, but |message.a.a| was set to nil so |
| 785 // shouldn't be. |
| 786 XCTAssertTrue([message hasA]); |
| 787 XCTAssertFalse([message.a hasA]); |
| 788 |
| 789 // Setting submessage to nil should cause it to lose its creator. |
| 790 XCTAssertFalse(GPBWasMessageAutocreatedBy(message_inner, message.a)); |
| 791 |
| 792 // After setting to nil, getting it again should create a new autocreated |
| 793 // message. |
| 794 // Intentionally doing a pointer comparison. |
| 795 XCTAssertNotEqual(message.a.a, message_inner); |
| 796 |
| 797 [message_inner release]; |
| 798 } |
| 799 |
| 800 - (void)testSetDoesntHaveAutocreatedSubmessage { |
| 801 // Clearing submessage (set has == NO) should NOT cause it to lose its |
| 802 // creator. |
| 803 TestAllTypes *message = [TestAllTypes message]; |
| 804 TestAllTypes_NestedMessage *nestedMessage = message.optionalNestedMessage; |
| 805 XCTAssertFalse([message hasOptionalNestedMessage]); |
| 806 [message setHasOptionalNestedMessage:NO]; |
| 807 XCTAssertFalse([message hasOptionalNestedMessage]); |
| 808 XCTAssertEqual(message.optionalNestedMessage, nestedMessage); |
| 809 } |
| 810 |
| 811 - (void)testSetAutocreatedMessageBecomesVisible { |
| 812 // Setting a value should cause the submessage to appear to its creator. |
| 813 // Test this several levels deep. |
| 814 TestRecursiveMessage *message = [TestRecursiveMessage message]; |
| 815 message.a.a.a.a.i = 42; |
| 816 XCTAssertTrue([message hasA]); |
| 817 XCTAssertTrue([message.a hasA]); |
| 818 XCTAssertTrue([message.a.a hasA]); |
| 819 XCTAssertTrue([message.a.a.a hasA]); |
| 820 XCTAssertFalse([message.a.a.a.a hasA]); |
| 821 XCTAssertEqual(message.a.a.a.a.i, 42); |
| 822 } |
| 823 |
| 824 - (void)testClearUnsetFieldOfAutocreatedMessage { |
| 825 // Clearing an unset field should not cause the submessage to appear to its |
| 826 // creator. |
| 827 TestRecursiveMessage *message = [TestRecursiveMessage message]; |
| 828 message.a.a.a.a.hasI = NO; |
| 829 XCTAssertFalse([message hasA]); |
| 830 XCTAssertFalse([message.a hasA]); |
| 831 XCTAssertFalse([message.a.a hasA]); |
| 832 XCTAssertFalse([message.a.a.a hasA]); |
| 833 } |
| 834 |
| 835 - (void)testAutocreatedSubmessageAssignSkip { |
| 836 TestRecursiveMessage *message = [TestRecursiveMessage message]; |
| 837 TestRecursiveMessage *messageLevel1 = [message.a retain]; |
| 838 TestRecursiveMessage *messageLevel2 = [message.a.a retain]; |
| 839 TestRecursiveMessage *messageLevel3 = [message.a.a.a retain]; |
| 840 TestRecursiveMessage *messageLevel4 = [message.a.a.a.a retain]; |
| 841 XCTAssertTrue(GPBWasMessageAutocreatedBy(messageLevel1, message)); |
| 842 XCTAssertTrue(GPBWasMessageAutocreatedBy(messageLevel2, messageLevel1)); |
| 843 XCTAssertTrue(GPBWasMessageAutocreatedBy(messageLevel3, messageLevel2)); |
| 844 XCTAssertTrue(GPBWasMessageAutocreatedBy(messageLevel4, messageLevel3)); |
| 845 |
| 846 // Test skipping over an autocreated submessage and ensure it gets unset. |
| 847 message.a = message.a.a; |
| 848 XCTAssertEqual(message.a, messageLevel2); |
| 849 XCTAssertTrue([message hasA]); |
| 850 XCTAssertEqual(message.a.a, messageLevel3); |
| 851 XCTAssertFalse([message.a hasA]); |
| 852 XCTAssertEqual(message.a.a.a, messageLevel4); |
| 853 XCTAssertFalse(GPBWasMessageAutocreatedBy(messageLevel1, |
| 854 message)); // Because it was orphan
ed. |
| 855 XCTAssertTrue(GPBWasMessageAutocreatedBy(messageLevel2, messageLevel1)); |
| 856 XCTAssertTrue(GPBWasMessageAutocreatedBy(messageLevel3, messageLevel2)); |
| 857 |
| 858 [messageLevel1 release]; |
| 859 [messageLevel2 release]; |
| 860 [messageLevel3 release]; |
| 861 [messageLevel4 release]; |
| 862 } |
| 863 |
| 864 - (void)testAutocreatedSubmessageAssignLoop { |
| 865 TestRecursiveMessage *message = [TestRecursiveMessage message]; |
| 866 TestRecursiveMessage *messageLevel1 = [message.a retain]; |
| 867 TestRecursiveMessage *messageLevel2 = [message.a.a retain]; |
| 868 TestRecursiveMessage *messageLevel3 = [message.a.a.a retain]; |
| 869 TestRecursiveMessage *messageLevel4 = [message.a.a.a.a retain]; |
| 870 XCTAssertTrue(GPBWasMessageAutocreatedBy(messageLevel1, message)); |
| 871 XCTAssertTrue(GPBWasMessageAutocreatedBy(messageLevel2, messageLevel1)); |
| 872 XCTAssertTrue(GPBWasMessageAutocreatedBy(messageLevel3, messageLevel2)); |
| 873 XCTAssertTrue(GPBWasMessageAutocreatedBy(messageLevel4, messageLevel3)); |
| 874 |
| 875 // Test a property with a loop. You'd never do this but at least ensure the |
| 876 // autocreated submessages behave sanely. |
| 877 message.a.a = message.a; |
| 878 XCTAssertTrue([message hasA]); |
| 879 XCTAssertEqual(message.a, messageLevel1); |
| 880 XCTAssertTrue([message.a hasA]); |
| 881 XCTAssertEqual(message.a.a, messageLevel1); |
| 882 XCTAssertTrue([message.a.a hasA]); |
| 883 XCTAssertEqual(message.a.a.a, messageLevel1); |
| 884 XCTAssertFalse(GPBWasMessageAutocreatedBy(messageLevel1, |
| 885 message)); // Because it was assign
ed. |
| 886 XCTAssertFalse(GPBWasMessageAutocreatedBy(messageLevel2, |
| 887 messageLevel1)); // Because it was
orphaned. |
| 888 XCTAssertFalse([messageLevel2 hasA]); |
| 889 |
| 890 // Break the retain loop. |
| 891 message.a.a = nil; |
| 892 XCTAssertTrue([message hasA]); |
| 893 XCTAssertFalse([message.a hasA]); |
| 894 |
| 895 [messageLevel1 release]; |
| 896 [messageLevel2 release]; |
| 897 [messageLevel3 release]; |
| 898 [messageLevel4 release]; |
| 899 } |
| 900 |
| 901 - (void)testSetAutocreatedSubmessage { |
| 902 // Setting autocreated submessage to another value should cause the old one to |
| 903 // lose its creator. |
| 904 TestAllTypes *message = [TestAllTypes message]; |
| 905 TestAllTypes_NestedMessage *nestedMessage = |
| 906 [message.optionalNestedMessage retain]; |
| 907 |
| 908 message.optionalNestedMessage = [TestAllTypes_NestedMessage message]; |
| 909 XCTAssertTrue([message hasOptionalNestedMessage]); |
| 910 XCTAssertTrue(message.optionalNestedMessage != nestedMessage); |
| 911 XCTAssertFalse(GPBWasMessageAutocreatedBy(nestedMessage, message)); |
| 912 |
| 913 [nestedMessage release]; |
| 914 } |
| 915 |
| 916 - (void)testAutocreatedUnknownFields { |
| 917 // Doing anything with (except reading) unknown fields should cause the |
| 918 // submessage to become visible. |
| 919 TestAllTypes *message = [TestAllTypes message]; |
| 920 XCTAssertNotNil(message.optionalNestedMessage); |
| 921 XCTAssertFalse([message hasOptionalNestedMessage]); |
| 922 XCTAssertNil(message.optionalNestedMessage.unknownFields); |
| 923 XCTAssertFalse([message hasOptionalNestedMessage]); |
| 924 |
| 925 GPBUnknownFieldSet *unknownFields = |
| 926 [[[GPBUnknownFieldSet alloc] init] autorelease]; |
| 927 message.optionalNestedMessage.unknownFields = unknownFields; |
| 928 XCTAssertTrue([message hasOptionalNestedMessage]); |
| 929 |
| 930 message.optionalNestedMessage = nil; |
| 931 XCTAssertFalse([message hasOptionalNestedMessage]); |
| 932 [message.optionalNestedMessage setUnknownFields:unknownFields]; |
| 933 XCTAssertTrue([message hasOptionalNestedMessage]); |
| 934 } |
| 935 |
| 936 - (void)testSetAutocreatedSubmessageToSelf { |
| 937 // Setting submessage to itself should cause it to become visible. |
| 938 TestAllTypes *message = [TestAllTypes message]; |
| 939 XCTAssertNotNil(message.optionalNestedMessage); |
| 940 XCTAssertFalse([message hasOptionalNestedMessage]); |
| 941 message.optionalNestedMessage = message.optionalNestedMessage; |
| 942 XCTAssertTrue([message hasOptionalNestedMessage]); |
| 943 } |
| 944 |
| 945 - (void)testAutocreatedSubmessageMemoryLeaks { |
| 946 // Test for memory leaks with autocreated submessages. |
| 947 TestRecursiveMessage *message; |
| 948 TestRecursiveMessage *messageLevel1; |
| 949 TestRecursiveMessage *messageLevel2; |
| 950 TestRecursiveMessage *messageLevel3; |
| 951 TestRecursiveMessage *messageLevel4; |
| 952 @autoreleasepool { |
| 953 message = [[TestRecursiveMessage alloc] init]; |
| 954 messageLevel1 = [message.a retain]; |
| 955 messageLevel2 = [message.a.a retain]; |
| 956 messageLevel3 = [message.a.a.a retain]; |
| 957 messageLevel4 = [message.a.a.a.a retain]; |
| 958 message.a.i = 1; |
| 959 } |
| 960 |
| 961 XCTAssertEqual(message.retainCount, (NSUInteger)1); |
| 962 [message release]; |
| 963 XCTAssertEqual(messageLevel1.retainCount, (NSUInteger)1); |
| 964 [messageLevel1 release]; |
| 965 XCTAssertEqual(messageLevel2.retainCount, (NSUInteger)1); |
| 966 [messageLevel2 release]; |
| 967 XCTAssertEqual(messageLevel3.retainCount, (NSUInteger)1); |
| 968 [messageLevel3 release]; |
| 969 XCTAssertEqual(messageLevel4.retainCount, (NSUInteger)1); |
| 970 [messageLevel4 release]; |
| 971 } |
| 972 |
| 973 - (void)testDefaultingArrays { |
| 974 // Basic tests for default creation of arrays in a message. |
| 975 TestRecursiveMessageWithRepeatedField *message = |
| 976 [TestRecursiveMessageWithRepeatedField message]; |
| 977 TestRecursiveMessageWithRepeatedField *message2 = |
| 978 [TestRecursiveMessageWithRepeatedField message]; |
| 979 |
| 980 // Simply accessing the array should not make any fields visible. |
| 981 XCTAssertNotNil(message.a.a.iArray); |
| 982 XCTAssertFalse([message hasA]); |
| 983 XCTAssertFalse([message.a hasA]); |
| 984 XCTAssertNotNil(message2.a.a.strArray); |
| 985 XCTAssertFalse([message2 hasA]); |
| 986 XCTAssertFalse([message2.a hasA]); |
| 987 |
| 988 // But adding an element to the array should. |
| 989 [message.a.a.iArray addValue:42]; |
| 990 XCTAssertTrue([message hasA]); |
| 991 XCTAssertTrue([message.a hasA]); |
| 992 XCTAssertEqual([message.a.a.iArray count], (NSUInteger)1); |
| 993 [message2.a.a.strArray addObject:@"foo"]; |
| 994 XCTAssertTrue([message2 hasA]); |
| 995 XCTAssertTrue([message2.a hasA]); |
| 996 XCTAssertEqual([message2.a.a.strArray count], (NSUInteger)1); |
| 997 } |
| 998 |
| 999 - (void)testAutocreatedArrayShared { |
| 1000 // Multiple objects pointing to the same array. |
| 1001 TestRecursiveMessageWithRepeatedField *message1a = |
| 1002 [TestRecursiveMessageWithRepeatedField message]; |
| 1003 TestRecursiveMessageWithRepeatedField *message1b = |
| 1004 [TestRecursiveMessageWithRepeatedField message]; |
| 1005 message1a.a.iArray = message1b.a.iArray; |
| 1006 XCTAssertTrue([message1a hasA]); |
| 1007 XCTAssertFalse([message1b hasA]); |
| 1008 [message1a.a.iArray addValue:1]; |
| 1009 XCTAssertTrue([message1a hasA]); |
| 1010 XCTAssertTrue([message1b hasA]); |
| 1011 XCTAssertEqual(message1a.a.iArray, message1b.a.iArray); |
| 1012 |
| 1013 TestRecursiveMessageWithRepeatedField *message2a = |
| 1014 [TestRecursiveMessageWithRepeatedField message]; |
| 1015 TestRecursiveMessageWithRepeatedField *message2b = |
| 1016 [TestRecursiveMessageWithRepeatedField message]; |
| 1017 message2a.a.strArray = message2b.a.strArray; |
| 1018 XCTAssertTrue([message2a hasA]); |
| 1019 XCTAssertFalse([message2b hasA]); |
| 1020 [message2a.a.strArray addObject:@"bar"]; |
| 1021 XCTAssertTrue([message2a hasA]); |
| 1022 XCTAssertTrue([message2b hasA]); |
| 1023 XCTAssertEqual(message2a.a.strArray, message2b.a.strArray); |
| 1024 } |
| 1025 |
| 1026 - (void)testAutocreatedArrayCopy { |
| 1027 // Copy should not copy autocreated arrays. |
| 1028 TestAllTypes *message = [TestAllTypes message]; |
| 1029 XCTAssertNotNil(message.repeatedStringArray); |
| 1030 XCTAssertNotNil(message.repeatedInt32Array); |
| 1031 TestAllTypes *message2 = [[message copy] autorelease]; |
| 1032 // Pointer conparisions. |
| 1033 XCTAssertNotEqual(message.repeatedStringArray, message2.repeatedStringArray); |
| 1034 XCTAssertNotEqual(message.repeatedInt32Array, message2.repeatedInt32Array); |
| 1035 |
| 1036 // Mutable copy should copy empty arrays that were explicitly set (end up |
| 1037 // with different objects that are equal). |
| 1038 TestAllTypes *message3 = [TestAllTypes message]; |
| 1039 message3.repeatedInt32Array = [GPBInt32Array arrayWithValue:42]; |
| 1040 message3.repeatedStringArray = [NSMutableArray arrayWithObject:@"wee"]; |
| 1041 XCTAssertNotNil(message.repeatedInt32Array); |
| 1042 XCTAssertNotNil(message.repeatedStringArray); |
| 1043 TestAllTypes *message4 = [[message3 copy] autorelease]; |
| 1044 XCTAssertNotEqual(message3.repeatedInt32Array, message4.repeatedInt32Array); |
| 1045 XCTAssertEqualObjects(message3.repeatedInt32Array, |
| 1046 message4.repeatedInt32Array); |
| 1047 XCTAssertNotEqual(message3.repeatedStringArray, message4.repeatedStringArray); |
| 1048 XCTAssertEqualObjects(message3.repeatedStringArray, |
| 1049 message4.repeatedStringArray); |
| 1050 } |
| 1051 |
| 1052 - (void)testAutocreatedArrayRetain { |
| 1053 // Should be able to retain autocreated array while the creator is dealloced. |
| 1054 TestAllTypes *message = [TestAllTypes message]; |
| 1055 |
| 1056 @autoreleasepool { |
| 1057 TestAllTypes *message2 = [TestAllTypes message]; |
| 1058 message.repeatedInt32Array = message2.repeatedInt32Array; |
| 1059 message.repeatedStringArray = message2.repeatedStringArray; |
| 1060 // Pointer conparision |
| 1061 XCTAssertEqual(message.repeatedInt32Array->_autocreator, message2); |
| 1062 XCTAssertTrue([message.repeatedStringArray |
| 1063 isKindOfClass:[GPBAutocreatedArray class]]); |
| 1064 XCTAssertEqual( |
| 1065 ((GPBAutocreatedArray *)message.repeatedStringArray)->_autocreator, |
| 1066 message2); |
| 1067 } |
| 1068 |
| 1069 XCTAssertNil(message.repeatedInt32Array->_autocreator); |
| 1070 XCTAssertTrue( |
| 1071 [message.repeatedStringArray isKindOfClass:[GPBAutocreatedArray class]]); |
| 1072 XCTAssertNil( |
| 1073 ((GPBAutocreatedArray *)message.repeatedStringArray)->_autocreator); |
| 1074 } |
| 1075 |
| 1076 - (void)testSetNilAutocreatedArray { |
| 1077 // Setting array to nil should cause it to lose its delegate. |
| 1078 TestAllTypes *message = [TestAllTypes message]; |
| 1079 GPBInt32Array *repeatedInt32Array = [message.repeatedInt32Array retain]; |
| 1080 GPBAutocreatedArray *repeatedStringArray = |
| 1081 (GPBAutocreatedArray *)[message.repeatedStringArray retain]; |
| 1082 XCTAssertTrue([repeatedStringArray isKindOfClass:[GPBAutocreatedArray class]])
; |
| 1083 XCTAssertEqual(repeatedInt32Array->_autocreator, message); |
| 1084 XCTAssertEqual(repeatedStringArray->_autocreator, message); |
| 1085 message.repeatedInt32Array = nil; |
| 1086 message.repeatedStringArray = nil; |
| 1087 XCTAssertNil(repeatedInt32Array->_autocreator); |
| 1088 XCTAssertNil(repeatedStringArray->_autocreator); |
| 1089 [repeatedInt32Array release]; |
| 1090 [repeatedStringArray release]; |
| 1091 } |
| 1092 |
| 1093 - (void)testReplaceAutocreatedArray { |
| 1094 // Replacing array should orphan the old one and cause its creator to become |
| 1095 // visible. |
| 1096 { |
| 1097 TestRecursiveMessageWithRepeatedField *message = |
| 1098 [TestRecursiveMessageWithRepeatedField message]; |
| 1099 XCTAssertNotNil(message.a); |
| 1100 XCTAssertNotNil(message.a.iArray); |
| 1101 XCTAssertFalse([message hasA]); |
| 1102 GPBInt32Array *iArray = [message.a.iArray retain]; |
| 1103 XCTAssertEqual(iArray->_autocreator, message.a); // Pointer comparision |
| 1104 message.a.iArray = [GPBInt32Array arrayWithValue:1]; |
| 1105 XCTAssertTrue([message hasA]); |
| 1106 XCTAssertNotEqual(message.a.iArray, iArray); // Pointer comparision |
| 1107 XCTAssertNil(iArray->_autocreator); |
| 1108 [iArray release]; |
| 1109 } |
| 1110 |
| 1111 { |
| 1112 TestRecursiveMessageWithRepeatedField *message = |
| 1113 [TestRecursiveMessageWithRepeatedField message]; |
| 1114 XCTAssertNotNil(message.a); |
| 1115 XCTAssertNotNil(message.a.strArray); |
| 1116 XCTAssertFalse([message hasA]); |
| 1117 GPBAutocreatedArray *strArray = |
| 1118 (GPBAutocreatedArray *)[message.a.strArray retain]; |
| 1119 XCTAssertTrue([strArray isKindOfClass:[GPBAutocreatedArray class]]); |
| 1120 XCTAssertEqual(strArray->_autocreator, message.a); // Pointer comparision |
| 1121 message.a.strArray = [NSMutableArray arrayWithObject:@"foo"]; |
| 1122 XCTAssertTrue([message hasA]); |
| 1123 XCTAssertNotEqual(message.a.strArray, strArray); // Pointer comparision |
| 1124 XCTAssertNil(strArray->_autocreator); |
| 1125 [strArray release]; |
| 1126 } |
| 1127 } |
| 1128 |
| 1129 - (void)testSetAutocreatedArrayToSelf { |
| 1130 // Setting array to itself should cause it to become visible. |
| 1131 { |
| 1132 TestRecursiveMessageWithRepeatedField *message = |
| 1133 [TestRecursiveMessageWithRepeatedField message]; |
| 1134 XCTAssertNotNil(message.a); |
| 1135 XCTAssertNotNil(message.a.iArray); |
| 1136 XCTAssertFalse([message hasA]); |
| 1137 message.a.iArray = message.a.iArray; |
| 1138 XCTAssertTrue([message hasA]); |
| 1139 XCTAssertNil(message.a.iArray->_autocreator); |
| 1140 } |
| 1141 |
| 1142 { |
| 1143 TestRecursiveMessageWithRepeatedField *message = |
| 1144 [TestRecursiveMessageWithRepeatedField message]; |
| 1145 XCTAssertNotNil(message.a); |
| 1146 XCTAssertNotNil(message.a.strArray); |
| 1147 XCTAssertFalse([message hasA]); |
| 1148 message.a.strArray = message.a.strArray; |
| 1149 XCTAssertTrue([message hasA]); |
| 1150 XCTAssertTrue([message.a.strArray isKindOfClass:[GPBAutocreatedArray class]]
); |
| 1151 XCTAssertNil(((GPBAutocreatedArray *)message.a.strArray)->_autocreator); |
| 1152 } |
| 1153 } |
| 1154 |
| 1155 - (void)testAutocreatedArrayRemoveAllValues { |
| 1156 // Calling removeAllValues on autocreated array should not cause it to be |
| 1157 // visible. |
| 1158 TestRecursiveMessageWithRepeatedField *message = |
| 1159 [TestRecursiveMessageWithRepeatedField message]; |
| 1160 [message.a.iArray removeAll]; |
| 1161 XCTAssertFalse([message hasA]); |
| 1162 [message.a.strArray removeAllObjects]; |
| 1163 XCTAssertFalse([message hasA]); |
| 1164 } |
| 1165 |
| 1166 - (void)testDefaultingMaps { |
| 1167 // Basic tests for default creation of maps in a message. |
| 1168 TestRecursiveMessageWithRepeatedField *message = |
| 1169 [TestRecursiveMessageWithRepeatedField message]; |
| 1170 TestRecursiveMessageWithRepeatedField *message2 = |
| 1171 [TestRecursiveMessageWithRepeatedField message]; |
| 1172 |
| 1173 // Simply accessing the map should not make any fields visible. |
| 1174 XCTAssertNotNil(message.a.a.iToI); |
| 1175 XCTAssertFalse([message hasA]); |
| 1176 XCTAssertFalse([message.a hasA]); |
| 1177 XCTAssertNotNil(message2.a.a.strToStr); |
| 1178 XCTAssertFalse([message2 hasA]); |
| 1179 XCTAssertFalse([message2.a hasA]); |
| 1180 |
| 1181 // But adding an element to the map should. |
| 1182 [message.a.a.iToI setValue:100 forKey:200]; |
| 1183 XCTAssertTrue([message hasA]); |
| 1184 XCTAssertTrue([message.a hasA]); |
| 1185 XCTAssertEqual([message.a.a.iToI count], (NSUInteger)1); |
| 1186 [message2.a.a.strToStr setObject:@"foo" forKey:@"bar"]; |
| 1187 XCTAssertTrue([message2 hasA]); |
| 1188 XCTAssertTrue([message2.a hasA]); |
| 1189 XCTAssertEqual([message2.a.a.strToStr count], (NSUInteger)1); |
| 1190 } |
| 1191 |
| 1192 - (void)testAutocreatedMapShared { |
| 1193 // Multiple objects pointing to the same map. |
| 1194 TestRecursiveMessageWithRepeatedField *message1a = |
| 1195 [TestRecursiveMessageWithRepeatedField message]; |
| 1196 TestRecursiveMessageWithRepeatedField *message1b = |
| 1197 [TestRecursiveMessageWithRepeatedField message]; |
| 1198 message1a.a.iToI = message1b.a.iToI; |
| 1199 XCTAssertTrue([message1a hasA]); |
| 1200 XCTAssertFalse([message1b hasA]); |
| 1201 [message1a.a.iToI setValue:1 forKey:2]; |
| 1202 XCTAssertTrue([message1a hasA]); |
| 1203 XCTAssertTrue([message1b hasA]); |
| 1204 XCTAssertEqual(message1a.a.iToI, message1b.a.iToI); |
| 1205 |
| 1206 TestRecursiveMessageWithRepeatedField *message2a = |
| 1207 [TestRecursiveMessageWithRepeatedField message]; |
| 1208 TestRecursiveMessageWithRepeatedField *message2b = |
| 1209 [TestRecursiveMessageWithRepeatedField message]; |
| 1210 message2a.a.strToStr = message2b.a.strToStr; |
| 1211 XCTAssertTrue([message2a hasA]); |
| 1212 XCTAssertFalse([message2b hasA]); |
| 1213 [message2a.a.strToStr setObject:@"bar" forKey:@"foo"]; |
| 1214 XCTAssertTrue([message2a hasA]); |
| 1215 XCTAssertTrue([message2b hasA]); |
| 1216 XCTAssertEqual(message2a.a.strToStr, message2b.a.strToStr); |
| 1217 } |
| 1218 |
| 1219 - (void)testAutocreatedMapCopy { |
| 1220 // Copy should not copy autocreated maps. |
| 1221 TestRecursiveMessageWithRepeatedField *message = |
| 1222 [TestRecursiveMessageWithRepeatedField message]; |
| 1223 XCTAssertNotNil(message.strToStr); |
| 1224 XCTAssertNotNil(message.iToI); |
| 1225 TestRecursiveMessageWithRepeatedField *message2 = |
| 1226 [[message copy] autorelease]; |
| 1227 // Pointer conparisions. |
| 1228 XCTAssertNotEqual(message.strToStr, message2.strToStr); |
| 1229 XCTAssertNotEqual(message.iToI, message2.iToI); |
| 1230 |
| 1231 // Mutable copy should copy empty arrays that were explicitly set (end up |
| 1232 // with different objects that are equal). |
| 1233 TestRecursiveMessageWithRepeatedField *message3 = |
| 1234 [TestRecursiveMessageWithRepeatedField message]; |
| 1235 message3.iToI = [GPBInt32Int32Dictionary dictionaryWithValue:10 forKey:20]; |
| 1236 message3.strToStr = |
| 1237 [NSMutableDictionary dictionaryWithObject:@"abc" forKey:@"123"]; |
| 1238 XCTAssertNotNil(message.iToI); |
| 1239 XCTAssertNotNil(message.iToI); |
| 1240 TestRecursiveMessageWithRepeatedField *message4 = |
| 1241 [[message3 copy] autorelease]; |
| 1242 XCTAssertNotEqual(message3.iToI, message4.iToI); |
| 1243 XCTAssertEqualObjects(message3.iToI, message4.iToI); |
| 1244 XCTAssertNotEqual(message3.strToStr, message4.strToStr); |
| 1245 XCTAssertEqualObjects(message3.strToStr, message4.strToStr); |
| 1246 } |
| 1247 |
| 1248 - (void)testAutocreatedMapRetain { |
| 1249 // Should be able to retain autocreated map while the creator is dealloced. |
| 1250 TestRecursiveMessageWithRepeatedField *message = |
| 1251 [TestRecursiveMessageWithRepeatedField message]; |
| 1252 |
| 1253 @autoreleasepool { |
| 1254 TestRecursiveMessageWithRepeatedField *message2 = |
| 1255 [TestRecursiveMessageWithRepeatedField message]; |
| 1256 message.iToI = message2.iToI; |
| 1257 message.strToStr = message2.strToStr; |
| 1258 // Pointer conparision |
| 1259 XCTAssertEqual(message.iToI->_autocreator, message2); |
| 1260 XCTAssertTrue([message.strToStr |
| 1261 isKindOfClass:[GPBAutocreatedDictionary class]]); |
| 1262 XCTAssertEqual( |
| 1263 ((GPBAutocreatedDictionary *)message.strToStr)->_autocreator, |
| 1264 message2); |
| 1265 } |
| 1266 |
| 1267 XCTAssertNil(message.iToI->_autocreator); |
| 1268 XCTAssertTrue( |
| 1269 [message.strToStr isKindOfClass:[GPBAutocreatedDictionary class]]); |
| 1270 XCTAssertNil( |
| 1271 ((GPBAutocreatedDictionary *)message.strToStr)->_autocreator); |
| 1272 } |
| 1273 |
| 1274 - (void)testSetNilAutocreatedMap { |
| 1275 // Setting map to nil should cause it to lose its delegate. |
| 1276 TestRecursiveMessageWithRepeatedField *message = |
| 1277 [TestRecursiveMessageWithRepeatedField message]; |
| 1278 GPBInt32Int32Dictionary *iToI = [message.iToI retain]; |
| 1279 GPBAutocreatedDictionary *strToStr = |
| 1280 (GPBAutocreatedDictionary *)[message.strToStr retain]; |
| 1281 XCTAssertTrue([strToStr isKindOfClass:[GPBAutocreatedDictionary class]]); |
| 1282 XCTAssertEqual(iToI->_autocreator, message); |
| 1283 XCTAssertEqual(strToStr->_autocreator, message); |
| 1284 message.iToI = nil; |
| 1285 message.strToStr = nil; |
| 1286 XCTAssertNil(iToI->_autocreator); |
| 1287 XCTAssertNil(strToStr->_autocreator); |
| 1288 [iToI release]; |
| 1289 [strToStr release]; |
| 1290 } |
| 1291 |
| 1292 - (void)testReplaceAutocreatedMap { |
| 1293 // Replacing map should orphan the old one and cause its creator to become |
| 1294 // visible. |
| 1295 { |
| 1296 TestRecursiveMessageWithRepeatedField *message = |
| 1297 [TestRecursiveMessageWithRepeatedField message]; |
| 1298 XCTAssertNotNil(message.a); |
| 1299 XCTAssertNotNil(message.a.iToI); |
| 1300 XCTAssertFalse([message hasA]); |
| 1301 GPBInt32Int32Dictionary *iToI = [message.a.iToI retain]; |
| 1302 XCTAssertEqual(iToI->_autocreator, message.a); // Pointer comparision |
| 1303 message.a.iToI = [GPBInt32Int32Dictionary dictionaryWithValue:6 forKey:7]; |
| 1304 XCTAssertTrue([message hasA]); |
| 1305 XCTAssertNotEqual(message.a.iToI, iToI); // Pointer comparision |
| 1306 XCTAssertNil(iToI->_autocreator); |
| 1307 [iToI release]; |
| 1308 } |
| 1309 |
| 1310 { |
| 1311 TestRecursiveMessageWithRepeatedField *message = |
| 1312 [TestRecursiveMessageWithRepeatedField message]; |
| 1313 XCTAssertNotNil(message.a); |
| 1314 XCTAssertNotNil(message.a.strToStr); |
| 1315 XCTAssertFalse([message hasA]); |
| 1316 GPBAutocreatedDictionary *strToStr = |
| 1317 (GPBAutocreatedDictionary *)[message.a.strToStr retain]; |
| 1318 XCTAssertTrue([strToStr isKindOfClass:[GPBAutocreatedDictionary class]]); |
| 1319 XCTAssertEqual(strToStr->_autocreator, message.a); // Pointer comparision |
| 1320 message.a.strToStr = |
| 1321 [NSMutableDictionary dictionaryWithObject:@"abc" forKey:@"def"]; |
| 1322 XCTAssertTrue([message hasA]); |
| 1323 XCTAssertNotEqual(message.a.strToStr, strToStr); // Pointer comparision |
| 1324 XCTAssertNil(strToStr->_autocreator); |
| 1325 [strToStr release]; |
| 1326 } |
| 1327 } |
| 1328 |
| 1329 - (void)testSetAutocreatedMapToSelf { |
| 1330 // Setting map to itself should cause it to become visible. |
| 1331 { |
| 1332 TestRecursiveMessageWithRepeatedField *message = |
| 1333 [TestRecursiveMessageWithRepeatedField message]; |
| 1334 XCTAssertNotNil(message.a); |
| 1335 XCTAssertNotNil(message.a.iToI); |
| 1336 XCTAssertFalse([message hasA]); |
| 1337 message.a.iToI = message.a.iToI; |
| 1338 XCTAssertTrue([message hasA]); |
| 1339 XCTAssertNil(message.a.iToI->_autocreator); |
| 1340 } |
| 1341 |
| 1342 { |
| 1343 TestRecursiveMessageWithRepeatedField *message = |
| 1344 [TestRecursiveMessageWithRepeatedField message]; |
| 1345 XCTAssertNotNil(message.a); |
| 1346 XCTAssertNotNil(message.a.strToStr); |
| 1347 XCTAssertFalse([message hasA]); |
| 1348 message.a.strToStr = message.a.strToStr; |
| 1349 XCTAssertTrue([message hasA]); |
| 1350 XCTAssertTrue([message.a.strToStr isKindOfClass:[GPBAutocreatedDictionary cl
ass]]); |
| 1351 XCTAssertNil(((GPBAutocreatedDictionary *)message.a.strToStr)->_autocreator)
; |
| 1352 } |
| 1353 } |
| 1354 |
| 1355 - (void)testAutocreatedMapRemoveAllValues { |
| 1356 // Calling removeAll on autocreated map should not cause it to be visible. |
| 1357 TestRecursiveMessageWithRepeatedField *message = |
| 1358 [TestRecursiveMessageWithRepeatedField message]; |
| 1359 [message.a.iToI removeAll]; |
| 1360 XCTAssertFalse([message hasA]); |
| 1361 [message.a.strToStr removeAllObjects]; |
| 1362 XCTAssertFalse([message hasA]); |
| 1363 } |
| 1364 |
| 1365 - (void)testExtensionAccessors { |
| 1366 TestAllExtensions *message = [TestAllExtensions message]; |
| 1367 [self setAllExtensions:message repeatedCount:kGPBDefaultRepeatCount]; |
| 1368 [self assertAllExtensionsSet:message repeatedCount:kGPBDefaultRepeatCount]; |
| 1369 } |
| 1370 |
| 1371 - (void)testExtensionRepeatedSetters { |
| 1372 TestAllExtensions *message = [TestAllExtensions message]; |
| 1373 [self setAllExtensions:message repeatedCount:kGPBDefaultRepeatCount]; |
| 1374 [self modifyRepeatedExtensions:message]; |
| 1375 [self assertRepeatedExtensionsModified:message |
| 1376 repeatedCount:kGPBDefaultRepeatCount]; |
| 1377 } |
| 1378 |
| 1379 - (void)testExtensionDefaults { |
| 1380 [self assertExtensionsClear:[TestAllExtensions message]]; |
| 1381 } |
| 1382 |
| 1383 - (void)testExtensionIsEquals { |
| 1384 TestAllExtensions *message = [TestAllExtensions message]; |
| 1385 [self setAllExtensions:message repeatedCount:kGPBDefaultRepeatCount]; |
| 1386 [self modifyRepeatedExtensions:message]; |
| 1387 TestAllExtensions *message2 = [TestAllExtensions message]; |
| 1388 [self setAllExtensions:message2 repeatedCount:kGPBDefaultRepeatCount]; |
| 1389 XCTAssertFalse([message isEqual:message2]); |
| 1390 message2 = [TestAllExtensions message]; |
| 1391 [self setAllExtensions:message2 repeatedCount:kGPBDefaultRepeatCount]; |
| 1392 [self modifyRepeatedExtensions:message2]; |
| 1393 XCTAssertEqualObjects(message, message2); |
| 1394 } |
| 1395 |
| 1396 - (void)testExtensionsMergeFrom { |
| 1397 TestAllExtensions *message = [TestAllExtensions message]; |
| 1398 [self setAllExtensions:message repeatedCount:kGPBDefaultRepeatCount]; |
| 1399 [self modifyRepeatedExtensions:message]; |
| 1400 |
| 1401 message = [TestAllExtensions message]; |
| 1402 [self setAllExtensions:message repeatedCount:kGPBDefaultRepeatCount]; |
| 1403 TestAllExtensions *message2 = [TestAllExtensions message]; |
| 1404 [self modifyRepeatedExtensions:message2]; |
| 1405 [message2 mergeFrom:message]; |
| 1406 |
| 1407 XCTAssertEqualObjects(message, message2); |
| 1408 } |
| 1409 |
| 1410 - (void)testDefaultingExtensionMessages { |
| 1411 TestAllExtensions *message = [TestAllExtensions message]; |
| 1412 |
| 1413 // Initially they should all not have values. |
| 1414 |
| 1415 XCTAssertFalse([message hasExtension:[UnittestRoot optionalGroupExtension]]); |
| 1416 XCTAssertFalse([message hasExtension:[UnittestRoot optionalGroupExtension]]); |
| 1417 XCTAssertFalse( |
| 1418 [message hasExtension:[UnittestRoot optionalNestedMessageExtension]]); |
| 1419 XCTAssertFalse( |
| 1420 [message hasExtension:[UnittestRoot optionalForeignMessageExtension]]); |
| 1421 XCTAssertFalse( |
| 1422 [message hasExtension:[UnittestRoot optionalImportMessageExtension]]); |
| 1423 XCTAssertFalse([message |
| 1424 hasExtension:[UnittestRoot optionalPublicImportMessageExtension]]); |
| 1425 XCTAssertFalse( |
| 1426 [message hasExtension:[UnittestRoot optionalLazyMessageExtension]]); |
| 1427 |
| 1428 // They should auto create something when fetched. |
| 1429 |
| 1430 TestAllTypes_OptionalGroup *optionalGroup = |
| 1431 [message getExtension:[UnittestRoot optionalGroupExtension]]; |
| 1432 TestAllTypes_NestedMessage *optionalNestedMessage = |
| 1433 [message getExtension:[UnittestRoot optionalNestedMessageExtension]]; |
| 1434 ForeignMessage *optionalForeignMessage = |
| 1435 [message getExtension:[UnittestRoot optionalForeignMessageExtension]]; |
| 1436 ImportMessage *optionalImportMessage = |
| 1437 [message getExtension:[UnittestRoot optionalImportMessageExtension]]; |
| 1438 PublicImportMessage *optionalPublicImportMessage = [message |
| 1439 getExtension:[UnittestRoot optionalPublicImportMessageExtension]]; |
| 1440 TestAllTypes_NestedMessage *optionalLazyMessage = |
| 1441 [message getExtension:[UnittestRoot optionalLazyMessageExtension]]; |
| 1442 |
| 1443 XCTAssertNotNil(optionalGroup); |
| 1444 XCTAssertNotNil(optionalNestedMessage); |
| 1445 XCTAssertNotNil(optionalForeignMessage); |
| 1446 XCTAssertNotNil(optionalImportMessage); |
| 1447 XCTAssertNotNil(optionalPublicImportMessage); |
| 1448 XCTAssertNotNil(optionalLazyMessage); |
| 1449 |
| 1450 // Although it auto-created empty messages, it should not show that it has |
| 1451 // them. |
| 1452 |
| 1453 XCTAssertFalse([message hasExtension:[UnittestRoot optionalGroupExtension]]); |
| 1454 XCTAssertFalse([message hasExtension:[UnittestRoot optionalGroupExtension]]); |
| 1455 XCTAssertFalse([message hasExtension:[UnittestRoot optionalNestedMessageExtens
ion]]); |
| 1456 XCTAssertFalse([message hasExtension:[UnittestRoot optionalForeignMessageExten
sion]]); |
| 1457 XCTAssertFalse([message hasExtension:[UnittestRoot optionalImportMessageExtens
ion]]); |
| 1458 XCTAssertFalse([message hasExtension:[UnittestRoot optionalPublicImportMessage
Extension]]); |
| 1459 XCTAssertFalse([message hasExtension:[UnittestRoot optionalLazyMessageExtensio
n]]); |
| 1460 |
| 1461 // And they set that value back in to the message since the value created was |
| 1462 // mutable (so a second fetch should give the same object). |
| 1463 |
| 1464 XCTAssertEqual([message getExtension:[UnittestRoot optionalGroupExtension]], |
| 1465 optionalGroup); |
| 1466 XCTAssertEqual( |
| 1467 [message getExtension:[UnittestRoot optionalNestedMessageExtension]], |
| 1468 optionalNestedMessage); |
| 1469 XCTAssertEqual( |
| 1470 [message getExtension:[UnittestRoot optionalForeignMessageExtension]], |
| 1471 optionalForeignMessage); |
| 1472 XCTAssertEqual( |
| 1473 [message getExtension:[UnittestRoot optionalImportMessageExtension]], |
| 1474 optionalImportMessage); |
| 1475 XCTAssertEqual( |
| 1476 [message getExtension:[UnittestRoot optionalPublicImportMessageExtension]]
, |
| 1477 optionalPublicImportMessage); |
| 1478 XCTAssertEqual( |
| 1479 [message getExtension:[UnittestRoot optionalLazyMessageExtension]], |
| 1480 optionalLazyMessage); |
| 1481 |
| 1482 // And the default objects for a second message should be distinct (again, |
| 1483 // since they are mutable, each needs their own copy). |
| 1484 |
| 1485 TestAllExtensions *message2 = [TestAllExtensions message]; |
| 1486 |
| 1487 // Intentionally doing a pointer comparison. |
| 1488 XCTAssertNotEqual( |
| 1489 [message2 getExtension:[UnittestRoot optionalGroupExtension]], |
| 1490 optionalGroup); |
| 1491 XCTAssertNotEqual( |
| 1492 [message2 getExtension:[UnittestRoot optionalNestedMessageExtension]], |
| 1493 optionalNestedMessage); |
| 1494 XCTAssertNotEqual( |
| 1495 [message2 getExtension:[UnittestRoot optionalForeignMessageExtension]], |
| 1496 optionalForeignMessage); |
| 1497 XCTAssertNotEqual( |
| 1498 [message2 getExtension:[UnittestRoot optionalImportMessageExtension]], |
| 1499 optionalImportMessage); |
| 1500 XCTAssertNotEqual( |
| 1501 [message2 getExtension:[UnittestRoot optionalPublicImportMessageExtension]
], |
| 1502 optionalPublicImportMessage); |
| 1503 XCTAssertNotEqual( |
| 1504 [message2 getExtension:[UnittestRoot optionalLazyMessageExtension]], |
| 1505 optionalLazyMessage); |
| 1506 |
| 1507 // Clear values, and on next access you get back new submessages. |
| 1508 |
| 1509 [message setExtension:[UnittestRoot optionalGroupExtension] value:nil]; |
| 1510 [message setExtension:[UnittestRoot optionalGroupExtension] value:nil]; |
| 1511 [message setExtension:[UnittestRoot optionalNestedMessageExtension] |
| 1512 value:nil]; |
| 1513 [message setExtension:[UnittestRoot optionalForeignMessageExtension] |
| 1514 value:nil]; |
| 1515 [message setExtension:[UnittestRoot optionalImportMessageExtension] |
| 1516 value:nil]; |
| 1517 [message setExtension:[UnittestRoot optionalPublicImportMessageExtension] |
| 1518 value:nil]; |
| 1519 [message setExtension:[UnittestRoot optionalLazyMessageExtension] value:nil]; |
| 1520 |
| 1521 XCTAssertFalse([message hasExtension:[UnittestRoot optionalGroupExtension]]); |
| 1522 XCTAssertFalse([message hasExtension:[UnittestRoot optionalGroupExtension]]); |
| 1523 XCTAssertFalse( |
| 1524 [message hasExtension:[UnittestRoot optionalNestedMessageExtension]]); |
| 1525 XCTAssertFalse( |
| 1526 [message hasExtension:[UnittestRoot optionalForeignMessageExtension]]); |
| 1527 XCTAssertFalse( |
| 1528 [message hasExtension:[UnittestRoot optionalImportMessageExtension]]); |
| 1529 XCTAssertFalse([message |
| 1530 hasExtension:[UnittestRoot optionalPublicImportMessageExtension]]); |
| 1531 XCTAssertFalse( |
| 1532 [message hasExtension:[UnittestRoot optionalLazyMessageExtension]]); |
| 1533 |
| 1534 XCTAssertEqual([message getExtension:[UnittestRoot optionalGroupExtension]], |
| 1535 optionalGroup); |
| 1536 XCTAssertEqual( |
| 1537 [message getExtension:[UnittestRoot optionalNestedMessageExtension]], |
| 1538 optionalNestedMessage); |
| 1539 XCTAssertEqual( |
| 1540 [message getExtension:[UnittestRoot optionalForeignMessageExtension]], |
| 1541 optionalForeignMessage); |
| 1542 XCTAssertEqual( |
| 1543 [message getExtension:[UnittestRoot optionalImportMessageExtension]], |
| 1544 optionalImportMessage); |
| 1545 XCTAssertEqual( |
| 1546 [message |
| 1547 getExtension:[UnittestRoot optionalPublicImportMessageExtension]], |
| 1548 optionalPublicImportMessage); |
| 1549 XCTAssertEqual( |
| 1550 [message getExtension:[UnittestRoot optionalLazyMessageExtension]], |
| 1551 optionalLazyMessage); |
| 1552 } |
| 1553 |
| 1554 - (void)testMultiplePointersToAutocreatedExtension { |
| 1555 // 2 objects point to the same auto-created extension. One should "has" it. |
| 1556 // The other should not. |
| 1557 TestAllExtensions *message = [TestAllExtensions message]; |
| 1558 TestAllExtensions *message2 = [TestAllExtensions message]; |
| 1559 GPBExtensionDescriptor *extension = [UnittestRoot optionalGroupExtension]; |
| 1560 [message setExtension:extension value:[message2 getExtension:extension]]; |
| 1561 XCTAssertEqual([message getExtension:extension], |
| 1562 [message2 getExtension:extension]); |
| 1563 XCTAssertFalse([message2 hasExtension:extension]); |
| 1564 XCTAssertTrue([message hasExtension:extension]); |
| 1565 |
| 1566 TestAllTypes_OptionalGroup *extensionValue = |
| 1567 [message2 getExtension:extension]; |
| 1568 extensionValue.a = 1; |
| 1569 XCTAssertTrue([message2 hasExtension:extension]); |
| 1570 XCTAssertTrue([message hasExtension:extension]); |
| 1571 } |
| 1572 |
| 1573 - (void)testCopyWithAutocreatedExtension { |
| 1574 // Mutable copy shouldn't copy autocreated extensions. |
| 1575 TestAllExtensions *message = [TestAllExtensions message]; |
| 1576 GPBExtensionDescriptor *optionalGroupExtension = |
| 1577 [UnittestRoot optionalGroupExtension]; |
| 1578 GPBExtensionDescriptor *optionalNestedMessageExtesion = |
| 1579 [UnittestRoot optionalNestedMessageExtension]; |
| 1580 TestAllTypes_OptionalGroup *optionalGroup = |
| 1581 [message getExtension:optionalGroupExtension]; |
| 1582 optionalGroup.a = 42; |
| 1583 XCTAssertNotNil(optionalGroup); |
| 1584 XCTAssertNotNil([message getExtension:optionalNestedMessageExtesion]); |
| 1585 XCTAssertTrue([message hasExtension:optionalGroupExtension]); |
| 1586 XCTAssertFalse([message hasExtension:optionalNestedMessageExtesion]); |
| 1587 |
| 1588 TestAllExtensions *message2 = [[message copy] autorelease]; |
| 1589 |
| 1590 // message2 should end up with its own copy of the optional group. |
| 1591 XCTAssertTrue([message2 hasExtension:optionalGroupExtension]); |
| 1592 XCTAssertEqualObjects([message getExtension:optionalGroupExtension], |
| 1593 [message2 getExtension:optionalGroupExtension]); |
| 1594 // Intentionally doing a pointer comparison. |
| 1595 XCTAssertNotEqual([message getExtension:optionalGroupExtension], |
| 1596 [message2 getExtension:optionalGroupExtension]); |
| 1597 |
| 1598 XCTAssertFalse([message2 hasExtension:optionalNestedMessageExtesion]); |
| 1599 // Intentionally doing a pointer comparison (auto creation should be |
| 1600 // different) |
| 1601 XCTAssertNotEqual([message getExtension:optionalNestedMessageExtesion], |
| 1602 [message2 getExtension:optionalNestedMessageExtesion]); |
| 1603 } |
| 1604 |
| 1605 - (void)testClearMessageAutocreatedExtension { |
| 1606 // Call clear should cause it to recreate its autocreated extensions. |
| 1607 TestAllExtensions *message = [TestAllExtensions message]; |
| 1608 GPBExtensionDescriptor *optionalGroupExtension = |
| 1609 [UnittestRoot optionalGroupExtension]; |
| 1610 TestAllTypes_OptionalGroup *optionalGroup = |
| 1611 [[message getExtension:optionalGroupExtension] retain]; |
| 1612 [message clear]; |
| 1613 TestAllTypes_OptionalGroup *optionalGroupNew = |
| 1614 [message getExtension:optionalGroupExtension]; |
| 1615 |
| 1616 // Intentionally doing a pointer comparison. |
| 1617 XCTAssertNotEqual(optionalGroup, optionalGroupNew); |
| 1618 [optionalGroup release]; |
| 1619 } |
| 1620 |
| 1621 - (void)testRetainAutocreatedExtension { |
| 1622 // Should be able to retain autocreated extension while the creator is |
| 1623 // dealloced. |
| 1624 TestAllExtensions *message = [TestAllExtensions message]; |
| 1625 GPBExtensionDescriptor *optionalGroupExtension = |
| 1626 [UnittestRoot optionalGroupExtension]; |
| 1627 |
| 1628 @autoreleasepool { |
| 1629 TestAllExtensions *message2 = [TestAllExtensions message]; |
| 1630 [message setExtension:optionalGroupExtension |
| 1631 value:[message2 getExtension:optionalGroupExtension]]; |
| 1632 XCTAssertTrue(GPBWasMessageAutocreatedBy( |
| 1633 [message getExtension:optionalGroupExtension], message2)); |
| 1634 } |
| 1635 |
| 1636 XCTAssertFalse(GPBWasMessageAutocreatedBy( |
| 1637 [message getExtension:optionalGroupExtension], message)); |
| 1638 } |
| 1639 |
| 1640 - (void)testClearAutocreatedExtension { |
| 1641 // Clearing autocreated extension should NOT cause it to lose its creator. |
| 1642 TestAllExtensions *message = [TestAllExtensions message]; |
| 1643 GPBExtensionDescriptor *optionalGroupExtension = |
| 1644 [UnittestRoot optionalGroupExtension]; |
| 1645 TestAllTypes_OptionalGroup *optionalGroup = |
| 1646 [[message getExtension:optionalGroupExtension] retain]; |
| 1647 [message clearExtension:optionalGroupExtension]; |
| 1648 TestAllTypes_OptionalGroup *optionalGroupNew = |
| 1649 [message getExtension:optionalGroupExtension]; |
| 1650 XCTAssertEqual(optionalGroup, optionalGroupNew); |
| 1651 XCTAssertFalse([message hasExtension:optionalGroupExtension]); |
| 1652 [optionalGroup release]; |
| 1653 |
| 1654 // Clearing autocreated extension should not cause its creator to become |
| 1655 // visible |
| 1656 GPBExtensionDescriptor *recursiveExtension = |
| 1657 [UnittestObjcRoot recursiveExtension]; |
| 1658 TestAllExtensions *message_lvl2 = [message getExtension:recursiveExtension]; |
| 1659 TestAllExtensions *message_lvl3 = |
| 1660 [message_lvl2 getExtension:recursiveExtension]; |
| 1661 [message_lvl3 clearExtension:recursiveExtension]; |
| 1662 XCTAssertFalse([message hasExtension:recursiveExtension]); |
| 1663 } |
| 1664 |
| 1665 - (void)testSetAutocreatedExtensionBecomesVisible { |
| 1666 // Setting an extension should cause the extension to appear to its creator. |
| 1667 // Test this several levels deep. |
| 1668 TestAllExtensions *message = [TestAllExtensions message]; |
| 1669 GPBExtensionDescriptor *recursiveExtension = |
| 1670 [UnittestObjcRoot recursiveExtension]; |
| 1671 TestAllExtensions *message_lvl2 = [message getExtension:recursiveExtension]; |
| 1672 TestAllExtensions *message_lvl3 = |
| 1673 [message_lvl2 getExtension:recursiveExtension]; |
| 1674 TestAllExtensions *message_lvl4 = |
| 1675 [message_lvl3 getExtension:recursiveExtension]; |
| 1676 XCTAssertFalse([message hasExtension:recursiveExtension]); |
| 1677 XCTAssertFalse([message_lvl2 hasExtension:recursiveExtension]); |
| 1678 XCTAssertFalse([message_lvl3 hasExtension:recursiveExtension]); |
| 1679 XCTAssertFalse([message_lvl4 hasExtension:recursiveExtension]); |
| 1680 [message_lvl4 setExtension:[UnittestRoot optionalInt32Extension] value:@(1)]; |
| 1681 XCTAssertTrue([message hasExtension:recursiveExtension]); |
| 1682 XCTAssertTrue([message_lvl2 hasExtension:recursiveExtension]); |
| 1683 XCTAssertTrue([message_lvl3 hasExtension:recursiveExtension]); |
| 1684 XCTAssertFalse([message_lvl4 hasExtension:recursiveExtension]); |
| 1685 XCTAssertFalse(GPBWasMessageAutocreatedBy(message_lvl4, message_lvl3)); |
| 1686 XCTAssertFalse(GPBWasMessageAutocreatedBy(message_lvl3, message_lvl2)); |
| 1687 XCTAssertFalse(GPBWasMessageAutocreatedBy(message_lvl2, message)); |
| 1688 } |
| 1689 |
| 1690 - (void)testSetAutocreatedExtensionToSelf { |
| 1691 // Setting extension to itself should cause it to become visible. |
| 1692 TestAllExtensions *message = [TestAllExtensions message]; |
| 1693 GPBExtensionDescriptor *optionalGroupExtension = |
| 1694 [UnittestRoot optionalGroupExtension]; |
| 1695 XCTAssertNotNil([message getExtension:optionalGroupExtension]); |
| 1696 XCTAssertFalse([message hasExtension:optionalGroupExtension]); |
| 1697 [message setExtension:optionalGroupExtension |
| 1698 value:[message getExtension:optionalGroupExtension]]; |
| 1699 XCTAssertTrue([message hasExtension:optionalGroupExtension]); |
| 1700 } |
| 1701 |
| 1702 - (void)testAutocreatedExtensionMemoryLeaks { |
| 1703 GPBExtensionDescriptor *recursiveExtension = |
| 1704 [UnittestObjcRoot recursiveExtension]; |
| 1705 |
| 1706 // Test for memory leaks with autocreated extensions. |
| 1707 TestAllExtensions *message; |
| 1708 TestAllExtensions *message_lvl2; |
| 1709 TestAllExtensions *message_lvl3; |
| 1710 TestAllExtensions *message_lvl4; |
| 1711 @autoreleasepool { |
| 1712 message = [[TestAllExtensions alloc] init]; |
| 1713 message_lvl2 = [[message getExtension:recursiveExtension] retain]; |
| 1714 message_lvl3 = [[message_lvl2 getExtension:recursiveExtension] retain]; |
| 1715 message_lvl4 = [[message_lvl3 getExtension:recursiveExtension] retain]; |
| 1716 [message_lvl2 setExtension:[UnittestRoot optionalInt32Extension] |
| 1717 value:@(1)]; |
| 1718 } |
| 1719 |
| 1720 XCTAssertEqual(message.retainCount, (NSUInteger)1); |
| 1721 @autoreleasepool { |
| 1722 [message release]; |
| 1723 } |
| 1724 XCTAssertEqual(message_lvl2.retainCount, (NSUInteger)1); |
| 1725 @autoreleasepool { |
| 1726 [message_lvl2 release]; |
| 1727 } |
| 1728 XCTAssertEqual(message_lvl3.retainCount, (NSUInteger)1); |
| 1729 @autoreleasepool { |
| 1730 [message_lvl3 release]; |
| 1731 } |
| 1732 XCTAssertEqual(message_lvl4.retainCount, (NSUInteger)1); |
| 1733 [message_lvl4 release]; |
| 1734 } |
| 1735 |
| 1736 - (void)testSetExtensionWithAutocreatedValue { |
| 1737 GPBExtensionDescriptor *recursiveExtension = |
| 1738 [UnittestObjcRoot recursiveExtension]; |
| 1739 |
| 1740 TestAllExtensions *message; |
| 1741 @autoreleasepool { |
| 1742 message = [[TestAllExtensions alloc] init]; |
| 1743 [message getExtension:recursiveExtension]; |
| 1744 } |
| 1745 |
| 1746 // This statements checks that the extension value isn't accidentally |
| 1747 // dealloced when removing it from the autocreated map. |
| 1748 [message setExtension:recursiveExtension |
| 1749 value:[message getExtension:recursiveExtension]]; |
| 1750 XCTAssertTrue([message hasExtension:recursiveExtension]); |
| 1751 [message release]; |
| 1752 } |
| 1753 |
| 1754 - (void)testRecursion { |
| 1755 TestRecursiveMessage *message = [TestRecursiveMessage message]; |
| 1756 XCTAssertNotNil(message.a); |
| 1757 XCTAssertNotNil(message.a.a); |
| 1758 XCTAssertEqual(message.a.a.i, 0); |
| 1759 } |
| 1760 |
| 1761 - (void)testGenerateAndParseUnknownMessage { |
| 1762 GPBUnknownFieldSet *unknowns = |
| 1763 [[[GPBUnknownFieldSet alloc] init] autorelease]; |
| 1764 [unknowns mergeVarintField:123 value:456]; |
| 1765 GPBMessage *message = [GPBMessage message]; |
| 1766 [message setUnknownFields:unknowns]; |
| 1767 NSData *data = [message data]; |
| 1768 GPBMessage *message2 = |
| 1769 [GPBMessage parseFromData:data extensionRegistry:nil error:NULL]; |
| 1770 XCTAssertEqualObjects(message, message2); |
| 1771 } |
| 1772 |
| 1773 - (void)testDelimitedWriteAndParseMultipleMessages { |
| 1774 GPBUnknownFieldSet *unknowns1 = |
| 1775 [[[GPBUnknownFieldSet alloc] init] autorelease]; |
| 1776 [unknowns1 mergeVarintField:123 value:456]; |
| 1777 GPBMessage *message1 = [GPBMessage message]; |
| 1778 [message1 setUnknownFields:unknowns1]; |
| 1779 |
| 1780 GPBUnknownFieldSet *unknowns2 = |
| 1781 [[[GPBUnknownFieldSet alloc] init] autorelease]; |
| 1782 [unknowns2 mergeVarintField:789 value:987]; |
| 1783 [unknowns2 mergeVarintField:654 value:321]; |
| 1784 GPBMessage *message2 = [GPBMessage message]; |
| 1785 [message2 setUnknownFields:unknowns2]; |
| 1786 |
| 1787 NSMutableData *delimitedData = [NSMutableData data]; |
| 1788 [delimitedData appendData:[message1 delimitedData]]; |
| 1789 [delimitedData appendData:[message2 delimitedData]]; |
| 1790 GPBCodedInputStream *input = |
| 1791 [GPBCodedInputStream streamWithData:delimitedData]; |
| 1792 GPBMessage *message3 = [GPBMessage parseDelimitedFromCodedInputStream:input |
| 1793 extensionRegistry:nil |
| 1794 error:NULL]; |
| 1795 GPBMessage *message4 = [GPBMessage parseDelimitedFromCodedInputStream:input |
| 1796 extensionRegistry:nil |
| 1797 error:NULL]; |
| 1798 XCTAssertEqualObjects(message1, message3); |
| 1799 XCTAssertEqualObjects(message2, message4); |
| 1800 } |
| 1801 |
| 1802 - (void)testDuplicateEnums { |
| 1803 XCTAssertEqual(TestEnumWithDupValue_Foo1, TestEnumWithDupValue_Foo2); |
| 1804 } |
| 1805 |
| 1806 - (void)testWeirdDefaults { |
| 1807 ObjcWeirdDefaults *message = [ObjcWeirdDefaults message]; |
| 1808 GPBDescriptor *descriptor = [[message class] descriptor]; |
| 1809 GPBFieldDescriptor *fieldDesc = [descriptor fieldWithName:@"foo"]; |
| 1810 XCTAssertNotNil(fieldDesc); |
| 1811 XCTAssertTrue(fieldDesc.hasDefaultValue); |
| 1812 XCTAssertFalse(message.hasFoo); |
| 1813 XCTAssertEqualObjects(message.foo, @""); |
| 1814 |
| 1815 fieldDesc = [descriptor fieldWithName:@"bar"]; |
| 1816 XCTAssertNotNil(fieldDesc); |
| 1817 XCTAssertTrue(fieldDesc.hasDefaultValue); |
| 1818 XCTAssertFalse(message.hasBar); |
| 1819 XCTAssertEqualObjects(message.bar, GPBEmptyNSData()); |
| 1820 } |
| 1821 |
| 1822 - (void)testEnumDescriptorFromExtensionDescriptor { |
| 1823 GPBExtensionDescriptor *extDescriptor = |
| 1824 [UnittestRoot optionalForeignEnumExtension]; |
| 1825 XCTAssertEqual(extDescriptor.dataType, GPBDataTypeEnum); |
| 1826 GPBEnumDescriptor *enumDescriptor = extDescriptor.enumDescriptor; |
| 1827 GPBEnumDescriptor *expectedDescriptor = ForeignEnum_EnumDescriptor(); |
| 1828 XCTAssertEqualObjects(enumDescriptor, expectedDescriptor); |
| 1829 } |
| 1830 |
| 1831 - (void)testEnumNaming { |
| 1832 // objectivec_helpers.cc has some interesting cases to deal with in |
| 1833 // EnumValueName/EnumValueShortName. Confirm that things generated as |
| 1834 // expected. |
| 1835 |
| 1836 // This block just has to compile to confirm we got the expected types/names. |
| 1837 // The *_IsValidValue() calls are just there to keep the projects warnings |
| 1838 // flags happy by providing use of the variables/values. |
| 1839 |
| 1840 Foo aFoo = Foo_SerializedSize; |
| 1841 Foo_IsValidValue(aFoo); |
| 1842 aFoo = Foo_Size; |
| 1843 Foo_IsValidValue(aFoo); |
| 1844 |
| 1845 Category_Enum aCat = Category_Enum_Red; |
| 1846 Category_Enum_IsValidValue(aCat); |
| 1847 |
| 1848 Time aTime = Time_Base; |
| 1849 Time_IsValidValue(aTime); |
| 1850 aTime = Time_SomethingElse; |
| 1851 Time_IsValidValue(aTime); |
| 1852 |
| 1853 // This block confirms the names in the decriptors is what we wanted. |
| 1854 |
| 1855 GPBEnumDescriptor *descriptor; |
| 1856 NSString *valueName; |
| 1857 |
| 1858 descriptor = Foo_EnumDescriptor(); |
| 1859 XCTAssertNotNil(descriptor); |
| 1860 XCTAssertEqualObjects(@"Foo", descriptor.name); |
| 1861 valueName = [descriptor enumNameForValue:Foo_SerializedSize]; |
| 1862 XCTAssertEqualObjects(@"Foo_SerializedSize", valueName); |
| 1863 valueName = [descriptor enumNameForValue:Foo_Size]; |
| 1864 XCTAssertEqualObjects(@"Foo_Size", valueName); |
| 1865 |
| 1866 descriptor = Category_Enum_EnumDescriptor(); |
| 1867 XCTAssertNotNil(descriptor); |
| 1868 XCTAssertEqualObjects(@"Category_Enum", descriptor.name); |
| 1869 valueName = [descriptor enumNameForValue:Category_Enum_Red]; |
| 1870 XCTAssertEqualObjects(@"Category_Enum_Red", valueName); |
| 1871 |
| 1872 descriptor = Time_EnumDescriptor(); |
| 1873 XCTAssertNotNil(descriptor); |
| 1874 XCTAssertEqualObjects(@"Time", descriptor.name); |
| 1875 valueName = [descriptor enumNameForValue:Time_Base]; |
| 1876 XCTAssertEqualObjects(@"Time_Base", valueName); |
| 1877 valueName = [descriptor enumNameForValue:Time_SomethingElse]; |
| 1878 XCTAssertEqualObjects(@"Time_SomethingElse", valueName); |
| 1879 } |
| 1880 |
| 1881 - (void)testNegativeEnums { |
| 1882 EnumTestMsg *msg = [EnumTestMsg message]; |
| 1883 |
| 1884 // Defaults |
| 1885 XCTAssertEqual(msg.foo, EnumTestMsg_MyEnum_Zero); |
| 1886 XCTAssertEqual(msg.bar, EnumTestMsg_MyEnum_One); |
| 1887 XCTAssertEqual(msg.baz, EnumTestMsg_MyEnum_NegOne); |
| 1888 // Bounce to wire and back. |
| 1889 NSData *data = [msg data]; |
| 1890 XCTAssertNotNil(data); |
| 1891 EnumTestMsg *msgPrime = [EnumTestMsg parseFromData:data error:NULL]; |
| 1892 XCTAssertEqualObjects(msgPrime, msg); |
| 1893 XCTAssertEqual(msgPrime.foo, EnumTestMsg_MyEnum_Zero); |
| 1894 XCTAssertEqual(msgPrime.bar, EnumTestMsg_MyEnum_One); |
| 1895 XCTAssertEqual(msgPrime.baz, EnumTestMsg_MyEnum_NegOne); |
| 1896 |
| 1897 // Other values |
| 1898 msg.bar = EnumTestMsg_MyEnum_Two; |
| 1899 msg.baz = EnumTestMsg_MyEnum_NegTwo; |
| 1900 XCTAssertEqual(msg.bar, EnumTestMsg_MyEnum_Two); |
| 1901 XCTAssertEqual(msg.baz, EnumTestMsg_MyEnum_NegTwo); |
| 1902 // Bounce to wire and back. |
| 1903 data = [msg data]; |
| 1904 XCTAssertNotNil(data); |
| 1905 msgPrime = [EnumTestMsg parseFromData:data error:NULL]; |
| 1906 XCTAssertEqualObjects(msgPrime, msg); |
| 1907 XCTAssertEqual(msgPrime.foo, EnumTestMsg_MyEnum_Zero); |
| 1908 XCTAssertEqual(msgPrime.bar, EnumTestMsg_MyEnum_Two); |
| 1909 XCTAssertEqual(msgPrime.baz, EnumTestMsg_MyEnum_NegTwo); |
| 1910 |
| 1911 // Repeated field (shouldn't ever be an issue since developer has to use the |
| 1912 // right GPBArray methods themselves). |
| 1913 msg.mumbleArray = [GPBEnumArray |
| 1914 arrayWithValidationFunction:EnumTestMsg_MyEnum_IsValidValue]; |
| 1915 [msg.mumbleArray addValue:EnumTestMsg_MyEnum_Zero]; |
| 1916 [msg.mumbleArray addValue:EnumTestMsg_MyEnum_One]; |
| 1917 [msg.mumbleArray addValue:EnumTestMsg_MyEnum_Two]; |
| 1918 [msg.mumbleArray addValue:EnumTestMsg_MyEnum_NegOne]; |
| 1919 [msg.mumbleArray addValue:EnumTestMsg_MyEnum_NegTwo]; |
| 1920 XCTAssertEqual([msg.mumbleArray valueAtIndex:0], EnumTestMsg_MyEnum_Zero); |
| 1921 XCTAssertEqual([msg.mumbleArray valueAtIndex:1], EnumTestMsg_MyEnum_One); |
| 1922 XCTAssertEqual([msg.mumbleArray valueAtIndex:2], EnumTestMsg_MyEnum_Two); |
| 1923 XCTAssertEqual([msg.mumbleArray valueAtIndex:3], EnumTestMsg_MyEnum_NegOne); |
| 1924 XCTAssertEqual([msg.mumbleArray valueAtIndex:4], EnumTestMsg_MyEnum_NegTwo); |
| 1925 // Bounce to wire and back. |
| 1926 data = [msg data]; |
| 1927 XCTAssertNotNil(data); |
| 1928 msgPrime = [EnumTestMsg parseFromData:data error:NULL]; |
| 1929 XCTAssertEqualObjects(msgPrime, msg); |
| 1930 XCTAssertEqual([msgPrime.mumbleArray valueAtIndex:0], |
| 1931 EnumTestMsg_MyEnum_Zero); |
| 1932 XCTAssertEqual([msgPrime.mumbleArray valueAtIndex:1], EnumTestMsg_MyEnum_One); |
| 1933 XCTAssertEqual([msgPrime.mumbleArray valueAtIndex:2], EnumTestMsg_MyEnum_Two); |
| 1934 XCTAssertEqual([msgPrime.mumbleArray valueAtIndex:3], |
| 1935 EnumTestMsg_MyEnum_NegOne); |
| 1936 XCTAssertEqual([msgPrime.mumbleArray valueAtIndex:4], |
| 1937 EnumTestMsg_MyEnum_NegTwo); |
| 1938 } |
| 1939 |
| 1940 @end |
OLD | NEW |