source: trunk/ICeCoffEE/ICeCoffEE/TestParser.m @ 388

Last change on this file since 388 was 388, checked in by Nicholas Riley, 12 years ago

APEMain.m: Note missing Xcode 3 support. Add Terminal 2.0 support.

English.lproj/APE Manager plugin.nib:

English.lproj/APEInfo.rtfd:

ICeCoffEE.[hm]: Bring triggering window and app to front if error
dialog displayed in ICCF_HandleException. Remove initial word
selection. Launch preexisting selection if present. Remove 10.3
support. Update for new ICeCoffEETrigger API. Rename downEvent
parameter for consistency.

ICeCoffEE.xcodeproj: Added files.

ICeCoffEEParser.m: Handle multiline URLs.

ICeCoffEETTView.[hm]: Terminal 2.0 support (thank you for having a
usable NSTextInput implementation!)

ICeCoffEETTViewTrigger.[hm]: ICeCoffEETrigger implementation for
TTView. Can't set range as we do for NSTextView because you can't
have an arbitrary empty selection range in Terminal, so we pass it
directly to ICCF_LaunchURLFromTTView.

ICeCoffEETerminal.[hm]: Fix capitalization on ICeCoffEETermSubviewSuper.
Pass downEvent to ICCF_HandleException so it can bring the triggering
window to the front.

ICeCoffEETextViewTrigger.[hm]: Moved from ICeCoffEETrigger.

ICeCoffEETrigger.[hm]: Now an abstract superclass. Replace direct
access to ICCF_sharedTrigger with +cancel. Create 0-character range
(if click outside selectedRange) or save selectedRange. Move
debugging logs here from ICeCoffEE.m. Add description method.

ICeCoffEEWebKit.m: Pass downEvent to ICCF_HandleException so it can
bring the triggering window to the front.

Installer components/ui/ui.plist: Updated for 1.5d3.

TestParser?.m: Handle multiline URLs; newlines are printed as \ and
tabs as >. Implement ICCF_StringByRemovingCharactersInSet, which will
move elsewhere once Internet Config support is removed.

VERSION.xcconfig: Updated for 1.5d3.

urls.plist: Test for multiline URLs.

File size: 5.1 KB
Line 
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
14static 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
23static 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
29static 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
36NSString *ICCF_ErrString(OSStatus err, NSString *context) {   
37    return [NSString stringWithFormat: @"%@ (%d)", context, (int)err];
38}
39
40void 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
52static 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
69static 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
102CFBundleRef ICCF_bundle = NULL;
103
104int main(int argc, char *argv[]) {
105    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
106   
107    NSArray *a = [NSArray arrayWithContentsOfFile: @"urls.plist"];
108    NSCAssert(a != nil, @"Can't get array from urls.plist (wrong cwd? invalid format?)");
109   
110    ICCF_StartIC();
111   
112    unsigned pass = 0, fail = 0;
113   
114    NSEnumerator *e = [a objectEnumerator];
115    NSDictionary *d;
116    while ( (d = [e nextObject]) != nil) {
117        ps(@"");
118
119        NSString *uri = [d objectForKey: @"uri"];
120        NSString *text = [d objectForKey: @"text"];
121        NSCAssert1(uri != nil, @"No URI in test case description %@", d);
122       
123        NSRange delimitedURIRange;
124        if (text != nil) {
125            NSArray *bits = [text componentsSeparatedByString: @"|"];
126            NSCAssert1([bits count] == 3, @"Sample text is not of the form 'foo|bar|baz': %@", text);
127            text = [bits componentsJoinedByString: @""];
128            delimitedURIRange.location = [(NSString *)[bits objectAtIndex: 0] length];
129            delimitedURIRange.length = [(NSString *)[bits objectAtIndex: 1] length];
130        } else {
131            text = uri;
132            delimitedURIRange.location = 0;
133            delimitedURIRange.length = [uri length];
134        }
135       
136        NSArray *bits = [text componentsSeparatedByString: @"^"];
137        if ([bits count] == 3) {
138            NSString *joined = [bits componentsJoinedByString: @""];
139            NSRange initialSelectionRange = {
140                [(NSString *)[bits objectAtIndex: 0] length],
141                [(NSString *)[bits objectAtIndex: 1] length]
142            };
143            delimitedURIRange.length -= 2; // remove ^^
144            if (check_parse(uri == text ? joined : uri, joined,
145                            delimitedURIRange, initialSelectionRange)) {
146                pass++;
147            } else {
148                fail++;
149                goto summary;
150            }
151            if (uri == text)
152                continue;
153            text = joined;
154        }
155       
156        for (unsigned startOffset = 0 ; startOffset < delimitedURIRange.length ; ++startOffset) {
157            NSRange initialSelectionRange = {
158                delimitedURIRange.location + startOffset,
159                delimitedURIRange.length != 0
160            };
161           
162            if (check_parse(uri, text, delimitedURIRange, initialSelectionRange)) {
163                pass++;
164            } else {
165                fail++;
166                goto summary;
167            }
168        }
169    }
170   
171summary:
172    ps(@"PASS %d FAIL %d", pass, fail);
173   
174    ICCF_StopIC();
175
176    [pool release];
177}
Note: See TracBrowser for help on using the repository browser.