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