Permalink
Browse files

Implemented methods unregistration for GBMethodsProvider and GBMethod…

…SectionData.

This functionality is needed to allow removal of undocumented methods during processing.
  • Loading branch information...
1 parent 187c0fb commit 33a37f12b675597bccb1b966a46df85b408cdb76 @tomaz committed Dec 2, 2010
@@ -29,11 +29,22 @@
@param method The method to register.
@exception NSException Thrown if the given method is `nil`.
+ @see unregisterMethod:
@see methods
@see sectionName
*/
- (void)registerMethod:(GBMethodData *)method;
+/** Unregisters the given method from the section's methods list.
+
+ If the method isn't part of the section, nothing happens.
+
+ @param method The method to remove.
+ @return Returns `YES` if the method was found in the list (and was consequently deleted), `NO` otherwise.
+ @see registerMethod:
+ */
+- (BOOL)unregisterMethod:(GBMethodData *)method;
+
/** The name of the section.
@see methods
@@ -18,6 +18,14 @@ - (void)registerMethod:(GBMethodData *)method {
[_methods addObject:method];
}
+- (BOOL)unregisterMethod:(GBMethodData *)method {
+ if ([_methods containsObject:method]) {
+ [_methods removeObject:method];
+ return YES;
+ }
+ return NO;
+}
+
#pragma mark Overiden methods
- (NSString *)description {
View
@@ -97,10 +97,20 @@
@exception NSException Thrown if a method with the same selector is already registered.
@see registerSectionWithName:
@see registerSectionIfNameIsValid:
+ @see unregisterMethod:
@see methods
*/
- (void)registerMethod:(GBMethodData *)method;
+/** Unregisters the given method from the providers data.
+
+ This effectively removes the method from all methods lists as well as from section the method belongs to. If the section becomes empty, the section is removed also. If the method isn't found in the lists, nothing happens.
+
+ @param method The method to remove.
+ @see registerMethod:
+ */
+- (void)unregisterMethod:(GBMethodData *)method;
+
/** Returns the method that matches the given selector.
If no method matches the given selector, `nil` is returned. If `nil` or empty string is passed for selector, `nil` is returned also.
View
@@ -91,6 +91,25 @@ - (void)registerMethod:(GBMethodData *)method {
[_methodsBySelectors setObject:method forKey:method.methodSelector];
}
+- (void)unregisterMethod:(GBMethodData *)method {
+ // Remove from all our lists.
+ [_methods removeObject:method];
+ [_classMethods removeObject:method];
+ [_instanceMethods removeObject:method];
+ [_properties removeObject:method];
+
+ // Ask all sections to remove the method from their lists.
+ [_sections enumerateObjectsUsingBlock:^(GBMethodSectionData *section, NSUInteger idx, BOOL *stop) {
+ if ([section unregisterMethod:method]) {
+ if ([section.methods count] == 0) {
+ [_sections removeObject:section];
+ [_sectionsByNames removeObjectForKey:section.sectionName];
+ }
+ *stop = YES;
+ }
+ }];
+}
+
- (void)addMethod:(GBMethodData *)method toSortedArray:(NSMutableArray *)array {
[array addObject:method];
[array sortUsingComparator:^(GBMethodData *obj1, GBMethodData *obj2) {
@@ -7,6 +7,7 @@
//
#import "GBTestObjectsRegistry.h"
+#import "GBDataObjects.h"
#import "GBMethodsProvider.h"
@interface GBMethodsProviderTesting : GHTestCase
@@ -416,6 +417,93 @@ - (void)testMergeDataFromObjectsProvider_shouldPreserveCurrentSectionForNewMetho
assertThat([[section.methods objectAtIndex:0] methodSelector], is(@"m2:"));
}
+#pragma mark Unregistering handling
+
+- (void)testUnregisterMethod_shouldRemoveMethodFromMethods {
+ // setup
+ GBMethodsProvider *provider = [[GBMethodsProvider alloc] initWithParentObject:self];
+ GBMethodData *method1 = [GBTestObjectsRegistry instanceMethodWithNames:@"method1", nil];
+ GBMethodData *method2 = [GBTestObjectsRegistry instanceMethodWithNames:@"method2", nil];
+ [provider registerMethod:method1];
+ [provider registerMethod:method2];
+ // execute
+ [provider unregisterMethod:method1];
+ // verify
+ assertThatInteger([provider.methods count], equalToInteger(1));
+ assertThat([provider.methods objectAtIndex:0], is(method2));
+}
+
+- (void)testUnregisterMethod_shouldRemoveMethodFromClassMethods {
+ // setup
+ GBMethodsProvider *provider = [[GBMethodsProvider alloc] initWithParentObject:self];
+ GBMethodData *method1 = [GBTestObjectsRegistry classMethodWithNames:@"method1", nil];
+ GBMethodData *method2 = [GBTestObjectsRegistry classMethodWithNames:@"method2", nil];
+ [provider registerMethod:method1];
+ [provider registerMethod:method2];
+ // execute
+ [provider unregisterMethod:method1];
+ // verify
+ assertThatInteger([provider.classMethods count], equalToInteger(1));
+ assertThat([provider.classMethods objectAtIndex:0], is(method2));
+}
+
+- (void)testUnregisterMethod_shouldRemoveMethodFromInstanceMethods {
+ // setup
+ GBMethodsProvider *provider = [[GBMethodsProvider alloc] initWithParentObject:self];
+ GBMethodData *method1 = [GBTestObjectsRegistry instanceMethodWithNames:@"method1", nil];
+ GBMethodData *method2 = [GBTestObjectsRegistry instanceMethodWithNames:@"method2", nil];
+ [provider registerMethod:method1];
+ [provider registerMethod:method2];
+ // execute
+ [provider unregisterMethod:method1];
+ // verify
+ assertThatInteger([provider.instanceMethods count], equalToInteger(1));
+ assertThat([provider.instanceMethods objectAtIndex:0], is(method2));
+}
+
+- (void)testUnregisterMethod_shouldRemoveMethodFromProperties {
+ // setup
+ GBMethodsProvider *provider = [[GBMethodsProvider alloc] initWithParentObject:self];
+ GBMethodData *method1 = [GBTestObjectsRegistry propertyMethodWithArgument:@"method1"];
+ GBMethodData *method2 = [GBTestObjectsRegistry propertyMethodWithArgument:@"method2"];
+ [provider registerMethod:method1];
+ [provider registerMethod:method2];
+ // execute
+ [provider unregisterMethod:method1];
+ // verify
+ assertThatInteger([provider.properties count], equalToInteger(1));
+ assertThat([provider.properties objectAtIndex:0], is(method2));
+}
+
+- (void)testUnregisterMethod_shouldRemoveMethodFromSection {
+ // setup
+ GBMethodsProvider *provider = [[GBMethodsProvider alloc] initWithParentObject:self];
+ GBMethodData *method1 = [GBTestObjectsRegistry classMethodWithNames:@"method1", nil];
+ GBMethodData *method2 = [GBTestObjectsRegistry classMethodWithNames:@"method2", nil];
+ [provider registerSectionWithName:@"Section"];
+ [provider registerMethod:method1];
+ [provider registerMethod:method2];
+ // execute
+ [provider unregisterMethod:method1];
+ // verify
+ assertThatInteger([provider.sections count], equalToInteger(1));
+ GBMethodSectionData *section = [provider.sections objectAtIndex:0];
+ assertThatInteger([section.methods count], equalToInteger(1));
+ assertThat([section.methods objectAtIndex:0], is(method2));
+}
+
+- (void)testUnregisterMethod_shouldRemoveSectionIfItContainsNoMoreMethod {
+ // setup
+ GBMethodsProvider *provider = [[GBMethodsProvider alloc] initWithParentObject:self];
+ GBMethodData *method1 = [GBTestObjectsRegistry classMethodWithNames:@"method1", nil];
+ [provider registerSectionWithName:@"Section1"];
+ [provider registerMethod:method1];
+ // execute
+ [provider unregisterMethod:method1];
+ // verify
+ assertThatInteger([provider.sections count], equalToInteger(0));
+}
+
#pragma mark Helper methods testing
- (void)testMethodBySelector_shouldReturnProperInstanceOrNil {

0 comments on commit 33a37f1

Please sign in to comment.