[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 |
|
---|
[375] | 18 | void ICCF_CheckRange(NSRange range) {}
|
---|
| 19 |
|
---|
[377] | 20 | static void ps(NSString *format, ...) {
|
---|
[322] | 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 |
|
---|
[377] | 29 | static void pr(NSString *s, NSRange r) {
|
---|
[322] | 30 | ps([[NSString alloc] initWithFormat: @"%@%@>%@<",
|
---|
| 31 | s, [@"" stringByPaddingToLength: r.location withString: @" " startingAtIndex: 0],
|
---|
| 32 | [@"" stringByPaddingToLength: r.length withString: @"-" startingAtIndex: 0]]);
|
---|
| 33 | }
|
---|
| 34 |
|
---|
[377] | 35 | static 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 |
|
---|
[322] | 67 | CFBundleRef ICCF_bundle = NULL;
|
---|
| 68 |
|
---|
| 69 | int 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 |
|
---|
[373] | 77 | unsigned pass = 0, fail = 0;
|
---|
| 78 |
|
---|
[322] | 79 | NSEnumerator *e = [a objectEnumerator];
|
---|
| 80 | NSDictionary *d;
|
---|
| 81 | while ( (d = [e nextObject]) != nil) {
|
---|
[373] | 82 | ps(@"");
|
---|
| 83 |
|
---|
[322] | 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];
|
---|
[377] | 99 | } // XXX add ^ ^ to indicate initialSelectionRange?
|
---|
[322] | 100 |
|
---|
[375] | 101 | for (unsigned startOffset = 0 ; startOffset < delimitedURIRange.length ; ++startOffset) {
|
---|
| 102 | NSRange initialSelectionRange = {
|
---|
| 103 | delimitedURIRange.location + startOffset,
|
---|
| 104 | delimitedURIRange.length != 0
|
---|
| 105 | };
|
---|
[373] | 106 |
|
---|
[377] | 107 | if (check_parse(uri, text, delimitedURIRange, initialSelectionRange)) {
|
---|
| 108 | pass++;
|
---|
[375] | 109 | } else {
|
---|
[377] | 110 | fail++;
|
---|
| 111 | goto summary;
|
---|
[375] | 112 | }
|
---|
[373] | 113 | }
|
---|
[322] | 114 | }
|
---|
| 115 |
|
---|
[375] | 116 | summary:
|
---|
| 117 | ps(@"PASS %d FAIL %d", pass, fail);
|
---|
| 118 |
|
---|
[322] | 119 | ICCF_StopIC();
|
---|
| 120 |
|
---|
| 121 | [pool release];
|
---|
| 122 | }
|
---|