Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

[WIP] Moved JSON dictionary and array transformers to MTLJSONAdapter #258

Closed
wants to merge 2 commits into from

3 participants

@dcaunt

Implements changes suggested in #203

Closes #203

Mantle/MTLJSONAdapter.h
@@ -170,6 +171,28 @@
@interface MTLJSONAdapter (ValueTransformers)
+// Creates a reversible transformer to convert a JSON dictionary into a MTLModel
+// object, and vice-versa.
+//
+// modelClass - The MTLModel subclass to attempt to parse from the JSON. This
+// class must conform to <MTLJSONSerializing>. This argument must
+// not be nil.
+//
+// Returns a reversible transformer which uses MTLJSONAdapter for transforming
@robb Owner
robb added a note

This should point out that we always use the class of the receiver.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Mantle/MTLJSONAdapter.h
((7 lines not shown))
+// modelClass - The MTLModel subclass to attempt to parse from the JSON. This
+// class must conform to <MTLJSONSerializing>. This argument must
+// not be nil.
+//
+// Returns a reversible transformer which uses MTLJSONAdapter for transforming
+// values back and forth.
++ (NSValueTransformer<MTLTransformerErrorHandling> *)dictionaryTransformerWithModelClass:(Class)modelClass;
+
+// Creates a reversible transformer to convert an array of JSON dictionaries
+// into an array of MTLModel objects, and vice-versa.
+//
+// modelClass - The MTLModel subclass to attempt to parse from each JSON
+// dictionary. This class must conform to <MTLJSONSerializing>.
+// This argument must not be nil.
+//
+// Returns a reversible transformer which uses MTLJSONAdapter for transforming
@robb Owner
robb added a note

Same as above.

@dcaunt
dcaunt added a note

:+1: I was going to suggest the same thing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@robb
Owner

:+1: nice!

Can you add a test that verifies that if this is invoked on a subclass of MTLJSONAdapter, the subclass is used for serialization?

@robb robb added this to the 2.0 milestone
@robb robb self-assigned this
@dcaunt

Sure, though I'm not sure how best to test it. Is there a better way than checking serialization matches that expected for a given subclass?

@robb
Owner

Hmm, not sure there is a nice way without adding mocks. I guess using a subclass with an implicit transformer is the easiest way.

@jspahrsummers

Sorry, just catching up—is this still a WIP?

@dcaunt
@robb
Owner
@jspahrsummers

:cool: No worries, just checking.

@dcaunt

Do you think we should add OCMock to verify this? I can't come up with a test that doesn't feel really hacky.

@robb
Owner

I'd rather not. What's wrong with adding a subclass like MTLTestJSONAdapter?

@robb robb was unassigned by dcaunt
@robb
Owner

@dcaunt are still interested in working on this?

@jspahrsummers jspahrsummers referenced this pull request
Merged

Move JSON transformers to MTLJSONAdapter (redux) #474

1 of 1 task complete
@jspahrsummers

Superseded by #474.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
23 Mantle/MTLJSONAdapter.h
@@ -9,6 +9,7 @@
#import <Foundation/Foundation.h>
@class MTLModel;
+@protocol MTLTransformerErrorHandling;
// A MTLModel object that supports being parsed from and serialized to JSON.
@protocol MTLJSONSerializing
@@ -170,6 +171,28 @@ extern const NSInteger MTLJSONAdapterErrorInvalidJSONDictionary;
@interface MTLJSONAdapter (ValueTransformers)
+// Creates a reversible transformer to convert a JSON dictionary into a MTLModel
+// object, and vice-versa.
+//
+// modelClass - The MTLModel subclass to attempt to parse from the JSON. This
+// class must conform to <MTLJSONSerializing>. This argument must
+// not be nil.
+//
+// Returns a reversible transformer which uses the class of the receiver for
+// transforming values back and forth.
++ (NSValueTransformer<MTLTransformerErrorHandling> *)dictionaryTransformerWithModelClass:(Class)modelClass;
+
+// Creates a reversible transformer to convert an array of JSON dictionaries
+// into an array of MTLModel objects, and vice-versa.
+//
+// modelClass - The MTLModel subclass to attempt to parse from each JSON
+// dictionary. This class must conform to <MTLJSONSerializing>.
+// This argument must not be nil.
+//
+// Returns a reversible transformer which uses the class of the receiver for
+// transforming array elements back and forth.
++ (NSValueTransformer<MTLTransformerErrorHandling> *)arrayTransformerWithModelClass:(Class)modelClass;
+
// This value transformer is used by MTLJSONAdapter to automatically convert
// NSURL properties to JSON strings and vice versa.
- (NSValueTransformer *)NSURLJSONTransformer;
View
150 Mantle/MTLJSONAdapter.m
@@ -15,6 +15,7 @@
#import "MTLTransformerErrorHandling.h"
#import "MTLReflection.h"
#import "NSValueTransformer+MTLPredefinedTransformerAdditions.h"
+#import "MTLValueTransformer.h"
NSString * const MTLJSONAdapterErrorDomain = @"MTLJSONAdapterErrorDomain";
const NSInteger MTLJSONAdapterErrorNoClassFound = 2;
@@ -355,6 +356,155 @@ - (NSValueTransformer *)transformerForModelPropertiesOfObjCType:(const char *)ob
@implementation MTLJSONAdapter (ValueTransformers)
++ (NSValueTransformer<MTLTransformerErrorHandling> *)dictionaryTransformerWithModelClass:(Class)modelClass {
+ NSParameterAssert([modelClass isSubclassOfClass:MTLModel.class]);
+ NSParameterAssert([modelClass conformsToProtocol:@protocol(MTLJSONSerializing)]);
+
+ return [MTLValueTransformer
+ transformerUsingForwardBlock:^ id (id JSONDictionary, BOOL *success, NSError **error) {
+ if (JSONDictionary == nil) return nil;
+
+ if (![JSONDictionary isKindOfClass:NSDictionary.class]) {
+ if (error != NULL) {
+ NSDictionary *userInfo = @{
+ NSLocalizedDescriptionKey: NSLocalizedString(@"Could not convert JSON dictionary to model object", @""),
+ NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:NSLocalizedString(@"Expected an NSDictionary, got: %@", @""), JSONDictionary],
+ MTLTransformerErrorHandlingInputValueErrorKey : JSONDictionary
+ };
+
+ *error = [NSError errorWithDomain:MTLTransformerErrorHandlingErrorDomain code:MTLTransformerErrorHandlingErrorInvalidInput userInfo:userInfo];
+ }
+ *success = NO;
+ return nil;
+ }
+
+ return [self modelOfClass:modelClass fromJSONDictionary:JSONDictionary error:error];
+ }
+ reverseBlock:^ id (id model, BOOL *success, NSError **error) {
+ if (model == nil) return nil;
+
+ if (![model isKindOfClass:MTLModel.class] || ![model conformsToProtocol:@protocol(MTLJSONSerializing)]) {
+ if (error != NULL) {
+ NSDictionary *userInfo = @{
+ NSLocalizedDescriptionKey: NSLocalizedString(@"Could not convert model object to JSON dictionary", @""),
+ NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:NSLocalizedString(@"Expected a MTLModel object conforming to <MTLJSONSerializing>, got: %@.", @""), model],
+ MTLTransformerErrorHandlingInputValueErrorKey : model
+ };
+
+ *error = [NSError errorWithDomain:MTLTransformerErrorHandlingErrorDomain code:MTLTransformerErrorHandlingErrorInvalidInput userInfo:userInfo];
+ }
+ *success = NO;
+ return nil;
+ }
+
+ return [self JSONDictionaryFromModel:model error:error];
+ }];
+}
+
++ (NSValueTransformer<MTLTransformerErrorHandling> *)arrayTransformerWithModelClass:(Class)modelClass {
+ id<MTLTransformerErrorHandling> dictionaryTransformer = [self dictionaryTransformerWithModelClass:modelClass];
+
+ return [MTLValueTransformer
+ transformerUsingForwardBlock:^ id (NSArray *dictionaries, BOOL *success, NSError **error) {
+ if (dictionaries == nil) return nil;
+
+ if (![dictionaries isKindOfClass:NSArray.class]) {
+ if (error != NULL) {
+ NSDictionary *userInfo = @{
+ NSLocalizedDescriptionKey: NSLocalizedString(@"Could not convert JSON array to model array", @""),
+ NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:NSLocalizedString(@"Expected an NSArray, got: %@.", @""), dictionaries],
+ MTLTransformerErrorHandlingInputValueErrorKey : dictionaries
+ };
+
+ *error = [NSError errorWithDomain:MTLTransformerErrorHandlingErrorDomain code:MTLTransformerErrorHandlingErrorInvalidInput userInfo:userInfo];
+ }
+ *success = NO;
+ return nil;
+ }
+
+ NSMutableArray *models = [NSMutableArray arrayWithCapacity:dictionaries.count];
+ for (id JSONDictionary in dictionaries) {
+ if (JSONDictionary == NSNull.null) {
+ [models addObject:NSNull.null];
+ continue;
+ }
+
+ if (![JSONDictionary isKindOfClass:NSDictionary.class]) {
+ if (error != NULL) {
+ NSDictionary *userInfo = @{
+ NSLocalizedDescriptionKey: NSLocalizedString(@"Could not convert JSON array to model array", @""),
+ NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:NSLocalizedString(@"Expected an NSDictionary or an NSNull, got: %@.", @""), JSONDictionary],
+ MTLTransformerErrorHandlingInputValueErrorKey : JSONDictionary
+ };
+
+ *error = [NSError errorWithDomain:MTLTransformerErrorHandlingErrorDomain code:MTLTransformerErrorHandlingErrorInvalidInput userInfo:userInfo];
+ }
+ *success = NO;
+ return nil;
+ }
+
+ id model = [dictionaryTransformer transformedValue:JSONDictionary success:success error:error];
+
+ if (*success == NO) return nil;
+
+ if (model == nil) continue;
+
+ [models addObject:model];
+ }
+
+ return models;
+ }
+ reverseBlock:^ id (NSArray *models, BOOL *success, NSError **error) {
+ if (models == nil) return nil;
+
+ if (![models isKindOfClass:NSArray.class]) {
+ if (error != NULL) {
+ NSDictionary *userInfo = @{
+ NSLocalizedDescriptionKey: NSLocalizedString(@"Could not convert model array to JSON array", @""),
+ NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:NSLocalizedString(@"Expected an NSArray, got: %@.", @""), models],
+ MTLTransformerErrorHandlingInputValueErrorKey : models
+ };
+
+ *error = [NSError errorWithDomain:MTLTransformerErrorHandlingErrorDomain code:MTLTransformerErrorHandlingErrorInvalidInput userInfo:userInfo];
+ }
+ *success = NO;
+ return nil;
+ }
+
+ NSMutableArray *dictionaries = [NSMutableArray arrayWithCapacity:models.count];
+ for (id model in models) {
+ if (model == NSNull.null) {
+ [dictionaries addObject:NSNull.null];
+ continue;
+ }
+
+ if (![model isKindOfClass:MTLModel.class]) {
+ if (error != NULL) {
+ NSDictionary *userInfo = @{
+ NSLocalizedDescriptionKey: NSLocalizedString(@"Could not convert JSON array to model array", @""),
+ NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:NSLocalizedString(@"Expected a MTLModel or an NSNull, got: %@.", @""), model],
+ MTLTransformerErrorHandlingInputValueErrorKey : model
+ };
+
+ *error = [NSError errorWithDomain:MTLTransformerErrorHandlingErrorDomain code:MTLTransformerErrorHandlingErrorInvalidInput userInfo:userInfo];
+ }
+ *success = NO;
+ return nil;
+ }
+
+ NSDictionary *dict = [dictionaryTransformer reverseTransformedValue:model success:success error:error];
+
+ if (*success == NO) return nil;
+
+ if (dict == nil) continue;
+
+ [dictionaries addObject:dict];
+ }
+
+ return dictionaries;
+ }];
+}
+
- (NSValueTransformer *)NSURLJSONTransformer {
return [NSValueTransformer valueTransformerForName:MTLURLValueTransformerName];
}
View
26 Mantle/NSValueTransformer+MTLPredefinedTransformerAdditions.h
@@ -24,28 +24,6 @@ extern NSString * const MTLBooleanValueTransformerName;
@interface NSValueTransformer (MTLPredefinedTransformerAdditions)
-// Creates a reversible transformer to convert a JSON dictionary into a MTLModel
-// object, and vice-versa.
-//
-// modelClass - The MTLModel subclass to attempt to parse from the JSON. This
-// class must conform to <MTLJSONSerializing>. This argument must
-// not be nil.
-//
-// Returns a reversible transformer which uses MTLJSONAdapter for transforming
-// values back and forth.
-+ (NSValueTransformer<MTLTransformerErrorHandling> *)mtl_JSONDictionaryTransformerWithModelClass:(Class)modelClass;
-
-// Creates a reversible transformer to convert an array of JSON dictionaries
-// into an array of MTLModel objects, and vice-versa.
-//
-// modelClass - The MTLModel subclass to attempt to parse from each JSON
-// dictionary. This class must conform to <MTLJSONSerializing>.
-// This argument must not be nil.
-//
-// Returns a reversible transformer which uses MTLJSONAdapter for transforming
-// array elements back and forth.
-+ (NSValueTransformer<MTLTransformerErrorHandling> *)mtl_JSONArrayTransformerWithModelClass:(Class)modelClass;
-
// A reversible value transformer to transform between the keys and objects of a
// dictionary.
//
@@ -63,4 +41,8 @@ extern NSString * const MTLBooleanValueTransformerName;
// transformations, and from objects to keys for reverse transformations.
+ (NSValueTransformer<MTLTransformerErrorHandling> *)mtl_valueMappingTransformerWithDictionary:(NSDictionary *)dictionary;
++ (NSValueTransformer<MTLTransformerErrorHandling> *)mtl_JSONDictionaryTransformerWithModelClass:(Class)modelClass __attribute__((deprecated("Replaced by +[MTLJSONAdapter dictionaryTransformerWithModelClass:]")));
+
++ (NSValueTransformer<MTLTransformerErrorHandling> *)mtl_JSONArrayTransformerWithModelClass:(Class)modelClass __attribute__((deprecated("Replaced by +[MTLJSONAdapter arrayTransformerWithModelClass:]")));
+
@end
View
163 Mantle/NSValueTransformer+MTLPredefinedTransformerAdditions.m
@@ -103,156 +103,6 @@ + (void)load {
#pragma mark Customizable Transformers
-+ (NSValueTransformer<MTLTransformerErrorHandling> *)mtl_JSONDictionaryTransformerWithModelClass:(Class)modelClass {
- NSParameterAssert([modelClass isSubclassOfClass:MTLModel.class]);
- NSParameterAssert([modelClass conformsToProtocol:@protocol(MTLJSONSerializing)]);
-
- return [MTLValueTransformer
- transformerUsingForwardBlock:^ id (id JSONDictionary, BOOL *success, NSError **error) {
- if (JSONDictionary == nil) return nil;
-
- if (![JSONDictionary isKindOfClass:NSDictionary.class]) {
- if (error != NULL) {
- NSDictionary *userInfo = @{
- NSLocalizedDescriptionKey: NSLocalizedString(@"Could not convert JSON dictionary to model object", @""),
- NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:NSLocalizedString(@"Expected an NSDictionary, got: %@", @""), JSONDictionary],
- MTLTransformerErrorHandlingInputValueErrorKey : JSONDictionary
- };
-
- *error = [NSError errorWithDomain:MTLTransformerErrorHandlingErrorDomain code:MTLTransformerErrorHandlingErrorInvalidInput userInfo:userInfo];
- }
- *success = NO;
- return nil;
- }
-
- return [MTLJSONAdapter modelOfClass:modelClass fromJSONDictionary:JSONDictionary error:error];
- }
- reverseBlock:^ id (id model, BOOL *success, NSError **error) {
- if (model == nil) return nil;
-
- if (![model isKindOfClass:MTLModel.class] || ![model conformsToProtocol:@protocol(MTLJSONSerializing)]) {
- if (error != NULL) {
- NSDictionary *userInfo = @{
- NSLocalizedDescriptionKey: NSLocalizedString(@"Could not convert model object to JSON dictionary", @""),
- NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:NSLocalizedString(@"Expected a MTLModel object conforming to <MTLJSONSerializing>, got: %@.", @""), model],
- MTLTransformerErrorHandlingInputValueErrorKey : model
- };
-
- *error = [NSError errorWithDomain:MTLTransformerErrorHandlingErrorDomain code:MTLTransformerErrorHandlingErrorInvalidInput userInfo:userInfo];
- }
- *success = NO;
- return nil;
- }
-
- return [MTLJSONAdapter JSONDictionaryFromModel:model error:error];
- }];
-}
-
-+ (NSValueTransformer<MTLTransformerErrorHandling> *)mtl_JSONArrayTransformerWithModelClass:(Class)modelClass {
- id<MTLTransformerErrorHandling> dictionaryTransformer = [self mtl_JSONDictionaryTransformerWithModelClass:modelClass];
-
- return [MTLValueTransformer
- transformerUsingForwardBlock:^ id (NSArray *dictionaries, BOOL *success, NSError **error) {
- if (dictionaries == nil) return nil;
-
- if (![dictionaries isKindOfClass:NSArray.class]) {
- if (error != NULL) {
-
- NSDictionary *userInfo = @{
- NSLocalizedDescriptionKey: NSLocalizedString(@"Could not convert JSON array to model array", @""),
- NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:NSLocalizedString(@"Expected an NSArray, got: %@.", @""), dictionaries],
- MTLTransformerErrorHandlingInputValueErrorKey : dictionaries
- };
-
- *error = [NSError errorWithDomain:MTLTransformerErrorHandlingErrorDomain code:MTLTransformerErrorHandlingErrorInvalidInput userInfo:userInfo];
- }
- *success = NO;
- return nil;
- }
-
- NSMutableArray *models = [NSMutableArray arrayWithCapacity:dictionaries.count];
- for (id JSONDictionary in dictionaries) {
- if (JSONDictionary == NSNull.null) {
- [models addObject:NSNull.null];
- continue;
- }
-
- if (![JSONDictionary isKindOfClass:NSDictionary.class]) {
- if (error != NULL) {
- NSDictionary *userInfo = @{
- NSLocalizedDescriptionKey: NSLocalizedString(@"Could not convert JSON array to model array", @""),
- NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:NSLocalizedString(@"Expected an NSDictionary or an NSNull, got: %@.", @""), JSONDictionary],
- MTLTransformerErrorHandlingInputValueErrorKey : JSONDictionary
- };
-
- *error = [NSError errorWithDomain:MTLTransformerErrorHandlingErrorDomain code:MTLTransformerErrorHandlingErrorInvalidInput userInfo:userInfo];
- }
- *success = NO;
- return nil;
- }
-
- id model = [dictionaryTransformer transformedValue:JSONDictionary success:success error:error];
-
- if (*success == NO) return nil;
-
- if (model == nil) continue;
-
- [models addObject:model];
- }
-
- return models;
- }
- reverseBlock:^ id (NSArray *models, BOOL *success, NSError **error) {
- if (models == nil) return nil;
-
- if (![models isKindOfClass:NSArray.class]) {
- if (error != NULL) {
- NSDictionary *userInfo = @{
- NSLocalizedDescriptionKey: NSLocalizedString(@"Could not convert model array to JSON array", @""),
- NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:NSLocalizedString(@"Expected an NSArray, got: %@.", @""), models],
- MTLTransformerErrorHandlingInputValueErrorKey : models
- };
-
- *error = [NSError errorWithDomain:MTLTransformerErrorHandlingErrorDomain code:MTLTransformerErrorHandlingErrorInvalidInput userInfo:userInfo];
- }
- *success = NO;
- return nil;
- }
-
- NSMutableArray *dictionaries = [NSMutableArray arrayWithCapacity:models.count];
- for (id model in models) {
- if (model == NSNull.null) {
- [dictionaries addObject:NSNull.null];
- continue;
- }
-
- if (![model isKindOfClass:MTLModel.class]) {
- if (error != NULL) {
- NSDictionary *userInfo = @{
- NSLocalizedDescriptionKey: NSLocalizedString(@"Could not convert JSON array to model array", @""),
- NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:NSLocalizedString(@"Expected a MTLModel or an NSNull, got: %@.", @""), model],
- MTLTransformerErrorHandlingInputValueErrorKey : model
- };
-
- *error = [NSError errorWithDomain:MTLTransformerErrorHandlingErrorDomain code:MTLTransformerErrorHandlingErrorInvalidInput userInfo:userInfo];
- }
- *success = NO;
- return nil;
- }
-
- NSDictionary *dict = [dictionaryTransformer reverseTransformedValue:model success:success error:error];
-
- if (*success == NO) return nil;
-
- if (dict == nil) continue;
-
- [dictionaries addObject:dict];
- }
-
- return dictionaries;
- }];
-}
-
+ (NSValueTransformer<MTLTransformerErrorHandling> *)mtl_valueMappingTransformerWithDictionary:(NSDictionary *)dictionary {
NSParameterAssert(dictionary != nil);
NSParameterAssert(dictionary.count == [[NSSet setWithArray:dictionary.allValues] count]);
@@ -306,4 +156,17 @@ + (void)load {
}];
}
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wdeprecated-implementations"
+
++ (NSValueTransformer<MTLTransformerErrorHandling> *)mtl_JSONDictionaryTransformerWithModelClass:(Class)modelClass {
+ return [MTLJSONAdapter dictionaryTransformerWithModelClass:modelClass];
+}
+
++ (NSValueTransformer<MTLTransformerErrorHandling> *)mtl_JSONArrayTransformerWithModelClass:(Class)modelClass {
+ return [MTLJSONAdapter arrayTransformerWithModelClass:modelClass];
+}
+
+#pragma clang diagnostic pop
+
@end
View
85 MantleTests/MTLJSONAdapterSpec.m
@@ -7,6 +7,7 @@
//
#import "MTLTestModel.h"
+#import "MTLTransformerErrorExamples.h"
SpecBegin(MTLJSONAdapter)
@@ -248,4 +249,88 @@
expect(error.code).to.equal(MTLJSONAdapterErrorNoClassFound);
});
+describe(@"JSON transformers", ^{
+ describe(@"dictionary transformer", ^{
+ __block NSValueTransformer *transformer;
+
+ __block MTLTestModel *model;
+ __block NSDictionary *JSONDictionary;
+
+ before(^{
+ model = [[MTLTestModel alloc] init];
+ JSONDictionary = [MTLJSONAdapter JSONDictionaryFromModel:model error:NULL];
+
+ transformer = [MTLJSONAdapter dictionaryTransformerWithModelClass:MTLTestModel.class];
+ expect(transformer).notTo.beNil();
+ });
+
+ it(@"should transform a JSON dictionary into a model", ^{
+ expect([transformer transformedValue:JSONDictionary]).to.equal(model);
+ });
+
+ it(@"should transform a model into a JSON dictionary", ^{
+ expect([transformer.class allowsReverseTransformation]).to.beTruthy();
+ expect([transformer reverseTransformedValue:model]).to.equal(JSONDictionary);
+ });
+
+ itShouldBehaveLike(MTLTransformerErrorExamples, ^{
+ return @{
+ MTLTransformerErrorExamplesTransformer: transformer,
+ MTLTransformerErrorExamplesInvalidTransformationInput: NSNull.null,
+ MTLTransformerErrorExamplesInvalidReverseTransformationInput: NSNull.null
+ };
+ });
+ });
+
+ describe(@"external representation array transformer", ^{
+ __block NSValueTransformer *transformer;
+
+ __block NSArray *models;
+ __block NSArray *JSONDictionaries;
+
+ beforeEach(^{
+ NSMutableArray *uniqueModels = [NSMutableArray array];
+ NSMutableArray *mutableDictionaries = [NSMutableArray array];
+
+ for (NSUInteger i = 0; i < 10; i++) {
+ MTLTestModel *model = [[MTLTestModel alloc] init];
+ model.count = i;
+
+ [uniqueModels addObject:model];
+
+ NSDictionary *dict = [MTLJSONAdapter JSONDictionaryFromModel:model error:NULL];
+ expect(dict).notTo.beNil();
+
+ [mutableDictionaries addObject:dict];
+ }
+
+ uniqueModels[2] = NSNull.null;
+ mutableDictionaries[2] = NSNull.null;
+
+ models = [uniqueModels copy];
+ JSONDictionaries = [mutableDictionaries copy];
+
+ transformer = [MTLJSONAdapter arrayTransformerWithModelClass:MTLTestModel.class];
+ expect(transformer).notTo.beNil();
+ });
+
+ it(@"should transform JSON dictionaries into models", ^{
+ expect([transformer transformedValue:JSONDictionaries]).to.equal(models);
+ });
+
+ it(@"should transform models into JSON dictionaries", ^{
+ expect([transformer.class allowsReverseTransformation]).to.beTruthy();
+ expect([transformer reverseTransformedValue:models]).to.equal(JSONDictionaries);
+ });
+
+ itShouldBehaveLike(MTLTransformerErrorExamples, ^{
+ return @{
+ MTLTransformerErrorExamplesTransformer: transformer,
+ MTLTransformerErrorExamplesInvalidTransformationInput: NSNull.null,
+ MTLTransformerErrorExamplesInvalidReverseTransformationInput: NSNull.null
+ };
+ });
+ });
+});
+
SpecEnd
View
85 MantleTests/MTLPredefinedTransformerAdditionsSpec.m
@@ -86,91 +86,6 @@
});
});
-
-describe(@"JSON transformers", ^{
- describe(@"dictionary transformer", ^{
- __block NSValueTransformer *transformer;
-
- __block MTLTestModel *model;
- __block NSDictionary *JSONDictionary;
-
- before(^{
- model = [[MTLTestModel alloc] init];
- JSONDictionary = [MTLJSONAdapter JSONDictionaryFromModel:model error:NULL];
-
- transformer = [NSValueTransformer mtl_JSONDictionaryTransformerWithModelClass:MTLTestModel.class];
- expect(transformer).notTo.beNil();
- });
-
- it(@"should transform a JSON dictionary into a model", ^{
- expect([transformer transformedValue:JSONDictionary]).to.equal(model);
- });
-
- it(@"should transform a model into a JSON dictionary", ^{
- expect([transformer.class allowsReverseTransformation]).to.beTruthy();
- expect([transformer reverseTransformedValue:model]).to.equal(JSONDictionary);
- });
-
- itShouldBehaveLike(MTLTransformerErrorExamples, ^{
- return @{
- MTLTransformerErrorExamplesTransformer: transformer,
- MTLTransformerErrorExamplesInvalidTransformationInput: NSNull.null,
- MTLTransformerErrorExamplesInvalidReverseTransformationInput: NSNull.null
- };
- });
- });
-
- describe(@"external representation array transformer", ^{
- __block NSValueTransformer *transformer;
-
- __block NSArray *models;
- __block NSArray *JSONDictionaries;
-
- beforeEach(^{
- NSMutableArray *uniqueModels = [NSMutableArray array];
- NSMutableArray *mutableDictionaries = [NSMutableArray array];
-
- for (NSUInteger i = 0; i < 10; i++) {
- MTLTestModel *model = [[MTLTestModel alloc] init];
- model.count = i;
-
- [uniqueModels addObject:model];
-
- NSDictionary *dict = [MTLJSONAdapter JSONDictionaryFromModel:model error:NULL];
- expect(dict).notTo.beNil();
-
- [mutableDictionaries addObject:dict];
- }
-
- uniqueModels[2] = NSNull.null;
- mutableDictionaries[2] = NSNull.null;
-
- models = [uniqueModels copy];
- JSONDictionaries = [mutableDictionaries copy];
-
- transformer = [NSValueTransformer mtl_JSONArrayTransformerWithModelClass:MTLTestModel.class];
- expect(transformer).notTo.beNil();
- });
-
- it(@"should transform JSON dictionaries into models", ^{
- expect([transformer transformedValue:JSONDictionaries]).to.equal(models);
- });
-
- it(@"should transform models into JSON dictionaries", ^{
- expect([transformer.class allowsReverseTransformation]).to.beTruthy();
- expect([transformer reverseTransformedValue:models]).to.equal(JSONDictionaries);
- });
-
- itShouldBehaveLike(MTLTransformerErrorExamples, ^{
- return @{
- MTLTransformerErrorExamplesTransformer: transformer,
- MTLTransformerErrorExamplesInvalidTransformationInput: NSNull.null,
- MTLTransformerErrorExamplesInvalidReverseTransformationInput: NSNull.null
- };
- });
- });
-});
-
describe(@"value mapping transformer", ^{
__block NSValueTransformer *transformer;
Something went wrong with that request. Please try again.