Add em and en dashes to the start set. Fixes #11490
2 // HyperlinkContextTest.m
3 // AIHyperlinks.framework
6 #import "HyperlinkContextTest.h"
7 #import "AutoHyperlinks.h"
9 @implementation HyperlinkContextTest
10 - (void)testLaxContext:(NSString *)linkString withURI:(NSString *)URIString
12 NSString *testString = [NSString stringWithFormat:linkString, URIString];
13 AHHyperlinkScanner *scanner = [AHHyperlinkScanner hyperlinkScannerWithString:testString];
14 AHMarkedHyperlink *link = [scanner nextURI];
16 STAssertNotNil(link, @"-[SHHyperlinkScanner nextURL] found no URI in \"%@\"", testString);
17 STAssertEqualObjects([[link parentString] substringWithRange:[link range]], URIString, @"in context: '%@'", testString);
20 - (void)testNegativeContext:(NSString *)linkString withURI:(NSString *)URIString
22 NSString *testString = [NSString stringWithFormat:linkString, URIString];
23 AHHyperlinkScanner *scanner = [AHHyperlinkScanner hyperlinkScannerWithString:testString];
24 AHMarkedHyperlink *link = [scanner nextURI];
26 STAssertNil(link, @"-[SHHyperlinkScanner nextURLFromString:] found no URI in \"%@\"", testString);
27 STAssertEqualObjects([[link parentString] substringWithRange:[link range]], nil, @"in context: '%@'", testString);
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];
36 [self testLaxContext:@"< %@ >" withURI:URIString];
37 [self testLaxContext:@"( %@ )" withURI:URIString];
38 [self testLaxContext:@"[ %@ ]" withURI:URIString];
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];
46 - (void)testEnclosedURIFollowedByCharacter:(NSString *)URIString {
48 kNumEnclosureCharacters = 3U,
49 kNumTerminalCharacters = 17U
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]
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];
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];
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];
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];
96 [self testLaxContext:@"words before %@" withURI:URIString];
97 [self testLaxContext:@"%@ words after" withURI:URIString];
98 [self testLaxContext:@"words before %@ and words after" withURI:URIString];
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];
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];
112 - (void)testNegativeEnclosedURIFollowedByCharacter:(NSString *)URIString {
114 kNumEnclosureCharacters = 3U,
115 kNumTerminalCharacters = 17U
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]
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];
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];
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];
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];
160 [self testNegativeContext:@"words before %@" withURI:URIString];
161 [self testNegativeContext:@"%@ words after" withURI:URIString];
162 [self testNegativeContext:@"words before %@ and words after" withURI:URIString];
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"];
174 [self testEnclosedURI:@"test@example.com"];
175 [self testEnclosedURIFollowedByCharacter:@"test@example.com"];
176 [self testURIBorder:@"test@example.com"];
177 [self testWhitespace:@"test@example.com"];
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"];
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/---"];
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"];
206 [self testEnclosedURI:@"http://www.example.com/___"];
207 [self testEnclosedURI:@"http://www.example.com/$$$"];
208 [self testEnclosedURI:@"http://www.example.com/---"];
210 [self testEnclosedURIFollowedByCharacter:@"http://example.com/"];
211 [self testEnclosedURIFollowedByCharacter:@"http://example.com"];
213 [self testLaxContext:@"<><><><><<<<><><><><%@><><><><><><<<><><><><><>" withURI:@"example.com"];
214 [self testLaxContext:@"l<><><><><<<<><><><><%@><><><><><><<<><><><><><>" withURI:@"http://example.com/foo_(bar)"];
216 [self testLaxContext:@"@%@" withURI:@"example.com"];
218 [self testLaxContext:@"foo (bar) %@" withURI:@"http://example.com/path/to/url.html"];
220 [self testLaxContext:@"%@" withURI:[NSString stringWithFormat:@"%@",@"http://example.com/hi%uthere"]]; //#11160
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;
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);
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);