[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 |
|
---|
[377] | 14 | static void ps(NSString *format, ...) {
|
---|
[322] | 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 |
|
---|
[377] | 23 | static void pr(NSString *s, NSRange r) {
|
---|
[322] | 24 | ps([[NSString alloc] initWithFormat: @"%@%@>%@<",
|
---|
| 25 | s, [@"" stringByPaddingToLength: r.location withString: @" " startingAtIndex: 0],
|
---|
| 26 | [@"" stringByPaddingToLength: r.length withString: @"-" startingAtIndex: 0]]);
|
---|
| 27 | }
|
---|
| 28 |
|
---|
[382] | 29 | NSString *ICCF_ErrString(OSStatus err, NSString *context) {
|
---|
| 30 | return [NSString stringWithFormat: @"%@ (%d)", context, (int)err];
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | void 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 |
|
---|
[377] | 44 | static 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 |
|
---|
[322] | 76 | CFBundleRef ICCF_bundle = NULL;
|
---|
| 77 |
|
---|
| 78 | int 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 |
|
---|
[373] | 86 | unsigned pass = 0, fail = 0;
|
---|
| 87 |
|
---|
[322] | 88 | NSEnumerator *e = [a objectEnumerator];
|
---|
| 89 | NSDictionary *d;
|
---|
| 90 | while ( (d = [e nextObject]) != nil) {
|
---|
[373] | 91 | ps(@"");
|
---|
| 92 |
|
---|
[322] | 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];
|
---|
[382] | 108 | }
|
---|
[322] | 109 |
|
---|
[382] | 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 |
|
---|
[375] | 130 | for (unsigned startOffset = 0 ; startOffset < delimitedURIRange.length ; ++startOffset) {
|
---|
| 131 | NSRange initialSelectionRange = {
|
---|
| 132 | delimitedURIRange.location + startOffset,
|
---|
| 133 | delimitedURIRange.length != 0
|
---|
| 134 | };
|
---|
[373] | 135 |
|
---|
[377] | 136 | if (check_parse(uri, text, delimitedURIRange, initialSelectionRange)) {
|
---|
| 137 | pass++;
|
---|
[375] | 138 | } else {
|
---|
[377] | 139 | fail++;
|
---|
| 140 | goto summary;
|
---|
[375] | 141 | }
|
---|
[373] | 142 | }
|
---|
[322] | 143 | }
|
---|
| 144 |
|
---|
[375] | 145 | summary:
|
---|
| 146 | ps(@"PASS %d FAIL %d", pass, fail);
|
---|
| 147 |
|
---|
[322] | 148 | ICCF_StopIC();
|
---|
| 149 |
|
---|
| 150 | [pool release];
|
---|
| 151 | }
|
---|