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

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

TestParser.m: Make testing more than a noop (oops).

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