Frameworks/AutoHyperlinks Framework/UnitTests/HyperlinkContextTest.m
author Stephen Holt <sholt@adium.im>
Wed Aug 12 13:32:55 2009 -0400 (2009-08-12)
changeset 2602 19c704d73e7f
parent 619 10d612b4d419
child 2623 80d3d47e02b0
permissions -rw-r--r--
Add em and en dashes to the start set. Fixes #11490
     1 //
     2 //  HyperlinkContextTest.m
     3 //  AIHyperlinks.framework
     4 //
     5 
     6 #import "HyperlinkContextTest.h"
     7 #import "AutoHyperlinks.h"
     8 
     9 @implementation HyperlinkContextTest
    10 - (void)testLaxContext:(NSString *)linkString withURI:(NSString *)URIString
    11 {
    12 	NSString			*testString = [NSString stringWithFormat:linkString, URIString];
    13 	AHHyperlinkScanner	*scanner = [AHHyperlinkScanner hyperlinkScannerWithString:testString];
    14 	AHMarkedHyperlink	*link = [scanner nextURI];
    15 	
    16 	STAssertNotNil(link, @"-[SHHyperlinkScanner nextURL] found no URI in \"%@\"", testString);
    17 	STAssertEqualObjects([[link parentString] substringWithRange:[link range]], URIString, @"in context: '%@'", testString);
    18 }
    19 
    20 - (void)testNegativeContext:(NSString *)linkString withURI:(NSString *)URIString
    21 {
    22 	NSString			*testString = [NSString stringWithFormat:linkString, URIString];
    23 	AHHyperlinkScanner	*scanner = [AHHyperlinkScanner hyperlinkScannerWithString:testString];
    24 	AHMarkedHyperlink	*link = [scanner nextURI];
    25 	
    26 	STAssertNil(link, @"-[SHHyperlinkScanner nextURLFromString:] found no URI in \"%@\"", testString);
    27 	STAssertEqualObjects([[link parentString] substringWithRange:[link range]], nil, @"in context: '%@'", testString);
    28 }
    29 
    30 #pragma mark positive tests
    31 - (void)testEnclosedURI:(NSString *)URIString {
    32 	[self testLaxContext:@"<%@>" withURI:URIString];
    33 	[self testLaxContext:@"(%@)" withURI:URIString];
    34 	[self testLaxContext:@"[%@]" withURI:URIString];
    35 	
    36 	[self testLaxContext:@"< %@ >" withURI:URIString];
    37 	[self testLaxContext:@"( %@ )" withURI:URIString];
    38 	[self testLaxContext:@"[ %@ ]" withURI:URIString];
    39 }
    40 
    41 - (void)testEnclosedURI:(NSString *)URIString enclosureOpeningCharacter:(unichar)openingChar enclosureClosingCharacter:(unichar)closingChar followedByCharacter:(unichar)terminalChar {
    42 	NSString *format = [NSString stringWithFormat:@"%C%%@%C%C", openingChar, closingChar, terminalChar];
    43 	[self testLaxContext:format withURI:URIString];
    44 }
    45 
    46 - (void)testEnclosedURIFollowedByCharacter:(NSString *)URIString {
    47 	enum {
    48 		kNumEnclosureCharacters = 3U,
    49 		kNumTerminalCharacters = 17U
    50 	};
    51 	unichar enclosureOpeningCharacters[kNumEnclosureCharacters] = { '<', '(', '[', };
    52 	unichar enclosureClosingCharacters[kNumEnclosureCharacters] = { '>', ')', ']', };
    53 	unichar terminalCharacters[kNumTerminalCharacters] = { '.', '!', '?', '<', '>', '(', ')', '{', '}', '[', ']', '"', '\'', '-', ',', ':', ';' };
    54 	for (unsigned int enclosureIndex = 0U; enclosureIndex < kNumEnclosureCharacters; ++enclosureIndex) {
    55 		for (unsigned int terminalCharacterIndex = 0U; terminalCharacterIndex < kNumTerminalCharacters; ++terminalCharacterIndex) {
    56 			[self         testEnclosedURI:URIString
    57 				enclosureOpeningCharacter:enclosureOpeningCharacters[enclosureIndex]
    58 				enclosureClosingCharacter:enclosureClosingCharacters[enclosureIndex]
    59 					  followedByCharacter:terminalCharacters[terminalCharacterIndex]
    60 			];
    61 		}
    62 	}
    63 }
    64 
    65 - (void)testURIBorder:(NSString *)URIString {
    66 	[self testLaxContext:@":%@" withURI:URIString];
    67 	[self testLaxContext:@"—%@" withURI:URIString];
    68 	[self testLaxContext:@"–%@" withURI:URIString];
    69 	[self testLaxContext:@"check it out:%@" withURI:URIString];
    70 	[self testLaxContext:@"%@:" withURI:URIString];
    71 	[self testLaxContext:@"%@." withURI:URIString];
    72 }
    73 
    74 - (void)testWhitespace:(NSString *)URIString {
    75 	[self testLaxContext:@"\t%@" withURI:URIString];
    76 	[self testLaxContext:@"\n%@" withURI:URIString];
    77 	[self testLaxContext:@"\v%@" withURI:URIString];
    78 	[self testLaxContext:@"\f%@" withURI:URIString];
    79 	[self testLaxContext:@"\r%@" withURI:URIString];
    80 	[self testLaxContext:@" %@" withURI:URIString];
    81 
    82 	[self testLaxContext:@"%@\t" withURI:URIString];
    83 	[self testLaxContext:@"%@\n" withURI:URIString];
    84 	[self testLaxContext:@"%@\v" withURI:URIString];
    85 	[self testLaxContext:@"%@\f" withURI:URIString];
    86 	[self testLaxContext:@"%@\r" withURI:URIString];
    87 	[self testLaxContext:@"%@ " withURI:URIString];
    88 
    89 	[self testLaxContext:@"\t%@\t" withURI:URIString];
    90 	[self testLaxContext:@"\n%@\n" withURI:URIString];
    91 	[self testLaxContext:@"\v%@\v" withURI:URIString];
    92 	[self testLaxContext:@"\f%@\f" withURI:URIString];
    93 	[self testLaxContext:@"\r%@\r" withURI:URIString];
    94 	[self testLaxContext:@" %@ " withURI:URIString];
    95 	
    96 	[self testLaxContext:@"words before %@" withURI:URIString];
    97 	[self testLaxContext:@"%@ words after" withURI:URIString];
    98 	[self testLaxContext:@"words before %@ and words after" withURI:URIString];
    99 }
   100 
   101 #pragma mark negative tests
   102 - (void)testNegativeEnclosedURI:(NSString *)URIString {
   103 	[self testNegativeContext:@"<%@>" withURI:URIString];
   104 	[self testNegativeContext:@"(%@)" withURI:URIString];
   105 	[self testNegativeContext:@"[%@]" withURI:URIString];
   106 }
   107 
   108 - (void)testNegativeEnclosedURI:(NSString *)URIString enclosureOpeningCharacter:(unichar)openingChar enclosureClosingCharacter:(unichar)closingChar followedByCharacter:(unichar)terminalChar {
   109 	NSString *format = [NSString stringWithFormat:@"%C%%@%C%C", openingChar, closingChar, terminalChar];
   110 	[self testNegativeContext:format withURI:URIString];
   111 }
   112 - (void)testNegativeEnclosedURIFollowedByCharacter:(NSString *)URIString {
   113 	enum {
   114 		kNumEnclosureCharacters = 3U,
   115 		kNumTerminalCharacters = 17U
   116 	};
   117 	unichar enclosureOpeningCharacters[kNumEnclosureCharacters] = { '<', '(', '[', };
   118 	unichar enclosureClosingCharacters[kNumEnclosureCharacters] = { '>', ')', ']', };
   119 	unichar terminalCharacters[kNumTerminalCharacters] = { '.', '!', '?', '<', '>', '(', ')', '{', '}', '[', ']', '"', '\'', '-', ',', ':', ';' };
   120 	for (unsigned int enclosureIndex = 0U; enclosureIndex < kNumEnclosureCharacters; ++enclosureIndex) {
   121 		for (unsigned int terminalCharacterIndex = 0U; terminalCharacterIndex < kNumTerminalCharacters; ++terminalCharacterIndex) {
   122 			[self         testNegativeEnclosedURI:URIString
   123 				enclosureOpeningCharacter:enclosureOpeningCharacters[enclosureIndex]
   124 				enclosureClosingCharacter:enclosureClosingCharacters[enclosureIndex]
   125 					  followedByCharacter:terminalCharacters[terminalCharacterIndex]
   126 			];
   127 		}
   128 	}
   129 }
   130 
   131 - (void)testNegativeURIBorder:(NSString *)URIString {
   132 	[self testNegativeContext:@":%@" withURI:URIString];
   133 	[self testNegativeContext:@"check it out:%@" withURI:URIString];
   134 	[self testNegativeContext:@"%@:" withURI:URIString];
   135 	[self testNegativeContext:@"%@." withURI:URIString];
   136 }
   137 
   138 - (void)testNegativeWhitespace:(NSString *)URIString {
   139 	[self testNegativeContext:@"\t%@" withURI:URIString];
   140 	[self testNegativeContext:@"\n%@" withURI:URIString];
   141 	[self testNegativeContext:@"\v%@" withURI:URIString];
   142 	[self testNegativeContext:@"\f%@" withURI:URIString];
   143 	[self testNegativeContext:@"\r%@" withURI:URIString];
   144 	[self testNegativeContext:@" %@" withURI:URIString];
   145 
   146 	[self testNegativeContext:@"%@\t" withURI:URIString];
   147 	[self testNegativeContext:@"%@\n" withURI:URIString];
   148 	[self testNegativeContext:@"%@\v" withURI:URIString];
   149 	[self testNegativeContext:@"%@\f" withURI:URIString];
   150 	[self testNegativeContext:@"%@\r" withURI:URIString];
   151 	[self testNegativeContext:@"%@ " withURI:URIString];
   152 
   153 	[self testNegativeContext:@"\t%@\t" withURI:URIString];
   154 	[self testNegativeContext:@"\n%@\n" withURI:URIString];
   155 	[self testNegativeContext:@"\v%@\v" withURI:URIString];
   156 	[self testNegativeContext:@"\f%@\f" withURI:URIString];
   157 	[self testNegativeContext:@"\r%@\r" withURI:URIString];
   158 	[self testNegativeContext:@" %@ " withURI:URIString];
   159 	
   160 	[self testNegativeContext:@"words before %@" withURI:URIString];
   161 	[self testNegativeContext:@"%@ words after" withURI:URIString];
   162 	[self testNegativeContext:@"words before %@ and words after" withURI:URIString];
   163 }
   164 
   165 #pragma mark URI tests
   166 - (void)testSimpleDomain {
   167 	[self testEnclosedURI:@"example.com"];
   168 	[self testEnclosedURIFollowedByCharacter:@"example.com"];
   169 	[self testURIBorder:@"example.com"];
   170 	[self testWhitespace:@"example.com"];
   171 }
   172 
   173 - (void)testEmail {
   174 	[self testEnclosedURI:@"test@example.com"];
   175 	[self testEnclosedURIFollowedByCharacter:@"test@example.com"];
   176 	[self testURIBorder:@"test@example.com"];
   177 	[self testWhitespace:@"test@example.com"];
   178 }
   179 
   180 - (void)testJID {
   181 	[self testNegativeEnclosedURI:@"jdoe@jabber.org/Adium"];
   182 	[self testNegativeEnclosedURIFollowedByCharacter:@"jdoe@jabber.org/Adium"];
   183 	[self testNegativeURIBorder:@"jdoe@jabber.org/Adium"];
   184 	[self testNegativeWhitespace:@"jdoe@jabber.org/Adium"];
   185 }
   186 
   187 - (void)testEdgeURI {
   188 	[self testEnclosedURI:@"example.com/foo_(bar)"];
   189 	[self testURIBorder:@"example.com/foo_(bar)"];
   190 	[self testEnclosedURI:@"http://example.com/foo_(bar)"];
   191 	[self testURIBorder:@"http://example.com/foo_(bar)"];
   192 	[self testEnclosedURI:@"http://example.com/f(oo_(ba)r)"];
   193 	[self testURIBorder:@"http://example.com/f(oo_(ba)r)"];
   194 	[self testEnclosedURI:@"http://example.com/f[oo_(ba]r)"];
   195 	[self testURIBorder:@"http://example.com/f[oo_(ba]r)"];
   196 	[self testEnclosedURI:@"http://example.com/f[oo_((ba]r))"];
   197 	[self testURIBorder:@"http://example.com/f[oo_((ba]r))"];
   198 	[self testURIBorder:@"http://www.example.com/___"];
   199 	[self testURIBorder:@"http://www.example.com/$$$"];
   200 	[self testURIBorder:@"http://www.example.com/---"];
   201 	
   202 	[self testEnclosedURI:@"http://www.example.com/query.php?test=YES&evilQuery=1"];
   203 	[self testURIBorder:@"http://www.example.com/query.php?test=YES&evilQuery=1"];
   204 	[self testEnclosedURIFollowedByCharacter:@"http://www.example.com/query.php?test=YES&evilQuery=1"];
   205 	
   206 	[self testEnclosedURI:@"http://www.example.com/___"];
   207 	[self testEnclosedURI:@"http://www.example.com/$$$"];
   208 	[self testEnclosedURI:@"http://www.example.com/---"];
   209 
   210 	[self testEnclosedURIFollowedByCharacter:@"http://example.com/"];
   211 	[self testEnclosedURIFollowedByCharacter:@"http://example.com"];
   212 
   213 	[self testLaxContext:@"<><><><><<<<><><><><%@><><><><><><<<><><><><><>" withURI:@"example.com"];
   214 	[self testLaxContext:@"l<><><><><<<<><><><><%@><><><><><><<<><><><><><>" withURI:@"http://example.com/foo_(bar)"];
   215 	
   216 	[self testLaxContext:@"@%@" withURI:@"example.com"];
   217 	
   218 	[self testLaxContext:@"foo (bar) %@" withURI:@"http://example.com/path/to/url.html"];
   219 	
   220 	[self testLaxContext:@"%@" withURI:[NSString stringWithFormat:@"%@",@"http://example.com/hi%uthere"]]; //#11160
   221 }
   222 
   223 - (void)testCompositeContext {
   224 	NSString			*URI1 = @"mailto:test@example.com";
   225 	NSString			*URI2 = @"xmpp:test@example.com";
   226 	NSString			*testString = [NSString stringWithFormat:@"%@ something %@", URI1, URI2];
   227 	AHHyperlinkScanner	*scanner = [AHHyperlinkScanner hyperlinkScannerWithString:testString];
   228 	AHMarkedHyperlink	*link;
   229 	
   230 	link = [scanner nextURI];
   231 	STAssertNotNil(link, @"-[SHHyperlinkScanner nextURL] found no URI in \"%@\"", testString);
   232 	STAssertEqualObjects([[link parentString] substringWithRange:[link range]], URI1, @"in context: '%@'", testString);
   233 	
   234 	link = [scanner nextURI];
   235 	STAssertNotNil(link, @"-[SHHyperlinkScanner nextURL] found no URI in \"%@\"", testString);
   236 	STAssertEqualObjects([[link parentString] substringWithRange:[link range]], URI2, @"in context: '%@'", testString);
   237 }
   238 @end