Permalink
Browse files

Fixed improper handling of custom property setters and getters. Closes

…#75.

Custom getters and setters were not handled correctly; the code assumed method attributes array would be contain "getter=value" or "setter=value", but in fact they were split into tokens, so array would in fact contain "getter", "=", "value" etc. Have no idea what I was thinking when implementing this :)
  • Loading branch information...
1 parent 83fb2ff commit 609574df2b4c5d8aa5fa776a9289284ba85434a5 @tomaz committed Mar 2, 2011
Showing with 26 additions and 24 deletions.
  1. +20 −18 Model/GBMethodData.m
  2. +3 −3 Testing/GBMethodDataTesting.m
  3. +3 −3 Testing/GBMethodsProviderTesting.m
View
@@ -19,6 +19,7 @@ - (NSString *)prefixFromAssignedData;
- (BOOL)formatTypesFromArray:(NSArray *)types toArray:(NSMutableArray *)array prefix:(NSString *)prefix suffix:(NSString *)suffix;
- (NSDictionary *)formattedComponentWithValue:(NSString *)value;
- (NSDictionary *)formattedComponentWithValue:(NSString *)value style:(NSUInteger)style href:(NSString *)href;
+- (NSString *)attributeValueForKey:(NSString *)key;
- (void)validateMergeWith:(GBMethodData *)source;
@property (readonly) NSString *methodSelectorDelimiter;
@property (readonly) NSString *methodPrefix;
@@ -197,30 +198,14 @@ - (NSString *)prefixFromAssignedData {
- (NSString *)propertyGetterSelector {
if (self.methodType != GBMethodTypeProperty) return nil;
- __block NSString *result = nil;
- [self.methodAttributes enumerateObjectsUsingBlock:^(NSString *attribute, NSUInteger idx, BOOL *stop) {
- if ([attribute hasPrefix:@"getter"]) {
- NSRange range = [attribute rangeOfString:@"="];
- result = [attribute substringFromIndex:range.location + range.length];
- result = [result stringByTrimmingWhitespaceAndNewLine];
- *stop = YES;
- }
- }];
+ NSString *result = [self attributeValueForKey:@"getter"];
if (!result) result = self.methodSelector;
return result;
}
- (NSString *)propertySetterSelector {
if (self.methodType != GBMethodTypeProperty) return nil;
- __block NSString *result = nil;
- [self.methodAttributes enumerateObjectsUsingBlock:^(NSString *attribute, NSUInteger idx, BOOL *stop) {
- if ([attribute hasPrefix:@"setter"]) {
- NSRange range = [attribute rangeOfString:@"="];
- result = [attribute substringFromIndex:range.location + range.length];
- result = [result stringByTrimmingWhitespaceAndNewLine];
- *stop = YES;
- }
- }];
+ NSString *result = [self attributeValueForKey:@"setter"];
if (!result) {
NSString *firstLetter = [[self.methodSelector substringToIndex:1] uppercaseString];
NSString *theRest = [self.methodSelector substringFromIndex:1];
@@ -229,6 +214,23 @@ - (NSString *)propertySetterSelector {
return result;
}
+- (NSString *)attributeValueForKey:(NSString *)key {
+ // Returns the value after equal sign for the given key (i.e. for attributes "getter", "=", "value", this would return "value"). Returns nil if either key isn't found or isn't followed by equal sign and/or a value.
+ __block NSString *result = nil;
+ __block BOOL foundKey = NO;
+ [self.methodAttributes enumerateObjectsUsingBlock:^(NSString *attribute, NSUInteger idx, BOOL *stop) {
+ if ([attribute isEqualToString:key]) {
+ foundKey = YES;
+ return;
+ }
+ if (foundKey && ![attribute isEqualToString:@"="]) {
+ result = attribute;
+ *stop = YES;
+ }
+ }];
+ return result;
+}
+
- (void)validateMergeWith:(GBMethodData *)source {
// Validates merging with the given method. This method raises exception if merging is not allowed based on method types. It takes into account manual propery accessors and mutators!
if (source.methodType != self.methodType) {
@@ -157,9 +157,9 @@ - (void)testPropertySelectors_shouldReturnProperValueForProperties {
// setup & execute
NSArray *components = [NSArray arrayWithObjects:@"BOOL", @"value", nil];
GBMethodData *property1 = [GBMethodData propertyDataWithAttributes:[NSArray arrayWithObjects:@"readonly", nil] components:components];
- GBMethodData *property2 = [GBMethodData propertyDataWithAttributes:[NSArray arrayWithObjects:@"getter=isValue", nil] components:components];
- GBMethodData *property3 = [GBMethodData propertyDataWithAttributes:[NSArray arrayWithObjects:@"setter=setTheValue:", nil] components:components];
- GBMethodData *property4 = [GBMethodData propertyDataWithAttributes:[NSArray arrayWithObjects:@"getter=isValue", @"setter=setTheValue:", nil] components:components];
+ GBMethodData *property2 = [GBMethodData propertyDataWithAttributes:[NSArray arrayWithObjects:@"getter", @"=", @"isValue", nil] components:components];
+ GBMethodData *property3 = [GBMethodData propertyDataWithAttributes:[NSArray arrayWithObjects:@"setter", @"=", @"setTheValue:", nil] components:components];
+ GBMethodData *property4 = [GBMethodData propertyDataWithAttributes:[NSArray arrayWithObjects:@"getter", @"=", @"isValue", @"setter", @"=", @"setTheValue:", nil] components:components];
// verify
assertThat(property1.propertyGetterSelector, is(@"value"));
assertThat(property1.propertySetterSelector, is(@"setValue:"));
@@ -180,9 +180,9 @@ - (void)testRegisterMethod_shouldProperlyHandlePropertyGettersAndSetters {
// setup
GBMethodsProvider *provider = [[GBMethodsProvider alloc] initWithParentObject:self];
GBMethodData *property1 = [GBTestObjectsRegistry propertyMethodWithArgument:@"name1"];
- GBMethodData *property2 = [GBMethodData propertyDataWithAttributes:[NSArray arrayWithObjects:@"getter=isName2", nil] components:[NSArray arrayWithObjects:@"BOOL", @"name2", nil]];
- GBMethodData *property3 = [GBMethodData propertyDataWithAttributes:[NSArray arrayWithObjects:@"setter=setTheName3:", nil] components:[NSArray arrayWithObjects:@"BOOL", @"name3", nil]];
- GBMethodData *property4 = [GBMethodData propertyDataWithAttributes:[NSArray arrayWithObjects:@"getter=isName4", @"setter=setTheName4:", nil] components:[NSArray arrayWithObjects:@"BOOL", @"name4", nil]];
+ GBMethodData *property2 = [GBMethodData propertyDataWithAttributes:[NSArray arrayWithObjects:@"getter",@"=",@"isName2", nil] components:[NSArray arrayWithObjects:@"BOOL", @"name2", nil]];
+ GBMethodData *property3 = [GBMethodData propertyDataWithAttributes:[NSArray arrayWithObjects:@"setter",@"=",@"setTheName3:", nil] components:[NSArray arrayWithObjects:@"BOOL", @"name3", nil]];
+ GBMethodData *property4 = [GBMethodData propertyDataWithAttributes:[NSArray arrayWithObjects:@"getter",@"=",@"isName4", @"setter",@"=",@"setTheName4:", nil] components:[NSArray arrayWithObjects:@"BOOL", @"name4", nil]];
// execute
[provider registerMethod:property1];
[provider registerMethod:property2];

0 comments on commit 609574d

Please sign in to comment.