Skip to content
This repository
Browse code

Fix color parsing tests for both Mac and iOS

  • Loading branch information...
commit fdc66fcb368b7482645821a01e2299925df9cd4f 1 parent 4a1d37a
Saul Mora authored July 24, 2011
3  .gitignore
@@ -15,4 +15,5 @@ build/
15 15
 *.mode2v3
16 16
 
17 17
 UserInterfaceState.xcuserstate
18  
-/Magical Record.xcodeproj/project.xcworkspace/xcuserdata/saul.xcuserdatad/UserInterfaceState.xcuserstate
  18
+/Magical Record.xcodeproj/project.xcworkspace/xcuserdata/saul.xcuserdatad/UserInterfaceState.xcuserstate
  19
+/Magical Record.xcodeproj/xcuserdata/saul.xcuserdatad/xcdebugger/Breakpoints.xcbkptlist
49  Magical Record.xcodeproj/xcuserdata/saul.xcuserdatad/xcdebugger/Breakpoints.xcbkptlist
... ...
@@ -1,49 +0,0 @@
1  
-<?xml version="1.0" encoding="UTF-8"?>
2  
-<Bucket
3  
-   type = "1"
4  
-   version = "1.0">
5  
-   <FileBreakpoints>
6  
-      <FileBreakpoint
7  
-         shouldBeEnabled = "No"
8  
-         ignoreCount = "0"
9  
-         continueAfterRunningActions = "No"
10  
-         isPathRelative = "1"
11  
-         filePath = "Source/Categories/NSManagedObject+MagicalDataImport.m"
12  
-         timestampString = "333180902.999718"
13  
-         startingColumnNumber = "9223372036854775807"
14  
-         endingColumnNumber = "9223372036854775807"
15  
-         startingLineNumber = "48"
16  
-         endingLineNumber = "48"
17  
-         landmarkName = "-mr_attributeValueFromJSONDicationary:forAttribute:"
18  
-         landmarkType = "5">
19  
-      </FileBreakpoint>
20  
-      <FileBreakpoint
21  
-         shouldBeEnabled = "No"
22  
-         ignoreCount = "0"
23  
-         continueAfterRunningActions = "No"
24  
-         isPathRelative = "1"
25  
-         filePath = "Unit Tests/ImportSingleEntityWithRelatedEntitiesTests.m"
26  
-         timestampString = "333180930.947903"
27  
-         startingColumnNumber = "9223372036854775807"
28  
-         endingColumnNumber = "9223372036854775807"
29  
-         startingLineNumber = "29"
30  
-         endingLineNumber = "29"
31  
-         landmarkName = "-setUp"
32  
-         landmarkType = "5">
33  
-      </FileBreakpoint>
34  
-      <FileBreakpoint
35  
-         shouldBeEnabled = "No"
36  
-         ignoreCount = "0"
37  
-         continueAfterRunningActions = "No"
38  
-         isPathRelative = "1"
39  
-         filePath = "Source/Categories/NSManagedObject+MagicalDataImport.m"
40  
-         timestampString = "333181283.225817"
41  
-         startingColumnNumber = "9223372036854775807"
42  
-         endingColumnNumber = "9223372036854775807"
43  
-         startingLineNumber = "102"
44  
-         endingLineNumber = "102"
45  
-         landmarkName = "-mr_addObject:forRelationship:"
46  
-         landmarkType = "5">
47  
-      </FileBreakpoint>
48  
-   </FileBreakpoints>
49  
-</Bucket>
2  Source/Categories/NSManagedObject+MagicalDataImport.m
@@ -34,7 +34,7 @@ - (id) mr_attributeValueFromJSONDicationary:(NSDictionary *)jsonData forAttribut
34 34
     NSString *desiredAttributeType = [[attributeInfo userInfo] valueForKey:kNSManagedObjectAttributeJSONValueClassNameKey];
35 35
     if (desiredAttributeType) 
36 36
     {
37  
-        if ([desiredAttributeType hasSuffix:@"NSColor"])
  37
+        if ([desiredAttributeType hasSuffix:@"Color"])
38 38
         {
39 39
             value = ColorFromString(value);
40 40
         }
32  Source/MagicalRecordHelpers.m
@@ -156,19 +156,18 @@ + (void) performSaveDataOperationInBackgroundWithBlock:(CoreDataBlock)block comp
156 156
     return [formatter dateFromString:value];
157 157
 }
158 158
 
159  
-#ifdef MAC_PLATFORM_ONLY
160  
-
161  
-NSColor * NSColorFromString(NSString *serializedColor)
  159
+NSInteger* newColorComponentsFromString(NSString *serializedColor);
  160
+NSInteger* newColorComponentsFromString(NSString *serializedColor)
162 161
 {
163 162
     NSScanner *colorScanner = [NSScanner scannerWithString:serializedColor];
164 163
     NSString *colorType;
165 164
     [colorScanner scanUpToString:@"(" intoString:&colorType];
166 165
     
167  
-    NSColor *color = nil;
  166
+    NSInteger *componentValues = malloc(4 * sizeof(NSInteger));
168 167
     if ([colorType hasPrefix:@"rgba"])
169 168
     {
170 169
         NSCharacterSet *rgbaCharacterSet = [NSCharacterSet characterSetWithCharactersInString:@"(,)"];
171  
-        NSInteger componentValues[4];
  170
+
172 171
         NSInteger *componentValue = componentValues;
173 172
         while (![colorScanner isAtEnd]) 
174 173
         {
@@ -176,12 +175,20 @@ + (void) performSaveDataOperationInBackgroundWithBlock:(CoreDataBlock)block comp
176 175
             [colorScanner scanInteger:componentValue];
177 176
             componentValue++;
178 177
         }
179  
-        color = [NSColor colorWithDeviceRed:(componentValues[0] / 255.)
  178
+    }
  179
+    return componentValues;
  180
+}
  181
+
  182
+#ifdef MAC_PLATFORM_ONLY
  183
+
  184
+NSColor * NSColorFromString(NSString *serializedColor)
  185
+{
  186
+    NSInteger *componentValues = newColorComponentsFromString(serializedColor);
  187
+    NSColor *color = [NSColor colorWithDeviceRed:(componentValues[0] / 255.)
180 188
                                       green:(componentValues[1] / 255.)
181 189
                                        blue:(componentValues[2] / 255.)
182 190
                                       alpha:componentValues[3]];
183  
-    }
184  
-    
  191
+    free(componentValues);
185 192
     return color;
186 193
 }
187 194
 id (*ColorFromString)(NSString *) = NSColorFromString;
@@ -190,7 +197,14 @@ + (void) performSaveDataOperationInBackgroundWithBlock:(CoreDataBlock)block comp
190 197
 
191 198
 UIColor * UIColorFromString(NSString *serializedColor)
192 199
 {
193  
-    return nil;
  200
+    NSInteger *componentValues = newColorComponentsFromString(serializedColor);
  201
+    UIColor *color = [UIColor colorWithRed:(componentValues[0] / 255.)
  202
+                                     green:(componentValues[1] / 255.)
  203
+                                      blue:(componentValues[2] / 255.)
  204
+                                     alpha:componentValues[3]];
  205
+    
  206
+    free(componentValues);
  207
+    return color;
194 208
 }
195 209
 
196 210
 id (*ColorFromString)(NSString *) = UIColorFromString;
12  Unit Tests/ImportSingleEntityTests.m
@@ -78,18 +78,18 @@ - (void) testImportBooleanAttributeToEntity
78 78
     assertThat(testEntity.booleanTestAttribute, is(equalToBool(NO)));
79 79
 }
80 80
 
81  
-#ifdef TARGET_OS_IPHONE
  81
+#if TARGET_OS_IPHONE == 1
82 82
 
83 83
 - (void) testImportUIColorAttributeToEntity
84 84
 {
85 85
     UIColor *actualColor = testEntity.colorTestAttribute;
86 86
     CGFloat red, blue, green, alpha;
87 87
     [actualColor getRed:&red green:&green blue:&blue alpha:&alpha];
88  
-    
89  
-    assertThatFloat(red, is(equalToFloat(128/255)));
90  
-    assertThatFloat(green, is(equalToFloat(128/255)));
91  
-    assertThatFloat(blue, is(equalToFloat(128/255)));
92  
-    assertThatFloat(alpha, is(equalToFloat(1)));
  88
+
  89
+    assertThatFloat(alpha, is(equalToFloat(1.)));
  90
+    assertThatFloat(red, is(equalToFloat(64./255.)));
  91
+    assertThatFloat(green, is(equalToFloat(128./255.)));
  92
+    assertThatFloat(blue, is(equalToFloat(225./255.)));
93 93
 }
94 94
 
95 95
 #else

0 notes on commit fdc66fc

Please sign in to comment.
Something went wrong with that request. Please try again.