1 | //
|
---|
2 | // TestParser.m
|
---|
3 | // ICeCoffEE
|
---|
4 | //
|
---|
5 | // Created by Nicholas Riley on 6/22/07.
|
---|
6 | // Copyright 2007 Nicholas Riley. All rights reserved.
|
---|
7 | //
|
---|
8 |
|
---|
9 | #import <Foundation/Foundation.h>
|
---|
10 | #import "ICeCoffEEParser.h"
|
---|
11 |
|
---|
12 | #include <stdarg.h>
|
---|
13 |
|
---|
14 | static void ps(NSString *format, ...) {
|
---|
15 | va_list args;
|
---|
16 | va_start(args, format);
|
---|
17 | NSString *s = [[NSString alloc] initWithFormat: format arguments: args];
|
---|
18 | va_end(args);
|
---|
19 | printf("%s\n", [s cString]);
|
---|
20 | [s release];
|
---|
21 | }
|
---|
22 |
|
---|
23 | static void pr(NSString *s, NSRange r) {
|
---|
24 | ps([[NSString alloc] initWithFormat: @"%@%@>%@<",
|
---|
25 | s, [@"" stringByPaddingToLength: r.location withString: @" " startingAtIndex: 0],
|
---|
26 | [@"" stringByPaddingToLength: r.length withString: @"-" startingAtIndex: 0]]);
|
---|
27 | }
|
---|
28 |
|
---|
29 | static NSString *tc(NSString *s) {
|
---|
30 | NSMutableString *rs = [s mutableCopy];
|
---|
31 | [rs replaceOccurrencesOfString: @"\n" withString: @"\\" options: 0 range: NSMakeRange(0, [s length])];
|
---|
32 | [rs replaceOccurrencesOfString: @"\t" withString: @">" options: 0 range: NSMakeRange(0, [s length])];
|
---|
33 | return [rs autorelease];
|
---|
34 | }
|
---|
35 |
|
---|
36 | NSString *ICCF_ErrString(OSStatus err, NSString *context) {
|
---|
37 | return [NSString stringWithFormat: @"%@ (%d)", context, (int)err];
|
---|
38 | }
|
---|
39 |
|
---|
40 | void ICCF_CheckRange(NSRange range) {
|
---|
41 | if (range.length > 0)
|
---|
42 | return;
|
---|
43 |
|
---|
44 | pr(@"Invalid ", range);
|
---|
45 | @throw([NSException exceptionWithName: NSRangeException
|
---|
46 | reason: @"No URL is selected"
|
---|
47 | userInfo: nil]);
|
---|
48 | }
|
---|
49 |
|
---|
50 |
|
---|
51 | // XXX move elsewhere
|
---|
52 | static NSString *ICCF_StringByRemovingCharactersInSet(NSString *s, NSCharacterSet *set) {
|
---|
53 | NSRange range = [s rangeOfCharacterFromSet: set options: 0 range: NSMakeRange(0, [s length])];
|
---|
54 | if (range.location == NSNotFound)
|
---|
55 | return s;
|
---|
56 |
|
---|
57 | NSMutableString *ms = [s mutableCopy];
|
---|
58 | do {
|
---|
59 | [ms deleteCharactersInRange: range];
|
---|
60 | range.length = [ms length] - range.location;
|
---|
61 | range = [ms rangeOfCharacterFromSet: set options: 0 range: range];
|
---|
62 | } while (range.location != NSNotFound);
|
---|
63 |
|
---|
64 | s = [[ms copy] autorelease];
|
---|
65 | [ms release];
|
---|
66 | return s;
|
---|
67 | }
|
---|
68 |
|
---|
69 | static BOOL check_parse(NSString *uri, NSString *text,
|
---|
70 | NSRange delimitedURIRange, NSRange initialSelectionRange) {
|
---|
71 | // XXX figure XFAIL-type error checking
|
---|
72 |
|
---|
73 | NSRange enclosingRange;
|
---|
74 | NSString *parsedURI = nil;
|
---|
75 | NSException *parseException = nil;
|
---|
76 | @try {
|
---|
77 | enclosingRange = ICCF_URLEnclosingRange(text, initialSelectionRange);
|
---|
78 | // XXX move trimming elsewhere
|
---|
79 | parsedURI = ICCF_StringByRemovingCharactersInSet([text substringWithRange: enclosingRange], [NSCharacterSet whitespaceAndNewlineCharacterSet]);
|
---|
80 | } @catch (NSException *e) {
|
---|
81 | parseException = e;
|
---|
82 | }
|
---|
83 |
|
---|
84 | if ([uri isEqualToString: parsedURI]) {
|
---|
85 | return YES;
|
---|
86 | }
|
---|
87 |
|
---|
88 | ps(@"Text %@", tc(text));
|
---|
89 | pr(@"Available ", delimitedURIRange);
|
---|
90 | pr(@"Starting ", initialSelectionRange);
|
---|
91 |
|
---|
92 | if (parseException != nil) {
|
---|
93 | ps(@"Exception %@", parseException);
|
---|
94 | } else {
|
---|
95 | pr(@"Parsed ", enclosingRange);
|
---|
96 | ps(@"Parsed %@", tc(parsedURI));
|
---|
97 | ps(@"Desired %@", tc(uri));
|
---|
98 | }
|
---|
99 | return NO;
|
---|
100 | }
|
---|
101 |
|
---|
102 | CFBundleRef ICCF_bundle = NULL;
|
---|
103 |
|
---|
104 | // from http://jens.ayton.se/code/files/JANSLogHack.m
|
---|
105 | extern void _NSSetLogCStringFunction(void (*)(const char *string, unsigned length, BOOL withSyslogBanner));
|
---|
106 |
|
---|
107 | static void PrintNSLogMessage(const char *string, unsigned length, BOOL withSyslogBanner) {
|
---|
108 | puts(string);
|
---|
109 | }
|
---|
110 |
|
---|
111 | int main(int argc, char *argv[]) {
|
---|
112 | NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
---|
113 |
|
---|
114 | _NSSetLogCStringFunction(PrintNSLogMessage);
|
---|
115 |
|
---|
116 | NSArray *a = [NSArray arrayWithContentsOfFile: @"urls.plist"];
|
---|
117 | NSCAssert(a != nil, @"Can't get array from urls.plist (wrong cwd? invalid format?)");
|
---|
118 |
|
---|
119 | ICCF_StartIC();
|
---|
120 |
|
---|
121 | unsigned pass = 0, fail = 0;
|
---|
122 |
|
---|
123 | NSEnumerator *e = [a objectEnumerator];
|
---|
124 | NSDictionary *d;
|
---|
125 | while ( (d = [e nextObject]) != nil) {
|
---|
126 | ps(@"");
|
---|
127 |
|
---|
128 | NSString *uri = [d objectForKey: @"uri"];
|
---|
129 | NSString *text = [d objectForKey: @"text"];
|
---|
130 | NSCAssert1(uri != nil, @"No URI in test case description %@", d);
|
---|
131 |
|
---|
132 | NSRange delimitedURIRange;
|
---|
133 | if (text != nil) {
|
---|
134 | NSArray *bits = [text componentsSeparatedByString: @"|"];
|
---|
135 | NSCAssert1([bits count] == 3, @"Sample text is not of the form 'foo|bar|baz': %@", text);
|
---|
136 | text = [bits componentsJoinedByString: @""];
|
---|
137 | delimitedURIRange.location = [(NSString *)[bits objectAtIndex: 0] length];
|
---|
138 | delimitedURIRange.length = [(NSString *)[bits objectAtIndex: 1] length];
|
---|
139 | } else {
|
---|
140 | text = uri;
|
---|
141 | delimitedURIRange.location = 0;
|
---|
142 | delimitedURIRange.length = [uri length];
|
---|
143 | }
|
---|
144 |
|
---|
145 | NSArray *bits = [text componentsSeparatedByString: @"^"];
|
---|
146 | if ([bits count] == 3) {
|
---|
147 | NSString *joined = [bits componentsJoinedByString: @""];
|
---|
148 | NSRange initialSelectionRange = {
|
---|
149 | [(NSString *)[bits objectAtIndex: 0] length],
|
---|
150 | [(NSString *)[bits objectAtIndex: 1] length]
|
---|
151 | };
|
---|
152 | delimitedURIRange.length -= 2; // remove ^^
|
---|
153 | if (check_parse(uri == text ? joined : uri, joined,
|
---|
154 | delimitedURIRange, initialSelectionRange)) {
|
---|
155 | pass++;
|
---|
156 | } else {
|
---|
157 | fail++;
|
---|
158 | goto summary;
|
---|
159 | }
|
---|
160 | if (uri == text)
|
---|
161 | continue;
|
---|
162 | text = joined;
|
---|
163 | }
|
---|
164 |
|
---|
165 | for (unsigned startOffset = 0 ; startOffset < delimitedURIRange.length ; ++startOffset) {
|
---|
166 | NSRange initialSelectionRange = {
|
---|
167 | delimitedURIRange.location + startOffset,
|
---|
168 | delimitedURIRange.length != 0
|
---|
169 | };
|
---|
170 |
|
---|
171 | if (check_parse(uri, text, delimitedURIRange, initialSelectionRange)) {
|
---|
172 | pass++;
|
---|
173 | } else {
|
---|
174 | fail++;
|
---|
175 | goto summary;
|
---|
176 | }
|
---|
177 | }
|
---|
178 | }
|
---|
179 |
|
---|
180 | summary:
|
---|
181 | ps(@"PASS %d FAIL %d", pass, fail);
|
---|
182 |
|
---|
183 | ICCF_StopIC();
|
---|
184 |
|
---|
185 | [pool release];
|
---|
186 | }
|
---|