diff options
Diffstat (limited to 'objectivec/Tests/GPBUtilitiesTests.m')
-rw-r--r-- | objectivec/Tests/GPBUtilitiesTests.m | 200 |
1 files changed, 200 insertions, 0 deletions
diff --git a/objectivec/Tests/GPBUtilitiesTests.m b/objectivec/Tests/GPBUtilitiesTests.m index dfaca660..2e206a54 100644 --- a/objectivec/Tests/GPBUtilitiesTests.m +++ b/objectivec/Tests/GPBUtilitiesTests.m @@ -39,6 +39,7 @@ #import "GPBDescriptor.h" #import "GPBDescriptor_PackagePrivate.h" #import "GPBMessage.h" +#import "GPBUnknownField_PackagePrivate.h" #import "google/protobuf/MapUnittest.pbobjc.h" #import "google/protobuf/Unittest.pbobjc.h" @@ -197,4 +198,203 @@ } } +// Helper to make an unknown field set with something in it. +static GPBUnknownFieldSet *UnknownFieldsSetHelper(int num) { + GPBUnknownFieldSet *result = + [[[GPBUnknownFieldSet alloc] init] autorelease]; + + GPBUnknownField *field = + [[[GPBUnknownField alloc] initWithNumber:num] autorelease]; + [field addVarint:num]; + [result addField:field]; + + return result; +} + +- (void)testDropMessageUnknownFieldsRecursively { + TestAllExtensions *message = [TestAllExtensions message]; + + // Give it unknownFields. + message.unknownFields = UnknownFieldsSetHelper(777); + + // Given it extensions that include a message with unknown fields of its own. + { + // Int + [message setExtension:[UnittestRoot optionalInt32Extension] value:@5]; + + // Group + OptionalGroup_extension *optionalGroup = [OptionalGroup_extension message]; + optionalGroup.a = 123; + optionalGroup.unknownFields = UnknownFieldsSetHelper(779); + [message setExtension:[UnittestRoot optionalGroupExtension] + value:optionalGroup]; + + // Message + TestAllTypes_NestedMessage *nestedMessage = + [TestAllTypes_NestedMessage message]; + nestedMessage.bb = 456; + nestedMessage.unknownFields = UnknownFieldsSetHelper(778); + [message setExtension:[UnittestRoot optionalNestedMessageExtension] + value:nestedMessage]; + + // Repeated Group + RepeatedGroup_extension *repeatedGroup = + [[RepeatedGroup_extension alloc] init]; + repeatedGroup.a = 567; + repeatedGroup.unknownFields = UnknownFieldsSetHelper(780); + [message addExtension:[UnittestRoot repeatedGroupExtension] + value:repeatedGroup]; + [repeatedGroup release]; + + // Repeated Message + nestedMessage = [[TestAllTypes_NestedMessage alloc] init]; + nestedMessage.bb = 678; + nestedMessage.unknownFields = UnknownFieldsSetHelper(781); + [message addExtension:[UnittestRoot repeatedNestedMessageExtension] + value:nestedMessage]; + [nestedMessage release]; + } + + // Confirm everything is there. + + XCTAssertNotNil(message); + XCTAssertNotNil(message.unknownFields); + XCTAssertTrue([message hasExtension:[UnittestRoot optionalInt32Extension]]); + + { + XCTAssertTrue([message hasExtension:[UnittestRoot optionalGroupExtension]]); + OptionalGroup_extension *optionalGroup = + [message getExtension:[UnittestRoot optionalGroupExtension]]; + XCTAssertNotNil(optionalGroup); + XCTAssertEqual(optionalGroup.a, 123); + XCTAssertNotNil(optionalGroup.unknownFields); + } + + { + XCTAssertTrue([message hasExtension:[UnittestRoot optionalNestedMessageExtension]]); + TestAllTypes_NestedMessage *nestedMessage = + [message getExtension:[UnittestRoot optionalNestedMessageExtension]]; + XCTAssertNotNil(nestedMessage); + XCTAssertEqual(nestedMessage.bb, 456); + XCTAssertNotNil(nestedMessage.unknownFields); + } + + { + XCTAssertTrue([message hasExtension:[UnittestRoot repeatedGroupExtension]]); + NSArray *repeatedGroups = [message getExtension:[UnittestRoot repeatedGroupExtension]]; + XCTAssertEqual(repeatedGroups.count, (NSUInteger)1); + RepeatedGroup_extension *repeatedGroup = repeatedGroups.firstObject; + XCTAssertNotNil(repeatedGroup); + XCTAssertEqual(repeatedGroup.a, 567); + XCTAssertNotNil(repeatedGroup.unknownFields); + } + + { + XCTAssertTrue([message hasExtension:[UnittestRoot repeatedNestedMessageExtension]]); + NSArray *repeatedNestedMessages = [message getExtension:[UnittestRoot repeatedNestedMessageExtension]]; + XCTAssertEqual(repeatedNestedMessages.count, (NSUInteger)1); + TestAllTypes_NestedMessage *repeatedNestedMessage = repeatedNestedMessages.firstObject; + XCTAssertNotNil(repeatedNestedMessage); + XCTAssertEqual(repeatedNestedMessage.bb, 678); + XCTAssertNotNil(repeatedNestedMessage.unknownFields); + } + + // Drop them. + GPBMessageDropUnknownFieldsRecursively(message); + + // Confirm unknowns are gone from within the messages. + + XCTAssertNotNil(message); + XCTAssertNil(message.unknownFields); + XCTAssertTrue([message hasExtension:[UnittestRoot optionalInt32Extension]]); + + { + XCTAssertTrue([message hasExtension:[UnittestRoot optionalGroupExtension]]); + OptionalGroup_extension *optionalGroup = + [message getExtension:[UnittestRoot optionalGroupExtension]]; + XCTAssertNotNil(optionalGroup); + XCTAssertEqual(optionalGroup.a, 123); + XCTAssertNil(optionalGroup.unknownFields); + } + + { + XCTAssertTrue([message hasExtension:[UnittestRoot optionalNestedMessageExtension]]); + TestAllTypes_NestedMessage *nestedMessage = + [message getExtension:[UnittestRoot optionalNestedMessageExtension]]; + XCTAssertNotNil(nestedMessage); + XCTAssertEqual(nestedMessage.bb, 456); + XCTAssertNil(nestedMessage.unknownFields); + } + + { + XCTAssertTrue([message hasExtension:[UnittestRoot repeatedGroupExtension]]); + NSArray *repeatedGroups = [message getExtension:[UnittestRoot repeatedGroupExtension]]; + XCTAssertEqual(repeatedGroups.count, (NSUInteger)1); + RepeatedGroup_extension *repeatedGroup = repeatedGroups.firstObject; + XCTAssertNotNil(repeatedGroup); + XCTAssertEqual(repeatedGroup.a, 567); + XCTAssertNil(repeatedGroup.unknownFields); + } + + { + XCTAssertTrue([message hasExtension:[UnittestRoot repeatedNestedMessageExtension]]); + NSArray *repeatedNestedMessages = [message getExtension:[UnittestRoot repeatedNestedMessageExtension]]; + XCTAssertEqual(repeatedNestedMessages.count, (NSUInteger)1); + TestAllTypes_NestedMessage *repeatedNestedMessage = repeatedNestedMessages.firstObject; + XCTAssertNotNil(repeatedNestedMessage); + XCTAssertEqual(repeatedNestedMessage.bb, 678); + XCTAssertNil(repeatedNestedMessage.unknownFields); + } + +} + +- (void)testDropMessageUnknownFieldsRecursively_Maps { + TestMap *message = [TestMap message]; + + { + ForeignMessage *foreignMessage = [ForeignMessage message]; + foreignMessage.unknownFields = UnknownFieldsSetHelper(100); + [message.mapInt32ForeignMessage setObject:foreignMessage forKey:100]; + + foreignMessage = [ForeignMessage message]; + foreignMessage.unknownFields = UnknownFieldsSetHelper(101); + [message.mapStringForeignMessage setObject:foreignMessage forKey:@"101"]; + } + + // Confirm everything is there. + + XCTAssertNotNil(message); + + { + ForeignMessage *foreignMessage = [message.mapInt32ForeignMessage objectForKey:100]; + XCTAssertNotNil(foreignMessage); + XCTAssertNotNil(foreignMessage.unknownFields); + } + + { + ForeignMessage *foreignMessage = [message.mapStringForeignMessage objectForKey:@"101"]; + XCTAssertNotNil(foreignMessage); + XCTAssertNotNil(foreignMessage.unknownFields); + } + + GPBMessageDropUnknownFieldsRecursively(message); + + // Confirm unknowns are gone from within the messages. + + XCTAssertNotNil(message); + + { + ForeignMessage *foreignMessage = [message.mapInt32ForeignMessage objectForKey:100]; + XCTAssertNotNil(foreignMessage); + XCTAssertNil(foreignMessage.unknownFields); + } + + { + ForeignMessage *foreignMessage = [message.mapStringForeignMessage objectForKey:@"101"]; + XCTAssertNotNil(foreignMessage); + XCTAssertNil(foreignMessage.unknownFields); + } + +} + @end |