Permalink
Browse files

Fix color parsing tests for both Mac and iOS

  • Loading branch information...
1 parent 4a1d37a commit fdc66fcb368b7482645821a01e2299925df9cd4f @casademora casademora committed Jul 24, 2011
View
@@ -15,4 +15,5 @@ build/
*.mode2v3
UserInterfaceState.xcuserstate
-/Magical Record.xcodeproj/project.xcworkspace/xcuserdata/saul.xcuserdatad/UserInterfaceState.xcuserstate
+/Magical Record.xcodeproj/project.xcworkspace/xcuserdata/saul.xcuserdatad/UserInterfaceState.xcuserstate
+/Magical Record.xcodeproj/xcuserdata/saul.xcuserdatad/xcdebugger/Breakpoints.xcbkptlist
@@ -1,49 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<Bucket
- type = "1"
- version = "1.0">
- <FileBreakpoints>
- <FileBreakpoint
- shouldBeEnabled = "No"
- ignoreCount = "0"
- continueAfterRunningActions = "No"
- isPathRelative = "1"
- filePath = "Source/Categories/NSManagedObject+MagicalDataImport.m"
- timestampString = "333180902.999718"
- startingColumnNumber = "9223372036854775807"
- endingColumnNumber = "9223372036854775807"
- startingLineNumber = "48"
- endingLineNumber = "48"
- landmarkName = "-mr_attributeValueFromJSONDicationary:forAttribute:"
- landmarkType = "5">
- </FileBreakpoint>
- <FileBreakpoint
- shouldBeEnabled = "No"
- ignoreCount = "0"
- continueAfterRunningActions = "No"
- isPathRelative = "1"
- filePath = "Unit Tests/ImportSingleEntityWithRelatedEntitiesTests.m"
- timestampString = "333180930.947903"
- startingColumnNumber = "9223372036854775807"
- endingColumnNumber = "9223372036854775807"
- startingLineNumber = "29"
- endingLineNumber = "29"
- landmarkName = "-setUp"
- landmarkType = "5">
- </FileBreakpoint>
- <FileBreakpoint
- shouldBeEnabled = "No"
- ignoreCount = "0"
- continueAfterRunningActions = "No"
- isPathRelative = "1"
- filePath = "Source/Categories/NSManagedObject+MagicalDataImport.m"
- timestampString = "333181283.225817"
- startingColumnNumber = "9223372036854775807"
- endingColumnNumber = "9223372036854775807"
- startingLineNumber = "102"
- endingLineNumber = "102"
- landmarkName = "-mr_addObject:forRelationship:"
- landmarkType = "5">
- </FileBreakpoint>
- </FileBreakpoints>
-</Bucket>
@@ -34,7 +34,7 @@ - (id) mr_attributeValueFromJSONDicationary:(NSDictionary *)jsonData forAttribut
NSString *desiredAttributeType = [[attributeInfo userInfo] valueForKey:kNSManagedObjectAttributeJSONValueClassNameKey];
if (desiredAttributeType)
{
- if ([desiredAttributeType hasSuffix:@"NSColor"])
+ if ([desiredAttributeType hasSuffix:@"Color"])
{
value = ColorFromString(value);
}
@@ -156,32 +156,39 @@ + (void) performSaveDataOperationInBackgroundWithBlock:(CoreDataBlock)block comp
return [formatter dateFromString:value];
}
-#ifdef MAC_PLATFORM_ONLY
-
-NSColor * NSColorFromString(NSString *serializedColor)
+NSInteger* newColorComponentsFromString(NSString *serializedColor);
+NSInteger* newColorComponentsFromString(NSString *serializedColor)
{
NSScanner *colorScanner = [NSScanner scannerWithString:serializedColor];
NSString *colorType;
[colorScanner scanUpToString:@"(" intoString:&colorType];
- NSColor *color = nil;
+ NSInteger *componentValues = malloc(4 * sizeof(NSInteger));
if ([colorType hasPrefix:@"rgba"])
{
NSCharacterSet *rgbaCharacterSet = [NSCharacterSet characterSetWithCharactersInString:@"(,)"];
- NSInteger componentValues[4];
+
NSInteger *componentValue = componentValues;
while (![colorScanner isAtEnd])
{
[colorScanner scanCharactersFromSet:rgbaCharacterSet intoString:nil];
[colorScanner scanInteger:componentValue];
componentValue++;
}
- color = [NSColor colorWithDeviceRed:(componentValues[0] / 255.)
+ }
+ return componentValues;
+}
+
+#ifdef MAC_PLATFORM_ONLY
+
+NSColor * NSColorFromString(NSString *serializedColor)
+{
+ NSInteger *componentValues = newColorComponentsFromString(serializedColor);
+ NSColor *color = [NSColor colorWithDeviceRed:(componentValues[0] / 255.)
green:(componentValues[1] / 255.)
blue:(componentValues[2] / 255.)
alpha:componentValues[3]];
- }
-
+ free(componentValues);
return color;
}
id (*ColorFromString)(NSString *) = NSColorFromString;
@@ -190,7 +197,14 @@ + (void) performSaveDataOperationInBackgroundWithBlock:(CoreDataBlock)block comp
UIColor * UIColorFromString(NSString *serializedColor)
{
- return nil;
+ NSInteger *componentValues = newColorComponentsFromString(serializedColor);
+ UIColor *color = [UIColor colorWithRed:(componentValues[0] / 255.)
+ green:(componentValues[1] / 255.)
+ blue:(componentValues[2] / 255.)
+ alpha:componentValues[3]];
+
+ free(componentValues);
+ return color;
}
id (*ColorFromString)(NSString *) = UIColorFromString;
@@ -78,18 +78,18 @@ - (void) testImportBooleanAttributeToEntity
assertThat(testEntity.booleanTestAttribute, is(equalToBool(NO)));
}
-#ifdef TARGET_OS_IPHONE
+#if TARGET_OS_IPHONE == 1
- (void) testImportUIColorAttributeToEntity
{
UIColor *actualColor = testEntity.colorTestAttribute;
CGFloat red, blue, green, alpha;
[actualColor getRed:&red green:&green blue:&blue alpha:&alpha];
-
- assertThatFloat(red, is(equalToFloat(128/255)));
- assertThatFloat(green, is(equalToFloat(128/255)));
- assertThatFloat(blue, is(equalToFloat(128/255)));
- assertThatFloat(alpha, is(equalToFloat(1)));
+
+ assertThatFloat(alpha, is(equalToFloat(1.)));
+ assertThatFloat(red, is(equalToFloat(64./255.)));
+ assertThatFloat(green, is(equalToFloat(128./255.)));
+ assertThatFloat(blue, is(equalToFloat(225./255.)));
}
#else

0 comments on commit fdc66fc

Please sign in to comment.