// Protocol Buffers - Google's data interchange format // Copyright 2008 Google Inc. All rights reserved. // // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file or at // https://developers.google.com/open-source/licenses/bsd #import "GPBTestUtilities.h" #import #import "GPBArray_PackagePrivate.h" #import "GPBDescriptor.h" #import "GPBDictionary_PackagePrivate.h" #import "GPBMessage_PackagePrivate.h" #import "GPBUnknownFieldSet_PackagePrivate.h" #import "GPBUnknownField_PackagePrivate.h" #import "objectivec/Tests/Unittest.pbobjc.h" #import "objectivec/Tests/UnittestImport.pbobjc.h" #import "objectivec/Tests/UnittestObjc.pbobjc.h" #import "objectivec/Tests/UnittestObjcOptions.pbobjc.h" // Helper class to test KVO. @interface GPBKVOTestObserver : NSObject { id observee_; NSString *keyPath_; } @property(nonatomic) BOOL didObserve; - (id)initWithObservee:(id)observee keyPath:(NSString *)keyPath; @end @implementation GPBKVOTestObserver @synthesize didObserve; - (id)initWithObservee:(id)observee keyPath:(NSString *)keyPath { if (self = [super init]) { observee_ = [observee retain]; keyPath_ = [keyPath copy]; [observee_ addObserver:self forKeyPath:keyPath_ options:0 context:NULL]; } return self; } - (void)dealloc { [observee_ removeObserver:self forKeyPath:keyPath_]; [observee_ release]; [keyPath_ release]; [super dealloc]; } - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(__unused id)object change:(__unused NSDictionary *)change context:(__unused void *)context { if ([keyPath isEqualToString:keyPath_]) { self.didObserve = YES; } } @end @interface MessageTests : GPBTestCase @end @implementation MessageTests // TODO: this should get split into a few files of logic junks, it is // a jumble of things at the moment (and the testutils have a bunch of the real // assertions). - (TestAllTypes *)mergeSource { TestAllTypes *message = [TestAllTypes message]; [message setOptionalInt32:1]; [message setOptionalString:@"foo"]; [message setOptionalForeignMessage:[ForeignMessage message]]; [message.repeatedStringArray addObject:@"bar"]; return message; } - (TestAllTypes *)mergeDestination { TestAllTypes *message = [TestAllTypes message]; [message setOptionalInt64:2]; [message setOptionalString:@"baz"]; ForeignMessage *foreignMessage = [ForeignMessage message]; [foreignMessage setC:3]; [message setOptionalForeignMessage:foreignMessage]; [message.repeatedStringArray addObject:@"qux"]; return message; } - (TestAllTypes *)mergeDestinationWithoutForeignMessageIvar { TestAllTypes *message = [TestAllTypes message]; [message setOptionalInt64:2]; [message setOptionalString:@"baz"]; [message.repeatedStringArray addObject:@"qux"]; return message; } - (TestAllTypes *)mergeResult { TestAllTypes *message = [TestAllTypes message]; [message setOptionalInt32:1]; [message setOptionalInt64:2]; [message setOptionalString:@"foo"]; ForeignMessage *foreignMessage = [ForeignMessage message]; [foreignMessage setC:3]; [message setOptionalForeignMessage:foreignMessage]; [message.repeatedStringArray addObject:@"qux"]; [message.repeatedStringArray addObject:@"bar"]; return message; } - (TestAllTypes *)mergeResultForDestinationWithoutForeignMessageIvar { TestAllTypes *message = [TestAllTypes message]; [message setOptionalInt32:1]; [message setOptionalInt64:2]; [message setOptionalString:@"foo"]; ForeignMessage *foreignMessage = [ForeignMessage message]; [message setOptionalForeignMessage:foreignMessage]; [message.repeatedStringArray addObject:@"qux"]; [message.repeatedStringArray addObject:@"bar"]; return message; } - (TestAllExtensions *)mergeExtensionsDestination { TestAllExtensions *message = [TestAllExtensions message]; [message setExtension:[UnittestRoot optionalInt32Extension] value:@5]; [message setExtension:[UnittestRoot optionalStringExtension] value:@"foo"]; ForeignMessage *foreignMessage = [ForeignMessage message]; foreignMessage.c = 4; [message setExtension:[UnittestRoot optionalForeignMessageExtension] value:foreignMessage]; TestAllTypes_NestedMessage *nestedMessage = [TestAllTypes_NestedMessage message]; [message setExtension:[UnittestRoot optionalNestedMessageExtension] value:nestedMessage]; return message; } - (TestAllExtensions *)mergeExtensionsSource { TestAllExtensions *message = [TestAllExtensions message]; [message setExtension:[UnittestRoot optionalInt64Extension] value:@6]; [message setExtension:[UnittestRoot optionalStringExtension] value:@"bar"]; ForeignMessage *foreignMessage = [ForeignMessage message]; [message setExtension:[UnittestRoot optionalForeignMessageExtension] value:foreignMessage]; TestAllTypes_NestedMessage *nestedMessage = [TestAllTypes_NestedMessage message]; nestedMessage.bb = 7; [message setExtension:[UnittestRoot optionalNestedMessageExtension] value:nestedMessage]; return message; } - (TestAllExtensions *)mergeExtensionsResult { TestAllExtensions *message = [TestAllExtensions message]; [message setExtension:[UnittestRoot optionalInt32Extension] value:@5]; [message setExtension:[UnittestRoot optionalInt64Extension] value:@6]; [message setExtension:[UnittestRoot optionalStringExtension] value:@"bar"]; ForeignMessage *foreignMessage = [ForeignMessage message]; foreignMessage.c = 4; [message setExtension:[UnittestRoot optionalForeignMessageExtension] value:foreignMessage]; TestAllTypes_NestedMessage *nestedMessage = [TestAllTypes_NestedMessage message]; nestedMessage.bb = 7; [message setExtension:[UnittestRoot optionalNestedMessageExtension] value:nestedMessage]; return message; } - (void)testMergeFrom { TestAllTypes *result = [[self.mergeDestination copy] autorelease]; [result mergeFrom:self.mergeSource]; NSData *resultData = [result data]; NSData *mergeResultData = [self.mergeResult data]; XCTAssertEqualObjects(resultData, mergeResultData); XCTAssertEqualObjects(result, self.mergeResult); // Test when destination does not have an Ivar (type is an object) but source // has such Ivar. // The result must has the Ivar which is same as the one in source. result = [[self.mergeDestinationWithoutForeignMessageIvar copy] autorelease]; [result mergeFrom:self.mergeSource]; resultData = [result data]; mergeResultData = [self.mergeResultForDestinationWithoutForeignMessageIvar data]; XCTAssertEqualObjects(resultData, mergeResultData); XCTAssertEqualObjects(result, self.mergeResultForDestinationWithoutForeignMessageIvar); // Test when destination is empty. // The result must is same as the source. result = [TestAllTypes message]; [result mergeFrom:self.mergeSource]; resultData = [result data]; mergeResultData = [self.mergeSource data]; XCTAssertEqualObjects(resultData, mergeResultData); XCTAssertEqualObjects(result, self.mergeSource); } - (void)testMergeFromWithExtensions { TestAllExtensions *result = [self mergeExtensionsDestination]; [result mergeFrom:[self mergeExtensionsSource]]; NSData *resultData = [result data]; NSData *mergeResultData = [[self mergeExtensionsResult] data]; XCTAssertEqualObjects(resultData, mergeResultData); XCTAssertEqualObjects(result, [self mergeExtensionsResult]); // Test merging from data. result = [self mergeExtensionsDestination]; NSData *data = [[self mergeExtensionsSource] data]; XCTAssertNotNil(data); NSError *error = nil; XCTAssertTrue([result mergeFromData:data extensionRegistry:[UnittestRoot extensionRegistry] error:&error]); XCTAssertNil(error); resultData = [result data]; XCTAssertEqualObjects(resultData, mergeResultData); XCTAssertEqualObjects(result, [self mergeExtensionsResult]); } - (void)testIsEquals { TestAllTypes *result = [[self.mergeDestination copy] autorelease]; [result mergeFrom:self.mergeSource]; XCTAssertEqualObjects(result.data, self.mergeResult.data); XCTAssertEqualObjects(result, self.mergeResult); TestAllTypes *result2 = [[self.mergeDestination copy] autorelease]; XCTAssertNotEqualObjects(result2.data, self.mergeResult.data); XCTAssertNotEqualObjects(result2, self.mergeResult); } // ================================================================= // Required-field-related tests. - (TestRequired *)testRequiredInitialized { TestRequired *message = [TestRequired message]; [message setA:1]; [message setB:2]; [message setC:3]; return message; } - (void)testRequired { TestRequired *message = [TestRequired message]; XCTAssertFalse(message.initialized); [message setA:1]; XCTAssertFalse(message.initialized); [message setB:1]; XCTAssertFalse(message.initialized); [message setC:1]; XCTAssertTrue(message.initialized); } - (void)testRequiredForeign { TestRequiredForeign *message = [TestRequiredForeign message]; XCTAssertTrue(message.initialized); [message setOptionalMessage:[TestRequired message]]; XCTAssertFalse(message.initialized); [message setOptionalMessage:self.testRequiredInitialized]; XCTAssertTrue(message.initialized); [message.repeatedMessageArray addObject:[TestRequired message]]; XCTAssertFalse(message.initialized); [message.repeatedMessageArray removeAllObjects]; [message.repeatedMessageArray addObject:self.testRequiredInitialized]; XCTAssertTrue(message.initialized); } - (void)testRequiredExtension { TestAllExtensions *message = [TestAllExtensions message]; XCTAssertTrue(message.initialized); [message setExtension:[TestRequired single] value:[TestRequired message]]; XCTAssertFalse(message.initialized); [message setExtension:[TestRequired single] value:self.testRequiredInitialized]; XCTAssertTrue(message.initialized); [message addExtension:[TestRequired multi] value:[TestRequired message]]; XCTAssertFalse(message.initialized); [message setExtension:[TestRequired multi] index:0 value:self.testRequiredInitialized]; XCTAssertTrue(message.initialized); } - (void)testDataFromUninitialized { TestRequired *message = [TestRequired message]; NSData *data = [message data]; // In DEBUG, the data generation will fail, but in non DEBUG, it passes // because the check isn't done (for speed). #ifdef DEBUG XCTAssertNil(data); #else XCTAssertNotNil(data); XCTAssertFalse(message.initialized); #endif // DEBUG } - (void)testInitialized { // We're mostly testing that no exception is thrown. TestRequired *message = [TestRequired message]; XCTAssertFalse(message.initialized); } - (void)testDataFromNestedUninitialized { TestRequiredForeign *message = [TestRequiredForeign message]; [message setOptionalMessage:[TestRequired message]]; [message.repeatedMessageArray addObject:[TestRequired message]]; [message.repeatedMessageArray addObject:[TestRequired message]]; NSData *data = [message data]; // In DEBUG, the data generation will fail, but in non DEBUG, it passes // because the check isn't done (for speed). #ifdef DEBUG XCTAssertNil(data); #else XCTAssertNotNil(data); XCTAssertFalse(message.initialized); #endif // DEBUG } - (void)testNestedInitialized { // We're mostly testing that no exception is thrown. TestRequiredForeign *message = [TestRequiredForeign message]; [message setOptionalMessage:[TestRequired message]]; [message.repeatedMessageArray addObject:[TestRequired message]]; [message.repeatedMessageArray addObject:[TestRequired message]]; XCTAssertFalse(message.initialized); } - (void)testParseUninitialized { NSError *error = nil; TestRequired *msg = [TestRequired parseFromData:GPBEmptyNSData() error:&error]; // In DEBUG, the parse will fail, but in non DEBUG, it passes because // the check isn't done (for speed). #ifdef DEBUG XCTAssertNil(msg); XCTAssertNotNil(error); XCTAssertEqualObjects(error.domain, GPBMessageErrorDomain); XCTAssertEqual(error.code, GPBMessageErrorCodeMissingRequiredField); #else XCTAssertNotNil(msg); XCTAssertNil(error); XCTAssertFalse(msg.initialized); #endif // DEBUG } #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" - (void)testCoding { GPBMessage *original = [self mergeResult]; NSData *data = [NSKeyedArchiver archivedDataWithRootObject:original]; id unarchivedObject = [NSKeyedUnarchiver unarchiveObjectWithData:data]; XCTAssertEqualObjects(unarchivedObject, original); // Intentionally doing a pointer comparison. XCTAssertNotEqual(unarchivedObject, original); } - (void)testSecureCoding { GPBMessage *original = [self mergeResult]; NSString *key = @"testing123"; NSMutableData *data = [NSMutableData data]; NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; [archiver setRequiresSecureCoding:YES]; [archiver encodeObject:original forKey:key]; [archiver finishEncoding]; NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; [unarchiver setRequiresSecureCoding:YES]; id unarchivedObject = [unarchiver decodeObjectOfClass:[GPBMessage class] forKey:key]; [unarchiver finishDecoding]; XCTAssertEqualObjects(unarchivedObject, original); // Intentionally doing a pointer comparison. XCTAssertNotEqual(unarchivedObject, original); } #pragma clang diagnostic pop - (void)testObjectReset { // Tests a failure where clearing out defaults values caused an over release. TestAllTypes *message = [TestAllTypes message]; message.hasOptionalNestedMessage = NO; [message setOptionalNestedMessage:[TestAllTypes_NestedMessage message]]; message.hasOptionalNestedMessage = NO; [message setOptionalNestedMessage:[TestAllTypes_NestedMessage message]]; [message setOptionalNestedMessage:nil]; message.hasOptionalNestedMessage = NO; } - (void)testSettingHasToYes { TestAllTypes *message = [TestAllTypes message]; XCTAssertThrows([message setHasOptionalNestedMessage:YES]); } - (void)testRoot { XCTAssertNotNil([UnittestRoot extensionRegistry]); } - (void)testGPBMessageSize { // See the note in GPBMessage_PackagePrivate.h about why we want to keep the // base instance size pointer size aligned. size_t messageSize = class_getInstanceSize([GPBMessage class]); XCTAssertEqual((messageSize % sizeof(void *)), (size_t)0, @"Base size isn't pointer size aligned"); // Since we add storage ourselves (see +allocWithZone: in GPBMessage), confirm // that the size of some generated classes is still the same as the base for // that logic to work as desired. size_t testMessageSize = class_getInstanceSize([TestAllTypes class]); XCTAssertEqual(testMessageSize, messageSize); } - (void)testInit { TestAllTypes *message = [TestAllTypes message]; [self assertClear:message]; } - (void)testAccessors { TestAllTypes *message = [TestAllTypes message]; [self setAllFields:message repeatedCount:kGPBDefaultRepeatCount]; [self assertAllFieldsSet:message repeatedCount:kGPBDefaultRepeatCount]; } - (void)testKVC_ValueForKey { TestAllTypes *message = [TestAllTypes message]; [self setAllFields:message repeatedCount:kGPBDefaultRepeatCount]; [self assertAllFieldsKVCMatch:message]; } - (void)testKVC_SetValue_ForKey { TestAllTypes *message = [TestAllTypes message]; [self setAllFieldsViaKVC:message repeatedCount:kGPBDefaultRepeatCount]; [self assertAllFieldsKVCMatch:message]; [self assertAllFieldsSet:message repeatedCount:kGPBDefaultRepeatCount]; [self assertAllFieldsKVCMatch:message]; } - (void)testKVOBasic { TestAllTypes *message = [TestAllTypes message]; GPBKVOTestObserver *observer = [[[GPBKVOTestObserver alloc] initWithObservee:message keyPath:@"optionalString"] autorelease]; XCTAssertFalse(observer.didObserve); message.defaultString = @"Hello"; XCTAssertFalse(observer.didObserve); message.optionalString = @"Hello"; XCTAssertTrue(observer.didObserve); } - (void)testKVOAutocreate { TestAllTypes *message = [TestAllTypes message]; GPBKVOTestObserver *autocreateObserver = [[[GPBKVOTestObserver alloc] initWithObservee:message keyPath:@"optionalImportMessage"] autorelease]; GPBKVOTestObserver *innerFieldObserver = [[[GPBKVOTestObserver alloc] initWithObservee:message keyPath:@"optionalImportMessage.d"] autorelease]; XCTAssertFalse(autocreateObserver.didObserve); XCTAssertFalse(innerFieldObserver.didObserve); int a = message.optionalImportMessage.d; XCTAssertEqual(a, 0); // Autocreation of fields is not observed by KVO when getting values. XCTAssertFalse(autocreateObserver.didObserve); XCTAssertFalse(innerFieldObserver.didObserve); message.optionalImportMessage.d = 2; // Autocreation of fields is not observed by KVO. // This is undefined behavior. The library makes no guarantees with regards // to KVO firing if an autocreation occurs as part of a setter. // This test exists just to be aware if the behavior changes. XCTAssertFalse(autocreateObserver.didObserve); // Values set inside of an autocreated field are observed. XCTAssertTrue(innerFieldObserver.didObserve); // Explicit setting of a message field is observed. message.optionalImportMessage = [ImportMessage message]; XCTAssertTrue(autocreateObserver.didObserve); } - (void)testDescription { // No real test, just exercise code TestAllTypes *message = [TestAllTypes message]; [self setAllFields:message repeatedCount:kGPBDefaultRepeatCount]; GPBUnknownFieldSet *unknownFields = [[[GPBUnknownFieldSet alloc] init] autorelease]; GPBUnknownField *field = [[[GPBUnknownField alloc] initWithNumber:2] autorelease]; [field addVarint:2]; [unknownFields addField:field]; field = [[[GPBUnknownField alloc] initWithNumber:3] autorelease]; [field addVarint:4]; [unknownFields addField:field]; [message setUnknownFields:unknownFields]; NSString *description = [message description]; XCTAssertGreaterThan([description length], 0U); GPBMessage *message2 = [TestAllExtensions message]; [message2 setExtension:[UnittestRoot optionalInt32Extension] value:@1]; [message2 addExtension:[UnittestRoot repeatedInt32Extension] value:@2]; description = [message2 description]; XCTAssertGreaterThan([description length], 0U); } - (void)testSetter { // Test to make sure that if we set a value that has a default value // with the default, that the has is set, and the value gets put into the // message correctly. TestAllTypes *message = [TestAllTypes message]; GPBDescriptor *descriptor = [[message class] descriptor]; XCTAssertNotNil(descriptor); GPBFieldDescriptor *fieldDescriptor = [descriptor fieldWithName:@"defaultInt32"]; XCTAssertNotNil(fieldDescriptor); GPBGenericValue defaultValue = [fieldDescriptor defaultValue]; [message setDefaultInt32:defaultValue.valueInt32]; XCTAssertTrue(message.hasDefaultInt32); XCTAssertEqual(message.defaultInt32, defaultValue.valueInt32); // Do the same thing with an object type. message = [TestAllTypes message]; fieldDescriptor = [descriptor fieldWithName:@"defaultString"]; XCTAssertNotNil(fieldDescriptor); defaultValue = [fieldDescriptor defaultValue]; [message setDefaultString:defaultValue.valueString]; XCTAssertTrue(message.hasDefaultString); XCTAssertEqualObjects(message.defaultString, defaultValue.valueString); // Test default string type. message = [TestAllTypes message]; XCTAssertEqualObjects(message.defaultString, @"hello"); XCTAssertFalse(message.hasDefaultString); fieldDescriptor = [descriptor fieldWithName:@"defaultString"]; XCTAssertNotNil(fieldDescriptor); defaultValue = [fieldDescriptor defaultValue]; [message setDefaultString:defaultValue.valueString]; XCTAssertEqualObjects(message.defaultString, @"hello"); XCTAssertTrue(message.hasDefaultString); [message setDefaultString:nil]; XCTAssertEqualObjects(message.defaultString, @"hello"); XCTAssertFalse(message.hasDefaultString); message.hasDefaultString = NO; XCTAssertFalse(message.hasDefaultString); XCTAssertEqualObjects(message.defaultString, @"hello"); // Test default bytes type. NSData *defaultBytes = [@"world" dataUsingEncoding:NSUTF8StringEncoding]; XCTAssertEqualObjects(message.defaultBytes, defaultBytes); XCTAssertFalse(message.hasDefaultString); fieldDescriptor = [descriptor fieldWithName:@"defaultBytes"]; XCTAssertNotNil(fieldDescriptor); defaultValue = [fieldDescriptor defaultValue]; [message setDefaultBytes:defaultValue.valueData]; XCTAssertEqualObjects(message.defaultBytes, defaultBytes); XCTAssertTrue(message.hasDefaultBytes); [message setDefaultBytes:nil]; XCTAssertEqualObjects(message.defaultBytes, defaultBytes); XCTAssertFalse(message.hasDefaultBytes); message.hasDefaultBytes = NO; XCTAssertFalse(message.hasDefaultBytes); XCTAssertEqualObjects(message.defaultBytes, defaultBytes); // Test optional string. XCTAssertFalse(message.hasOptionalString); XCTAssertEqualObjects(message.optionalString, @""); XCTAssertFalse(message.hasOptionalString); message.optionalString = nil; XCTAssertFalse(message.hasOptionalString); XCTAssertEqualObjects(message.optionalString, @""); NSString *string = @"string"; message.optionalString = string; XCTAssertEqualObjects(message.optionalString, string); XCTAssertTrue(message.hasOptionalString); message.optionalString = nil; XCTAssertFalse(message.hasOptionalString); XCTAssertEqualObjects(message.optionalString, @""); // Test optional data. XCTAssertFalse(message.hasOptionalBytes); XCTAssertEqualObjects(message.optionalBytes, GPBEmptyNSData()); XCTAssertFalse(message.hasOptionalBytes); message.optionalBytes = nil; XCTAssertFalse(message.hasOptionalBytes); XCTAssertEqualObjects(message.optionalBytes, GPBEmptyNSData()); NSData *data = [@"bytes" dataUsingEncoding:NSUTF8StringEncoding]; message.optionalBytes = data; XCTAssertEqualObjects(message.optionalBytes, data); XCTAssertTrue(message.hasOptionalBytes); message.optionalBytes = nil; XCTAssertFalse(message.hasOptionalBytes); XCTAssertEqualObjects(message.optionalBytes, GPBEmptyNSData()); // Test lazy message setting XCTAssertFalse(message.hasOptionalLazyMessage); XCTAssertNotNil(message.optionalLazyMessage); XCTAssertFalse(message.hasOptionalLazyMessage); message.hasOptionalLazyMessage = NO; XCTAssertFalse(message.hasOptionalLazyMessage); XCTAssertNotNil(message.optionalLazyMessage); XCTAssertFalse(message.hasOptionalLazyMessage); message.optionalLazyMessage = nil; XCTAssertFalse(message.hasOptionalLazyMessage); // Test nested messages XCTAssertFalse(message.hasOptionalLazyMessage); message.optionalLazyMessage.bb = 1; XCTAssertTrue(message.hasOptionalLazyMessage); XCTAssertEqual(message.optionalLazyMessage.bb, 1); XCTAssertNotNil(message.optionalLazyMessage); message.optionalLazyMessage = nil; XCTAssertFalse(message.hasOptionalLazyMessage); XCTAssertEqual(message.optionalLazyMessage.bb, 0); XCTAssertFalse(message.hasOptionalLazyMessage); XCTAssertNotNil(message.optionalLazyMessage); // -testDefaultSubMessages tests the "defaulting" handling of fields // containing messages. } - (void)testRepeatedSetters { TestAllTypes *message = [TestAllTypes message]; [self setAllFields:message repeatedCount:kGPBDefaultRepeatCount]; [self modifyRepeatedFields:message]; [self assertRepeatedFieldsModified:message repeatedCount:kGPBDefaultRepeatCount]; } - (void)testClear { TestAllTypes *message = [TestAllTypes message]; [self setAllFields:message repeatedCount:kGPBDefaultRepeatCount]; [self clearAllFields:message]; [self assertClear:message]; TestAllTypes *message2 = [TestAllTypes message]; XCTAssertEqualObjects(message, message2); } - (void)testClearKVC { TestAllTypes *message = [TestAllTypes message]; [self setAllFields:message repeatedCount:kGPBDefaultRepeatCount]; [self clearAllFields:message]; [self assertClear:message]; [self assertClearKVC:message]; } - (void)testClearExtension { // clearExtension() is not actually used in TestUtil, so try it manually. GPBMessage *message1 = [TestAllExtensions message]; [message1 setExtension:[UnittestRoot optionalInt32Extension] value:@1]; XCTAssertTrue([message1 hasExtension:[UnittestRoot optionalInt32Extension]]); [message1 clearExtension:[UnittestRoot optionalInt32Extension]]; XCTAssertFalse([message1 hasExtension:[UnittestRoot optionalInt32Extension]]); GPBMessage *message2 = [TestAllExtensions message]; [message2 addExtension:[UnittestRoot repeatedInt32Extension] value:@1]; XCTAssertEqual([[message2 getExtension:[UnittestRoot repeatedInt32Extension]] count], (NSUInteger)1); [message2 clearExtension:[UnittestRoot repeatedInt32Extension]]; XCTAssertEqual([[message2 getExtension:[UnittestRoot repeatedInt32Extension]] count], (NSUInteger)0); // Clearing an unset extension field shouldn't make the target message // visible. GPBMessage *message3 = [TestAllExtensions message]; GPBMessage *extension_msg = [message3 getExtension:[UnittestObjcRoot recursiveExtension]]; XCTAssertFalse([message3 hasExtension:[UnittestObjcRoot recursiveExtension]]); [extension_msg clearExtension:[UnittestRoot optionalInt32Extension]]; XCTAssertFalse([message3 hasExtension:[UnittestObjcRoot recursiveExtension]]); } - (void)testDefaultingSubMessages { TestAllTypes *message = [TestAllTypes message]; // Initially they should all not have values. XCTAssertFalse(message.hasOptionalGroup); XCTAssertFalse(message.hasOptionalNestedMessage); XCTAssertFalse(message.hasOptionalForeignMessage); XCTAssertFalse(message.hasOptionalImportMessage); XCTAssertFalse(message.hasOptionalPublicImportMessage); XCTAssertFalse(message.hasOptionalLazyMessage); // They should auto create something when fetched. TestAllTypes_OptionalGroup *optionalGroup = [message.optionalGroup retain]; TestAllTypes_NestedMessage *optionalNestedMessage = [message.optionalNestedMessage retain]; ForeignMessage *optionalForeignMessage = [message.optionalForeignMessage retain]; ImportMessage *optionalImportMessage = [message.optionalImportMessage retain]; PublicImportMessage *optionalPublicImportMessage = [message.optionalPublicImportMessage retain]; TestAllTypes_NestedMessage *optionalLazyMessage = [message.optionalLazyMessage retain]; XCTAssertNotNil(optionalGroup); XCTAssertNotNil(optionalNestedMessage); XCTAssertNotNil(optionalForeignMessage); XCTAssertNotNil(optionalImportMessage); XCTAssertNotNil(optionalPublicImportMessage); XCTAssertNotNil(optionalLazyMessage); // Although they were created, they should not respond to hasValue until that // submessage is mutated. XCTAssertFalse(message.hasOptionalGroup); XCTAssertFalse(message.hasOptionalNestedMessage); XCTAssertFalse(message.hasOptionalForeignMessage); XCTAssertFalse(message.hasOptionalImportMessage); XCTAssertFalse(message.hasOptionalPublicImportMessage); XCTAssertFalse(message.hasOptionalLazyMessage); // And they set that value back in to the message since the value created was // mutable (so a second fetch should give the same object). XCTAssertEqual(message.optionalGroup, optionalGroup); XCTAssertEqual(message.optionalNestedMessage, optionalNestedMessage); XCTAssertEqual(message.optionalForeignMessage, optionalForeignMessage); XCTAssertEqual(message.optionalImportMessage, optionalImportMessage); XCTAssertEqual(message.optionalPublicImportMessage, optionalPublicImportMessage); XCTAssertEqual(message.optionalLazyMessage, optionalLazyMessage); // And the default objects for a second message should be distinct (again, // since they are mutable, each needs their own copy). TestAllTypes *message2 = [TestAllTypes message]; // Intentionally doing a pointer comparison. XCTAssertNotEqual(message2.optionalGroup, optionalGroup); XCTAssertNotEqual(message2.optionalNestedMessage, optionalNestedMessage); XCTAssertNotEqual(message2.optionalForeignMessage, optionalForeignMessage); XCTAssertNotEqual(message2.optionalImportMessage, optionalImportMessage); XCTAssertNotEqual(message2.optionalPublicImportMessage, optionalPublicImportMessage); XCTAssertNotEqual(message2.optionalLazyMessage, optionalLazyMessage); // Setting the values to nil will clear the has flag, and on next access you // get back new submessages. message.optionalGroup = nil; message.optionalNestedMessage = nil; message.optionalForeignMessage = nil; message.optionalImportMessage = nil; message.optionalPublicImportMessage = nil; message.optionalLazyMessage = nil; XCTAssertFalse(message.hasOptionalGroup); XCTAssertFalse(message.hasOptionalNestedMessage); XCTAssertFalse(message.hasOptionalForeignMessage); XCTAssertFalse(message.hasOptionalImportMessage); XCTAssertFalse(message.hasOptionalPublicImportMessage); XCTAssertFalse(message.hasOptionalLazyMessage); // Intentionally doing a pointer comparison. XCTAssertNotEqual(message.optionalGroup, optionalGroup); XCTAssertNotEqual(message.optionalNestedMessage, optionalNestedMessage); XCTAssertNotEqual(message.optionalForeignMessage, optionalForeignMessage); XCTAssertNotEqual(message.optionalImportMessage, optionalImportMessage); XCTAssertNotEqual(message.optionalPublicImportMessage, optionalPublicImportMessage); XCTAssertNotEqual(message.optionalLazyMessage, optionalLazyMessage); [optionalGroup release]; [optionalNestedMessage release]; [optionalForeignMessage release]; [optionalImportMessage release]; [optionalPublicImportMessage release]; [optionalLazyMessage release]; } - (void)testMultiplePointersToAutocreatedMessage { // Multiple objects pointing to the same autocreated message. TestAllTypes *message = [TestAllTypes message]; TestAllTypes *message2 = [TestAllTypes message]; message2.optionalGroup = message.optionalGroup; XCTAssertTrue([message2 hasOptionalGroup]); XCTAssertFalse([message hasOptionalGroup]); message2.optionalGroup.a = 42; XCTAssertTrue([message hasOptionalGroup]); XCTAssertTrue([message2 hasOptionalGroup]); } - (void)testCopyWithAutocreatedMessage { // Mutable copy should not copy autocreated messages. TestAllTypes *message = [TestAllTypes message]; message.optionalGroup.a = 42; XCTAssertNotNil(message.optionalNestedMessage); TestAllTypes *message2 = [[message copy] autorelease]; XCTAssertTrue([message2 hasOptionalGroup]); XCTAssertFalse([message2 hasOptionalNestedMessage]); // Intentionally doing a pointer comparison. XCTAssertNotEqual(message.optionalNestedMessage, message2.optionalNestedMessage); } - (void)testClearAutocreatedSubmessage { // Call clear on an intermediate submessage should cause it to get recreated // on the next call. TestRecursiveMessage *message = [TestRecursiveMessage message]; TestRecursiveMessage *message_inner = [message.a.a.a retain]; XCTAssertNotNil(message_inner); XCTAssertTrue(GPBWasMessageAutocreatedBy(message_inner, message.a.a)); [message.a.a clear]; XCTAssertFalse(GPBWasMessageAutocreatedBy(message_inner, message.a.a)); // Intentionally doing a pointer comparison. XCTAssertNotEqual(message.a.a.a, message_inner); [message_inner release]; } - (void)testRetainAutocreatedSubmessage { // Should be able to retain autocreated submessage while the creator is // dealloced. TestAllTypes *message = [TestAllTypes message]; ForeignMessage *subMessage; @autoreleasepool { TestAllTypes *message2 = [TestAllTypes message]; subMessage = message2.optionalForeignMessage; // Autocreated message.optionalForeignMessage = subMessage; XCTAssertTrue(GPBWasMessageAutocreatedBy(message.optionalForeignMessage, message2)); } // Should be the same object, and should still be live. XCTAssertEqual(message.optionalForeignMessage, subMessage); XCTAssertNotNil([subMessage description]); } - (void)testSetNilAutocreatedSubmessage { TestRecursiveMessage *message = [TestRecursiveMessage message]; TestRecursiveMessage *message_inner = [message.a.a retain]; XCTAssertFalse([message hasA]); XCTAssertFalse([message.a hasA]); message.a.a = nil; // |message.a| has to be made visible, but |message.a.a| was set to nil so // shouldn't be. XCTAssertTrue([message hasA]); XCTAssertFalse([message.a hasA]); // Setting submessage to nil should cause it to lose its creator. XCTAssertFalse(GPBWasMessageAutocreatedBy(message_inner, message.a)); // After setting to nil, getting it again should create a new autocreated // message. // Intentionally doing a pointer comparison. XCTAssertNotEqual(message.a.a, message_inner); [message_inner release]; } - (void)testSetDoesntHaveAutocreatedSubmessage { // Clearing submessage (set has == NO) should NOT cause it to lose its // creator. TestAllTypes *message = [TestAllTypes message]; TestAllTypes_NestedMessage *nestedMessage = message.optionalNestedMessage; XCTAssertFalse([message hasOptionalNestedMessage]); [message setHasOptionalNestedMessage:NO]; XCTAssertFalse([message hasOptionalNestedMessage]); XCTAssertEqual(message.optionalNestedMessage, nestedMessage); } - (void)testSetAutocreatedMessageBecomesVisible { // Setting a value should cause the submessage to appear to its creator. // Test this several levels deep. TestRecursiveMessage *message = [TestRecursiveMessage message]; message.a.a.a.a.i = 42; XCTAssertTrue([message hasA]); XCTAssertTrue([message.a hasA]); XCTAssertTrue([message.a.a hasA]); XCTAssertTrue([message.a.a.a hasA]); XCTAssertFalse([message.a.a.a.a hasA]); XCTAssertEqual(message.a.a.a.a.i, 42); } - (void)testClearUnsetFieldOfAutocreatedMessage { // Clearing an unset field should not cause the submessage to appear to its // creator. TestRecursiveMessage *message = [TestRecursiveMessage message]; message.a.a.a.a.hasI = NO; XCTAssertFalse([message hasA]); XCTAssertFalse([message.a hasA]); XCTAssertFalse([message.a.a hasA]); XCTAssertFalse([message.a.a.a hasA]); } - (void)testAutocreatedSubmessageAssignSkip { TestRecursiveMessage *message = [TestRecursiveMessage message]; TestRecursiveMessage *messageLevel1 = [message.a retain]; TestRecursiveMessage *messageLevel2 = [message.a.a retain]; TestRecursiveMessage *messageLevel3 = [message.a.a.a retain]; TestRecursiveMessage *messageLevel4 = [message.a.a.a.a retain]; XCTAssertTrue(GPBWasMessageAutocreatedBy(messageLevel1, message)); XCTAssertTrue(GPBWasMessageAutocreatedBy(messageLevel2, messageLevel1)); XCTAssertTrue(GPBWasMessageAutocreatedBy(messageLevel3, messageLevel2)); XCTAssertTrue(GPBWasMessageAutocreatedBy(messageLevel4, messageLevel3)); // Test skipping over an autocreated submessage and ensure it gets unset. message.a = message.a.a; XCTAssertEqual(message.a, messageLevel2); XCTAssertTrue([message hasA]); XCTAssertEqual(message.a.a, messageLevel3); XCTAssertFalse([message.a hasA]); XCTAssertEqual(message.a.a.a, messageLevel4); XCTAssertFalse(GPBWasMessageAutocreatedBy(messageLevel1, message)); // Because it was orphaned. XCTAssertTrue(GPBWasMessageAutocreatedBy(messageLevel2, messageLevel1)); XCTAssertTrue(GPBWasMessageAutocreatedBy(messageLevel3, messageLevel2)); [messageLevel1 release]; [messageLevel2 release]; [messageLevel3 release]; [messageLevel4 release]; } - (void)testAutocreatedSubmessageAssignLoop { TestRecursiveMessage *message = [TestRecursiveMessage message]; TestRecursiveMessage *messageLevel1 = [message.a retain]; TestRecursiveMessage *messageLevel2 = [message.a.a retain]; TestRecursiveMessage *messageLevel3 = [message.a.a.a retain]; TestRecursiveMessage *messageLevel4 = [message.a.a.a.a retain]; XCTAssertTrue(GPBWasMessageAutocreatedBy(messageLevel1, message)); XCTAssertTrue(GPBWasMessageAutocreatedBy(messageLevel2, messageLevel1)); XCTAssertTrue(GPBWasMessageAutocreatedBy(messageLevel3, messageLevel2)); XCTAssertTrue(GPBWasMessageAutocreatedBy(messageLevel4, messageLevel3)); // Test a property with a loop. You'd never do this but at least ensure the // autocreated submessages behave sanely. message.a.a = message.a; XCTAssertTrue([message hasA]); XCTAssertEqual(message.a, messageLevel1); XCTAssertTrue([message.a hasA]); XCTAssertEqual(message.a.a, messageLevel1); XCTAssertTrue([message.a.a hasA]); XCTAssertEqual(message.a.a.a, messageLevel1); XCTAssertFalse(GPBWasMessageAutocreatedBy(messageLevel1, message)); // Because it was assigned. XCTAssertFalse(GPBWasMessageAutocreatedBy(messageLevel2, messageLevel1)); // Because it was orphaned. XCTAssertFalse([messageLevel2 hasA]); // Break the retain loop. message.a.a = nil; XCTAssertTrue([message hasA]); XCTAssertFalse([message.a hasA]); [messageLevel1 release]; [messageLevel2 release]; [messageLevel3 release]; [messageLevel4 release]; } - (void)testSetAutocreatedSubmessage { // Setting autocreated submessage to another value should cause the old one to // lose its creator. TestAllTypes *message = [TestAllTypes message]; TestAllTypes_NestedMessage *nestedMessage = [message.optionalNestedMessage retain]; message.optionalNestedMessage = [TestAllTypes_NestedMessage message]; XCTAssertTrue([message hasOptionalNestedMessage]); XCTAssertTrue(message.optionalNestedMessage != nestedMessage); XCTAssertFalse(GPBWasMessageAutocreatedBy(nestedMessage, message)); [nestedMessage release]; } - (void)testAutocreatedUnknownFields { // Doing anything with (except reading) unknown fields should cause the // submessage to become visible. TestAllTypes *message = [TestAllTypes message]; XCTAssertNotNil(message.optionalNestedMessage); XCTAssertFalse([message hasOptionalNestedMessage]); XCTAssertNil(message.optionalNestedMessage.unknownFields); XCTAssertFalse([message hasOptionalNestedMessage]); GPBUnknownFieldSet *unknownFields = [[[GPBUnknownFieldSet alloc] init] autorelease]; message.optionalNestedMessage.unknownFields = unknownFields; XCTAssertTrue([message hasOptionalNestedMessage]); message.optionalNestedMessage = nil; XCTAssertFalse([message hasOptionalNestedMessage]); [message.optionalNestedMessage setUnknownFields:unknownFields]; XCTAssertTrue([message hasOptionalNestedMessage]); } - (void)testSetAutocreatedSubmessageToSelf { // Setting submessage to itself should cause it to become visible. TestAllTypes *message = [TestAllTypes message]; XCTAssertNotNil(message.optionalNestedMessage); XCTAssertFalse([message hasOptionalNestedMessage]); message.optionalNestedMessage = message.optionalNestedMessage; XCTAssertTrue([message hasOptionalNestedMessage]); } - (void)testAutocreatedSubmessageMemoryLeaks { // Test for memory leaks with autocreated submessages. TestRecursiveMessage *message; TestRecursiveMessage *messageLevel1; TestRecursiveMessage *messageLevel2; TestRecursiveMessage *messageLevel3; TestRecursiveMessage *messageLevel4; @autoreleasepool { message = [[TestRecursiveMessage alloc] init]; messageLevel1 = [message.a retain]; messageLevel2 = [message.a.a retain]; messageLevel3 = [message.a.a.a retain]; messageLevel4 = [message.a.a.a.a retain]; message.a.i = 1; } XCTAssertEqual(message.retainCount, (NSUInteger)1); [message release]; XCTAssertEqual(messageLevel1.retainCount, (NSUInteger)1); [messageLevel1 release]; XCTAssertEqual(messageLevel2.retainCount, (NSUInteger)1); [messageLevel2 release]; XCTAssertEqual(messageLevel3.retainCount, (NSUInteger)1); [messageLevel3 release]; XCTAssertEqual(messageLevel4.retainCount, (NSUInteger)1); [messageLevel4 release]; } - (void)testDefaultingArrays { // Basic tests for default creation of arrays in a message. TestRecursiveMessageWithRepeatedField *message = [TestRecursiveMessageWithRepeatedField message]; TestRecursiveMessageWithRepeatedField *message2 = [TestRecursiveMessageWithRepeatedField message]; // Simply accessing the array should not make any fields visible. XCTAssertNotNil(message.a.a.iArray); XCTAssertFalse([message hasA]); XCTAssertFalse([message.a hasA]); XCTAssertNotNil(message2.a.a.strArray); XCTAssertFalse([message2 hasA]); XCTAssertFalse([message2.a hasA]); // But adding an element to the array should. [message.a.a.iArray addValue:42]; XCTAssertTrue([message hasA]); XCTAssertTrue([message.a hasA]); XCTAssertEqual([message.a.a.iArray count], (NSUInteger)1); [message2.a.a.strArray addObject:@"foo"]; XCTAssertTrue([message2 hasA]); XCTAssertTrue([message2.a hasA]); XCTAssertEqual([message2.a.a.strArray count], (NSUInteger)1); } - (void)testAutocreatedArrayShared { // Multiple objects pointing to the same array. TestRecursiveMessageWithRepeatedField *message1a = [TestRecursiveMessageWithRepeatedField message]; TestRecursiveMessageWithRepeatedField *message1b = [TestRecursiveMessageWithRepeatedField message]; message1a.a.iArray = message1b.a.iArray; XCTAssertTrue([message1a hasA]); XCTAssertFalse([message1b hasA]); [message1a.a.iArray addValue:1]; XCTAssertTrue([message1a hasA]); XCTAssertTrue([message1b hasA]); XCTAssertEqual(message1a.a.iArray, message1b.a.iArray); TestRecursiveMessageWithRepeatedField *message2a = [TestRecursiveMessageWithRepeatedField message]; TestRecursiveMessageWithRepeatedField *message2b = [TestRecursiveMessageWithRepeatedField message]; message2a.a.strArray = message2b.a.strArray; XCTAssertTrue([message2a hasA]); XCTAssertFalse([message2b hasA]); [message2a.a.strArray addObject:@"bar"]; XCTAssertTrue([message2a hasA]); XCTAssertTrue([message2b hasA]); XCTAssertEqual(message2a.a.strArray, message2b.a.strArray); } - (void)testAutocreatedArrayCopy { // Copy should not copy autocreated arrays. TestAllTypes *message = [TestAllTypes message]; XCTAssertNotNil(message.repeatedStringArray); XCTAssertNotNil(message.repeatedInt32Array); TestAllTypes *message2 = [[message copy] autorelease]; // Pointer conparisions. XCTAssertNotEqual(message.repeatedStringArray, message2.repeatedStringArray); XCTAssertNotEqual(message.repeatedInt32Array, message2.repeatedInt32Array); // Mutable copy should copy empty arrays that were explicitly set (end up // with different objects that are equal). TestAllTypes *message3 = [TestAllTypes message]; message3.repeatedInt32Array = [GPBInt32Array arrayWithValue:42]; message3.repeatedStringArray = [NSMutableArray arrayWithObject:@"wee"]; XCTAssertNotNil(message.repeatedInt32Array); XCTAssertNotNil(message.repeatedStringArray); TestAllTypes *message4 = [[message3 copy] autorelease]; XCTAssertNotEqual(message3.repeatedInt32Array, message4.repeatedInt32Array); XCTAssertEqualObjects(message3.repeatedInt32Array, message4.repeatedInt32Array); XCTAssertNotEqual(message3.repeatedStringArray, message4.repeatedStringArray); XCTAssertEqualObjects(message3.repeatedStringArray, message4.repeatedStringArray); } - (void)testAutocreatedArrayRetain { // Should be able to retain autocreated array while the creator is dealloced. TestAllTypes *message = [TestAllTypes message]; @autoreleasepool { TestAllTypes *message2 = [TestAllTypes message]; message.repeatedInt32Array = message2.repeatedInt32Array; message.repeatedStringArray = message2.repeatedStringArray; // Pointer conparision XCTAssertEqual(message.repeatedInt32Array->_autocreator, message2); XCTAssertTrue([message.repeatedStringArray isKindOfClass:[GPBAutocreatedArray class]]); XCTAssertEqual(((GPBAutocreatedArray *)message.repeatedStringArray)->_autocreator, message2); } XCTAssertNil(message.repeatedInt32Array->_autocreator); XCTAssertTrue([message.repeatedStringArray isKindOfClass:[GPBAutocreatedArray class]]); XCTAssertNil(((GPBAutocreatedArray *)message.repeatedStringArray)->_autocreator); } - (void)testSetNilAutocreatedArray { // Setting array to nil should cause it to lose its delegate. TestAllTypes *message = [TestAllTypes message]; GPBInt32Array *repeatedInt32Array = [message.repeatedInt32Array retain]; GPBAutocreatedArray *repeatedStringArray = (GPBAutocreatedArray *)[message.repeatedStringArray retain]; XCTAssertTrue([repeatedStringArray isKindOfClass:[GPBAutocreatedArray class]]); XCTAssertEqual(repeatedInt32Array->_autocreator, message); XCTAssertEqual(repeatedStringArray->_autocreator, message); message.repeatedInt32Array = nil; message.repeatedStringArray = nil; XCTAssertNil(repeatedInt32Array->_autocreator); XCTAssertNil(repeatedStringArray->_autocreator); [repeatedInt32Array release]; [repeatedStringArray release]; } - (void)testSetOverAutocreatedArrayAndSetAgain { // Ensure when dealing with replacing an array it is handled being either // an autocreated one or a straight NSArray. // The real test here is that nothing crashes while doing the work. TestAllTypes *message = [TestAllTypes message]; [message.repeatedStringArray addObject:@"foo"]; XCTAssertEqual(message.repeatedStringArray_Count, (NSUInteger)1); message.repeatedStringArray = [NSMutableArray arrayWithObjects:@"bar", @"bar2", nil]; XCTAssertEqual(message.repeatedStringArray_Count, (NSUInteger)2); message.repeatedStringArray = [NSMutableArray arrayWithObject:@"baz"]; XCTAssertEqual(message.repeatedStringArray_Count, (NSUInteger)1); } - (void)testReplaceAutocreatedArray { // Replacing array should orphan the old one and cause its creator to become // visible. { TestRecursiveMessageWithRepeatedField *message = [TestRecursiveMessageWithRepeatedField message]; XCTAssertNotNil(message.a); XCTAssertNotNil(message.a.iArray); XCTAssertFalse([message hasA]); GPBInt32Array *iArray = [message.a.iArray retain]; XCTAssertEqual(iArray->_autocreator, message.a); // Pointer comparison message.a.iArray = [GPBInt32Array arrayWithValue:1]; XCTAssertTrue([message hasA]); XCTAssertNotEqual(message.a.iArray, iArray); // Pointer comparison XCTAssertNil(iArray->_autocreator); [iArray release]; } { TestRecursiveMessageWithRepeatedField *message = [TestRecursiveMessageWithRepeatedField message]; XCTAssertNotNil(message.a); XCTAssertNotNil(message.a.strArray); XCTAssertFalse([message hasA]); GPBAutocreatedArray *strArray = (GPBAutocreatedArray *)[message.a.strArray retain]; XCTAssertTrue([strArray isKindOfClass:[GPBAutocreatedArray class]]); XCTAssertEqual(strArray->_autocreator, message.a); // Pointer comparison message.a.strArray = [NSMutableArray arrayWithObject:@"foo"]; XCTAssertTrue([message hasA]); XCTAssertNotEqual(message.a.strArray, strArray); // Pointer comparison XCTAssertNil(strArray->_autocreator); [strArray release]; } } - (void)testSetAutocreatedArrayToSelf { // Setting array to itself should cause it to become visible. { TestRecursiveMessageWithRepeatedField *message = [TestRecursiveMessageWithRepeatedField message]; XCTAssertNotNil(message.a); XCTAssertNotNil(message.a.iArray); XCTAssertFalse([message hasA]); message.a.iArray = message.a.iArray; XCTAssertTrue([message hasA]); XCTAssertNil(message.a.iArray->_autocreator); } { TestRecursiveMessageWithRepeatedField *message = [TestRecursiveMessageWithRepeatedField message]; XCTAssertNotNil(message.a); XCTAssertNotNil(message.a.strArray); XCTAssertFalse([message hasA]); message.a.strArray = message.a.strArray; XCTAssertTrue([message hasA]); XCTAssertTrue([message.a.strArray isKindOfClass:[GPBAutocreatedArray class]]); XCTAssertNil(((GPBAutocreatedArray *)message.a.strArray)->_autocreator); } } - (void)testAutocreatedArrayRemoveAllValues { // Calling removeAllValues on autocreated array should not cause it to be // visible. TestRecursiveMessageWithRepeatedField *message = [TestRecursiveMessageWithRepeatedField message]; [message.a.iArray removeAll]; XCTAssertFalse([message hasA]); [message.a.strArray removeAllObjects]; XCTAssertFalse([message hasA]); } - (void)testDefaultingMaps { // Basic tests for default creation of maps in a message. TestRecursiveMessageWithRepeatedField *message = [TestRecursiveMessageWithRepeatedField message]; TestRecursiveMessageWithRepeatedField *message2 = [TestRecursiveMessageWithRepeatedField message]; // Simply accessing the map should not make any fields visible. XCTAssertNotNil(message.a.a.iToI); XCTAssertFalse([message hasA]); XCTAssertFalse([message.a hasA]); XCTAssertNotNil(message2.a.a.strToStr); XCTAssertFalse([message2 hasA]); XCTAssertFalse([message2.a hasA]); // But adding an element to the map should. [message.a.a.iToI setInt32:100 forKey:200]; XCTAssertTrue([message hasA]); XCTAssertTrue([message.a hasA]); XCTAssertEqual([message.a.a.iToI count], (NSUInteger)1); [message2.a.a.strToStr setObject:@"foo" forKey:@"bar"]; XCTAssertTrue([message2 hasA]); XCTAssertTrue([message2.a hasA]); XCTAssertEqual([message2.a.a.strToStr count], (NSUInteger)1); } - (void)testAutocreatedMapShared { // Multiple objects pointing to the same map. TestRecursiveMessageWithRepeatedField *message1a = [TestRecursiveMessageWithRepeatedField message]; TestRecursiveMessageWithRepeatedField *message1b = [TestRecursiveMessageWithRepeatedField message]; message1a.a.iToI = message1b.a.iToI; XCTAssertTrue([message1a hasA]); XCTAssertFalse([message1b hasA]); [message1a.a.iToI setInt32:1 forKey:2]; XCTAssertTrue([message1a hasA]); XCTAssertTrue([message1b hasA]); XCTAssertEqual(message1a.a.iToI, message1b.a.iToI); TestRecursiveMessageWithRepeatedField *message2a = [TestRecursiveMessageWithRepeatedField message]; TestRecursiveMessageWithRepeatedField *message2b = [TestRecursiveMessageWithRepeatedField message]; message2a.a.strToStr = message2b.a.strToStr; XCTAssertTrue([message2a hasA]); XCTAssertFalse([message2b hasA]); [message2a.a.strToStr setObject:@"bar" forKey:@"foo"]; XCTAssertTrue([message2a hasA]); XCTAssertTrue([message2b hasA]); XCTAssertEqual(message2a.a.strToStr, message2b.a.strToStr); } - (void)testAutocreatedMapCopy { // Copy should not copy autocreated maps. TestRecursiveMessageWithRepeatedField *message = [TestRecursiveMessageWithRepeatedField message]; XCTAssertNotNil(message.strToStr); XCTAssertNotNil(message.iToI); TestRecursiveMessageWithRepeatedField *message2 = [[message copy] autorelease]; // Pointer conparisions. XCTAssertNotEqual(message.strToStr, message2.strToStr); XCTAssertNotEqual(message.iToI, message2.iToI); // Mutable copy should copy empty arrays that were explicitly set (end up // with different objects that are equal). TestRecursiveMessageWithRepeatedField *message3 = [TestRecursiveMessageWithRepeatedField message]; message3.iToI = [[[GPBInt32Int32Dictionary alloc] init] autorelease]; [message3.iToI setInt32:10 forKey:20]; message3.strToStr = [NSMutableDictionary dictionaryWithObject:@"abc" forKey:@"123"]; XCTAssertNotNil(message.iToI); XCTAssertNotNil(message.iToI); TestRecursiveMessageWithRepeatedField *message4 = [[message3 copy] autorelease]; XCTAssertNotEqual(message3.iToI, message4.iToI); XCTAssertEqualObjects(message3.iToI, message4.iToI); XCTAssertNotEqual(message3.strToStr, message4.strToStr); XCTAssertEqualObjects(message3.strToStr, message4.strToStr); } - (void)testAutocreatedMapRetain { // Should be able to retain autocreated map while the creator is dealloced. TestRecursiveMessageWithRepeatedField *message = [TestRecursiveMessageWithRepeatedField message]; @autoreleasepool { TestRecursiveMessageWithRepeatedField *message2 = [TestRecursiveMessageWithRepeatedField message]; message.iToI = message2.iToI; message.strToStr = message2.strToStr; // Pointer conparision XCTAssertEqual(message.iToI->_autocreator, message2); XCTAssertTrue([message.strToStr isKindOfClass:[GPBAutocreatedDictionary class]]); XCTAssertEqual(((GPBAutocreatedDictionary *)message.strToStr)->_autocreator, message2); } XCTAssertNil(message.iToI->_autocreator); XCTAssertTrue([message.strToStr isKindOfClass:[GPBAutocreatedDictionary class]]); XCTAssertNil(((GPBAutocreatedDictionary *)message.strToStr)->_autocreator); } - (void)testSetNilAutocreatedMap { // Setting map to nil should cause it to lose its delegate. TestRecursiveMessageWithRepeatedField *message = [TestRecursiveMessageWithRepeatedField message]; GPBInt32Int32Dictionary *iToI = [message.iToI retain]; GPBAutocreatedDictionary *strToStr = (GPBAutocreatedDictionary *)[message.strToStr retain]; XCTAssertTrue([strToStr isKindOfClass:[GPBAutocreatedDictionary class]]); XCTAssertEqual(iToI->_autocreator, message); XCTAssertEqual(strToStr->_autocreator, message); message.iToI = nil; message.strToStr = nil; XCTAssertNil(iToI->_autocreator); XCTAssertNil(strToStr->_autocreator); [iToI release]; [strToStr release]; } - (void)testSetOverAutocreatedMapAndSetAgain { // Ensure when dealing with replacing a map it is handled being either // an autocreated one or a straight NSDictionary. // The real test here is that nothing crashes while doing the work. TestRecursiveMessageWithRepeatedField *message = [TestRecursiveMessageWithRepeatedField message]; message.strToStr[@"foo"] = @"bar"; XCTAssertEqual(message.strToStr_Count, (NSUInteger)1); message.strToStr = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"bar", @"key1", @"baz", @"key2", nil]; XCTAssertEqual(message.strToStr_Count, (NSUInteger)2); message.strToStr = [NSMutableDictionary dictionaryWithObject:@"baz" forKey:@"mumble"]; XCTAssertEqual(message.strToStr_Count, (NSUInteger)1); } - (void)testReplaceAutocreatedMap { // Replacing map should orphan the old one and cause its creator to become // visible. { TestRecursiveMessageWithRepeatedField *message = [TestRecursiveMessageWithRepeatedField message]; XCTAssertNotNil(message.a); XCTAssertNotNil(message.a.iToI); XCTAssertFalse([message hasA]); GPBInt32Int32Dictionary *iToI = [message.a.iToI retain]; XCTAssertEqual(iToI->_autocreator, message.a); // Pointer comparison message.a.iToI = [[[GPBInt32Int32Dictionary alloc] init] autorelease]; [message.a.iToI setInt32:6 forKey:7]; XCTAssertTrue([message hasA]); XCTAssertNotEqual(message.a.iToI, iToI); // Pointer comparison XCTAssertNil(iToI->_autocreator); [iToI release]; } { TestRecursiveMessageWithRepeatedField *message = [TestRecursiveMessageWithRepeatedField message]; XCTAssertNotNil(message.a); XCTAssertNotNil(message.a.strToStr); XCTAssertFalse([message hasA]); GPBAutocreatedDictionary *strToStr = (GPBAutocreatedDictionary *)[message.a.strToStr retain]; XCTAssertTrue([strToStr isKindOfClass:[GPBAutocreatedDictionary class]]); XCTAssertEqual(strToStr->_autocreator, message.a); // Pointer comparison message.a.strToStr = [NSMutableDictionary dictionaryWithObject:@"abc" forKey:@"def"]; XCTAssertTrue([message hasA]); XCTAssertNotEqual(message.a.strToStr, strToStr); // Pointer comparison XCTAssertNil(strToStr->_autocreator); [strToStr release]; } } - (void)testSetAutocreatedMapToSelf { // Setting map to itself should cause it to become visible. { TestRecursiveMessageWithRepeatedField *message = [TestRecursiveMessageWithRepeatedField message]; XCTAssertNotNil(message.a); XCTAssertNotNil(message.a.iToI); XCTAssertFalse([message hasA]); message.a.iToI = message.a.iToI; XCTAssertTrue([message hasA]); XCTAssertNil(message.a.iToI->_autocreator); } { TestRecursiveMessageWithRepeatedField *message = [TestRecursiveMessageWithRepeatedField message]; XCTAssertNotNil(message.a); XCTAssertNotNil(message.a.strToStr); XCTAssertFalse([message hasA]); message.a.strToStr = message.a.strToStr; XCTAssertTrue([message hasA]); XCTAssertTrue([message.a.strToStr isKindOfClass:[GPBAutocreatedDictionary class]]); XCTAssertNil(((GPBAutocreatedDictionary *)message.a.strToStr)->_autocreator); } } - (void)testAutocreatedMapRemoveAllValues { // Calling removeAll on autocreated map should not cause it to be visible. TestRecursiveMessageWithRepeatedField *message = [TestRecursiveMessageWithRepeatedField message]; [message.a.iToI removeAll]; XCTAssertFalse([message hasA]); [message.a.strToStr removeAllObjects]; XCTAssertFalse([message hasA]); } - (void)testExtensionAccessors { TestAllExtensions *message = [TestAllExtensions message]; [self setAllExtensions:message repeatedCount:kGPBDefaultRepeatCount]; [self assertAllExtensionsSet:message repeatedCount:kGPBDefaultRepeatCount]; } - (void)testExtensionRepeatedSetters { TestAllExtensions *message = [TestAllExtensions message]; [self setAllExtensions:message repeatedCount:kGPBDefaultRepeatCount]; [self modifyRepeatedExtensions:message]; [self assertRepeatedExtensionsModified:message repeatedCount:kGPBDefaultRepeatCount]; } - (void)testExtensionDefaults { [self assertExtensionsClear:[TestAllExtensions message]]; } - (void)testExtensionIsEquals { TestAllExtensions *message = [TestAllExtensions message]; [self setAllExtensions:message repeatedCount:kGPBDefaultRepeatCount]; [self modifyRepeatedExtensions:message]; TestAllExtensions *message2 = [TestAllExtensions message]; [self setAllExtensions:message2 repeatedCount:kGPBDefaultRepeatCount]; XCTAssertFalse([message isEqual:message2]); message2 = [TestAllExtensions message]; [self setAllExtensions:message2 repeatedCount:kGPBDefaultRepeatCount]; [self modifyRepeatedExtensions:message2]; XCTAssertEqualObjects(message, message2); } - (void)testExtensionsMergeFrom { TestAllExtensions *message = [TestAllExtensions message]; [self setAllExtensions:message repeatedCount:kGPBDefaultRepeatCount]; [self modifyRepeatedExtensions:message]; message = [TestAllExtensions message]; [self setAllExtensions:message repeatedCount:kGPBDefaultRepeatCount]; TestAllExtensions *message2 = [TestAllExtensions message]; [self modifyRepeatedExtensions:message2]; [message2 mergeFrom:message]; XCTAssertEqualObjects(message, message2); } - (void)testClosedEnumsInExtensions { // Only unknown values. NSData *data = DataFromCStr("\xA8\x01\x0A" // optional_nested_enum_extension set to 10 "\x98\x03\x0B" // repeated_nested_enum_extension set to 11 "\xA2\x03\x01\x0C" // repeated_foreign_enum_extension set to 12 (packed) ); NSError *error = nil; TestAllExtensions *msg = [TestAllExtensions parseFromData:data extensionRegistry:[self extensionRegistry] error:&error]; XCTAssertNil(error); XCTAssertFalse([msg hasExtension:[UnittestRoot optionalNestedEnumExtension]]); XCTAssertFalse([msg hasExtension:[UnittestRoot repeatedNestedEnumExtension]]); XCTAssertFalse([msg hasExtension:[UnittestRoot repeatedForeignEnumExtension]]); GPBUnknownFieldSet *unknownFields = msg.unknownFields; GPBUnknownField *field = [unknownFields getField:[UnittestRoot optionalNestedEnumExtension].fieldNumber]; XCTAssertNotNil(field); XCTAssertEqual(field.varintList.count, 1); XCTAssertEqual([field.varintList valueAtIndex:0], 10); field = [unknownFields getField:[UnittestRoot repeatedNestedEnumExtension].fieldNumber]; XCTAssertNotNil(field); XCTAssertEqual(field.varintList.count, 1); XCTAssertEqual([field.varintList valueAtIndex:0], 11); field = [unknownFields getField:[UnittestRoot repeatedForeignEnumExtension].fieldNumber]; XCTAssertNotNil(field); XCTAssertEqual(field.varintList.count, 1); XCTAssertEqual([field.varintList valueAtIndex:0], 12); // Unknown and known, the known come though an unknown go to unknown fields. data = DataFromCStr( "\xA8\x01\x01" // optional_nested_enum_extension set to 1 "\xA8\x01\x0A" // optional_nested_enum_extension set to 10 "\xA8\x01\x02" // optional_nested_enum_extension set to 2 "\x98\x03\x02" // repeated_nested_enum_extension set to 2 "\x98\x03\x0B" // repeated_nested_enum_extension set to 11 "\x98\x03\x03" // repeated_nested_enum_extension set to 3 "\xA2\x03\x03\x04\x0C\x06" // repeated_foreign_enum_extension set to 4, 12, 6 (packed) ); error = nil; msg = [TestAllExtensions parseFromData:data extensionRegistry:[self extensionRegistry] error:&error]; XCTAssertNil(error); XCTAssertTrue([msg hasExtension:[UnittestRoot optionalNestedEnumExtension]]); XCTAssertEqualObjects([msg getExtension:[UnittestRoot optionalNestedEnumExtension]], @2); XCTAssertTrue([msg hasExtension:[UnittestRoot repeatedNestedEnumExtension]]); id expected = @[ @2, @3 ]; XCTAssertEqualObjects([msg getExtension:[UnittestRoot repeatedNestedEnumExtension]], expected); XCTAssertTrue([msg hasExtension:[UnittestRoot repeatedForeignEnumExtension]]); expected = @[ @4, @6 ]; XCTAssertEqualObjects([msg getExtension:[UnittestRoot repeatedForeignEnumExtension]], expected); unknownFields = msg.unknownFields; field = [unknownFields getField:[UnittestRoot optionalNestedEnumExtension].fieldNumber]; XCTAssertNotNil(field); XCTAssertEqual(field.varintList.count, 1); XCTAssertEqual([field.varintList valueAtIndex:0], 10); field = [unknownFields getField:[UnittestRoot repeatedNestedEnumExtension].fieldNumber]; XCTAssertNotNil(field); XCTAssertEqual(field.varintList.count, 1); XCTAssertEqual([field.varintList valueAtIndex:0], 11); field = [unknownFields getField:[UnittestRoot repeatedForeignEnumExtension].fieldNumber]; XCTAssertNotNil(field); XCTAssertEqual(field.varintList.count, 1); XCTAssertEqual([field.varintList valueAtIndex:0], 12); } - (void)testDefaultingExtensionMessages { TestAllExtensions *message = [TestAllExtensions message]; // Initially they should all not have values. XCTAssertFalse([message hasExtension:[UnittestRoot optionalGroupExtension]]); XCTAssertFalse([message hasExtension:[UnittestRoot optionalGroupExtension]]); XCTAssertFalse([message hasExtension:[UnittestRoot optionalNestedMessageExtension]]); XCTAssertFalse([message hasExtension:[UnittestRoot optionalForeignMessageExtension]]); XCTAssertFalse([message hasExtension:[UnittestRoot optionalImportMessageExtension]]); XCTAssertFalse([message hasExtension:[UnittestRoot optionalPublicImportMessageExtension]]); XCTAssertFalse([message hasExtension:[UnittestRoot optionalLazyMessageExtension]]); // They should auto create something when fetched. TestAllTypes_OptionalGroup *optionalGroup = [message getExtension:[UnittestRoot optionalGroupExtension]]; TestAllTypes_NestedMessage *optionalNestedMessage = [message getExtension:[UnittestRoot optionalNestedMessageExtension]]; ForeignMessage *optionalForeignMessage = [message getExtension:[UnittestRoot optionalForeignMessageExtension]]; ImportMessage *optionalImportMessage = [message getExtension:[UnittestRoot optionalImportMessageExtension]]; PublicImportMessage *optionalPublicImportMessage = [message getExtension:[UnittestRoot optionalPublicImportMessageExtension]]; TestAllTypes_NestedMessage *optionalLazyMessage = [message getExtension:[UnittestRoot optionalLazyMessageExtension]]; XCTAssertNotNil(optionalGroup); XCTAssertNotNil(optionalNestedMessage); XCTAssertNotNil(optionalForeignMessage); XCTAssertNotNil(optionalImportMessage); XCTAssertNotNil(optionalPublicImportMessage); XCTAssertNotNil(optionalLazyMessage); // Although it auto-created empty messages, it should not show that it has // them. XCTAssertFalse([message hasExtension:[UnittestRoot optionalGroupExtension]]); XCTAssertFalse([message hasExtension:[UnittestRoot optionalGroupExtension]]); XCTAssertFalse([message hasExtension:[UnittestRoot optionalNestedMessageExtension]]); XCTAssertFalse([message hasExtension:[UnittestRoot optionalForeignMessageExtension]]); XCTAssertFalse([message hasExtension:[UnittestRoot optionalImportMessageExtension]]); XCTAssertFalse([message hasExtension:[UnittestRoot optionalPublicImportMessageExtension]]); XCTAssertFalse([message hasExtension:[UnittestRoot optionalLazyMessageExtension]]); // And they set that value back in to the message since the value created was // mutable (so a second fetch should give the same object). XCTAssertEqual([message getExtension:[UnittestRoot optionalGroupExtension]], optionalGroup); XCTAssertEqual([message getExtension:[UnittestRoot optionalNestedMessageExtension]], optionalNestedMessage); XCTAssertEqual([message getExtension:[UnittestRoot optionalForeignMessageExtension]], optionalForeignMessage); XCTAssertEqual([message getExtension:[UnittestRoot optionalImportMessageExtension]], optionalImportMessage); XCTAssertEqual([message getExtension:[UnittestRoot optionalPublicImportMessageExtension]], optionalPublicImportMessage); XCTAssertEqual([message getExtension:[UnittestRoot optionalLazyMessageExtension]], optionalLazyMessage); // And the default objects for a second message should be distinct (again, // since they are mutable, each needs their own copy). TestAllExtensions *message2 = [TestAllExtensions message]; // Intentionally doing a pointer comparison. XCTAssertNotEqual([message2 getExtension:[UnittestRoot optionalGroupExtension]], optionalGroup); XCTAssertNotEqual([message2 getExtension:[UnittestRoot optionalNestedMessageExtension]], optionalNestedMessage); XCTAssertNotEqual([message2 getExtension:[UnittestRoot optionalForeignMessageExtension]], optionalForeignMessage); XCTAssertNotEqual([message2 getExtension:[UnittestRoot optionalImportMessageExtension]], optionalImportMessage); XCTAssertNotEqual([message2 getExtension:[UnittestRoot optionalPublicImportMessageExtension]], optionalPublicImportMessage); XCTAssertNotEqual([message2 getExtension:[UnittestRoot optionalLazyMessageExtension]], optionalLazyMessage); // Clear values, and on next access you get back new submessages. [message setExtension:[UnittestRoot optionalGroupExtension] value:nil]; [message setExtension:[UnittestRoot optionalGroupExtension] value:nil]; [message setExtension:[UnittestRoot optionalNestedMessageExtension] value:nil]; [message setExtension:[UnittestRoot optionalForeignMessageExtension] value:nil]; [message setExtension:[UnittestRoot optionalImportMessageExtension] value:nil]; [message setExtension:[UnittestRoot optionalPublicImportMessageExtension] value:nil]; [message setExtension:[UnittestRoot optionalLazyMessageExtension] value:nil]; XCTAssertFalse([message hasExtension:[UnittestRoot optionalGroupExtension]]); XCTAssertFalse([message hasExtension:[UnittestRoot optionalGroupExtension]]); XCTAssertFalse([message hasExtension:[UnittestRoot optionalNestedMessageExtension]]); XCTAssertFalse([message hasExtension:[UnittestRoot optionalForeignMessageExtension]]); XCTAssertFalse([message hasExtension:[UnittestRoot optionalImportMessageExtension]]); XCTAssertFalse([message hasExtension:[UnittestRoot optionalPublicImportMessageExtension]]); XCTAssertFalse([message hasExtension:[UnittestRoot optionalLazyMessageExtension]]); XCTAssertEqual([message getExtension:[UnittestRoot optionalGroupExtension]], optionalGroup); XCTAssertEqual([message getExtension:[UnittestRoot optionalNestedMessageExtension]], optionalNestedMessage); XCTAssertEqual([message getExtension:[UnittestRoot optionalForeignMessageExtension]], optionalForeignMessage); XCTAssertEqual([message getExtension:[UnittestRoot optionalImportMessageExtension]], optionalImportMessage); XCTAssertEqual([message getExtension:[UnittestRoot optionalPublicImportMessageExtension]], optionalPublicImportMessage); XCTAssertEqual([message getExtension:[UnittestRoot optionalLazyMessageExtension]], optionalLazyMessage); } - (void)testMultiplePointersToAutocreatedExtension { // 2 objects point to the same auto-created extension. One should "has" it. // The other should not. TestAllExtensions *message = [TestAllExtensions message]; TestAllExtensions *message2 = [TestAllExtensions message]; GPBExtensionDescriptor *extension = [UnittestRoot optionalGroupExtension]; [message setExtension:extension value:[message2 getExtension:extension]]; XCTAssertEqual([message getExtension:extension], [message2 getExtension:extension]); XCTAssertFalse([message2 hasExtension:extension]); XCTAssertTrue([message hasExtension:extension]); TestAllTypes_OptionalGroup *extensionValue = [message2 getExtension:extension]; extensionValue.a = 1; XCTAssertTrue([message2 hasExtension:extension]); XCTAssertTrue([message hasExtension:extension]); } - (void)testCopyWithAutocreatedExtension { // Mutable copy shouldn't copy autocreated extensions. TestAllExtensions *message = [TestAllExtensions message]; GPBExtensionDescriptor *optionalGroupExtension = [UnittestRoot optionalGroupExtension]; GPBExtensionDescriptor *optionalNestedMessageExtesion = [UnittestRoot optionalNestedMessageExtension]; TestAllTypes_OptionalGroup *optionalGroup = [message getExtension:optionalGroupExtension]; optionalGroup.a = 42; XCTAssertNotNil(optionalGroup); XCTAssertNotNil([message getExtension:optionalNestedMessageExtesion]); XCTAssertTrue([message hasExtension:optionalGroupExtension]); XCTAssertFalse([message hasExtension:optionalNestedMessageExtesion]); TestAllExtensions *message2 = [[message copy] autorelease]; // message2 should end up with its own copy of the optional group. XCTAssertTrue([message2 hasExtension:optionalGroupExtension]); XCTAssertEqualObjects([message getExtension:optionalGroupExtension], [message2 getExtension:optionalGroupExtension]); // Intentionally doing a pointer comparison. XCTAssertNotEqual([message getExtension:optionalGroupExtension], [message2 getExtension:optionalGroupExtension]); XCTAssertFalse([message2 hasExtension:optionalNestedMessageExtesion]); // Intentionally doing a pointer comparison (auto creation should be // different) XCTAssertNotEqual([message getExtension:optionalNestedMessageExtesion], [message2 getExtension:optionalNestedMessageExtesion]); } - (void)testClearMessageAutocreatedExtension { // Call clear should cause it to recreate its autocreated extensions. TestAllExtensions *message = [TestAllExtensions message]; GPBExtensionDescriptor *optionalGroupExtension = [UnittestRoot optionalGroupExtension]; TestAllTypes_OptionalGroup *optionalGroup = [[message getExtension:optionalGroupExtension] retain]; [message clear]; TestAllTypes_OptionalGroup *optionalGroupNew = [message getExtension:optionalGroupExtension]; // Intentionally doing a pointer comparison. XCTAssertNotEqual(optionalGroup, optionalGroupNew); [optionalGroup release]; } - (void)testRetainAutocreatedExtension { // Should be able to retain autocreated extension while the creator is // dealloced. TestAllExtensions *message = [TestAllExtensions message]; GPBExtensionDescriptor *optionalGroupExtension = [UnittestRoot optionalGroupExtension]; @autoreleasepool { TestAllExtensions *message2 = [TestAllExtensions message]; [message setExtension:optionalGroupExtension value:[message2 getExtension:optionalGroupExtension]]; XCTAssertTrue( GPBWasMessageAutocreatedBy([message getExtension:optionalGroupExtension], message2)); } XCTAssertFalse( GPBWasMessageAutocreatedBy([message getExtension:optionalGroupExtension], message)); } - (void)testClearAutocreatedExtension { // Clearing autocreated extension should NOT cause it to lose its creator. TestAllExtensions *message = [TestAllExtensions message]; GPBExtensionDescriptor *optionalGroupExtension = [UnittestRoot optionalGroupExtension]; TestAllTypes_OptionalGroup *optionalGroup = [[message getExtension:optionalGroupExtension] retain]; [message clearExtension:optionalGroupExtension]; TestAllTypes_OptionalGroup *optionalGroupNew = [message getExtension:optionalGroupExtension]; XCTAssertEqual(optionalGroup, optionalGroupNew); XCTAssertFalse([message hasExtension:optionalGroupExtension]); [optionalGroup release]; // Clearing autocreated extension should not cause its creator to become // visible GPBExtensionDescriptor *recursiveExtension = [UnittestObjcRoot recursiveExtension]; TestAllExtensions *message_lvl2 = [message getExtension:recursiveExtension]; TestAllExtensions *message_lvl3 = [message_lvl2 getExtension:recursiveExtension]; [message_lvl3 clearExtension:recursiveExtension]; XCTAssertFalse([message hasExtension:recursiveExtension]); } - (void)testSetAutocreatedExtensionBecomesVisible { // Setting an extension should cause the extension to appear to its creator. // Test this several levels deep. TestAllExtensions *message = [TestAllExtensions message]; GPBExtensionDescriptor *recursiveExtension = [UnittestObjcRoot recursiveExtension]; TestAllExtensions *message_lvl2 = [message getExtension:recursiveExtension]; TestAllExtensions *message_lvl3 = [message_lvl2 getExtension:recursiveExtension]; TestAllExtensions *message_lvl4 = [message_lvl3 getExtension:recursiveExtension]; XCTAssertFalse([message hasExtension:recursiveExtension]); XCTAssertFalse([message_lvl2 hasExtension:recursiveExtension]); XCTAssertFalse([message_lvl3 hasExtension:recursiveExtension]); XCTAssertFalse([message_lvl4 hasExtension:recursiveExtension]); [message_lvl4 setExtension:[UnittestRoot optionalInt32Extension] value:@(1)]; XCTAssertTrue([message hasExtension:recursiveExtension]); XCTAssertTrue([message_lvl2 hasExtension:recursiveExtension]); XCTAssertTrue([message_lvl3 hasExtension:recursiveExtension]); XCTAssertFalse([message_lvl4 hasExtension:recursiveExtension]); XCTAssertFalse(GPBWasMessageAutocreatedBy(message_lvl4, message_lvl3)); XCTAssertFalse(GPBWasMessageAutocreatedBy(message_lvl3, message_lvl2)); XCTAssertFalse(GPBWasMessageAutocreatedBy(message_lvl2, message)); } - (void)testSetAutocreatedExtensionToSelf { // Setting extension to itself should cause it to become visible. TestAllExtensions *message = [TestAllExtensions message]; GPBExtensionDescriptor *optionalGroupExtension = [UnittestRoot optionalGroupExtension]; XCTAssertNotNil([message getExtension:optionalGroupExtension]); XCTAssertFalse([message hasExtension:optionalGroupExtension]); [message setExtension:optionalGroupExtension value:[message getExtension:optionalGroupExtension]]; XCTAssertTrue([message hasExtension:optionalGroupExtension]); } - (void)testAutocreatedExtensionMemoryLeaks { GPBExtensionDescriptor *recursiveExtension = [UnittestObjcRoot recursiveExtension]; // Test for memory leaks with autocreated extensions. TestAllExtensions *message; TestAllExtensions *message_lvl2; TestAllExtensions *message_lvl3; TestAllExtensions *message_lvl4; @autoreleasepool { message = [[TestAllExtensions alloc] init]; message_lvl2 = [[message getExtension:recursiveExtension] retain]; message_lvl3 = [[message_lvl2 getExtension:recursiveExtension] retain]; message_lvl4 = [[message_lvl3 getExtension:recursiveExtension] retain]; [message_lvl2 setExtension:[UnittestRoot optionalInt32Extension] value:@(1)]; } XCTAssertEqual(message.retainCount, (NSUInteger)1); @autoreleasepool { [message release]; } XCTAssertEqual(message_lvl2.retainCount, (NSUInteger)1); @autoreleasepool { [message_lvl2 release]; } XCTAssertEqual(message_lvl3.retainCount, (NSUInteger)1); @autoreleasepool { [message_lvl3 release]; } XCTAssertEqual(message_lvl4.retainCount, (NSUInteger)1); [message_lvl4 release]; } - (void)testSetExtensionWithAutocreatedValue { GPBExtensionDescriptor *recursiveExtension = [UnittestObjcRoot recursiveExtension]; TestAllExtensions *message; @autoreleasepool { message = [[TestAllExtensions alloc] init]; [message getExtension:recursiveExtension]; } // This statements checks that the extension value isn't accidentally // dealloced when removing it from the autocreated map. [message setExtension:recursiveExtension value:[message getExtension:recursiveExtension]]; XCTAssertTrue([message hasExtension:recursiveExtension]); [message release]; } - (void)testRecursion { TestRecursiveMessage *message = [TestRecursiveMessage message]; XCTAssertNotNil(message.a); XCTAssertNotNil(message.a.a); XCTAssertEqual(message.a.a.i, 0); } - (void)testGenerateAndParseUnknownMessage { GPBUnknownFieldSet *unknowns = [[[GPBUnknownFieldSet alloc] init] autorelease]; [unknowns mergeVarintField:123 value:456]; GPBMessage *message = [GPBMessage message]; [message setUnknownFields:unknowns]; NSData *data = [message data]; GPBMessage *message2 = [GPBMessage parseFromData:data extensionRegistry:nil error:NULL]; XCTAssertEqualObjects(message, message2); } - (void)testDelimitedWriteAndParseMultipleMessages { GPBUnknownFieldSet *unknowns1 = [[[GPBUnknownFieldSet alloc] init] autorelease]; [unknowns1 mergeVarintField:123 value:456]; GPBMessage *message1 = [GPBMessage message]; [message1 setUnknownFields:unknowns1]; GPBUnknownFieldSet *unknowns2 = [[[GPBUnknownFieldSet alloc] init] autorelease]; [unknowns2 mergeVarintField:789 value:987]; [unknowns2 mergeVarintField:654 value:321]; GPBMessage *message2 = [GPBMessage message]; [message2 setUnknownFields:unknowns2]; NSMutableData *delimitedData = [NSMutableData data]; [delimitedData appendData:[message1 delimitedData]]; [delimitedData appendData:[message2 delimitedData]]; GPBCodedInputStream *input = [GPBCodedInputStream streamWithData:delimitedData]; GPBMessage *message3 = [GPBMessage parseDelimitedFromCodedInputStream:input extensionRegistry:nil error:NULL]; GPBMessage *message4 = [GPBMessage parseDelimitedFromCodedInputStream:input extensionRegistry:nil error:NULL]; XCTAssertEqualObjects(message1, message3); XCTAssertEqualObjects(message2, message4); } - (void)testDuplicateEnums { XCTAssertEqual(TestEnumWithDupValue_Foo1, TestEnumWithDupValue_Foo2); } - (void)testWeirdDefaults { ObjcWeirdDefaults *message = [ObjcWeirdDefaults message]; GPBDescriptor *descriptor = [[message class] descriptor]; GPBFieldDescriptor *fieldDesc = [descriptor fieldWithName:@"foo"]; XCTAssertNotNil(fieldDesc); XCTAssertTrue(fieldDesc.hasDefaultValue); XCTAssertFalse(message.hasFoo); XCTAssertEqualObjects(message.foo, @""); fieldDesc = [descriptor fieldWithName:@"bar"]; XCTAssertNotNil(fieldDesc); XCTAssertTrue(fieldDesc.hasDefaultValue); XCTAssertFalse(message.hasBar); XCTAssertEqualObjects(message.bar, GPBEmptyNSData()); } - (void)testEnumDescriptorFromExtensionDescriptor { GPBExtensionDescriptor *extDescriptor = [UnittestRoot optionalForeignEnumExtension]; XCTAssertEqual(extDescriptor.dataType, GPBDataTypeEnum); GPBEnumDescriptor *enumDescriptor = extDescriptor.enumDescriptor; GPBEnumDescriptor *expectedDescriptor = ForeignEnum_EnumDescriptor(); XCTAssertEqualObjects(enumDescriptor, expectedDescriptor); } - (void)testPropertyNaming { // names.cc has some special handing to get proper all caps for a few cases to // meet objc developer expectations. // // This "test" confirms that the expected names are generated, otherwise the // test itself will fail to compile. ObjCPropertyNaming *msg = [ObjCPropertyNaming message]; // On their own, at the end, in the middle. msg.URL = @"good"; msg.thumbnailURL = @"good"; msg.URLFoo = @"good"; msg.someURLBlah = @"good"; msg.HTTP = @"good"; msg.HTTPS = @"good"; // No caps since it was "urls". [msg.urlsArray addObject:@"good"]; } - (void)testEnumNaming { // names.cc has some interesting cases to deal with in // EnumValueName/EnumValueShortName. Confirm that things generated as // expected. // This block just has to compile to confirm we got the expected types/names. // The *_IsValidValue() calls are just there to keep the projects warnings // flags happy by providing use of the variables/values. Foo aFoo = Foo_SerializedSize; Foo_IsValidValue(aFoo); aFoo = Foo_Size; Foo_IsValidValue(aFoo); Category_Enum aCat = Category_Enum_Red; Category_Enum_IsValidValue(aCat); Time aTime = Time_Base; Time_IsValidValue(aTime); aTime = Time_SomethingElse; Time_IsValidValue(aTime); // This block confirms the names in the descriptors is what we wanted. GPBEnumDescriptor *descriptor; NSString *valueName; descriptor = Foo_EnumDescriptor(); XCTAssertNotNil(descriptor); XCTAssertEqualObjects(@"Foo", descriptor.name); valueName = [descriptor enumNameForValue:Foo_SerializedSize]; XCTAssertEqualObjects(@"Foo_SerializedSize", valueName); valueName = [descriptor enumNameForValue:Foo_Size]; XCTAssertEqualObjects(@"Foo_Size", valueName); descriptor = Category_Enum_EnumDescriptor(); XCTAssertNotNil(descriptor); XCTAssertEqualObjects(@"Category_Enum", descriptor.name); valueName = [descriptor enumNameForValue:Category_Enum_Red]; XCTAssertEqualObjects(@"Category_Enum_Red", valueName); descriptor = Time_EnumDescriptor(); XCTAssertNotNil(descriptor); XCTAssertEqualObjects(@"Time", descriptor.name); valueName = [descriptor enumNameForValue:Time_Base]; XCTAssertEqualObjects(@"Time_Base", valueName); valueName = [descriptor enumNameForValue:Time_SomethingElse]; XCTAssertEqualObjects(@"Time_SomethingElse", valueName); } - (void)testNegativeEnums { EnumTestMsg *msg = [EnumTestMsg message]; // Defaults XCTAssertEqual(msg.foo, EnumTestMsg_MyEnum_Zero); XCTAssertEqual(msg.bar, EnumTestMsg_MyEnum_One); XCTAssertEqual(msg.baz, EnumTestMsg_MyEnum_NegOne); // Bounce to wire and back. NSData *data = [msg data]; XCTAssertNotNil(data); EnumTestMsg *msgPrime = [EnumTestMsg parseFromData:data error:NULL]; XCTAssertEqualObjects(msgPrime, msg); XCTAssertEqual(msgPrime.foo, EnumTestMsg_MyEnum_Zero); XCTAssertEqual(msgPrime.bar, EnumTestMsg_MyEnum_One); XCTAssertEqual(msgPrime.baz, EnumTestMsg_MyEnum_NegOne); // Other values msg.bar = EnumTestMsg_MyEnum_Two; msg.baz = EnumTestMsg_MyEnum_NegTwo; XCTAssertEqual(msg.bar, EnumTestMsg_MyEnum_Two); XCTAssertEqual(msg.baz, EnumTestMsg_MyEnum_NegTwo); // Bounce to wire and back. data = [msg data]; XCTAssertNotNil(data); msgPrime = [EnumTestMsg parseFromData:data error:NULL]; XCTAssertEqualObjects(msgPrime, msg); XCTAssertEqual(msgPrime.foo, EnumTestMsg_MyEnum_Zero); XCTAssertEqual(msgPrime.bar, EnumTestMsg_MyEnum_Two); XCTAssertEqual(msgPrime.baz, EnumTestMsg_MyEnum_NegTwo); // Repeated field (shouldn't ever be an issue since developer has to use the // right GPBArray methods themselves). msg.mumbleArray = [GPBEnumArray arrayWithValidationFunction:EnumTestMsg_MyEnum_IsValidValue]; [msg.mumbleArray addValue:EnumTestMsg_MyEnum_Zero]; [msg.mumbleArray addValue:EnumTestMsg_MyEnum_One]; [msg.mumbleArray addValue:EnumTestMsg_MyEnum_Two]; [msg.mumbleArray addValue:EnumTestMsg_MyEnum_NegOne]; [msg.mumbleArray addValue:EnumTestMsg_MyEnum_NegTwo]; XCTAssertEqual([msg.mumbleArray valueAtIndex:0], EnumTestMsg_MyEnum_Zero); XCTAssertEqual([msg.mumbleArray valueAtIndex:1], EnumTestMsg_MyEnum_One); XCTAssertEqual([msg.mumbleArray valueAtIndex:2], EnumTestMsg_MyEnum_Two); XCTAssertEqual([msg.mumbleArray valueAtIndex:3], EnumTestMsg_MyEnum_NegOne); XCTAssertEqual([msg.mumbleArray valueAtIndex:4], EnumTestMsg_MyEnum_NegTwo); // Bounce to wire and back. data = [msg data]; XCTAssertNotNil(data); msgPrime = [EnumTestMsg parseFromData:data error:NULL]; XCTAssertEqualObjects(msgPrime, msg); XCTAssertEqual([msgPrime.mumbleArray valueAtIndex:0], EnumTestMsg_MyEnum_Zero); XCTAssertEqual([msgPrime.mumbleArray valueAtIndex:1], EnumTestMsg_MyEnum_One); XCTAssertEqual([msgPrime.mumbleArray valueAtIndex:2], EnumTestMsg_MyEnum_Two); XCTAssertEqual([msgPrime.mumbleArray valueAtIndex:3], EnumTestMsg_MyEnum_NegOne); XCTAssertEqual([msgPrime.mumbleArray valueAtIndex:4], EnumTestMsg_MyEnum_NegTwo); } - (void)testReservedWordNaming { // names.cc has some special handing to make sure that some "reserved" objc // names get renamed in a way so they don't conflict. // // This "test" confirms that the expected names are generated, // otherwise the test itself will fail to compile. self_Class *msg = [self_Class message]; // Some ObjC/C/C++ keywords. msg.className_p = msg.hasClassName_p; msg.cmd = msg.hasCmd; msg.nullable_p = msg.hasNullable_p; msg.typeof_p = msg.hasTypeof_p; msg.instancetype_p = msg.hasInstancetype_p; msg.nil_p = msg.hasNil_p; msg.instancetype_p = msg.hasInstancetype_p; msg.public_p = msg.hasPublic_p; // Some that would override NSObject methods msg.camltype = msg.hasCamltype; msg.isNsdictionary = msg.hasIsNsdictionary; msg.dealloc_p = msg.hasDealloc_p; msg.zone_p = msg.hasZone_p; msg.accessibilityLabel_p = msg.hasAccessibilityLabel_p; // Some that we shouldn't need to handle. msg.atomic = msg.hasAtomic; msg.nonatomic = msg.hasNonatomic; msg.strong = msg.hasStrong; msg.nullResettable = msg.hasNullResettable; // Some that would override GPBMessage methods msg.clear_p = msg.hasClear_p; msg.data_p = msg.hasData_p; // Some MacTypes msg.fixed = msg.hasFixed; msg.style = msg.hasStyle; // Some C Identifiers msg.generic = msg.hasGeneric; msg.block = msg.hasBlock; } - (void)testOneBasedEnumHolder { // Test case for https://github.com/protocolbuffers/protobuf/issues/1453 // Message with no explicit defaults, but a non zero default for an enum. MessageWithOneBasedEnum *enumMsg = [MessageWithOneBasedEnum message]; XCTAssertEqual(enumMsg.enumField, MessageWithOneBasedEnum_OneBasedEnum_One); } - (void)testBoolOffsetUsage { // Bools use storage within has_bits; this test ensures that this is honored // in all places where things should crash or fail based on reading out of // field storage instead. BoolOnlyMessage *msg1 = [BoolOnlyMessage message]; BoolOnlyMessage *msg2 = [BoolOnlyMessage message]; msg1.boolField1 = YES; msg2.boolField1 = YES; msg1.boolField3 = YES; msg2.boolField3 = YES; msg1.boolField5 = YES; msg2.boolField5 = YES; msg1.boolField7 = YES; msg2.boolField7 = YES; msg1.boolField9 = YES; msg2.boolField9 = YES; msg1.boolField11 = YES; msg2.boolField11 = YES; msg1.boolField13 = YES; msg2.boolField13 = YES; msg1.boolField15 = YES; msg2.boolField15 = YES; msg1.boolField17 = YES; msg2.boolField17 = YES; msg1.boolField19 = YES; msg2.boolField19 = YES; msg1.boolField21 = YES; msg2.boolField21 = YES; msg1.boolField23 = YES; msg2.boolField23 = YES; msg1.boolField25 = YES; msg2.boolField25 = YES; msg1.boolField27 = YES; msg2.boolField27 = YES; msg1.boolField29 = YES; msg2.boolField29 = YES; msg1.boolField31 = YES; msg2.boolField31 = YES; msg1.boolField32 = YES; msg2.boolField32 = YES; XCTAssertTrue(msg1 != msg2); // Different pointers. XCTAssertEqual([msg1 hash], [msg2 hash]); XCTAssertEqualObjects(msg1, msg2); BoolOnlyMessage *msg1Prime = [[msg1 copy] autorelease]; XCTAssertTrue(msg1Prime != msg1); // Different pointers. XCTAssertEqual([msg1 hash], [msg1Prime hash]); XCTAssertEqualObjects(msg1, msg1Prime); // Field set in one, but not the other means they don't match (even if // set to default value). msg1Prime.boolField2 = NO; XCTAssertNotEqualObjects(msg1Prime, msg1); // And when set to different values. msg1.boolField2 = YES; XCTAssertNotEqualObjects(msg1Prime, msg1); // And then they match again. msg1.boolField2 = NO; XCTAssertEqualObjects(msg1Prime, msg1); XCTAssertEqual([msg1 hash], [msg1Prime hash]); } - (void)testCopyingMapFields { TestMessageOfMaps *msg = [TestMessageOfMaps message]; msg.strToStr[@"foo"] = @"bar"; [msg.strToInt setInt32:1 forKey:@"mumble"]; [msg.intToStr setObject:@"wee" forKey:42]; [msg.intToInt setInt32:123 forKey:321]; [msg.strToBool setBool:YES forKey:@"one"]; [msg.boolToStr setObject:@"something" forKey:YES]; [msg.boolToBool setBool:YES forKey:NO]; [msg.intToBool setBool:YES forKey:13]; [msg.boolToInt setInt32:111 forKey:NO]; TestAllTypes *subMsg1 = [TestAllTypes message]; subMsg1.optionalInt32 = 1; TestAllTypes *subMsg2 = [TestAllTypes message]; subMsg1.optionalInt32 = 2; TestAllTypes *subMsg3 = [TestAllTypes message]; subMsg1.optionalInt32 = 3; msg.strToMsg[@"baz"] = subMsg1; [msg.intToMsg setObject:subMsg2 forKey:222]; [msg.boolToMsg setObject:subMsg3 forKey:YES]; TestMessageOfMaps *msg2 = [[msg copy] autorelease]; XCTAssertNotNil(msg2); XCTAssertEqualObjects(msg2, msg); XCTAssertTrue(msg2 != msg); // ptr compare XCTAssertTrue(msg.strToStr != msg2.strToStr); // ptr compare XCTAssertTrue(msg.intToStr != msg2.intToStr); // ptr compare XCTAssertTrue(msg.intToInt != msg2.intToInt); // ptr compare XCTAssertTrue(msg.strToBool != msg2.strToBool); // ptr compare XCTAssertTrue(msg.boolToStr != msg2.boolToStr); // ptr compare XCTAssertTrue(msg.boolToBool != msg2.boolToBool); // ptr compare XCTAssertTrue(msg.intToBool != msg2.intToBool); // ptr compare XCTAssertTrue(msg.boolToInt != msg2.boolToInt); // ptr compare XCTAssertTrue(msg.strToMsg != msg2.strToMsg); // ptr compare XCTAssertTrue(msg.intToMsg != msg2.intToMsg); // ptr compare XCTAssertTrue(msg.boolToMsg != msg2.boolToMsg); // ptr compare XCTAssertTrue(msg.strToMsg[@"baz"] != msg2.strToMsg[@"baz"]); // ptr compare XCTAssertEqualObjects(msg.strToMsg[@"baz"], msg2.strToMsg[@"baz"]); XCTAssertTrue([msg.intToMsg objectForKey:222] != [msg2.intToMsg objectForKey:222]); // ptr compare XCTAssertEqualObjects([msg.intToMsg objectForKey:222], [msg2.intToMsg objectForKey:222]); XCTAssertTrue([msg.boolToMsg objectForKey:YES] != [msg2.boolToMsg objectForKey:YES]); // ptr compare XCTAssertEqualObjects([msg.boolToMsg objectForKey:YES], [msg2.boolToMsg objectForKey:YES]); } - (void)testPrefixedNames { // The fact that this compiles is sufficient as a test. // The assertions are just there to avoid "not-used" warnings. // Verify that enum types and values get the prefix. GPBTESTTestObjcProtoPrefixEnum value = GPBTESTTestObjcProtoPrefixEnum_Value; XCTAssertNotEqual(value, 0); // Verify that roots get the prefix. GPBTESTUnittestObjcOptionsRoot *root = nil; XCTAssertNil(root); // Verify that messages that don't already have the prefix get a prefix. GPBTESTTestObjcProtoPrefixMessage *prefixedMessage = nil; XCTAssertNil(prefixedMessage); // Verify that messages that already have a prefix aren't prefixed twice. GPBTESTTestHasAPrefixMessage *alreadyPrefixedMessage = nil; XCTAssertNil(alreadyPrefixedMessage); // Verify that enums that already have a prefix aren't prefixed twice. GPBTESTTestHasAPrefixEnum prefixedValue = GPBTESTTestHasAPrefixEnum_ValueB; XCTAssertNotEqual(prefixedValue, 0); // Verify that classes named the same as prefixes are prefixed. GPBTESTGPBTEST *prefixMessage = nil; XCTAssertNil(prefixMessage); // Verify that classes that have the prefix followed by a lowercase // letter DO get the prefix. GPBTESTGPBTESTshouldGetAPrefixMessage *shouldGetAPrefixMessage = nil; XCTAssertNil(shouldGetAPrefixMessage); } - (void)testWriteToFullOutputStreamShouldThrow { uint8_t buffer[1] = {0}; // Output stream which can write precisely 1 byte of data before it's full. NSOutputStream *output = [[[NSOutputStream alloc] initToBuffer:buffer capacity:sizeof(buffer)] autorelease]; TestAllTypes *message = [TestAllTypes message]; [message setOptionalInt32:1]; XCTAssertThrowsSpecificNamed([message writeToOutputStream:output], NSException, GPBCodedOutputStreamException_WriteFailed); } @end