diff options
author | Thomas Van Lenten <thomasvl@google.com> | 2017-01-04 15:03:42 -0500 |
---|---|---|
committer | Thomas Van Lenten <thomasvl@google.com> | 2017-01-05 09:15:40 -0500 |
commit | 988ffe0a78ebda0410e61ce31a3bd689c774f59e (patch) | |
tree | f5e92cdc634e4eef2ef2b891de95ad23bec3fba4 /objectivec/Tests | |
parent | 4cb113a91b180559f0eedbca0244ef1181a7204c (diff) | |
download | protobuf-988ffe0a78ebda0410e61ce31a3bd689c774f59e.tar.gz protobuf-988ffe0a78ebda0410e61ce31a3bd689c774f59e.tar.bz2 protobuf-988ffe0a78ebda0410e61ce31a3bd689c774f59e.zip |
Minor fix for autocreated object repeated fields and maps.
- If setting/clearing a repeated field/map that was objects, check the class
before checking the autocreator.
- Just to be paranoid, don’t mutate within copy/mutableCopy for the autocreated
classes to ensure there is less chance of issues if someone does something
really crazy threading wise.
- Some more tests for the internal AutocreatedArray/AutocreatedDictionary
classes to ensure things are working as expected.
- Add Xcode 8.2 to the full_mac_build.sh supported list.
Diffstat (limited to 'objectivec/Tests')
-rw-r--r-- | objectivec/Tests/GPBArrayTests.m | 173 | ||||
-rw-r--r-- | objectivec/Tests/GPBDictionaryTests.m | 186 | ||||
-rw-r--r-- | objectivec/Tests/GPBMessageTests.m | 31 |
3 files changed, 390 insertions, 0 deletions
diff --git a/objectivec/Tests/GPBArrayTests.m b/objectivec/Tests/GPBArrayTests.m index 0fb15e40..31f75501 100644 --- a/objectivec/Tests/GPBArrayTests.m +++ b/objectivec/Tests/GPBArrayTests.m @@ -32,6 +32,7 @@ #import <XCTest/XCTest.h> #import "GPBArray.h" +#import "GPBArray_PackagePrivate.h" #import "GPBTestUtilities.h" @@ -3436,3 +3437,175 @@ static BOOL TestingEnum_IsValidValue2(int32_t value) { } @end + +#pragma mark - GPBAutocreatedArray Tests + +// These are hand written tests to double check some behaviors of the +// GPBAutocreatedArray. + +// NOTE: GPBAutocreatedArray is private to the library, users of the library +// should never have to directly deal with this class. + +@interface GPBAutocreatedArrayTests : XCTestCase +@end + +@implementation GPBAutocreatedArrayTests + +- (void)testEquality { + GPBAutocreatedArray *array = [[GPBAutocreatedArray alloc] init]; + + XCTAssertTrue([array isEqual:@[]]); + XCTAssertTrue([array isEqualToArray:@[]]); + + XCTAssertFalse([array isEqual:@[ @"foo" ]]); + XCTAssertFalse([array isEqualToArray:@[ @"foo" ]]); + + [array addObject:@"foo"]; + + XCTAssertFalse([array isEqual:@[]]); + XCTAssertFalse([array isEqualToArray:@[]]); + XCTAssertTrue([array isEqual:@[ @"foo" ]]); + XCTAssertTrue([array isEqualToArray:@[ @"foo" ]]); + XCTAssertFalse([array isEqual:@[ @"bar" ]]); + XCTAssertFalse([array isEqualToArray:@[ @"bar" ]]); + + GPBAutocreatedArray *array2 = [[GPBAutocreatedArray alloc] init]; + + XCTAssertFalse([array isEqual:array2]); + XCTAssertFalse([array isEqualToArray:array2]); + + [array2 addObject:@"bar"]; + XCTAssertFalse([array isEqual:array2]); + XCTAssertFalse([array isEqualToArray:array2]); + + [array2 replaceObjectAtIndex:0 withObject:@"foo"]; + XCTAssertTrue([array isEqual:array2]); + XCTAssertTrue([array isEqualToArray:array2]); + + [array2 release]; + [array release]; +} + +- (void)testCopy { + { + GPBAutocreatedArray *array = [[GPBAutocreatedArray alloc] init]; + + NSArray *cpy = [array copy]; + XCTAssertTrue(cpy != array); // Ptr compare + XCTAssertTrue([cpy isKindOfClass:[NSArray class]]); + XCTAssertFalse([cpy isKindOfClass:[GPBAutocreatedArray class]]); + XCTAssertEqual(cpy.count, (NSUInteger)0); + + NSArray *cpy2 = [array copy]; + XCTAssertTrue(cpy2 != array); // Ptr compare + // Can't compare cpy and cpy2 because NSArray has a singleton empty + // array it uses, so the ptrs are the same. + XCTAssertTrue([cpy2 isKindOfClass:[NSArray class]]); + XCTAssertFalse([cpy2 isKindOfClass:[GPBAutocreatedArray class]]); + XCTAssertEqual(cpy2.count, (NSUInteger)0); + + [cpy2 release]; + [cpy release]; + [array release]; + } + + { + GPBAutocreatedArray *array = [[GPBAutocreatedArray alloc] init]; + + NSMutableArray *cpy = [array mutableCopy]; + XCTAssertTrue(cpy != array); // Ptr compare + XCTAssertTrue([cpy isKindOfClass:[NSMutableArray class]]); + XCTAssertFalse([cpy isKindOfClass:[GPBAutocreatedArray class]]); + XCTAssertEqual(cpy.count, (NSUInteger)0); + + NSMutableArray *cpy2 = [array mutableCopy]; + XCTAssertTrue(cpy2 != array); // Ptr compare + XCTAssertTrue(cpy2 != cpy); // Ptr compare + XCTAssertTrue([cpy2 isKindOfClass:[NSMutableArray class]]); + XCTAssertFalse([cpy2 isKindOfClass:[GPBAutocreatedArray class]]); + XCTAssertEqual(cpy2.count, (NSUInteger)0); + + [cpy2 release]; + [cpy release]; + [array release]; + } + + { + GPBAutocreatedArray *array = [[GPBAutocreatedArray alloc] init]; + [array addObject:@"foo"]; + [array addObject:@"bar"]; + + NSArray *cpy = [array copy]; + XCTAssertTrue(cpy != array); // Ptr compare + XCTAssertTrue([cpy isKindOfClass:[NSArray class]]); + XCTAssertFalse([cpy isKindOfClass:[GPBAutocreatedArray class]]); + XCTAssertEqual(cpy.count, (NSUInteger)2); + XCTAssertEqualObjects(cpy[0], @"foo"); + XCTAssertEqualObjects(cpy[1], @"bar"); + + NSArray *cpy2 = [array copy]; + XCTAssertTrue(cpy2 != array); // Ptr compare + XCTAssertTrue(cpy2 != cpy); // Ptr compare + XCTAssertTrue([cpy2 isKindOfClass:[NSArray class]]); + XCTAssertFalse([cpy2 isKindOfClass:[GPBAutocreatedArray class]]); + XCTAssertEqual(cpy2.count, (NSUInteger)2); + XCTAssertEqualObjects(cpy2[0], @"foo"); + XCTAssertEqualObjects(cpy2[1], @"bar"); + + [cpy2 release]; + [cpy release]; + [array release]; + } + + { + GPBAutocreatedArray *array = [[GPBAutocreatedArray alloc] init]; + [array addObject:@"foo"]; + [array addObject:@"bar"]; + + NSMutableArray *cpy = [array mutableCopy]; + XCTAssertTrue(cpy != array); // Ptr compare + XCTAssertTrue([cpy isKindOfClass:[NSArray class]]); + XCTAssertFalse([cpy isKindOfClass:[GPBAutocreatedArray class]]); + XCTAssertEqual(cpy.count, (NSUInteger)2); + XCTAssertEqualObjects(cpy[0], @"foo"); + XCTAssertEqualObjects(cpy[1], @"bar"); + + NSMutableArray *cpy2 = [array mutableCopy]; + XCTAssertTrue(cpy2 != array); // Ptr compare + XCTAssertTrue(cpy2 != cpy); // Ptr compare + XCTAssertTrue([cpy2 isKindOfClass:[NSArray class]]); + XCTAssertFalse([cpy2 isKindOfClass:[GPBAutocreatedArray class]]); + XCTAssertEqual(cpy2.count, (NSUInteger)2); + XCTAssertEqualObjects(cpy2[0], @"foo"); + XCTAssertEqualObjects(cpy2[1], @"bar"); + + [cpy2 release]; + [cpy release]; + [array release]; + } +} + +- (void)testIndexedSubscriptSupport { + // The base NSArray/NSMutableArray behaviors for *IndexedSubscript methods + // should still work via the methods that one has to override to make an + // NSMutableArray subclass. i.e. - this should "just work" and if these + // crash/fail, then something is wrong in how NSMutableArray is subclassed. + + GPBAutocreatedArray *array = [[GPBAutocreatedArray alloc] init]; + + [array addObject:@"foo"]; + [array addObject:@"bar"]; + XCTAssertEqual(array.count, (NSUInteger)2); + XCTAssertEqualObjects(array[0], @"foo"); + XCTAssertEqualObjects(array[1], @"bar"); + array[0] = @"foo2"; + array[2] = @"baz"; + XCTAssertEqual(array.count, (NSUInteger)3); + XCTAssertEqualObjects(array[0], @"foo2"); + XCTAssertEqualObjects(array[1], @"bar"); + XCTAssertEqualObjects(array[2], @"baz"); + + [array release]; +} + +@end diff --git a/objectivec/Tests/GPBDictionaryTests.m b/objectivec/Tests/GPBDictionaryTests.m new file mode 100644 index 00000000..52b4b328 --- /dev/null +++ b/objectivec/Tests/GPBDictionaryTests.m @@ -0,0 +1,186 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2017 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#import <Foundation/Foundation.h> +#import <XCTest/XCTest.h> + +#import "GPBDictionary.h" +#import "GPBDictionary_PackagePrivate.h" + +#import "GPBTestUtilities.h" + +#pragma mark - GPBAutocreatedDictionary Tests + +// These are hand written tests to double check some behaviors of the +// GPBAutocreatedDictionary. The GPBDictionary+[type]Tests files are generate +// tests. + +// NOTE: GPBAutocreatedDictionary is private to the library, users of the +// library should never have to directly deal with this class. + +@interface GPBAutocreatedDictionaryTests : XCTestCase +@end + +@implementation GPBAutocreatedDictionaryTests + +- (void)testEquality { + GPBAutocreatedDictionary *dict = [[GPBAutocreatedDictionary alloc] init]; + + XCTAssertTrue([dict isEqual:@{}]); + XCTAssertTrue([dict isEqualToDictionary:@{}]); + + XCTAssertFalse([dict isEqual:@{ @"foo" : @"bar" }]); + XCTAssertFalse([dict isEqualToDictionary:@{ @"foo" : @"bar" }]); + + [dict setObject:@"bar" forKey:@"foo"]; + + XCTAssertFalse([dict isEqual:@{}]); + XCTAssertFalse([dict isEqualToDictionary:@{}]); + XCTAssertTrue([dict isEqual:@{ @"foo" : @"bar" }]); + XCTAssertTrue([dict isEqualToDictionary:@{ @"foo" : @"bar" }]); + XCTAssertFalse([dict isEqual:@{ @"bar" : @"baz" }]); + XCTAssertFalse([dict isEqualToDictionary:@{ @"bar" : @"baz" }]); + + GPBAutocreatedDictionary *dict2 = [[GPBAutocreatedDictionary alloc] init]; + + XCTAssertFalse([dict isEqual:dict2]); + XCTAssertFalse([dict isEqualToDictionary:dict2]); + + [dict2 setObject:@"mumble" forKey:@"foo"]; + XCTAssertFalse([dict isEqual:dict2]); + XCTAssertFalse([dict isEqualToDictionary:dict2]); + + [dict2 setObject:@"bar" forKey:@"foo"]; + XCTAssertTrue([dict isEqual:dict2]); + XCTAssertTrue([dict isEqualToDictionary:dict2]); + + [dict2 release]; + [dict release]; +} + +- (void)testCopy { + { + GPBAutocreatedDictionary *dict = [[GPBAutocreatedDictionary alloc] init]; + + NSDictionary *cpy = [dict copy]; + XCTAssertTrue(cpy != dict); // Ptr compare + XCTAssertTrue([cpy isKindOfClass:[NSDictionary class]]); + XCTAssertFalse([cpy isKindOfClass:[GPBAutocreatedDictionary class]]); + XCTAssertEqual(cpy.count, (NSUInteger)0); + + NSDictionary *cpy2 = [dict copy]; + XCTAssertTrue(cpy2 != dict); // Ptr compare + XCTAssertTrue(cpy2 != cpy); // Ptr compare + XCTAssertTrue([cpy2 isKindOfClass:[NSDictionary class]]); + XCTAssertFalse([cpy2 isKindOfClass:[GPBAutocreatedDictionary class]]); + XCTAssertEqual(cpy2.count, (NSUInteger)0); + + [cpy2 release]; + [cpy release]; + [dict release]; + } + + { + GPBAutocreatedDictionary *dict = [[GPBAutocreatedDictionary alloc] init]; + + NSMutableDictionary *cpy = [dict mutableCopy]; + XCTAssertTrue(cpy != dict); // Ptr compare + XCTAssertTrue([cpy isKindOfClass:[NSMutableDictionary class]]); + XCTAssertFalse([cpy isKindOfClass:[GPBAutocreatedDictionary class]]); + XCTAssertEqual(cpy.count, (NSUInteger)0); + + NSMutableDictionary *cpy2 = [dict mutableCopy]; + XCTAssertTrue(cpy2 != dict); // Ptr compare + XCTAssertTrue(cpy2 != cpy); // Ptr compare + XCTAssertTrue([cpy2 isKindOfClass:[NSMutableDictionary class]]); + XCTAssertFalse([cpy2 isKindOfClass:[GPBAutocreatedDictionary class]]); + XCTAssertEqual(cpy2.count, (NSUInteger)0); + + [cpy2 release]; + [cpy release]; + [dict release]; + } + + { + GPBAutocreatedDictionary *dict = [[GPBAutocreatedDictionary alloc] init]; + dict[@"foo"] = @"bar"; + dict[@"baz"] = @"mumble"; + + NSDictionary *cpy = [dict copy]; + XCTAssertTrue(cpy != dict); // Ptr compare + XCTAssertTrue([cpy isKindOfClass:[NSDictionary class]]); + XCTAssertFalse([cpy isKindOfClass:[GPBAutocreatedDictionary class]]); + XCTAssertEqual(cpy.count, (NSUInteger)2); + XCTAssertEqualObjects(cpy[@"foo"], @"bar"); + XCTAssertEqualObjects(cpy[@"baz"], @"mumble"); + + NSDictionary *cpy2 = [dict copy]; + XCTAssertTrue(cpy2 != dict); // Ptr compare + XCTAssertTrue(cpy2 != cpy); // Ptr compare + XCTAssertTrue([cpy2 isKindOfClass:[NSDictionary class]]); + XCTAssertFalse([cpy2 isKindOfClass:[GPBAutocreatedDictionary class]]); + XCTAssertEqual(cpy2.count, (NSUInteger)2); + XCTAssertEqualObjects(cpy2[@"foo"], @"bar"); + XCTAssertEqualObjects(cpy2[@"baz"], @"mumble"); + + [cpy2 release]; + [cpy release]; + [dict release]; + } + + { + GPBAutocreatedDictionary *dict = [[GPBAutocreatedDictionary alloc] init]; + dict[@"foo"] = @"bar"; + dict[@"baz"] = @"mumble"; + + NSMutableDictionary *cpy = [dict mutableCopy]; + XCTAssertTrue(cpy != dict); // Ptr compare + XCTAssertTrue([cpy isKindOfClass:[NSMutableDictionary class]]); + XCTAssertFalse([cpy isKindOfClass:[GPBAutocreatedDictionary class]]); + XCTAssertEqual(cpy.count, (NSUInteger)2); + XCTAssertEqualObjects(cpy[@"foo"], @"bar"); + XCTAssertEqualObjects(cpy[@"baz"], @"mumble"); + + NSMutableDictionary *cpy2 = [dict mutableCopy]; + XCTAssertTrue(cpy2 != dict); // Ptr compare + XCTAssertTrue(cpy2 != cpy); // Ptr compare + XCTAssertTrue([cpy2 isKindOfClass:[NSMutableDictionary class]]); + XCTAssertFalse([cpy2 isKindOfClass:[GPBAutocreatedDictionary class]]); + XCTAssertEqual(cpy2.count, (NSUInteger)2); + XCTAssertEqualObjects(cpy2[@"foo"], @"bar"); + XCTAssertEqualObjects(cpy2[@"baz"], @"mumble"); + + [cpy2 release]; + [cpy release]; + [dict release]; + } +} + +@end diff --git a/objectivec/Tests/GPBMessageTests.m b/objectivec/Tests/GPBMessageTests.m index a3c5a6b4..c15535c5 100644 --- a/objectivec/Tests/GPBMessageTests.m +++ b/objectivec/Tests/GPBMessageTests.m @@ -1082,6 +1082,20 @@ [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. @@ -1281,6 +1295,23 @@ [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. |