Permalink
Browse files

Integrate tests verifying the behavior of using an externally integra…

…ted value transformer for mapping and object parameterization
  • Loading branch information...
1 parent e87a917 commit cee1337eec714dd6f346cd916d17f1603974a187 Blake Watters committed Sep 24, 2013
@@ -421,7 +421,7 @@ - (NSArray *)relationshipMappings
- (BOOL)transformValue:(id)inputValue toValue:(__autoreleasing id *)outputValue withPropertyMapping:(RKPropertyMapping *)propertyMapping error:(NSError *__autoreleasing *)error
{
- Class transformedValueClass = [self.objectMapping classForKeyPath:propertyMapping.destinationKeyPath];
+ Class transformedValueClass = propertyMapping.propertyValueClass ?: [self.objectMapping classForKeyPath:propertyMapping.destinationKeyPath];
if (! transformedValueClass) {
*outputValue = inputValue;
return YES;
@@ -56,6 +56,13 @@
///-------------------------------------
/**
+ Specifies the class used to represent the value of the mapped property. A value of `Nil` (which is the default value) indicates the property class is to be determined by runtime introspection.
+
+ In cases where run-time type introspection cannot be performed (such as during object parameterization) you can specify the class used to represent the value of the property being mapped.
+ */
+@property (nonatomic, strong) Class propertyValueClass;
+
+/**
A value transformer with which to process input values being mapped with the receiver. If `nil`, then the `valueTransformer` of the parent `objectMapping` will be used instead.
*/
@property (nonatomic, strong) id<RKValueTransforming> valueTransformer;
@@ -37,6 +37,7 @@
#import "RKDynamicMapping.h"
#import "RKMIMETypeSerialization.h"
#import "ISO8601DateFormatterValueTransformer.h"
+#import "RKCLLocationValueTransformer.h"
// Managed Object Serialization Testific
#import "RKHuman.h"
@@ -2519,6 +2520,25 @@ - (void)testAggregatingPropertyMappingUsingNilKeyPath
expect(user.coordinate.longitude).to.equal(200.5);
}
+- (void)testMappingDictionaryToCLLocationUsingValueTransformer
+{
+ NSDictionary *objectRepresentation = @{ @"name": @"Blake", @"latitude": @(125.55), @"longitude": @(200.5) };
+ RKObjectMapping *userMapping = [RKObjectMapping mappingForClass:[RKTestUser class]];
+ [userMapping addAttributeMappingsFromArray:@[ @"name" ]];
+ RKAttributeMapping *attributeMapping = [RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"location"];
+ attributeMapping.valueTransformer = [RKCLLocationValueTransformer locationValueTransformerWithLatitudeKey:@"latitude" longitudeKey:@"longitude"];
+ [userMapping addPropertyMapping:attributeMapping];
+ RKTestUser *user = [RKTestUser new];
+ RKMappingOperation *mappingOperation = [[RKMappingOperation alloc] initWithSourceObject:objectRepresentation destinationObject:user mapping:userMapping];
+ RKObjectMappingOperationDataSource *dataSource = [RKObjectMappingOperationDataSource new];
+ mappingOperation.dataSource = dataSource;
+ [mappingOperation start];
+ expect(mappingOperation.error).to.beNil();
+ expect(user.location).notTo.beNil();
+ expect(user.location.coordinate.latitude).to.equal(125.55);
+ expect(user.location.coordinate.longitude).to.equal(200.5);
+}
+
- (void)testThatAggregatedRelationshipMappingsAreOnlyAppliedIfThereIsAtLeastOneValueInTheRepresentation
{
NSDictionary *objectRepresentation = @{ @"name": @"Blake" };
@@ -27,6 +27,7 @@
#import "RKHuman.h"
#import "RKTestUser.h"
#import "RKCat.h"
+#import "RKCLLocationValueTransformer.h"
@interface RKMIMETypeSerialization ()
@property (nonatomic, strong) NSMutableArray *registrations;
@@ -504,6 +505,29 @@ - (void)testParameterizationOfCoreDataEntityWithDateToNestedKeypath
expect(string).to.equal(@"{\"nestedPath\":{\"birthday\":\"1970-01-01T00:00:00Z\"}}");
}
+- (void)testParameterizationFromLocationToNestedDictionaryUsingValueTransformer
+{
+ RKTestUser *user = [RKTestUser new];
+ user.name = @"Blake";
+ user.location = [[CLLocation alloc] initWithLatitude:125.55 longitude:200.5];
+ RKObjectMapping *userMapping = [RKObjectMapping requestMapping];
+ [userMapping addAttributeMappingsFromArray:@[ @"name" ]];
+ RKAttributeMapping *attributeMapping = [RKAttributeMapping attributeMappingFromKeyPath:@"location" toKeyPath:@"location"];
+ attributeMapping.propertyValueClass = [NSDictionary class];
+ attributeMapping.valueTransformer = [RKCLLocationValueTransformer locationValueTransformerWithLatitudeKey:@"latitude" longitudeKey:@"longitude"];
+ [userMapping addPropertyMapping:attributeMapping];
+
+ NSError *error = nil;
+ RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:userMapping objectClass:[RKTestUser class] rootKeyPath:nil method:RKRequestMethodAny];
+ NSDictionary *parameters = [RKObjectParameterization parametersWithObject:user requestDescriptor:requestDescriptor error:&error];
+
+ expect(parameters).notTo.beNil();
+ expect(error).to.beNil();
+ expect(parameters[@"location"]).notTo.beNil();
+ expect(parameters[@"location"][@"latitude"]).to.equal(125.55);
+ expect(parameters[@"location"][@"longitude"]).to.equal(200.5);
+}
+
@end
#pragma mark - Dynamic Request Paramterization
@@ -7,6 +7,7 @@
//
#import <Foundation/Foundation.h>
+#import <CoreLocation/CoreLocation.h>
#import "RKTestAddress.h"
#import "RKHuman.h"
@@ -38,6 +39,7 @@
@property (nonatomic, strong) NSOrderedSet *friendsOrderedSet;
@property (nonatomic, strong) NSData *data;
@property (nonatomic, strong) RKTestCoordinate *coordinate;
+@property (nonatomic, copy) CLLocation *location;
@property (nonatomic, assign) NSInteger age;
@property (nonatomic, strong) NSDate *createdAt;
@property (nonatomic, assign) NSInteger position;

0 comments on commit cee1337

Please sign in to comment.