Skip to content

Commit

Permalink
Fixed categories links detection when a class with same name exists.
Browse files Browse the repository at this point in the history
This fixes stuff like `Class(Category)` which should prefer category link if found instead of class link if also found on the same location.
  • Loading branch information
tomaz committed Feb 18, 2011
1 parent 36b7a71 commit c738bb0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
16 changes: 13 additions & 3 deletions Processing/GBCommentsProcessor.m
Expand Up @@ -30,6 +30,13 @@ static BOOL GBIsCrossRefValid(GBCrossRefData data) {
return (data.range.location != NSNotFound);
}

static BOOL GBIsCrossRefOnSameRange(GBCrossRefData a, GBCrossRefData b) {
if (!GBIsCrossRefValid(a) || !GBIsCrossRefValid(b)) return NO;
if (a.range.location < b.range.location) return NO;
if (a.range.location > b.range.location) return NO;
return YES;
}

#pragma mark -

@interface GBCommentsProcessor ()
Expand Down Expand Up @@ -433,7 +440,7 @@ - (NSString *)stringByConvertingCrossReferencesInString:(NSString *)string {
// Add objects to handler array. Note that we don't add class/protocol if category is found on the same index! Exit if no link was found.
[links setCount:0];
if (GBIsCrossRefValid(urlData)) [links addPointer:&urlData];
if (GBIsCrossRefValid(objectData)) [links addPointer:&objectData];
if (GBIsCrossRefValid(objectData) && !GBIsCrossRefOnSameRange(objectData, categoryData)) [links addPointer:&objectData];
if (GBIsCrossRefValid(categoryData)) [links addPointer:&categoryData];
if (GBIsCrossRefValid(localMemberData)) [links addPointer:&localMemberData];
if (GBIsCrossRefValid(remoteMemberData)) [links addPointer:&remoteMemberData];
Expand Down Expand Up @@ -469,6 +476,9 @@ - (NSString *)stringByConvertingCrossReferencesInString:(NSString *)string {
searchRange.length = [string length] - location;
[links removePointerAtIndex:index];
}

// Exit if there's nothing more to process.
if (searchRange.location >= [string length]) break;
}

// If there's some text remaining after all links, append it.
Expand Down Expand Up @@ -522,8 +532,8 @@ - (GBCrossRefData)dataForFirstCategoryLinkInString:(NSString *)string searchRang
if ([components count] == 0) return result;

// Get link components. Index 0 contains full text, including optional template prefix/suffix, index 1 just the object name.
NSString *linkText = [components objectAtIndex:0];
NSString *objectName = [components objectAtIndex:1];
NSString *linkText = [[components objectAtIndex:0] stringByTrimmingWhitespaceAndNewLine];
NSString *objectName = [[components objectAtIndex:1] stringByTrimmingWhitespaceAndNewLine];

// Validate object name with a class or protocol.
id referencedObject = [self.store categoryWithName:objectName];
Expand Down
30 changes: 30 additions & 0 deletions Testing/GBCommentsProcessor-PreprocessingTesting.m
Expand Up @@ -428,6 +428,36 @@ - (void)testStringByConvertingCrossReferencesInString_shouldConvertMailto {
assertThat(result2, is(@"[appledoc@gentlebytes.com](mailto:appledoc@gentlebytes.com)"));
}

#pragma mark Combinations detection testing

- (void)testStringByConvertingCrossReferencesInString_shouldConvertCategoryAndClass {
// setup
GBCategoryData *category = [GBCategoryData categoryDataWithName:@"Category" className:@"Class"];
GBClassData *class = [GBClassData classDataWithName:@"Class"];
GBStore *store = [GBTestObjectsRegistry storeWithObjects:category, class, nil];
GBCommentsProcessor *processor = [self processorWithStore:store];
// execute
NSString *result1 = [processor stringByConvertingCrossReferencesInString:@"Class(Category) Class"];
NSString *result2 = [processor stringByConvertingCrossReferencesInString:@"Class Class(Category)"];
// verify
assertThat(result1, is(@"[Class(Category)](Categories/Class(Category).html) [Class](Classes/Class.html)"));
assertThat(result2, is(@"[Class](Classes/Class.html) [Class(Category)](Categories/Class(Category).html)"));
}

- (void)testStringByConvertingCrossReferencesInString_shouldConvertCategoryAndProtocol {
// setup - although it's not possible to do categories on protocols, we still test to properly cover these...
GBCategoryData *category = [GBCategoryData categoryDataWithName:@"Category" className:@"Protocol"];
GBProtocolData *protocol = [GBProtocolData protocolDataWithName:@"Protocol"];
GBStore *store = [GBTestObjectsRegistry storeWithObjects:category, protocol, nil];
GBCommentsProcessor *processor = [self processorWithStore:store];
// execute
NSString *result1 = [processor stringByConvertingCrossReferencesInString:@"Protocol(Category) Protocol"];
NSString *result2 = [processor stringByConvertingCrossReferencesInString:@"Protocol Protocol(Category)"];
// verify
assertThat(result1, is(@"[Protocol(Category)](Categories/Protocol(Category).html) [Protocol](Protocols/Protocol.html)"));
assertThat(result2, is(@"[Protocol](Protocols/Protocol.html) [Protocol(Category)](Categories/Protocol(Category).html)"));
}

#pragma mark Creation methods

- (GBCommentsProcessor *)processorWithStore:(id)store {
Expand Down

0 comments on commit c738bb0

Please sign in to comment.