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 | NSString *ICCF_ErrString(OSStatus err, NSString *context) {
|
---|
15 | return [NSString stringWithFormat: @"%@ (%d)", context, (int)err];
|
---|
16 | }
|
---|
17 |
|
---|
18 | void ICCF_CheckRange(NSRange range) {}
|
---|
19 |
|
---|
20 | void ps(NSString *format, ...) {
|
---|
21 | va_list args;
|
---|
22 | va_start(args, format);
|
---|
23 | NSString *s = [[NSString alloc] initWithFormat: format arguments: args];
|
---|
24 | va_end(args);
|
---|
25 | printf("%s\n", [s cString]);
|
---|
26 | [s release];
|
---|
27 | }
|
---|
28 |
|
---|
29 | void pr(NSString *s, NSRange r) {
|
---|
30 | ps([[NSString alloc] initWithFormat: @"%@%@>%@<",
|
---|
31 | s, [@"" stringByPaddingToLength: r.location withString: @" " startingAtIndex: 0],
|
---|
32 | [@"" stringByPaddingToLength: r.length withString: @"-" startingAtIndex: 0]]);
|
---|
33 | }
|
---|
34 |
|
---|
35 | CFBundleRef ICCF_bundle = NULL;
|
---|
36 |
|
---|
37 | int main(int argc, char *argv[]) {
|
---|
38 | NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
---|
39 |
|
---|
40 | NSArray *a = [NSArray arrayWithContentsOfFile: @"urls.plist"];
|
---|
41 | NSCAssert(a != nil, @"Can't get array from urls.plist (wrong cwd? invalid format?)");
|
---|
42 |
|
---|
43 | ICCF_StartIC();
|
---|
44 |
|
---|
45 | unsigned pass = 0, fail = 0;
|
---|
46 |
|
---|
47 | NSEnumerator *e = [a objectEnumerator];
|
---|
48 | NSDictionary *d;
|
---|
49 | while ( (d = [e nextObject]) != nil) {
|
---|
50 | ps(@"");
|
---|
51 |
|
---|
52 | NSString *uri = [d objectForKey: @"uri"];
|
---|
53 | NSString *text = [d objectForKey: @"text"];
|
---|
54 | NSCAssert1(uri != nil, @"No URI in test case description %@", d);
|
---|
55 |
|
---|
56 | NSRange delimitedURIRange;
|
---|
57 | if (text != nil) {
|
---|
58 | NSArray *bits = [text componentsSeparatedByString: @"|"];
|
---|
59 | NSCAssert1([bits count] == 3, @"Sample text is not of the form 'foo|bar|baz': %@", text);
|
---|
60 | text = [bits componentsJoinedByString: @""];
|
---|
61 | delimitedURIRange.location = [(NSString *)[bits objectAtIndex: 0] length];
|
---|
62 | delimitedURIRange.length = [(NSString *)[bits objectAtIndex: 1] length];
|
---|
63 | } else {
|
---|
64 | text = uri;
|
---|
65 | delimitedURIRange.location = 0;
|
---|
66 | delimitedURIRange.length = [uri length];
|
---|
67 | }
|
---|
68 |
|
---|
69 | for (unsigned startOffset = 0 ; startOffset < delimitedURIRange.length ; ++startOffset) {
|
---|
70 | NSRange initialSelectionRange = {
|
---|
71 | delimitedURIRange.location + startOffset,
|
---|
72 | delimitedURIRange.length != 0
|
---|
73 | };
|
---|
74 |
|
---|
75 | // XXX figure XFAIL-type error checking
|
---|
76 |
|
---|
77 | NSRange enclosingRange;
|
---|
78 | NSString *parsedURI = nil;
|
---|
79 | NSException *parseException = nil;
|
---|
80 | @try {
|
---|
81 | enclosingRange = ICCF_URLEnclosingRange(text, delimitedURIRange);
|
---|
82 | parsedURI = [text substringWithRange: enclosingRange];
|
---|
83 | } @catch (NSException *e) {
|
---|
84 | parseException = e;
|
---|
85 | }
|
---|
86 |
|
---|
87 | if ([uri isEqualToString: parsedURI]) {
|
---|
88 | ++pass;
|
---|
89 | continue;
|
---|
90 | }
|
---|
91 |
|
---|
92 | ps(@"Text %@", text);
|
---|
93 | pr(@"Available ", delimitedURIRange);
|
---|
94 | pr(@"Starting ", initialSelectionRange);
|
---|
95 |
|
---|
96 | if (parseException != nil) {
|
---|
97 | ps(@"Exception %@", parseException);
|
---|
98 | } else {
|
---|
99 | pr(@"Parsed ", enclosingRange);
|
---|
100 | ps(@"Parsed %@", parsedURI);
|
---|
101 | ps(@"Desired %@", uri);
|
---|
102 | }
|
---|
103 | ++fail;
|
---|
104 | goto summary;
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | summary:
|
---|
109 | ps(@"PASS %d FAIL %d", pass, fail);
|
---|
110 |
|
---|
111 | ICCF_StopIC();
|
---|
112 |
|
---|
113 | [pool release];
|
---|
114 | }
|
---|