From 67b7761aceaa8f078e3be6959bd30b9c7ed49771 Mon Sep 17 00:00:00 2001 From: Joan Martin Date: Fri, 11 Dec 2015 16:24:33 +0100 Subject: [PATCH] Improvimg custom container mapping. Adding tests. --- NSObject+Motis.m | 6 +- SampleProject/Motis Tests/MJValidationTest.m | 55 +++++++++++++++++++ .../MJTestMotisMappingObject.m | 6 ++ .../Validation Test/MJTestObject.h | 6 ++ 4 files changed, 72 insertions(+), 1 deletion(-) diff --git a/NSObject+Motis.m b/NSObject+Motis.m index 7db638a..615b79e 100644 --- a/NSObject+Motis.m +++ b/NSObject+Motis.m @@ -1134,6 +1134,11 @@ - (BOOL)mts_validateAutomaticallyValue:(inout __autoreleasing id *)ioValue toCla *ioValue = [NSMutableArray arrayWithArray:*ioValue]; return *ioValue != nil; } + else if ([typeClass isSubclassOfClass:NSArray.class]) + { + *ioValue = [NSArray arrayWithArray:*ioValue]; + return *ioValue != nil; + } else if ([typeClass isSubclassOfClass:NSMutableSet.class]) { *ioValue = [NSMutableSet setWithArray:*ioValue]; @@ -1154,7 +1159,6 @@ - (BOOL)mts_validateAutomaticallyValue:(inout __autoreleasing id *)ioValue toCla *ioValue = [NSOrderedSet orderedSetWithArray:*ioValue]; return *ioValue != nil; } - return *ioValue != nil; } else if ([*ioValue isKindOfClass:NSDictionary.class]) // <-- DICTIONARIES { diff --git a/SampleProject/Motis Tests/MJValidationTest.m b/SampleProject/Motis Tests/MJValidationTest.m index 5869174..74fc1d2 100644 --- a/SampleProject/Motis Tests/MJValidationTest.m +++ b/SampleProject/Motis Tests/MJValidationTest.m @@ -629,6 +629,61 @@ - (void)testArrayToDateArray XCTAssertEqual(array.count, object.datesArray.count, @"Date array conversion failed"); } +- (void)testMutableArray +{ + MJTestMotisMappingObject *object = [MJTestMotisMappingObject new]; + + NSArray *array = @[@1, @"string"]; + [object mts_setValue:array forKey:@"mutable_array"]; + + XCTAssert([object.mutableArray isKindOfClass:NSMutableArray.class], @"Failed to create mutable array"); + XCTAssertEqualObjects(array, object.mutableArray, @"Failed to map mutable array %@", array.description); +} + +- (void)testSet +{ + MJTestMotisMappingObject *object = [MJTestMotisMappingObject new]; + + NSArray *array = @[@1, @"string"]; + [object mts_setValue:array forKey:@"set"]; + + XCTAssert([object.set isKindOfClass:NSSet.class], @"Failed to create set"); + XCTAssertEqualObjects([NSSet setWithArray:array], object.set, @"Failed to map set %@", array.description); +} + +- (void)testMutableSet +{ + MJTestMotisMappingObject *object = [MJTestMotisMappingObject new]; + + NSArray *array = @[@1, @"string"]; + [object mts_setValue:array forKey:@"mutable_set"]; + + XCTAssert([object.mutableSet isKindOfClass:NSMutableSet.class], @"Failed to create mutable set"); + XCTAssertEqualObjects([NSSet setWithArray:array], object.mutableSet, @"Failed to map mutable set %@", array.description); +} + +- (void)testOrderedSet +{ + MJTestMotisMappingObject *object = [MJTestMotisMappingObject new]; + + NSArray *array = @[@1, @"string"]; + [object mts_setValue:array forKey:@"ordered_set"]; + + XCTAssert([object.orderedSet isKindOfClass:NSOrderedSet.class], @"Failed to create ordered set"); + XCTAssertEqualObjects([NSOrderedSet orderedSetWithArray:array], object.orderedSet, @"Failed to map ordered set %@", array.description); +} + +- (void)testMutableOrderedSet +{ + MJTestMotisMappingObject *object = [MJTestMotisMappingObject new]; + + NSArray *array = @[@1, @"string"]; + [object mts_setValue:array forKey:@"mutable_ordered_set"]; + + XCTAssert([object.mutableOrderedSet isKindOfClass:NSMutableOrderedSet.class], @"Failed to create mutable ordered set"); + XCTAssertEqualObjects([NSMutableOrderedSet orderedSetWithArray:array], object.mutableOrderedSet, @"Failed to map mutable ordered set %@", array.description); +} + #pragma mark - UNDEFINED MAPPINGS // ------------------------------------------------------------------------------------------------------------------------ // diff --git a/SampleProject/Motis Tests/Validation Test/MJTestMotisMappingObject.m b/SampleProject/Motis Tests/Validation Test/MJTestMotisMappingObject.m index 5e12a95..5a087ee 100644 --- a/SampleProject/Motis Tests/Validation Test/MJTestMotisMappingObject.m +++ b/SampleProject/Motis Tests/Validation Test/MJTestMotisMappingObject.m @@ -43,6 +43,12 @@ + (NSDictionary*)mts_mapping @"url_array": mts_key(urlsArray), @"date_array": mts_key(datesArray), + @"mutable_array": mts_key(mutableArray), + @"set": mts_key(set), + @"mutable_set": mts_key(mutableSet), + @"ordered_set": mts_key(orderedSet), + @"mutable_ordered_set": mts_key(mutableOrderedSet), + @"unsigned_enum": mts_key(unsignedEnum), @"signed_enum": mts_key(signedEnum), diff --git a/SampleProject/Motis Tests/Validation Test/MJTestObject.h b/SampleProject/Motis Tests/Validation Test/MJTestObject.h index 4932d25..779f8d7 100644 --- a/SampleProject/Motis Tests/Validation Test/MJTestObject.h +++ b/SampleProject/Motis Tests/Validation Test/MJTestObject.h @@ -56,6 +56,12 @@ typedef NS_ENUM(NSInteger, MJSignedEnum) @property (nonatomic, strong) NSArray *urlsArray; @property (nonatomic, strong) NSArray *datesArray; +@property (nonatomic, strong) NSMutableArray *mutableArray; +@property (nonatomic, strong) NSSet *set; +@property (nonatomic, strong) NSMutableSet *mutableSet; +@property (nonatomic, strong) NSOrderedSet *orderedSet; +@property (nonatomic, strong) NSMutableOrderedSet *mutableOrderedSet; + @property (nonatomic, assign) MJUnsignedEnum unsignedEnum; @property (nonatomic, assign) MJSignedEnum signedEnum;