Skip to content

Commit

Permalink
Add support for booleanAsStringTestAttribute (where the attribute t…
Browse files Browse the repository at this point in the history
…ype is `boolean` yet the data source is a string value).

Prior to this patch, this would cause a crash if encountered.
Also added a test for the new behavior.
All tests pass.
  • Loading branch information
Joshua Greene committed Feb 18, 2014
1 parent eb72053 commit 4c91c4d
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 0 deletions.
Expand Up @@ -50,6 +50,11 @@ - (id) MR_valueForKeyPath:(NSString *)keyPath fromObjectData:(id)objectData;
value = numberFromString([value description]); value = numberFromString([value description]);
} }
} }
else if (attributeType == NSBooleanAttributeType) {
if (![value isKindOfClass:[NSNumber class]] && value != [NSNull null]) {
value = [NSNumber numberWithBool:[value boolValue]];
}
}
else if (attributeType == NSStringAttributeType) { else if (attributeType == NSStringAttributeType) {
if (![value isKindOfClass:[NSString class]] && value != [NSNull null]) { if (![value isKindOfClass:[NSString class]] && value != [NSNull null]) {
value = [value description]; value = [value description];
Expand Down
5 changes: 5 additions & 0 deletions Tests/DataImport/ImportSingleEntityWithNoRelationshipsTests.m
Expand Up @@ -99,6 +99,11 @@ - (void)testImportNumberAsStringAttributeToEntity
XCTAssertEqualObjects(testEntity.numberAsStringTestAttribute, @"10248909829", @"numberAsStringTestAttribute did not contain expected value, instead found: %@", testEntity.numberAsStringTestAttribute); XCTAssertEqualObjects(testEntity.numberAsStringTestAttribute, @"10248909829", @"numberAsStringTestAttribute did not contain expected value, instead found: %@", testEntity.numberAsStringTestAttribute);
} }


- (void)testImportBooleanAsStringAttributeToEntity
{
XCTAssertTrue(testEntity.booleanAsStringTestAttribute, @"booleanFromStringTestAttribute did not contain expected value, instead found: %@", testEntity.booleanAsStringTestAttribute);
}

- (void)testImportAttributeNotInJsonData - (void)testImportAttributeNotInJsonData
{ {
NSRange rangeOfString = [testEntity.notInJsonAttribute rangeOfString:@"Core Data Model"]; NSRange rangeOfString = [testEntity.notInJsonAttribute rangeOfString:@"Core Data Model"];
Expand Down
1 change: 1 addition & 0 deletions Tests/Fixtures/SingleEntityWithNoRelationships.json
Expand Up @@ -19,4 +19,5 @@
"doubleAsStringTestAttribute": "124.3", "doubleAsStringTestAttribute": "124.3",
"floatAsStringTestAttribute": "10000000000", "floatAsStringTestAttribute": "10000000000",
"numberAsStringTestAttribute" : 10248909829, "numberAsStringTestAttribute" : 10248909829,
"booleanAsStringTestAttribute": "true",
} }
2 changes: 2 additions & 0 deletions Tests/Fixtures/SingleEntityWithNoRelationships.plist
Expand Up @@ -30,5 +30,7 @@
<string>8/5/2011 1-56-04 AM</string> <string>8/5/2011 1-56-04 AM</string>
<key>nullTestAttribte</key> <key>nullTestAttribte</key>
<string></string> <string></string>
<key>booleanAsStringTestAttribute</key>
<string>true</string>
</dict> </dict>
</plist> </plist>
Expand Up @@ -67,6 +67,7 @@
</relationship> </relationship>
</entity> </entity>
<entity name="SingleEntityWithNoRelationships" representedClassName="SingleEntityWithNoRelationships" syncable="YES"> <entity name="SingleEntityWithNoRelationships" representedClassName="SingleEntityWithNoRelationships" syncable="YES">
<attribute name="booleanAsStringTestAttribute" optional="YES" attributeType="Boolean" syncable="YES"/>
<attribute name="booleanTestAttribute" optional="YES" attributeType="Boolean" syncable="YES"/> <attribute name="booleanTestAttribute" optional="YES" attributeType="Boolean" syncable="YES"/>
<attribute name="colorTestAttribute" optional="YES" attributeType="Transformable" syncable="YES"> <attribute name="colorTestAttribute" optional="YES" attributeType="Transformable" syncable="YES">
<userInfo> <userInfo>
Expand Down
26 changes: 26 additions & 0 deletions Tests/Fixtures/TestModel/_SingleEntityWithNoRelationships.h
Expand Up @@ -6,6 +6,7 @@




extern const struct SingleEntityWithNoRelationshipsAttributes { extern const struct SingleEntityWithNoRelationshipsAttributes {
__unsafe_unretained NSString *booleanAsStringTestAttribute;
__unsafe_unretained NSString *booleanTestAttribute; __unsafe_unretained NSString *booleanTestAttribute;
__unsafe_unretained NSString *colorTestAttribute; __unsafe_unretained NSString *colorTestAttribute;
__unsafe_unretained NSString *dateTestAttribute; __unsafe_unretained NSString *dateTestAttribute;
Expand Down Expand Up @@ -76,6 +77,22 @@ extern const struct SingleEntityWithNoRelationshipsAttributes {






@property (nonatomic, strong) NSNumber* booleanAsStringTestAttribute;




@property (atomic) BOOL booleanAsStringTestAttributeValue;
- (BOOL)booleanAsStringTestAttributeValue;
- (void)setBooleanAsStringTestAttributeValue:(BOOL)value_;


//- (BOOL)validateBooleanAsStringTestAttribute:(id*)value_ error:(NSError**)error_;





@property (nonatomic, strong) NSNumber* booleanTestAttribute; @property (nonatomic, strong) NSNumber* booleanTestAttribute;




Expand Down Expand Up @@ -276,6 +293,15 @@ extern const struct SingleEntityWithNoRelationshipsAttributes {
@interface _SingleEntityWithNoRelationships (CoreDataGeneratedPrimitiveAccessors) @interface _SingleEntityWithNoRelationships (CoreDataGeneratedPrimitiveAccessors)




- (NSNumber*)primitiveBooleanAsStringTestAttribute;
- (void)setPrimitiveBooleanAsStringTestAttribute:(NSNumber*)value;

- (BOOL)primitiveBooleanAsStringTestAttributeValue;
- (void)setPrimitiveBooleanAsStringTestAttributeValue:(BOOL)value_;




- (NSNumber*)primitiveBooleanTestAttribute; - (NSNumber*)primitiveBooleanTestAttribute;
- (void)setPrimitiveBooleanTestAttribute:(NSNumber*)value; - (void)setPrimitiveBooleanTestAttribute:(NSNumber*)value;


Expand Down
34 changes: 34 additions & 0 deletions Tests/Fixtures/TestModel/_SingleEntityWithNoRelationships.m
Expand Up @@ -5,6 +5,7 @@




const struct SingleEntityWithNoRelationshipsAttributes SingleEntityWithNoRelationshipsAttributes = { const struct SingleEntityWithNoRelationshipsAttributes SingleEntityWithNoRelationshipsAttributes = {
.booleanAsStringTestAttribute = @"booleanAsStringTestAttribute",
.booleanTestAttribute = @"booleanTestAttribute", .booleanTestAttribute = @"booleanTestAttribute",
.colorTestAttribute = @"colorTestAttribute", .colorTestAttribute = @"colorTestAttribute",
.dateTestAttribute = @"dateTestAttribute", .dateTestAttribute = @"dateTestAttribute",
Expand Down Expand Up @@ -55,6 +56,11 @@ - (SingleEntityWithNoRelationshipsID*)objectID {
+ (NSSet*)keyPathsForValuesAffectingValueForKey:(NSString*)key { + (NSSet*)keyPathsForValuesAffectingValueForKey:(NSString*)key {
NSSet *keyPaths = [super keyPathsForValuesAffectingValueForKey:key]; NSSet *keyPaths = [super keyPathsForValuesAffectingValueForKey:key];


if ([key isEqualToString:@"booleanAsStringTestAttributeValue"]) {
NSSet *affectingKey = [NSSet setWithObject:@"booleanAsStringTestAttribute"];
keyPaths = [keyPaths setByAddingObjectsFromSet:affectingKey];
return keyPaths;
}
if ([key isEqualToString:@"booleanTestAttributeValue"]) { if ([key isEqualToString:@"booleanTestAttributeValue"]) {
NSSet *affectingKey = [NSSet setWithObject:@"booleanTestAttribute"]; NSSet *affectingKey = [NSSet setWithObject:@"booleanTestAttribute"];
keyPaths = [keyPaths setByAddingObjectsFromSet:affectingKey]; keyPaths = [keyPaths setByAddingObjectsFromSet:affectingKey];
Expand Down Expand Up @@ -97,6 +103,34 @@ + (NSSet*)keyPathsForValuesAffectingValueForKey:(NSString*)key {






@dynamic booleanAsStringTestAttribute;



- (BOOL)booleanAsStringTestAttributeValue {
NSNumber *result = [self booleanAsStringTestAttribute];
return [result boolValue];
}


- (void)setBooleanAsStringTestAttributeValue:(BOOL)value_ {
[self setBooleanAsStringTestAttribute:@(value_)];
}


- (BOOL)primitiveBooleanAsStringTestAttributeValue {
NSNumber *result = [self primitiveBooleanAsStringTestAttribute];
return [result boolValue];
}

- (void)setPrimitiveBooleanAsStringTestAttributeValue:(BOOL)value_ {
[self setPrimitiveBooleanAsStringTestAttribute:@(value_)];
}





@dynamic booleanTestAttribute; @dynamic booleanTestAttribute;




Expand Down

0 comments on commit 4c91c4d

Please sign in to comment.