root/open-source/SimpleRegexSupport/trunk/RegexTest.m

Revision 143, 2.5 kB (checked in by mhaecker, 3 months ago)

made the code tiger compatible (at least according to the docs) lets see how that works out

Line 
1 //
2 //  Created by Martin HÀcker on 09.10.08.
3 //  This Code is in the public domain.
4 //
5
6 #import "RegexTest.h"
7
8 /// For the regex syntax see <http://www.icu-project.org/userguide/regexp.html>
9 @interface NSString (SimpleRegexMatching)
10 - (BOOL) matchesRegex:(NSString *)aRegex;
11 - (BOOL) containsRegex:(NSString *)aRegex;
12 @end
13
14 @implementation NSString (SimpleRegexMatching)
15 - (BOOL) matchesRegex:(NSString *)aRegex;
16 {
17     id tigerFix = [NSString stringWithFormat:@"(?m)%@", aRegex];
18     id predicate = [NSPredicate predicateWithFormat:@"self matches %@", tigerFix]; //aRegex];
19     return [predicate evaluateWithObject:self];
20 }
21 - (BOOL) containsRegex:(NSString *)aRegex;
22 {
23     return [self matchesRegex:[NSString stringWithFormat:@".*%@.*", aRegex]];
24 }
25 @end
26
27 @implementation RegexTest
28
29 - (void) testSmoke
30 {
31     STAssertNotNil(@"", nil);
32 }
33
34 - (void) testHasMatchingMethod
35 {
36     STAssertTrue([@"" respondsToSelector:@selector(matchesRegex:)], nil);
37 }
38
39 - (void) testEmptyStringNeverMatches
40 {
41     STAssertFalse([@"" matchesRegex:nil], nil);
42     STAssertFalse([@"nil" matchesRegex:nil], nil);
43     STAssertFalse([@"(nil)" matchesRegex:nil], nil);
44
45     // TODO: Sure this is an edge case but still, I had different results when I ran this in non tiger compatibility mode. Find out why.
46     STAssertTrue([@"" matchesRegex:@""], nil);
47 }
48
49 - (void) testSimpleMatches
50 {
51     STAssertTrue([@"nil" matchesRegex:@"nil"], nil);
52     STAssertTrue([@" " matchesRegex:@" "], nil);
53 }
54
55 - (void) testDotStarMatchesEverything
56 {
57     STAssertTrue([@"" matchesRegex:@".*"], nil);
58     STAssertTrue([@"fnord" matchesRegex:@".*"], nil);
59     STAssertTrue([@"fnord\nfnord" matchesRegex:@".*"], nil);
60 }
61
62 - (void) testMoreComplexMatches
63 {
64     STAssertTrue([@"aaa" matchesRegex:@"a{3}"], nil);
65     STAssertFalse([@"aa" matchesRegex:@"a{3}"], nil);
66     STAssertFalse([@"aaaa" matchesRegex:@"a{3}"], nil);
67
68     STAssertTrue([@"argh fnord argh" matchesRegex:@".*\\bfnord\\b.*"], nil);
69     STAssertFalse([@"argh fnorda argh" matchesRegex:@".*\\bfnord\\b.*"], nil);
70     STAssertFalse([@"argh afnord argh" matchesRegex:@".*\\bfnord\\b.*"], nil);
71    
72     STAssertTrue([@"aaa" matchesRegex:@"\\w{3}"], nil);
73     STAssertFalse([@"aa" matchesRegex:@"\\w{3}"], nil);
74 }
75
76 - (void) testCanFindContainedRegexes
77 {
78     STAssertTrue([@"aaa" containsRegex:@"a"], nil);
79     STAssertTrue([@"aaa" containsRegex:@"aa"], nil);
80     STAssertTrue([@"aaa" containsRegex:@"aaa"], nil);
81     STAssertTrue([@"aaa" containsRegex:@"^a"], nil);
82     STAssertTrue([@"aaa" containsRegex:@"a$"], nil);
83     STAssertFalse([@"aaa" containsRegex:@"b"], nil);
84     STAssertFalse([@"aaa" containsRegex:@"aaaa"], nil);
85
86     STAssertTrue([@"aaab" containsRegex:@"^a"], nil);
87 }
88
89 @end
Note: See TracBrowser for help on using the browser.