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