William Bowling is sharing code with you
Bitbucket is a code hosting site. Unlimited public and private repositories. Free for small teams.
Don't show this again1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | #import "TestRichTextCoercion.h"
#import <AIUtilities/AIDictionaryAdditions.h>
#import <AIUtilities/AIFileManagerAdditions.h>
#import <AIUtilities/AIRichTextCoercer.h>
#import <unistd.h>
#import <sysexits.h>
#import <sys/wait.h>
@implementation TestRichTextCoercion
- (NSDictionary *)dictionaryForScriptSuiteNamed:(NSString *)suiteName fromSdefFile:(NSString *)path
{
NSDictionary *scriptSuite = nil;
NSFileManager *mgr = [NSFileManager defaultManager];
#warning 64BIT: Check formatting arguments
NSString *scriptSuitesFolder = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"AdiumTest-%u-ScriptSuites", getpid()]];
NSLog(@"scriptSuitesFolder: %@", scriptSuitesFolder);
if([mgr createDirectoryAtPath:scriptSuitesFolder attributes:nil]) {
NSArray *args = [NSArray arrayWithObjects:
//scriptSuite format.
@"-f", @"s",
//Minimum system version: Tiger. (So we specify 10.3, because it does not recognize “10.4”.)
@"-V", @"10.3",
//Output to our temp folder.
@"-o", scriptSuitesFolder,
//Input from the sdef file.
path,
nil];
NSLog(@"launchedTaskWithLaunchPath:arguments: %@", args);
NSTask *sdp = [NSTask launchedTaskWithLaunchPath:@"/usr/bin/sdp"
arguments:args];
[sdp waitUntilExit];
STAssertEquals([sdp terminationStatus], 0, @"sdp didn't exited with status 0");
NSString *scriptSuitePath = [scriptSuitesFolder stringByAppendingPathComponent:[suiteName stringByAppendingPathExtension:@"scriptSuite"]];
scriptSuite = [NSDictionary dictionaryWithContentsOfFile:scriptSuitePath];
STAssertNotNil(scriptSuite, @"No script suite named %@ in sdef file %@", suiteName, path);
NSLog(@"deleting files in directory: %@", scriptSuitesFolder);
[mgr removeFilesInDirectory:scriptSuitesFolder withPrefix:nil movingToTrash:NO];
NSLog(@"deleted files in directory: %@", scriptSuitesFolder);
}
return scriptSuite;
}
- (NSDictionary *)dictionaryForScriptTerminologyNamed:(NSString *)suiteName fromSdefFile:(NSString *)path
{
NSDictionary *scriptTerminology = nil;
NSFileManager *mgr = [NSFileManager defaultManager];
#warning 64BIT: Check formatting arguments
NSString *scriptTerminologiesFolder = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"AdiumTest-%u-ScriptTerminologies", getpid()]];
NSLog(@"scriptTerminologiesFolder: %@", scriptTerminologiesFolder);
if([mgr createDirectoryAtPath:scriptTerminologiesFolder attributes:nil]) {
NSArray *args = [NSArray arrayWithObjects:
//scriptTerminology format.
@"-f", @"t",
//Minimum system version: Tiger. (So we specify 10.3, because it does not recognize “10.4”.)
@"-V", @"10.3",
//Output to our temp folder.
@"-o", scriptTerminologiesFolder,
//Input from the sdef file.
path,
nil];
NSLog(@"launchedTaskWithLaunchPath:arguments: %@", args);
NSTask *sdp = [NSTask launchedTaskWithLaunchPath:@"/usr/bin/sdp"
arguments:args];
[sdp waitUntilExit];
STAssertEquals([sdp terminationStatus], 0, @"sdp didn't exited with status 0");
NSString *scriptTerminologyPath = [scriptTerminologiesFolder stringByAppendingPathComponent:[suiteName stringByAppendingPathExtension:@"scriptTerminology"]];
scriptTerminology = [NSDictionary dictionaryWithContentsOfFile:scriptTerminologyPath];
STAssertNotNil(scriptTerminology, @"No script suite named %@ in sdef file %@", suiteName, path);
NSLog(@"deleting files in directory: %@", scriptTerminologiesFolder);
[mgr removeFilesInDirectory:scriptTerminologiesFolder withPrefix:nil movingToTrash:NO];
NSLog(@"deleted files in directory: %@", scriptTerminologiesFolder);
}
return scriptTerminology;
}
- (NSDictionary *)dictionaryByMergingSuiteDictionary:(NSDictionary *)scriptSuite withTerminologyDictionary:(NSDictionary *)scriptTerminology
{
NSMutableDictionary *merged = [[scriptSuite mutableCopy] autorelease];
NSMutableDictionary *classes = [[[scriptSuite objectForKey:@"Classes"] mutableCopy] autorelease];
NSDictionary *terminologyClasses = [scriptTerminology objectForKey:@"Classes"];
NSLog(@"Before add: %@", classes);
NSEnumerator *keyEnum = [classes keyEnumerator];
NSString *className;
while ((className = [keyEnum nextObject])) {
NSMutableDictionary *classDict = [[[classes objectForKey:className] mutableCopy] autorelease];
[classDict translate:nil
add:[terminologyClasses objectForKey:className]
remove:nil];
[classes setObject:classDict forKey:className];
}
NSLog(@" After add: %@", classes);
[merged setObject:classes
forKey:@"Classes"];
NSLog(@"Suite %C Terminology: %@", 0x222A, merged);
return merged;
}
- (void)setUp
{
//Load Adium.sdef in order to have the Text suite.
NSBundle *testBundle = [NSBundle bundleForClass:[self class]];
NSDictionary *scriptSuite = [self dictionaryForScriptSuiteNamed:@"NSTextSuite" fromSdefFile:[testBundle pathForResource:@"Adium" ofType:@"sdef"]];
NSDictionary *scriptTerminology = [self dictionaryForScriptTerminologyNamed:@"NSTextSuite" fromSdefFile:[testBundle pathForResource:@"Adium" ofType:@"sdef"]];
NSDictionary *scriptSuiteAndTerminology = [self dictionaryByMergingSuiteDictionary:scriptSuite withTerminologyDictionary:scriptTerminology];
// NSLog(@"scriptSuite: %@", scriptSuite);
[[NSScriptSuiteRegistry sharedScriptSuiteRegistry] loadSuiteWithDictionary:scriptSuiteAndTerminology fromBundle:testBundle];
[AIRichTextCoercer enableRichTextCoercion];
}
- (id)giveMeA:(Class)class ofThisString:(NSString *)str
{
id result = nil;
if([class isSubclassOfClass:[NSString class]]) {
result = [class stringWithString:str];
} else if([class isSubclassOfClass:[NSAttributedString class]]) {
NSDictionary *attrs = [NSDictionary dictionaryWithObject:[NSFont fontWithName:@"Helvetica" size:12.0f] forKey:NSFontAttributeName];
result = [[class alloc] initWithString:str attributes:attrs];
}
return result;
}
#pragma mark Test case methods
/*
- (void)testAttributedStringToPlainText {
NSString *str = @"Too close for missiles; I'm switching to ducks!";
NSAttributedString *input = [self giveMeA:[NSAttributedString class] ofThisString:str];
NSString *output = [[NSScriptCoercionHandler sharedCoercionHandler] coerceValue:input toClass:[NSString class]];
STAssertNotNil(output, @"Coercion returned nil");
STAssertTrue([output isKindOfClass:[NSString class]], @"Coercion to NSString must result in an NSString; instead, it resulted in an %@", [output class]);
STAssertEqualObjects(output, input, @"Coercion must not change the object's value");
}
- (void)testMutableAttributedStringToPlainText {
NSString *str = @"The quack is strong with this one.";
NSMutableAttributedString *input = [self giveMeA:[NSMutableAttributedString class] ofThisString:str];
NSString *output = [[NSScriptCoercionHandler sharedCoercionHandler] coerceValue:input toClass:[NSString class]];
STAssertNotNil(output, @"Coercion returned nil");
STAssertTrue([output isKindOfClass:[NSString class]], @"Coercion to NSString must result in an NSString; instead, it resulted in an %@", [output class]);
STAssertEqualObjects(output, input, @"Coercion must not change the object's value");
}
*/
- (void)testTextStorageToPlainText {
NSString *str = @"David's gerbil";
NSTextStorage *input = [self giveMeA:[NSTextStorage class] ofThisString:str];
NSString *output = [[NSScriptCoercionHandler sharedCoercionHandler] coerceValue:input toClass:[NSString class]];
STAssertNotNil(output, @"Coercion returned nil");
STAssertTrue([output isKindOfClass:[NSString class]], @"Coercion to NSString must result in an NSString; instead, it resulted in an %@", [output class]);
STAssertEqualObjects(output, [input string], @"Coercion must not change the object's value");
}
#pragma mark -
/*- (void)testPlainTextToAttributedString {
NSString *str = @"Now with a kitchen sink plugin.";
Class destClass = [NSAttributedString class];
NSString *input = [self giveMeA:[NSString class] ofThisString:str];
NSAttributedString *output = [[NSScriptCoercionHandler sharedCoercionHandler] coerceValue:input toClass:destClass];
STAssertNotNil(output, @"Coercion returned nil");
STAssertTrue([output isKindOfClass:destClass], @"Coercion to %@ must result in an %@; instead, it resulted in an %@", destClass, destClass, [output class]);
STAssertEqualObjects(output, input, @"Coercion must not change the object's value");
}
- (void)testPlainTextToMutableAttributedString {
NSString *str = @"More fun than a bag of chips.";
Class destClass = [NSMutableAttributedString class];
NSString *input = [self giveMeA:[NSString class] ofThisString:str];
NSMutableAttributedString *output = [[NSScriptCoercionHandler sharedCoercionHandler] coerceValue:input toClass:destClass];
STAssertNotNil(output, @"Coercion returned nil");
STAssertTrue([output isKindOfClass:destClass], @"Coercion to %@ must result in an %@; instead, it resulted in an %@", destClass, destClass, [output class]);
STAssertEqualObjects(output, input, @"Coercion must not change the object's value");
}
*/
- (void)testPlainTextToTextStorage {
NSString *str = @"Now with 30% more Zing!";
Class destClass = [NSTextStorage class];
NSString *input = [self giveMeA:[NSString class] ofThisString:str];
NSTextStorage *output = [[NSScriptCoercionHandler sharedCoercionHandler] coerceValue:input toClass:destClass];
STAssertNotNil(output, @"Coercion returned nil");
STAssertTrue([output isKindOfClass:destClass], @"Coercion to %@ must result in an %@; instead, it resulted in an %@", destClass, destClass, [output class]);
STAssertEqualObjects([output string], input, @"Coercion must not change the object's value");
}
#pragma mark -
//Coerce the string, then mutate the original.
/*- (void)testMutableAttributedStringToPlainTextWithMutations {
NSString *str = @"One time it got me a cookie...";
NSMutableAttributedString *input = [self giveMeA:[NSMutableAttributedString class] ofThisString:str];
NSString *output = [[NSScriptCoercionHandler sharedCoercionHandler] coerceValue:input toClass:[NSString class]];
//Mutate the original.
NSDictionary *attrs = [NSDictionary dictionaryWithObject:[NSFont fontWithName:@"Courier" size:14.0f] forKey:NSFontAttributeName];
NSAttributedString *replacement = [[[NSAttributedString alloc] initWithString:@" (one of the slogans from the old Adium X site)" attributes:attrs] autorelease];
[input replaceCharactersInRange:(NSRange){ [input length], 0U }
withAttributedString:replacement];
STAssertNotNil(output, @"Coercion returned nil");
STAssertTrue([output isKindOfClass:[NSString class]], @"Coercion to NSString must result in an NSString; instead, it resulted in an %@", [output class]);
STAssertEqualObjects(output, str, @"Coercion must not change the object's value");
}
*/
- (void)testTextStorageToPlainTextWithMutations {
NSString *str = @"The only IM client with duck 'n' cover.";
NSTextStorage *input = [self giveMeA:[NSTextStorage class] ofThisString:str];
NSString *output = [[NSScriptCoercionHandler sharedCoercionHandler] coerceValue:input toClass:[NSString class]];
//Mutate the original.
NSDictionary *attrs = [NSDictionary dictionaryWithObject:[NSFont fontWithName:@"Courier" size:14.0f] forKey:NSFontAttributeName];
NSAttributedString *replacement = [[[NSAttributedString alloc] initWithString:@" (one of the slogans from the old Adium X site)" attributes:attrs] autorelease];
[input replaceCharactersInRange:(NSRange){ [input length], 0U }
withAttributedString:replacement];
STAssertNotNil(output, @"Coercion returned nil");
STAssertTrue([output isKindOfClass:[NSString class]], @"Coercion to NSString must result in an NSString; instead, it resulted in an %@", [output class]);
STAssertEqualObjects(output, str, @"Coercion must not change the object's value");
}
#pragma mark -
//Run the AppleScript “x as y”, where x is an AS object and y is an AS class.
- (void)testRichTextToPlainTextInAppleScript {
NSString *source =
@"set x to (\"So you don't have to IM like it's 1999.\" as rich text)\n"
@"x as text"
;
#warning Implement me
}
- (void)testPlainTextToRichTextInAppleScript {
NSString *source =
@"\"Ducks eat for free at Subway!\" as rich text"
;
NSDictionary *errorInfo = nil;
[[[[NSAppleScript alloc] initWithSource:source] autorelease] executeAndReturnError:&errorInfo];
STAssertNil(errorInfo, @"AppleScript returned an error: %@", errorInfo);
}
@end
|