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

Last change on this file since 386 was 382, checked in by Nicholas Riley, 16 years ago

TestParser.m: Don't permit empty strings to pass through; they break
ICCF_ParseURL. Print invalid range in ICCF_CheckRange. Support
delimiters in 'uri' or 'text' for initialSelectionRange; can be used
in addition to | |, which as before tries every single-character
starting range.

urls.plist: A few more tests, for IPv6 address recognition, delimited
expansion and multiple ending delimiters.

File size: 4.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
29NSString *ICCF_ErrString(OSStatus err, NSString *context) {
30 return [NSString stringWithFormat: @"%@ (%d)", context, (int)err];
31}
32
33void ICCF_CheckRange(NSRange range) {
34 if (range.length > 0)
35 return;
36
37 pr(@"Invalid ", range);
38 @throw([NSException exceptionWithName: NSRangeException
39 reason: @"No URL is selected"
40 userInfo: nil]);
41}
42
43
44static BOOL check_parse(NSString *uri, NSString *text,
45 NSRange delimitedURIRange, NSRange initialSelectionRange) {
46 // XXX figure XFAIL-type error checking
47
48 NSRange enclosingRange;
49 NSString *parsedURI = nil;
50 NSException *parseException = nil;
51 @try {
52 enclosingRange = ICCF_URLEnclosingRange(text, initialSelectionRange);
53 parsedURI = [text substringWithRange: enclosingRange];
54 } @catch (NSException *e) {
55 parseException = e;
56 }
57
58 if ([uri isEqualToString: parsedURI]) {
59 return YES;
60 }
61
62 ps(@"Text %@", text);
63 pr(@"Available ", delimitedURIRange);
64 pr(@"Starting ", initialSelectionRange);
65
66 if (parseException != nil) {
67 ps(@"Exception %@", parseException);
68 } else {
69 pr(@"Parsed ", enclosingRange);
70 ps(@"Parsed %@", parsedURI);
71 ps(@"Desired %@", uri);
72 }
73 return NO;
74}
75
76CFBundleRef ICCF_bundle = NULL;
77
78int main(int argc, char *argv[]) {
79 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
80
81 NSArray *a = [NSArray arrayWithContentsOfFile: @"urls.plist"];
82 NSCAssert(a != nil, @"Can't get array from urls.plist (wrong cwd? invalid format?)");
83
84 ICCF_StartIC();
85
86 unsigned pass = 0, fail = 0;
87
88 NSEnumerator *e = [a objectEnumerator];
89 NSDictionary *d;
90 while ( (d = [e nextObject]) != nil) {
91 ps(@"");
92
93 NSString *uri = [d objectForKey: @"uri"];
94 NSString *text = [d objectForKey: @"text"];
95 NSCAssert1(uri != nil, @"No URI in test case description %@", d);
96
97 NSRange delimitedURIRange;
98 if (text != nil) {
99 NSArray *bits = [text componentsSeparatedByString: @"|"];
100 NSCAssert1([bits count] == 3, @"Sample text is not of the form 'foo|bar|baz': %@", text);
101 text = [bits componentsJoinedByString: @""];
102 delimitedURIRange.location = [(NSString *)[bits objectAtIndex: 0] length];
103 delimitedURIRange.length = [(NSString *)[bits objectAtIndex: 1] length];
104 } else {
105 text = uri;
106 delimitedURIRange.location = 0;
107 delimitedURIRange.length = [uri length];
108 }
109
110 NSArray *bits = [text componentsSeparatedByString: @"^"];
111 if ([bits count] == 3) {
112 NSString *joined = [bits componentsJoinedByString: @""];
113 NSRange initialSelectionRange = {
114 [(NSString *)[bits objectAtIndex: 0] length],
115 [(NSString *)[bits objectAtIndex: 1] length]
116 };
117 delimitedURIRange.length -= 2; // remove ^^
118 if (check_parse(uri == text ? joined : uri, joined,
119 delimitedURIRange, initialSelectionRange)) {
120 pass++;
121 } else {
122 fail++;
123 goto summary;
124 }
125 if (uri == text)
126 continue;
127 text = joined;
128 }
129
130 for (unsigned startOffset = 0 ; startOffset < delimitedURIRange.length ; ++startOffset) {
131 NSRange initialSelectionRange = {
132 delimitedURIRange.location + startOffset,
133 delimitedURIRange.length != 0
134 };
135
136 if (check_parse(uri, text, delimitedURIRange, initialSelectionRange)) {
137 pass++;
138 } else {
139 fail++;
140 goto summary;
141 }
142 }
143 }
144
145summary:
146 ps(@"PASS %d FAIL %d", pass, fail);
147
148 ICCF_StopIC();
149
150 [pool release];
151}
Note: See TracBrowser for help on using the repository browser.