PageRenderTime 242ms CodeModel.GetById 11ms app.highlight 221ms RepoModel.GetById 2ms app.codeStats 0ms

/core/externals/google-toolbox-for-mac/Foundation/GTMRegexTest.m

http://macfuse.googlecode.com/
Objective C | 1245 lines | 961 code | 76 blank | 208 comment | 0 complexity | 0b2ad123e72a8272441f42fe7ec8d405 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1//
   2//  GTMRegexTest.m
   3//
   4//  Copyright 2007-2008 Google Inc.
   5//
   6//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
   7//  use this file except in compliance with the License.  You may obtain a copy
   8//  of the License at
   9// 
  10//  http://www.apache.org/licenses/LICENSE-2.0
  11// 
  12//  Unless required by applicable law or agreed to in writing, software
  13//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  14//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
  15//  License for the specific language governing permissions and limitations under
  16//  the License.
  17//
  18
  19#import "GTMSenTestCase.h"
  20#import "GTMRegex.h"
  21#import "GTMUnitTestDevLog.h"
  22
  23//
  24// NOTE:
  25//
  26// We don't really test any of the pattern matching since that's testing
  27// libregex, we just want to test our wrapper.
  28//
  29
  30@interface GTMRegexTest : GTMTestCase
  31@end
  32
  33@interface NSString_GTMRegexAdditions : GTMTestCase
  34@end
  35
  36@implementation GTMRegexTest
  37
  38- (void)testEscapedPatternForString {
  39  STAssertEqualStrings([GTMRegex escapedPatternForString:@"abcdefghijklmnopqrstuvwxyz0123456789"],
  40                       @"abcdefghijklmnopqrstuvwxyz0123456789",
  41                       nil);
  42  STAssertEqualStrings([GTMRegex escapedPatternForString:@"^.[$()|*+?{\\"],
  43                       @"\\^\\.\\[\\$\\(\\)\\|\\*\\+\\?\\{\\\\",
  44                       nil);
  45  STAssertEqualStrings([GTMRegex escapedPatternForString:@"a^b.c[d$e(f)g|h*i+j?k{l\\m"],
  46                       @"a\\^b\\.c\\[d\\$e\\(f\\)g\\|h\\*i\\+j\\?k\\{l\\\\m",
  47                       nil);
  48
  49  STAssertNil([GTMRegex escapedPatternForString:nil], nil);
  50  STAssertEqualStrings([GTMRegex escapedPatternForString:@""], @"", nil);
  51}
  52
  53
  54- (void)testInit {
  55
  56  // fail cases
  57  STAssertNil([[[GTMRegex alloc] init] autorelease], nil);
  58  STAssertNil([[[GTMRegex alloc] initWithPattern:nil] autorelease], nil);
  59  STAssertNil([[[GTMRegex alloc] initWithPattern:nil
  60                                         options:kGTMRegexOptionIgnoreCase] autorelease], nil);
  61  [GTMUnitTestDevLog expectString:@"Invalid pattern \"(.\", error: \"parentheses not balanced\""];
  62  STAssertNil([[[GTMRegex alloc] initWithPattern:@"(."] autorelease], nil);
  63  [GTMUnitTestDevLog expectString:@"Invalid pattern \"(.\", error: \"parentheses not balanced\""];
  64  STAssertNil([[[GTMRegex alloc] initWithPattern:@"(."
  65                                         options:kGTMRegexOptionIgnoreCase] autorelease], nil);
  66  // fail cases w/ error param
  67  NSError *error = nil;
  68  STAssertNil([[[GTMRegex alloc] initWithPattern:nil
  69                                         options:kGTMRegexOptionIgnoreCase
  70                                       withError:&error] autorelease], nil);
  71  STAssertNil(error, @"no pattern, shouldn't get error object");
  72  STAssertNil([[[GTMRegex alloc] initWithPattern:@"(."
  73                                         options:kGTMRegexOptionIgnoreCase
  74                                       withError:&error] autorelease], nil);
  75  STAssertNotNil(error, nil);
  76  STAssertEqualObjects([error domain], kGTMRegexErrorDomain, nil);
  77  STAssertEquals([error code], (NSInteger)kGTMRegexPatternParseFailedError, nil);
  78  NSDictionary *userInfo = [error userInfo];
  79  STAssertNotNil(userInfo, @"failed to get userInfo from error");
  80  STAssertEqualObjects([userInfo objectForKey:kGTMRegexPatternErrorPattern], @"(.", nil);
  81  STAssertNotNil([userInfo objectForKey:kGTMRegexPatternErrorErrorString], nil);
  82
  83  // basic pattern w/ options
  84  STAssertNotNil([[[GTMRegex alloc] initWithPattern:@"(.*)"] autorelease], nil);
  85  STAssertNotNil([[[GTMRegex alloc] initWithPattern:@"(.*)"
  86                                            options:0] autorelease], nil);
  87  STAssertNotNil([[[GTMRegex alloc] initWithPattern:@"(.*)"
  88                                            options:kGTMRegexOptionIgnoreCase] autorelease], nil);
  89  error = nil;
  90  STAssertNotNil([[[GTMRegex alloc] initWithPattern:@"(.*)"
  91                                            options:kGTMRegexOptionIgnoreCase
  92                                          withError:&error] autorelease], nil);
  93  STAssertNil(error, @"shouldn't have been any error");
  94
  95  // fail cases (helper)
  96  STAssertNil([GTMRegex regexWithPattern:nil], nil);
  97  STAssertNil([GTMRegex regexWithPattern:nil
  98                                 options:0], nil);
  99  [GTMUnitTestDevLog expectString:@"Invalid pattern \"(.\", error: \"parentheses not balanced\""];
 100  STAssertNil([GTMRegex regexWithPattern:@"(."], nil);
 101  [GTMUnitTestDevLog expectString:@"Invalid pattern \"(.\", error: \"parentheses not balanced\""];
 102  STAssertNil([GTMRegex regexWithPattern:@"(."
 103                                 options:0], nil);
 104  // fail cases (helper) w/ error param
 105  STAssertNil([GTMRegex regexWithPattern:nil
 106                                 options:kGTMRegexOptionIgnoreCase
 107                               withError:&error], nil);
 108  STAssertNil(error, @"no pattern, shouldn't get error object");
 109  STAssertNil([GTMRegex regexWithPattern:@"(."
 110                                 options:kGTMRegexOptionIgnoreCase
 111                               withError:&error], nil);
 112  STAssertNotNil(error, nil);
 113  STAssertEqualObjects([error domain], kGTMRegexErrorDomain, nil);
 114  STAssertEquals([error code], (NSInteger)kGTMRegexPatternParseFailedError, nil);
 115  userInfo = [error userInfo];
 116  STAssertNotNil(userInfo, @"failed to get userInfo from error");
 117  STAssertEqualObjects([userInfo objectForKey:kGTMRegexPatternErrorPattern], @"(.", nil);
 118  STAssertNotNil([userInfo objectForKey:kGTMRegexPatternErrorErrorString], nil);
 119  
 120  // basic pattern w/ options (helper)
 121  STAssertNotNil([GTMRegex regexWithPattern:@"(.*)"], nil);
 122  STAssertNotNil([GTMRegex regexWithPattern:@"(.*)"
 123                                    options:0], nil);
 124  STAssertNotNil([GTMRegex regexWithPattern:@"(.*)"
 125                                    options:kGTMRegexOptionIgnoreCase], nil);
 126  error = nil;
 127  STAssertNotNil([GTMRegex regexWithPattern:@"(.*)"
 128                                    options:kGTMRegexOptionIgnoreCase
 129                                  withError:&error], nil);
 130  STAssertNil(error, @"shouldn't have been any error");
 131  
 132  // not really a test on GTMRegex, but make sure we block attempts to directly
 133  // alloc/init a GTMRegexStringSegment.
 134  STAssertThrowsSpecificNamed([[[GTMRegexStringSegment alloc] init] autorelease],
 135                              NSException, NSInvalidArgumentException,
 136                              @"shouldn't have been able to alloc/init a GTMRegexStringSegment");
 137}
 138
 139- (void)testOptions {
 140
 141  NSString *testString = @"aaa AAA\nbbb BBB\n aaa aAa\n bbb BbB";
 142
 143  // default options
 144  GTMRegex *regex = [GTMRegex regexWithPattern:@"a+"];
 145  STAssertNotNil(regex, nil);
 146  NSEnumerator *enumerator = [regex segmentEnumeratorForString:testString];
 147  STAssertNotNil(enumerator, nil);
 148  // "aaa"
 149  GTMRegexStringSegment *seg = [enumerator nextObject];
 150  STAssertNotNil(seg, nil);
 151  STAssertTrue([seg isMatch], nil);
 152  STAssertEqualStrings([seg string], @"aaa", nil);
 153  // " AAA\nbbb BBB\n "
 154  seg = [enumerator nextObject];
 155  STAssertNotNil(seg, nil);
 156  STAssertFalse([seg isMatch], nil);
 157  STAssertEqualStrings([seg string], @" AAA\nbbb BBB\n ", nil);
 158  // "aaa"
 159  seg = [enumerator nextObject];
 160  STAssertNotNil(seg, nil);
 161  STAssertTrue([seg isMatch], nil);
 162  STAssertEqualStrings([seg string], @"aaa", nil);
 163  // " "
 164  seg = [enumerator nextObject];
 165  STAssertNotNil(seg, nil);
 166  STAssertFalse([seg isMatch], nil);
 167  STAssertEqualStrings([seg string], @" ", nil);
 168  // "a"
 169  seg = [enumerator nextObject];
 170  STAssertNotNil(seg, nil);
 171  STAssertTrue([seg isMatch], nil);
 172  STAssertEqualStrings([seg string], @"a", nil);
 173  // "A"
 174  seg = [enumerator nextObject];
 175  STAssertNotNil(seg, nil);
 176  STAssertFalse([seg isMatch], nil);
 177  STAssertEqualStrings([seg string], @"A", nil);
 178  // "a"
 179  seg = [enumerator nextObject];
 180  STAssertNotNil(seg, nil);
 181  STAssertTrue([seg isMatch], nil);
 182  STAssertEqualStrings([seg string], @"a", nil);
 183  // "\n bbb BbB"
 184  seg = [enumerator nextObject];
 185  STAssertNotNil(seg, nil);
 186  STAssertFalse([seg isMatch], nil);
 187  STAssertEqualStrings([seg string], @"\n bbb BbB", nil);
 188  // (end)
 189  seg = [enumerator nextObject];
 190  STAssertNil(seg, nil);
 191
 192  // kGTMRegexOptionIgnoreCase
 193  regex = [GTMRegex regexWithPattern:@"a+" options:kGTMRegexOptionIgnoreCase];
 194  STAssertNotNil(regex, nil);
 195  enumerator = [regex segmentEnumeratorForString:testString];
 196  STAssertNotNil(enumerator, nil);
 197  // "aaa"
 198  seg = [enumerator nextObject];
 199  STAssertNotNil(seg, nil);
 200  STAssertTrue([seg isMatch], nil);
 201  STAssertEqualStrings([seg string], @"aaa", nil);
 202  // " "
 203  seg = [enumerator nextObject];
 204  STAssertNotNil(seg, nil);
 205  STAssertFalse([seg isMatch], nil);
 206  STAssertEqualStrings([seg string], @" ", nil);
 207  // "AAA"
 208  seg = [enumerator nextObject];
 209  STAssertNotNil(seg, nil);
 210  STAssertTrue([seg isMatch], nil);
 211  STAssertEqualStrings([seg string], @"AAA", nil);
 212  // "\nbbb BBB\n "
 213  seg = [enumerator nextObject];
 214  STAssertNotNil(seg, nil);
 215  STAssertFalse([seg isMatch], nil);
 216  STAssertEqualStrings([seg string], @"\nbbb BBB\n ", nil);
 217  // "aaa"
 218  seg = [enumerator nextObject];
 219  STAssertNotNil(seg, nil);
 220  STAssertTrue([seg isMatch], nil);
 221  STAssertEqualStrings([seg string], @"aaa", nil);
 222  // " "
 223  seg = [enumerator nextObject];
 224  STAssertNotNil(seg, nil);
 225  STAssertFalse([seg isMatch], nil);
 226  STAssertEqualStrings([seg string], @" ", nil);
 227  // "aAa"
 228  seg = [enumerator nextObject];
 229  STAssertNotNil(seg, nil);
 230  STAssertTrue([seg isMatch], nil);
 231  STAssertEqualStrings([seg string], @"aAa", nil);
 232  // "\n bbb BbB"
 233  seg = [enumerator nextObject];
 234  STAssertNotNil(seg, nil);
 235  STAssertFalse([seg isMatch], nil);
 236  STAssertEqualStrings([seg string], @"\n bbb BbB", nil);
 237  // (end)
 238  seg = [enumerator nextObject];
 239  STAssertNil(seg, nil);
 240
 241  // defaults w/ '^'
 242  regex = [GTMRegex regexWithPattern:@"^a+"];
 243  STAssertNotNil(regex, nil);
 244  enumerator = [regex segmentEnumeratorForString:testString];
 245  STAssertNotNil(enumerator, nil);
 246  // "aaa"
 247  seg = [enumerator nextObject];
 248  STAssertNotNil(seg, nil);
 249  STAssertTrue([seg isMatch], nil);
 250  STAssertEqualStrings([seg string], @"aaa", nil);
 251  // " AAA\nbbb BBB\n aaa aAa\n bbb BbB"
 252  seg = [enumerator nextObject];
 253  STAssertNotNil(seg, nil);
 254  STAssertFalse([seg isMatch], nil);
 255  STAssertEqualStrings([seg string], @" AAA\nbbb BBB\n aaa aAa\n bbb BbB", nil);
 256  // (end)
 257  seg = [enumerator nextObject];
 258  STAssertNil(seg, nil);
 259
 260  // defaults w/ '$'
 261  regex = [GTMRegex regexWithPattern:@"B+$"];
 262  STAssertNotNil(regex, nil);
 263  enumerator = [regex segmentEnumeratorForString:testString];
 264  STAssertNotNil(enumerator, nil);
 265  // "aaa AAA\nbbb "
 266  seg = [enumerator nextObject];
 267  STAssertNotNil(seg, nil);
 268  STAssertFalse([seg isMatch], nil);
 269  STAssertEqualStrings([seg string], @"aaa AAA\nbbb ", nil);
 270  // "BBB"
 271  seg = [enumerator nextObject];
 272  STAssertNotNil(seg, nil);
 273  STAssertTrue([seg isMatch], nil);
 274  STAssertEqualStrings([seg string], @"BBB", nil);
 275  // "\n aaa aAa\n bbb Bb"
 276  seg = [enumerator nextObject];
 277  STAssertNotNil(seg, nil);
 278  STAssertFalse([seg isMatch], nil);
 279  STAssertEqualStrings([seg string], @"\n aaa aAa\n bbb Bb", nil);
 280  // "B"
 281  seg = [enumerator nextObject];
 282  STAssertNotNil(seg, nil);
 283  STAssertTrue([seg isMatch], nil);
 284  STAssertEqualStrings([seg string], @"B", nil);
 285  // (end)
 286  seg = [enumerator nextObject];
 287  STAssertNil(seg, nil);
 288
 289  // kGTMRegexOptionIgnoreCase w/ '$'
 290  regex = [GTMRegex regexWithPattern:@"B+$"
 291                            options:kGTMRegexOptionIgnoreCase];
 292  STAssertNotNil(regex, nil);
 293  enumerator = [regex segmentEnumeratorForString:testString];
 294  STAssertNotNil(enumerator, nil);
 295  // "aaa AAA\nbbb "
 296  seg = [enumerator nextObject];
 297  STAssertNotNil(seg, nil);
 298  STAssertFalse([seg isMatch], nil);
 299  STAssertEqualStrings([seg string], @"aaa AAA\nbbb ", nil);
 300  // "BBB"
 301  seg = [enumerator nextObject];
 302  STAssertNotNil(seg, nil);
 303  STAssertTrue([seg isMatch], nil);
 304  STAssertEqualStrings([seg string], @"BBB", nil);
 305  // "\n aaa aAa\n bbb "
 306  seg = [enumerator nextObject];
 307  STAssertNotNil(seg, nil);
 308  STAssertFalse([seg isMatch], nil);
 309  STAssertEqualStrings([seg string], @"\n aaa aAa\n bbb ", nil);
 310  // "BbB"
 311  seg = [enumerator nextObject];
 312  STAssertNotNil(seg, nil);
 313  STAssertTrue([seg isMatch], nil);
 314  STAssertEqualStrings([seg string], @"BbB", nil);
 315  // (end)
 316  seg = [enumerator nextObject];
 317  STAssertNil(seg, nil);
 318
 319  // test w/ kGTMRegexOptionSupressNewlineSupport and \n in the string
 320  regex = [GTMRegex regexWithPattern:@"a.*b" options:kGTMRegexOptionSupressNewlineSupport];
 321  STAssertNotNil(regex, nil);
 322  enumerator = [regex segmentEnumeratorForString:testString];
 323  STAssertNotNil(enumerator, nil);
 324  // "aaa AAA\nbbb BBB\n aaa aAa\n bbb Bb"
 325  seg = [enumerator nextObject];
 326  STAssertNotNil(seg, nil);
 327  STAssertTrue([seg isMatch], nil);
 328  STAssertEqualStrings([seg string], @"aaa AAA\nbbb BBB\n aaa aAa\n bbb Bb", nil);
 329  // "B"
 330  seg = [enumerator nextObject];
 331  STAssertNotNil(seg, nil);
 332  STAssertFalse([seg isMatch], nil);
 333  STAssertEqualStrings([seg string], @"B", nil);
 334  // (end)
 335  seg = [enumerator nextObject];
 336  STAssertNil(seg, nil);
 337
 338  // test w/o kGTMRegexOptionSupressNewlineSupport and \n in the string
 339  // (this is no match since it '.' can't match the '\n')
 340  regex = [GTMRegex regexWithPattern:@"a.*b"];
 341  STAssertNotNil(regex, nil);
 342  enumerator = [regex segmentEnumeratorForString:testString];
 343  STAssertNotNil(enumerator, nil);
 344  // "aaa AAA\nbbb BBB\n aaa aAa\n bbb BbB"
 345  seg = [enumerator nextObject];
 346  STAssertNotNil(seg, nil);
 347  STAssertFalse([seg isMatch], nil);
 348  STAssertEqualStrings([seg string], @"aaa AAA\nbbb BBB\n aaa aAa\n bbb BbB", nil);
 349  // (end)
 350  seg = [enumerator nextObject];
 351  STAssertNil(seg, nil);
 352  
 353  // kGTMRegexOptionSupressNewlineSupport w/ '^'
 354  regex = [GTMRegex regexWithPattern:@"^a+" options:kGTMRegexOptionSupressNewlineSupport];
 355  STAssertNotNil(regex, nil);
 356  enumerator = [regex segmentEnumeratorForString:testString];
 357  STAssertNotNil(enumerator, nil);
 358  // "aaa"
 359  seg = [enumerator nextObject];
 360  STAssertNotNil(seg, nil);
 361  STAssertTrue([seg isMatch], nil);
 362  STAssertEqualStrings([seg string], @"aaa", nil);
 363  // " AAA\nbbb BBB\n aaa aAa\n bbb BbB"
 364  seg = [enumerator nextObject];
 365  STAssertNotNil(seg, nil);
 366  STAssertFalse([seg isMatch], nil);
 367  STAssertEqualStrings([seg string], @" AAA\nbbb BBB\n aaa aAa\n bbb BbB", nil);
 368  // (end)
 369  seg = [enumerator nextObject];
 370  STAssertNil(seg, nil);
 371
 372  // kGTMRegexOptionSupressNewlineSupport w/ '$'
 373  regex = [GTMRegex regexWithPattern:@"B+$" options:kGTMRegexOptionSupressNewlineSupport];
 374  STAssertNotNil(regex, nil);
 375  enumerator = [regex segmentEnumeratorForString:testString];
 376  STAssertNotNil(enumerator, nil);
 377  // "aaa AAA\nbbb BBB\n aaa aAa\n bbb Bb"
 378  seg = [enumerator nextObject];
 379  STAssertNotNil(seg, nil);
 380  STAssertFalse([seg isMatch], nil);
 381  STAssertEqualStrings([seg string], @"aaa AAA\nbbb BBB\n aaa aAa\n bbb Bb", nil);
 382  // "B"
 383  seg = [enumerator nextObject];
 384  STAssertNotNil(seg, nil);
 385  STAssertTrue([seg isMatch], nil);
 386  STAssertEqualStrings([seg string], @"B", nil);
 387  // (end)
 388  seg = [enumerator nextObject];
 389  STAssertNil(seg, nil);
 390}
 391
 392- (void)testSubPatternCount {
 393  STAssertEquals((NSUInteger)0, [[GTMRegex regexWithPattern:@".*"] subPatternCount], nil);
 394  STAssertEquals((NSUInteger)1, [[GTMRegex regexWithPattern:@"(.*)"] subPatternCount], nil);
 395  STAssertEquals((NSUInteger)1, [[GTMRegex regexWithPattern:@"[fo]*(.*)[bar]*"] subPatternCount], nil);
 396  STAssertEquals((NSUInteger)3, [[GTMRegex regexWithPattern:@"([fo]*)(.*)([bar]*)"] subPatternCount], nil);
 397  STAssertEquals((NSUInteger)7, [[GTMRegex regexWithPattern:@"(([bar]*)|([fo]*))(.*)(([bar]*)|([fo]*))"] subPatternCount], nil);
 398}
 399
 400- (void)testMatchesString {
 401  // simple pattern
 402  GTMRegex *regex = [GTMRegex regexWithPattern:@"foo.*bar"];
 403  STAssertNotNil(regex, nil);
 404  STAssertTrue([regex matchesString:@"foobar"], nil);
 405  STAssertTrue([regex matchesString:@"foobydoo spambar"], nil);
 406  STAssertFalse([regex matchesString:@"zzfoobarzz"], nil);
 407  STAssertFalse([regex matchesString:@"zzfoobydoo spambarzz"], nil);
 408  STAssertFalse([regex matchesString:@"abcdef"], nil);
 409  STAssertFalse([regex matchesString:@""], nil);
 410  STAssertFalse([regex matchesString:nil], nil);
 411  // pattern w/ sub patterns
 412  regex = [GTMRegex regexWithPattern:@"(foo)(.*)(bar)"];
 413  STAssertNotNil(regex, nil);
 414  STAssertTrue([regex matchesString:@"foobar"], nil);
 415  STAssertTrue([regex matchesString:@"foobydoo spambar"], nil);
 416  STAssertFalse([regex matchesString:@"zzfoobarzz"], nil);
 417  STAssertFalse([regex matchesString:@"zzfoobydoo spambarzz"], nil);
 418  STAssertFalse([regex matchesString:@"abcdef"], nil);
 419  STAssertFalse([regex matchesString:@""], nil);
 420  STAssertFalse([regex matchesString:nil], nil);
 421}
 422
 423- (void)testSubPatternsOfString {
 424  GTMRegex *regex = [GTMRegex regexWithPattern:@"(fo(o+))((bar)|(baz))"];
 425  STAssertNotNil(regex, nil);
 426  STAssertEquals((NSUInteger)5, [regex subPatternCount], nil);
 427  NSArray *subPatterns = [regex subPatternsOfString:@"foooooobaz"];
 428  STAssertNotNil(subPatterns, nil);
 429  STAssertEquals((NSUInteger)6, [subPatterns count], nil);
 430  STAssertEqualStrings(@"foooooobaz", [subPatterns objectAtIndex:0], nil);
 431  STAssertEqualStrings(@"foooooo", [subPatterns objectAtIndex:1], nil);
 432  STAssertEqualStrings(@"ooooo", [subPatterns objectAtIndex:2], nil);
 433  STAssertEqualStrings(@"baz", [subPatterns objectAtIndex:3], nil);
 434  STAssertEqualObjects([NSNull null], [subPatterns objectAtIndex:4], nil);
 435  STAssertEqualStrings(@"baz", [subPatterns objectAtIndex:5], nil);
 436
 437  // not there
 438  subPatterns = [regex subPatternsOfString:@"aaa"];
 439  STAssertNil(subPatterns, nil);
 440
 441  // not extra stuff on either end
 442  subPatterns = [regex subPatternsOfString:@"ZZZfoooooobaz"];
 443  STAssertNil(subPatterns, nil);
 444  subPatterns = [regex subPatternsOfString:@"foooooobazZZZ"];
 445  STAssertNil(subPatterns, nil);
 446  subPatterns = [regex subPatternsOfString:@"ZZZfoooooobazZZZ"];
 447  STAssertNil(subPatterns, nil);
 448}
 449
 450- (void)testFirstSubStringMatchedInString {
 451  // simple pattern
 452  GTMRegex *regex = [GTMRegex regexWithPattern:@"foo.*bar"];
 453  STAssertNotNil(regex, nil);
 454  STAssertEqualStrings([regex firstSubStringMatchedInString:@"foobar"],
 455                       @"foobar", nil);
 456  STAssertEqualStrings([regex firstSubStringMatchedInString:@"foobydoo spambar"],
 457                       @"foobydoo spambar", nil);
 458  STAssertEqualStrings([regex firstSubStringMatchedInString:@"zzfoobarzz"],
 459                       @"foobar", nil);
 460  STAssertEqualStrings([regex firstSubStringMatchedInString:@"zzfoobydoo spambarzz"],
 461                       @"foobydoo spambar", nil);
 462  STAssertNil([regex firstSubStringMatchedInString:@"abcdef"], nil);
 463  STAssertNil([regex firstSubStringMatchedInString:@""], nil);
 464  // pattern w/ sub patterns
 465  regex = [GTMRegex regexWithPattern:@"(foo)(.*)(bar)"];
 466  STAssertNotNil(regex, nil);
 467  STAssertEqualStrings([regex firstSubStringMatchedInString:@"foobar"],
 468                       @"foobar", nil);
 469  STAssertEqualStrings([regex firstSubStringMatchedInString:@"foobydoo spambar"],
 470                       @"foobydoo spambar", nil);
 471  STAssertEqualStrings([regex firstSubStringMatchedInString:@"zzfoobarzz"],
 472                       @"foobar", nil);
 473  STAssertEqualStrings([regex firstSubStringMatchedInString:@"zzfoobydoo spambarzz"],
 474                       @"foobydoo spambar", nil);
 475  STAssertNil([regex firstSubStringMatchedInString:@"abcdef"], nil);
 476  STAssertNil([regex firstSubStringMatchedInString:@""], nil);
 477}
 478
 479- (void)testMatchesSubStringInString {
 480  // simple pattern
 481  GTMRegex *regex = [GTMRegex regexWithPattern:@"foo.*bar"];
 482  STAssertNotNil(regex, nil);
 483  STAssertTrue([regex matchesSubStringInString:@"foobar"], nil);
 484  STAssertTrue([regex matchesSubStringInString:@"foobydoo spambar"], nil);
 485  STAssertTrue([regex matchesSubStringInString:@"zzfoobarzz"], nil);
 486  STAssertTrue([regex matchesSubStringInString:@"zzfoobydoo spambarzz"], nil);
 487  STAssertFalse([regex matchesSubStringInString:@"abcdef"], nil);
 488  STAssertFalse([regex matchesSubStringInString:@""], nil);
 489  // pattern w/ sub patterns
 490  regex = [GTMRegex regexWithPattern:@"(foo)(.*)(bar)"];
 491  STAssertNotNil(regex, nil);
 492  STAssertTrue([regex matchesSubStringInString:@"foobar"], nil);
 493  STAssertTrue([regex matchesSubStringInString:@"foobydoo spambar"], nil);
 494  STAssertTrue([regex matchesSubStringInString:@"zzfoobarzz"], nil);
 495  STAssertTrue([regex matchesSubStringInString:@"zzfoobydoo spambarzz"], nil);
 496  STAssertFalse([regex matchesSubStringInString:@"abcdef"], nil);
 497  STAssertFalse([regex matchesSubStringInString:@""], nil);
 498}
 499
 500- (void)testSegmentEnumeratorForString {
 501  GTMRegex *regex = [GTMRegex regexWithPattern:@"foo+ba+r"];
 502  STAssertNotNil(regex, nil);
 503  
 504  // test odd input
 505  NSEnumerator *enumerator = [regex segmentEnumeratorForString:@""];
 506  STAssertNotNil(enumerator, nil);
 507  enumerator = [regex segmentEnumeratorForString:nil];
 508  STAssertNil(enumerator, nil);
 509  
 510  // on w/ the normal tests
 511  enumerator = [regex segmentEnumeratorForString:@"afoobarbfooobaarfoobarzz"];
 512  STAssertNotNil(enumerator, nil);
 513  // "a"
 514  GTMRegexStringSegment *seg = [enumerator nextObject];
 515  STAssertNotNil(seg, nil);
 516  STAssertFalse([seg isMatch], nil);
 517  STAssertEqualStrings([seg string], @"a", nil);
 518  // "foobar"
 519  seg = [enumerator nextObject];
 520  STAssertNotNil(seg, nil);
 521  STAssertTrue([seg isMatch], nil);
 522  STAssertEqualStrings([seg string], @"foobar", nil);
 523  // "b"
 524  seg = [enumerator nextObject];
 525  STAssertNotNil(seg, nil);
 526  STAssertFalse([seg isMatch], nil);
 527  STAssertEqualStrings([seg string], @"b", nil);
 528  // "fooobaar"
 529  seg = [enumerator nextObject];
 530  STAssertNotNil(seg, nil);
 531  STAssertTrue([seg isMatch], nil);
 532  STAssertEqualStrings([seg string], @"fooobaar", nil);
 533  // "foobar"
 534  seg = [enumerator nextObject];
 535  STAssertNotNil(seg, nil);
 536  STAssertTrue([seg isMatch], nil);
 537  STAssertEqualStrings([seg string], @"foobar", nil);
 538  // "zz"
 539  seg = [enumerator nextObject];
 540  STAssertNotNil(seg, nil);
 541  STAssertFalse([seg isMatch], nil);
 542  STAssertEqualStrings([seg string], @"zz", nil);
 543  // (end)
 544  seg = [enumerator nextObject];
 545  STAssertNil(seg, nil);
 546
 547  // test no match
 548  enumerator = [regex segmentEnumeratorForString:@"aaa"];
 549  STAssertNotNil(enumerator, nil);
 550  seg = [enumerator nextObject];
 551  STAssertNotNil(seg, nil);
 552  STAssertFalse([seg isMatch], nil);
 553  STAssertEqualStrings([seg string], @"aaa", nil);
 554  seg = [enumerator nextObject];
 555  STAssertNil(seg, nil);
 556
 557  // test only match
 558  enumerator = [regex segmentEnumeratorForString:@"foobar"];
 559  STAssertNotNil(enumerator, nil);
 560  seg = [enumerator nextObject];
 561  STAssertNotNil(seg, nil);
 562  STAssertTrue([seg isMatch], nil);
 563  STAssertEqualStrings([seg string], @"foobar", nil);
 564  seg = [enumerator nextObject];
 565  STAssertNil(seg, nil);
 566
 567  // now test the saved sub segments
 568  regex = [GTMRegex regexWithPattern:@"(foo)((bar)|(baz))"];
 569  STAssertNotNil(regex, nil);
 570  STAssertEquals((NSUInteger)4, [regex subPatternCount], nil);
 571  enumerator = [regex segmentEnumeratorForString:@"foobarxxfoobaz"];
 572  STAssertNotNil(enumerator, nil);
 573  // "foobar"
 574  seg = [enumerator nextObject];
 575  STAssertNotNil(seg, nil);
 576  STAssertTrue([seg isMatch], nil);
 577  STAssertEqualStrings([seg string], @"foobar", nil);
 578  STAssertEqualStrings([seg subPatternString:0], @"foobar", nil);
 579  STAssertEqualStrings([seg subPatternString:1], @"foo", nil);
 580  STAssertEqualStrings([seg subPatternString:2], @"bar", nil);
 581  STAssertEqualStrings([seg subPatternString:3], @"bar", nil);
 582  STAssertNil([seg subPatternString:4], nil); // nothing matched "(baz)"
 583  STAssertNil([seg subPatternString:5], nil);
 584  // "xx"
 585  seg = [enumerator nextObject];
 586  STAssertNotNil(seg, nil);
 587  STAssertFalse([seg isMatch], nil);
 588  STAssertEqualStrings([seg string], @"xx", nil);
 589  STAssertEqualStrings([seg subPatternString:0], @"xx", nil);
 590  STAssertNil([seg subPatternString:1], nil);
 591  // "foobaz"
 592  seg = [enumerator nextObject];
 593  STAssertNotNil(seg, nil);
 594  STAssertTrue([seg isMatch], nil);
 595  STAssertEqualStrings([seg string], @"foobaz", nil);
 596  STAssertEqualStrings([seg subPatternString:0], @"foobaz", nil);
 597  STAssertEqualStrings([seg subPatternString:1], @"foo", nil);
 598  STAssertEqualStrings([seg subPatternString:2], @"baz", nil);
 599  STAssertNil([seg subPatternString:3], nil); // (nothing matched "(bar)"
 600  STAssertEqualStrings([seg subPatternString:4], @"baz", nil);
 601  STAssertNil([seg subPatternString:5], nil);
 602  // (end)
 603  seg = [enumerator nextObject];
 604  STAssertNil(seg, nil);
 605
 606  // test all objects
 607  regex = [GTMRegex regexWithPattern:@"foo+ba+r"];
 608  STAssertNotNil(regex, nil);
 609  enumerator = [regex segmentEnumeratorForString:@"afoobarbfooobaarfoobarzz"];
 610  STAssertNotNil(enumerator, nil);
 611  NSArray *allSegments = [enumerator allObjects];
 612  STAssertNotNil(allSegments, nil);
 613  STAssertEquals((NSUInteger)6, [allSegments count], nil);
 614
 615  // test we are getting the flags right for newline
 616  regex = [GTMRegex regexWithPattern:@"^a"];
 617  STAssertNotNil(regex, nil);
 618  enumerator = [regex segmentEnumeratorForString:@"aa\naa"];
 619  STAssertNotNil(enumerator, nil);
 620  // "a"
 621  seg = [enumerator nextObject];
 622  STAssertNotNil(seg, nil);
 623  STAssertTrue([seg isMatch], nil);
 624  STAssertEqualStrings([seg string], @"a", nil);
 625  // "a\n"
 626  seg = [enumerator nextObject];
 627  STAssertNotNil(seg, nil);
 628  STAssertFalse([seg isMatch], nil);
 629  STAssertEqualStrings([seg string], @"a\n", nil);
 630  // "a"
 631  seg = [enumerator nextObject];
 632  STAssertNotNil(seg, nil);
 633  STAssertTrue([seg isMatch], nil);
 634  STAssertEqualStrings([seg string], @"a", nil);
 635  // "a"
 636  seg = [enumerator nextObject];
 637  STAssertNotNil(seg, nil);
 638  STAssertFalse([seg isMatch], nil);
 639  STAssertEqualStrings([seg string], @"a", nil);
 640  // (end)
 641  seg = [enumerator nextObject];
 642  STAssertNil(seg, nil);
 643
 644  // test we are getting the flags right for newline, part 2
 645  regex = [GTMRegex regexWithPattern:@"^a*$"];
 646  STAssertNotNil(regex, nil);
 647  enumerator = [regex segmentEnumeratorForString:@"aa\naa\nbb\naa"];
 648  STAssertNotNil(enumerator, nil);
 649  // "aa"
 650  seg = [enumerator nextObject];
 651  STAssertNotNil(seg, nil);
 652  STAssertTrue([seg isMatch], nil);
 653  STAssertEqualStrings([seg string], @"aa", nil);
 654  // "\n"
 655  seg = [enumerator nextObject];
 656  STAssertNotNil(seg, nil);
 657  STAssertFalse([seg isMatch], nil);
 658  STAssertEqualStrings([seg string], @"\n", nil);
 659  // "aa"
 660  seg = [enumerator nextObject];
 661  STAssertNotNil(seg, nil);
 662  STAssertTrue([seg isMatch], nil);
 663  STAssertEqualStrings([seg string], @"aa", nil);
 664  // "\nbb\n"
 665  seg = [enumerator nextObject];
 666  STAssertNotNil(seg, nil);
 667  STAssertFalse([seg isMatch], nil);
 668  STAssertEqualStrings([seg string], @"\nbb\n", nil);
 669  // "aa"
 670  seg = [enumerator nextObject];
 671  STAssertNotNil(seg, nil);
 672  STAssertTrue([seg isMatch], nil);
 673  STAssertEqualStrings([seg string], @"aa", nil);
 674  // (end)
 675  seg = [enumerator nextObject];
 676  STAssertNil(seg, nil);
 677
 678  // make sure the enum cleans up if not walked to the end
 679  regex = [GTMRegex regexWithPattern:@"b+"];
 680  STAssertNotNil(regex, nil);
 681  enumerator = [regex segmentEnumeratorForString:@"aabbcc"];
 682  STAssertNotNil(enumerator, nil);
 683  // "aa"
 684  seg = [enumerator nextObject];
 685  STAssertNotNil(seg, nil);
 686  STAssertFalse([seg isMatch], nil);
 687  STAssertEqualStrings([seg string], @"aa", nil);
 688  // and done w/o walking the rest
 689}
 690
 691- (void)testMatchSegmentEnumeratorForString {
 692  GTMRegex *regex = [GTMRegex regexWithPattern:@"foo+ba+r"];
 693  STAssertNotNil(regex, nil);
 694
 695  // test odd input
 696  NSEnumerator *enumerator = [regex matchSegmentEnumeratorForString:@""];
 697  STAssertNotNil(enumerator, nil);
 698  enumerator = [regex matchSegmentEnumeratorForString:nil];
 699  STAssertNil(enumerator, nil);
 700  
 701  // on w/ the normal tests
 702  enumerator = [regex matchSegmentEnumeratorForString:@"afoobarbfooobaarfoobarzz"];
 703  STAssertNotNil(enumerator, nil);
 704  // "a" - skipped
 705  // "foobar"
 706  GTMRegexStringSegment *seg = [enumerator nextObject];
 707  STAssertNotNil(seg, nil);
 708  STAssertTrue([seg isMatch], nil);
 709  STAssertEqualStrings([seg string], @"foobar", nil);
 710  // "b" - skipped
 711  // "fooobaar"
 712  seg = [enumerator nextObject];
 713  STAssertNotNil(seg, nil);
 714  STAssertTrue([seg isMatch], nil);
 715  STAssertEqualStrings([seg string], @"fooobaar", nil);
 716  // "foobar"
 717  seg = [enumerator nextObject];
 718  STAssertNotNil(seg, nil);
 719  STAssertTrue([seg isMatch], nil);
 720  STAssertEqualStrings([seg string], @"foobar", nil);
 721  // "zz" - skipped
 722  // (end)
 723  seg = [enumerator nextObject];
 724  STAssertNil(seg, nil);
 725
 726  // test no match
 727  enumerator = [regex matchSegmentEnumeratorForString:@"aaa"];
 728  STAssertNotNil(enumerator, nil);
 729  seg = [enumerator nextObject];
 730  STAssertNil(seg, nil); // should have gotten nothing
 731
 732  // test only match
 733  enumerator = [regex matchSegmentEnumeratorForString:@"foobar"];
 734  STAssertNotNil(enumerator, nil);
 735  seg = [enumerator nextObject];
 736  STAssertNotNil(seg, nil);
 737  STAssertTrue([seg isMatch], nil);
 738  STAssertEqualStrings([seg string], @"foobar", nil);
 739  seg = [enumerator nextObject];
 740  STAssertNil(seg, nil);
 741
 742  // now test the saved sub segments
 743  regex = [GTMRegex regexWithPattern:@"(foo)((bar)|(baz))"];
 744  STAssertNotNil(regex, nil);
 745  STAssertEquals((NSUInteger)4, [regex subPatternCount], nil);
 746  enumerator = [regex matchSegmentEnumeratorForString:@"foobarxxfoobaz"];
 747  STAssertNotNil(enumerator, nil);
 748  // "foobar"
 749  seg = [enumerator nextObject];
 750  STAssertNotNil(seg, nil);
 751  STAssertTrue([seg isMatch], nil);
 752  STAssertEqualStrings([seg string], @"foobar", nil);
 753  STAssertEqualStrings([seg subPatternString:0], @"foobar", nil);
 754  STAssertEqualStrings([seg subPatternString:1], @"foo", nil);
 755  STAssertEqualStrings([seg subPatternString:2], @"bar", nil);
 756  STAssertEqualStrings([seg subPatternString:3], @"bar", nil);
 757  STAssertNil([seg subPatternString:4], nil); // nothing matched "(baz)"
 758  STAssertNil([seg subPatternString:5], nil);
 759  // "xx" - skipped
 760  // "foobaz"
 761  seg = [enumerator nextObject];
 762  STAssertNotNil(seg, nil);
 763  STAssertTrue([seg isMatch], nil);
 764  STAssertEqualStrings([seg string], @"foobaz", nil);
 765  STAssertEqualStrings([seg subPatternString:0], @"foobaz", nil);
 766  STAssertEqualStrings([seg subPatternString:1], @"foo", nil);
 767  STAssertEqualStrings([seg subPatternString:2], @"baz", nil);
 768  STAssertNil([seg subPatternString:3], nil); // (nothing matched "(bar)"
 769  STAssertEqualStrings([seg subPatternString:4], @"baz", nil);
 770  STAssertNil([seg subPatternString:5], nil);
 771  // (end)
 772  seg = [enumerator nextObject];
 773  STAssertNil(seg, nil);
 774
 775  // test all objects
 776  regex = [GTMRegex regexWithPattern:@"foo+ba+r"];
 777  STAssertNotNil(regex, nil);
 778  enumerator = [regex matchSegmentEnumeratorForString:@"afoobarbfooobaarfoobarzz"];
 779  STAssertNotNil(enumerator, nil);
 780  NSArray *allSegments = [enumerator allObjects];
 781  STAssertNotNil(allSegments, nil);
 782  STAssertEquals((NSUInteger)3, [allSegments count], nil);
 783  
 784  // test we are getting the flags right for newline
 785  regex = [GTMRegex regexWithPattern:@"^a"];
 786  STAssertNotNil(regex, nil);
 787  enumerator = [regex matchSegmentEnumeratorForString:@"aa\naa"];
 788  STAssertNotNil(enumerator, nil);
 789  // "a"
 790  seg = [enumerator nextObject];
 791  STAssertNotNil(seg, nil);
 792  STAssertTrue([seg isMatch], nil);
 793  STAssertEqualStrings([seg string], @"a", nil);
 794  // "a\n" - skipped
 795  // "a"
 796  seg = [enumerator nextObject];
 797  STAssertNotNil(seg, nil);
 798  STAssertTrue([seg isMatch], nil);
 799  STAssertEqualStrings([seg string], @"a", nil);
 800  // "a" - skipped
 801  // (end)
 802  seg = [enumerator nextObject];
 803  STAssertNil(seg, nil);
 804
 805  // test we are getting the flags right for newline, part 2
 806  regex = [GTMRegex regexWithPattern:@"^a*$"];
 807  STAssertNotNil(regex, nil);
 808  enumerator = [regex matchSegmentEnumeratorForString:@"aa\naa\nbb\naa"];
 809  STAssertNotNil(enumerator, nil);
 810  // "aa"
 811  seg = [enumerator nextObject];
 812  STAssertNotNil(seg, nil);
 813  STAssertTrue([seg isMatch], nil);
 814  STAssertEqualStrings([seg string], @"aa", nil);
 815  // "\n" - skipped
 816  // "aa"
 817  seg = [enumerator nextObject];
 818  STAssertNotNil(seg, nil);
 819  STAssertTrue([seg isMatch], nil);
 820  STAssertEqualStrings([seg string], @"aa", nil);
 821  // "\nbb\n" - skipped
 822  // "aa"
 823  seg = [enumerator nextObject];
 824  STAssertNotNil(seg, nil);
 825  STAssertTrue([seg isMatch], nil);
 826  STAssertEqualStrings([seg string], @"aa", nil);
 827  // (end)
 828  seg = [enumerator nextObject];
 829  STAssertNil(seg, nil);
 830}
 831
 832- (void)testStringByReplacingMatchesInStringWithReplacement {
 833  GTMRegex *regex = [GTMRegex regexWithPattern:@"(foo)(.*)(bar)"];
 834  STAssertNotNil(regex, nil);
 835  // the basics
 836  STAssertEqualStrings(@"weeZbarZbydoo spamZfooZdoggies",
 837                       [regex stringByReplacingMatchesInString:@"weefoobydoo spambardoggies"
 838                                               withReplacement:@"Z\\3Z\\2Z\\1Z"],
 839                       nil);
 840  // nil/empty replacement
 841  STAssertEqualStrings(@"weedoggies",
 842                       [regex stringByReplacingMatchesInString:@"weefoobydoo spambardoggies"
 843                                               withReplacement:nil],
 844                       nil);
 845  STAssertEqualStrings(@"weedoggies",
 846                       [regex stringByReplacingMatchesInString:@"weefoobydoo spambardoggies"
 847                                               withReplacement:@""],
 848                       nil);
 849  STAssertEqualStrings(@"",
 850                       [regex stringByReplacingMatchesInString:@""
 851                                               withReplacement:@"abc"],
 852                       nil);
 853  STAssertNil([regex stringByReplacingMatchesInString:nil
 854                                      withReplacement:@"abc"],
 855              nil);
 856  // use optional and invale subexpression parts to confirm that works
 857  regex = [GTMRegex regexWithPattern:@"(fo(o+))((bar)|(baz))"];
 858  STAssertNotNil(regex, nil);
 859  STAssertEqualStrings(@"aaa baz bar bar foo baz aaa",
 860                       [regex stringByReplacingMatchesInString:@"aaa foooooobaz fooobar bar foo baz aaa"
 861                                               withReplacement:@"\\4\\5"],
 862                       nil);
 863  STAssertEqualStrings(@"aaa ZZZ ZZZ bar foo baz aaa",
 864                       [regex stringByReplacingMatchesInString:@"aaa foooooobaz fooobar bar foo baz aaa"
 865                                               withReplacement:@"Z\\10Z\\12Z"],
 866                       nil);
 867  // test slashes in replacement that aren't part of the subpattern reference
 868  regex = [GTMRegex regexWithPattern:@"a+"];
 869  STAssertNotNil(regex, nil);
 870  STAssertEqualStrings(@"z\\\\0 \\\\a \\\\\\\\0z",
 871                       [regex stringByReplacingMatchesInString:@"zaz"
 872                                               withReplacement:@"\\\\0 \\\\\\0 \\\\\\\\0"],
 873                       nil);
 874  STAssertEqualStrings(@"z\\\\a \\\\\\\\0 \\\\\\\\az",
 875                       [regex stringByReplacingMatchesInString:@"zaz"
 876                                               withReplacement:@"\\\\\\0 \\\\\\\\0 \\\\\\\\\\0"],
 877                       nil);
 878  STAssertEqualStrings(@"z\\\\\\\\0 \\\\\\\\a \\\\\\\\\\\\0z",
 879                       [regex stringByReplacingMatchesInString:@"zaz"
 880                                               withReplacement:@"\\\\\\\\0 \\\\\\\\\\0 \\\\\\\\\\\\0"],
 881                       nil);
 882}
 883
 884- (void)testDescriptions {
 885  // default options
 886  GTMRegex *regex = [GTMRegex regexWithPattern:@"a+"];
 887  STAssertNotNil(regex, nil);
 888  STAssertGreaterThan([[regex description] length], (NSUInteger)10,
 889                      @"failed to get a reasonable description for regex");
 890  // enumerator
 891  NSEnumerator *enumerator = [regex segmentEnumeratorForString:@"aaabbbccc"];
 892  STAssertNotNil(enumerator, nil);
 893  STAssertGreaterThan([[enumerator description] length], (NSUInteger)10,
 894                      @"failed to get a reasonable description for regex enumerator");
 895  // string segment
 896  GTMRegexStringSegment *seg = [enumerator nextObject];
 897  STAssertNotNil(seg, nil);
 898  STAssertGreaterThan([[seg description] length], (NSUInteger)10,
 899                      @"failed to get a reasonable description for regex string segment");
 900  // regex w/ other options
 901  regex = [GTMRegex regexWithPattern:@"a+"
 902                             options:(kGTMRegexOptionIgnoreCase | kGTMRegexOptionSupressNewlineSupport)];
 903  STAssertNotNil(regex, nil);
 904  STAssertGreaterThan([[regex description] length], (NSUInteger)10,
 905                      @"failed to get a reasonable description for regex w/ options");
 906}
 907
 908@end
 909
 910@implementation NSString_GTMRegexAdditions
 911// Only partial tests to test that the call get through correctly since the
 912// above really tests them.
 913
 914- (void)testMatchesPattern {
 915  // simple pattern
 916  STAssertTrue([@"foobar" gtm_matchesPattern:@"foo.*bar"], nil);
 917  STAssertTrue([@"foobydoo spambar" gtm_matchesPattern:@"foo.*bar"], nil);
 918  STAssertFalse([@"zzfoobarzz" gtm_matchesPattern:@"foo.*bar"], nil);
 919  STAssertFalse([@"zzfoobydoo spambarzz" gtm_matchesPattern:@"foo.*bar"], nil);
 920  STAssertFalse([@"abcdef" gtm_matchesPattern:@"foo.*bar"], nil);
 921  STAssertFalse([@"" gtm_matchesPattern:@"foo.*bar"], nil);
 922  // pattern w/ sub patterns
 923  STAssertTrue([@"foobar" gtm_matchesPattern:@"(foo)(.*)(bar)"], nil);
 924  STAssertTrue([@"foobydoo spambar" gtm_matchesPattern:@"(foo)(.*)(bar)"], nil);
 925  STAssertFalse([@"zzfoobarzz" gtm_matchesPattern:@"(foo)(.*)(bar)"], nil);
 926  STAssertFalse([@"zzfoobydoo spambarzz" gtm_matchesPattern:@"(foo)(.*)(bar)"], nil);
 927  STAssertFalse([@"abcdef" gtm_matchesPattern:@"(foo)(.*)(bar)"], nil);
 928  STAssertFalse([@"" gtm_matchesPattern:@"(foo)(.*)(bar)"], nil);
 929}
 930
 931- (void)testSubPatternsOfPattern {
 932  NSArray *subPatterns = [@"foooooobaz" gtm_subPatternsOfPattern:@"(fo(o+))((bar)|(baz))"];
 933  STAssertNotNil(subPatterns, nil);
 934  STAssertEquals((NSUInteger)6, [subPatterns count], nil);
 935  STAssertEqualStrings(@"foooooobaz", [subPatterns objectAtIndex:0], nil);
 936  STAssertEqualStrings(@"foooooo", [subPatterns objectAtIndex:1], nil);
 937  STAssertEqualStrings(@"ooooo", [subPatterns objectAtIndex:2], nil);
 938  STAssertEqualStrings(@"baz", [subPatterns objectAtIndex:3], nil);
 939  STAssertEqualObjects([NSNull null], [subPatterns objectAtIndex:4], nil);
 940  STAssertEqualStrings(@"baz", [subPatterns objectAtIndex:5], nil);
 941
 942  // not there
 943  subPatterns = [@"aaa" gtm_subPatternsOfPattern:@"(fo(o+))((bar)|(baz))"];
 944  STAssertNil(subPatterns, nil);
 945
 946  // not extra stuff on either end
 947  subPatterns = [@"ZZZfoooooobaz" gtm_subPatternsOfPattern:@"(fo(o+))((bar)|(baz))"];
 948  STAssertNil(subPatterns, nil);
 949  subPatterns = [@"foooooobazZZZ" gtm_subPatternsOfPattern:@"(fo(o+))((bar)|(baz))"];
 950  STAssertNil(subPatterns, nil);
 951  subPatterns = [@"ZZZfoooooobazZZZ" gtm_subPatternsOfPattern:@"(fo(o+))((bar)|(baz))"];
 952  STAssertNil(subPatterns, nil);
 953}
 954
 955- (void)testFirstSubStringMatchedByPattern {
 956  // simple pattern
 957  STAssertEqualStrings([@"foobar" gtm_firstSubStringMatchedByPattern:@"foo.*bar"],
 958                       @"foobar", nil);
 959  STAssertEqualStrings([@"foobydoo spambar" gtm_firstSubStringMatchedByPattern:@"foo.*bar"],
 960                       @"foobydoo spambar", nil);
 961  STAssertEqualStrings([@"zzfoobarzz" gtm_firstSubStringMatchedByPattern:@"foo.*bar"],
 962                       @"foobar", nil);
 963  STAssertEqualStrings([@"zzfoobydoo spambarzz" gtm_firstSubStringMatchedByPattern:@"foo.*bar"],
 964                       @"foobydoo spambar", nil);
 965  STAssertNil([@"abcdef" gtm_firstSubStringMatchedByPattern:@"foo.*bar"], nil);
 966  STAssertNil([@"" gtm_firstSubStringMatchedByPattern:@"foo.*bar"], nil);
 967  // pattern w/ sub patterns
 968  STAssertEqualStrings([@"foobar" gtm_firstSubStringMatchedByPattern:@"(foo)(.*)(bar)"],
 969                       @"foobar", nil);
 970  STAssertEqualStrings([@"foobydoo spambar" gtm_firstSubStringMatchedByPattern:@"(foo)(.*)(bar)"],
 971                       @"foobydoo spambar", nil);
 972  STAssertEqualStrings([@"zzfoobarzz" gtm_firstSubStringMatchedByPattern:@"(foo)(.*)(bar)"],
 973                       @"foobar", nil);
 974  STAssertEqualStrings([@"zzfoobydoo spambarzz" gtm_firstSubStringMatchedByPattern:@"(foo)(.*)(bar)"],
 975                       @"foobydoo spambar", nil);
 976  STAssertNil([@"abcdef" gtm_firstSubStringMatchedByPattern:@"(foo)(.*)(bar)"], nil);
 977  STAssertNil([@"" gtm_firstSubStringMatchedByPattern:@"(foo)(.*)(bar)"], nil);
 978}
 979
 980- (void)testSubStringMatchesPattern {
 981  // simple pattern
 982  STAssertTrue([@"foobar" gtm_subStringMatchesPattern:@"foo.*bar"], nil);
 983  STAssertTrue([@"foobydoo spambar" gtm_subStringMatchesPattern:@"foo.*bar"], nil);
 984  STAssertTrue([@"zzfoobarzz" gtm_subStringMatchesPattern:@"foo.*bar"], nil);
 985  STAssertTrue([@"zzfoobydoo spambarzz" gtm_subStringMatchesPattern:@"foo.*bar"], nil);
 986  STAssertFalse([@"abcdef" gtm_subStringMatchesPattern:@"foo.*bar"], nil);
 987  STAssertFalse([@"" gtm_subStringMatchesPattern:@"foo.*bar"], nil);
 988  // pattern w/ sub patterns
 989  STAssertTrue([@"foobar" gtm_subStringMatchesPattern:@"(foo)(.*)(bar)"], nil);
 990  STAssertTrue([@"foobydoo spambar" gtm_subStringMatchesPattern:@"(foo)(.*)(bar)"], nil);
 991  STAssertTrue([@"zzfoobarzz" gtm_subStringMatchesPattern:@"(foo)(.*)(bar)"], nil);
 992  STAssertTrue([@"zzfoobydoo spambarzz" gtm_subStringMatchesPattern:@"(foo)(.*)(bar)"], nil);
 993  STAssertFalse([@"abcdef" gtm_subStringMatchesPattern:@"(foo)(.*)(bar)"], nil);
 994  STAssertFalse([@"" gtm_subStringMatchesPattern:@"(foo)(.*)(bar)"], nil);
 995}
 996
 997- (void)testSegmentEnumeratorForPattern {
 998  NSEnumerator *enumerator =
 999    [@"afoobarbfooobaarfoobarzz" gtm_segmentEnumeratorForPattern:@"foo+ba+r"];
1000  STAssertNotNil(enumerator, nil);
1001  // "a"
1002  GTMRegexStringSegment *seg = [enumerator nextObject];
1003  STAssertNotNil(seg, nil);
1004  STAssertFalse([seg isMatch], nil);
1005  STAssertEqualStrings([seg string], @"a", nil);
1006  // "foobar"
1007  seg = [enumerator nextObject];
1008  STAssertNotNil(seg, nil);
1009  STAssertTrue([seg isMatch], nil);
1010  STAssertEqualStrings([seg string], @"foobar", nil);
1011  // "b"
1012  seg = [enumerator nextObject];
1013  STAssertNotNil(seg, nil);
1014  STAssertFalse([seg isMatch], nil);
1015  STAssertEqualStrings([seg string], @"b", nil);
1016  // "fooobaar"
1017  seg = [enumerator nextObject];
1018  STAssertNotNil(seg, nil);
1019  STAssertTrue([seg isMatch], nil);
1020  STAssertEqualStrings([seg string], @"fooobaar", nil);
1021  // "foobar"
1022  seg = [enumerator nextObject];
1023  STAssertNotNil(seg, nil);
1024  STAssertTrue([seg isMatch], nil);
1025  STAssertEqualStrings([seg string], @"foobar", nil);
1026  // "zz"
1027  seg = [enumerator nextObject];
1028  STAssertNotNil(seg, nil);
1029  STAssertFalse([seg isMatch], nil);
1030  STAssertEqualStrings([seg string], @"zz", nil);
1031  // (end)
1032  seg = [enumerator nextObject];
1033  STAssertNil(seg, nil);
1034
1035  // test no match
1036  enumerator = [@"aaa" gtm_segmentEnumeratorForPattern:@"foo+ba+r"];
1037  STAssertNotNil(enumerator, nil);
1038  seg = [enumerator nextObject];
1039  STAssertNotNil(seg, nil);
1040  STAssertFalse([seg isMatch], nil);
1041  STAssertEqualStrings([seg string], @"aaa", nil);
1042  seg = [enumerator nextObject];
1043  STAssertNil(seg, nil);
1044
1045  // test only match
1046  enumerator = [@"foobar" gtm_segmentEnumeratorForPattern:@"foo+ba+r"];
1047  STAssertNotNil(enumerator, nil);
1048  seg = [enumerator nextObject];
1049  STAssertNotNil(seg, nil);
1050  STAssertTrue([seg isMatch], nil);
1051  STAssertEqualStrings([seg string], @"foobar", nil);
1052  seg = [enumerator nextObject];
1053  STAssertNil(seg, nil);
1054
1055  // now test the saved sub segments
1056  enumerator =
1057    [@"foobarxxfoobaz" gtm_segmentEnumeratorForPattern:@"(foo)((bar)|(baz))"];
1058  STAssertNotNil(enumerator, nil);
1059  // "foobar"
1060  seg = [enumerator nextObject];
1061  STAssertNotNil(seg, nil);
1062  STAssertTrue([seg isMatch], nil);
1063  STAssertEqualStrings([seg string], @"foobar", nil);
1064  STAssertEqualStrings([seg subPatternString:0], @"foobar", nil);
1065  STAssertEqualStrings([seg subPatternString:1], @"foo", nil);
1066  STAssertEqualStrings([seg subPatternString:2], @"bar", nil);
1067  STAssertEqualStrings([seg subPatternString:3], @"bar", nil);
1068  STAssertNil([seg subPatternString:4], nil); // nothing matched "(baz)"
1069  STAssertNil([seg subPatternString:5], nil);
1070  // "xx"
1071  seg = [enumerator nextObject];
1072  STAssertNotNil(seg, nil);
1073  STAssertFalse([seg isMatch], nil);
1074  STAssertEqualStrings([seg string], @"xx", nil);
1075  STAssertEqualStrings([seg subPatternString:0], @"xx", nil);
1076  STAssertNil([seg subPatternString:1], nil);
1077  // "foobaz"
1078  seg = [enumerator nextObject];
1079  STAssertNotNil(seg, nil);
1080  STAssertTrue([seg isMatch], nil);
1081  STAssertEqualStrings([seg string], @"foobaz", nil);
1082  STAssertEqualStrings([seg subPatternString:0], @"foobaz", nil);
1083  STAssertEqualStrings([seg subPatternString:1], @"foo", nil);
1084  STAssertEqualStrings([seg subPatternString:2], @"baz", nil);
1085  STAssertNil([seg subPatternString:3], nil); // (nothing matched "(bar)"
1086  STAssertEqualStrings([seg subPatternString:4], @"baz", nil);
1087  STAssertNil([seg subPatternString:5], nil);
1088  // (end)
1089  seg = [enumerator nextObject];
1090  STAssertNil(seg, nil);
1091
1092  // test all objects
1093  enumerator = [@"afoobarbfooobaarfoobarzz" gtm_segmentEnumeratorForPattern:@"foo+ba+r"];
1094  STAssertNotNil(enumerator, nil);
1095  NSArray *allSegments = [enumerator allObjects];
1096  STAssertNotNil(allSegments, nil);
1097  STAssertEquals((NSUInteger)6, [allSegments count], nil);
1098}
1099
1100- (void)testMatchSegmentEnumeratorForPattern {
1101  NSEnumerator *enumerator =
1102    [@"afoobarbfooobaarfoobarzz" gtm_matchSegmentEnumeratorForPattern:@"foo+ba+r"];
1103  STAssertNotNil(enumerator, nil);
1104  // "a" - skipped
1105  // "foobar"
1106  GTMRegexStringSegment *seg = [enumerator nextObject];
1107  STAssertNotNil(seg, nil);
1108  STAssertTrue([seg isMatch], nil);
1109  STAssertEqualStrings([seg string], @"foobar", nil);
1110  // "b" - skipped
1111  // "fooobaar"
1112  seg = [enumerator nextObject];
1113  STAssertNotNil(seg, nil);
1114  STAssertTrue([seg isMatch], nil);
1115  STAssertEqualStrings([seg string], @"fooobaar", nil);
1116  // "foobar"
1117  seg = [enumerator nextObject];
1118  STAssertNotNil(seg, nil);
1119  STAssertTrue([seg isMatch], nil);
1120  STAssertEqualStrings([seg string], @"foobar", nil);
1121  // "zz" - skipped
1122  // (end)
1123  seg = [enumerator nextObject];
1124  STAssertNil(seg, nil);
1125
1126  // test no match
1127  enumerator = [@"aaa" gtm_matchSegmentEnumeratorForPattern:@"foo+ba+r"];
1128  STAssertNotNil(enumerator, nil);
1129  seg = [enumerator nextObject];
1130  STAssertNil(seg, nil);
1131
1132  // test only match
1133  enumerator = [@"foobar" gtm_matchSegmentEnumeratorForPattern:@"foo+ba+r"];
1134  STAssertNotNil(enumerator, nil);
1135  seg = [enumerator nextObject];
1136  STAssertNotNil(seg, nil);
1137  STAssertTrue([seg isMatch], nil);
1138  STAssertEqualStrings([seg string], @"foobar", nil);
1139  seg = [enumerator nextObject];
1140  STAssertNil(seg, nil);
1141
1142  // now test the saved sub segments
1143  enumerator =
1144    [@"foobarxxfoobaz" gtm_matchSegmentEnumeratorForPattern:@"(foo)((bar)|(baz))"];
1145  STAssertNotNil(enumerator, nil);
1146  // "foobar"
1147  seg = [enumerator nextObject];
1148  STAssertNotNil(seg, nil);
1149  STAssertTrue([seg isMatch], nil);
1150  STAssertEqualStrings([seg string], @"foobar", nil);
1151  STAssertEqualStrings([seg subPatternString:0], @"foobar", nil);
1152  STAssertEqualStrings([seg subPatternString:1], @"foo", nil);
1153  STAssertEqualStrings([seg subPatternString:2], @"bar", nil);
1154  STAssertEqualStrings([seg subPatternString:3], @"bar", nil);
1155  STAssertNil([seg subPatternString:4], nil); // nothing matched "(baz)"
1156  STAssertNil([seg subPatternString:5], nil);
1157  // "xx" - skipped
1158  // "foobaz"
1159  seg = [enumerator nextObject];
1160  STAssertNotNil(seg, nil);
1161  STAssertTrue([seg isMatch], nil);
1162  STAssertEqualStrings([seg string], @"foobaz", nil);
1163  STAssertEqualStrings([seg subPatternString:0], @"foobaz", nil);
1164  STAssertEqualStrings([seg subPatternString:1], @"foo", nil);
1165  STAssertEqualStrings([seg subPatternString:2], @"baz", nil);
1166  STAssertNil([seg subPatternString:3], nil); // (nothing matched "(bar)"
1167  STAssertEqualStrings([seg subPatternString:4], @"baz", nil);
1168  STAssertNil([seg subPatternString:5], nil);
1169  // (end)
1170  seg = [enumerator nextObject];
1171  STAssertNil(seg, nil);
1172
1173  // test all objects
1174  enumerator = [@"afoobarbfooobaarfoobarzz" gtm_matchSegmentEnumeratorForPattern:@"foo+ba+r"];
1175  STAssertNotNil(enumerator, nil);
1176  NSArray *allSegments = [enumerator allObjects];
1177  STAssertNotNil(allSegments, nil);
1178  STAssertEquals((NSUInteger)3, [allSegments count], nil);
1179}
1180
1181- (void)testAllSubstringsMatchedByPattern {
1182  NSArray *segments =
1183    [@"afoobarbfooobaarfoobarzz" gtm_allSubstringsMatchedByPattern:@"foo+ba+r"];
1184  STAssertNotNil(segments, nil);
1185  STAssertEquals((NSUInteger)3, [segments count], nil);
1186  STAssertEqualStrings([segments objectAtIndex:0], @"foobar", nil);
1187  STAssertEqualStrings([segments objectAtIndex:1], @"fooobaar", nil);
1188  STAssertEqualStrings([segments objectAtIndex:2], @"foobar", nil);
1189
1190  // test no match
1191  segments = [@"aaa" gtm_allSubstringsMatchedByPattern:@"foo+ba+r"];
1192  STAssertNotNil(segments, nil);
1193  STAssertEquals((NSUInteger)0, [segments count], nil);
1194
1195  // test only match
1196  segments = [@"foobar" gtm_allSubstringsMatchedByPattern:@"foo+ba+r"];
1197  STAssertNotNil(segments, nil);
1198  STAssertEquals((NSUInteger)1, [segments count], nil);
1199  STAssertEqualStrings([segments objectAtIndex:0], @"foobar", nil);
1200}
1201
1202- (void)testStringByReplacingMatchesOfPatternWithReplacement {
1203  // the basics
1204  STAssertEqualStrings(@"weeZbarZbydoo spamZfooZdoggies",
1205                       [@"weefoobydoo spambardoggies" gtm_stringByReplacingMatchesOfPattern:@"(foo)(.*)(bar)"
1206                                                                            withReplacement:@"Z\\3Z\\2Z\\1Z"],
1207                       nil);
1208  // nil/empty replacement
1209  STAssertEqualStrings(@"weedoggies",
1210                       [@"weefoobydoo spambardoggies" gtm_stringByReplacingMatchesOfPattern:@"(foo)(.*)(bar)"
1211                                                                            withReplacement:nil],
1212                       nil);
1213  STAssertEqualStrings(@"weedoggies",
1214                       [@"weefoobydoo spambardoggies" gtm_stringByReplacingMatchesOfPattern:@"(foo)(.*)(bar)"
1215                                                                            withReplacement:@""],
1216

Large files files are truncated, but you can click here to view the full file