UnitTests/TestRichTextCoercion.m
changeset 5949 334dc418b409
parent 5512 2a23d9019455
equal deleted inserted replaced
5893:1e1382dbd844 5949:334dc418b409
    42 			nil];
    42 			nil];
    43 		NSLog(@"launchedTaskWithLaunchPath:arguments: %@", args);
    43 		NSLog(@"launchedTaskWithLaunchPath:arguments: %@", args);
    44 		NSTask *sdp = [NSTask launchedTaskWithLaunchPath:@"/usr/bin/sdp"
    44 		NSTask *sdp = [NSTask launchedTaskWithLaunchPath:@"/usr/bin/sdp"
    45 											   arguments:args];
    45 											   arguments:args];
    46 		[sdp waitUntilExit];
    46 		[sdp waitUntilExit];
    47 		STAssertEquals([sdp terminationStatus], 0, @"sdp didn't exited with status 0");
    47 		XCTAssertEqual([sdp terminationStatus], 0, @"sdp didn't exited with status 0");
    48 
    48 
    49 		NSString *scriptSuitePath = [scriptSuitesFolder stringByAppendingPathComponent:[suiteName stringByAppendingPathExtension:@"scriptSuite"]];
    49 		NSString *scriptSuitePath = [scriptSuitesFolder stringByAppendingPathComponent:[suiteName stringByAppendingPathExtension:@"scriptSuite"]];
    50 		scriptSuite = [NSDictionary dictionaryWithContentsOfFile:scriptSuitePath];
    50 		scriptSuite = [NSDictionary dictionaryWithContentsOfFile:scriptSuitePath];
    51 		STAssertNotNil(scriptSuite, @"No script suite named %@ in sdef file %@", suiteName, path);
    51 		XCTAssertNotNil(scriptSuite, @"No script suite named %@ in sdef file %@", suiteName, path);
    52 
    52 
    53 		NSLog(@"deleting files in directory: %@", scriptSuitesFolder);
    53 		NSLog(@"deleting files in directory: %@", scriptSuitesFolder);
    54 		[mgr removeFilesInDirectory:scriptSuitesFolder withPrefix:nil movingToTrash:NO];
    54 		[mgr removeFilesInDirectory:scriptSuitesFolder withPrefix:nil movingToTrash:NO];
    55 		NSLog(@"deleted files in directory: %@", scriptSuitesFolder);
    55 		NSLog(@"deleted files in directory: %@", scriptSuitesFolder);
    56 	}
    56 	}
    78 			nil];
    78 			nil];
    79 		NSLog(@"launchedTaskWithLaunchPath:arguments: %@", args);
    79 		NSLog(@"launchedTaskWithLaunchPath:arguments: %@", args);
    80 		NSTask *sdp = [NSTask launchedTaskWithLaunchPath:@"/usr/bin/sdp"
    80 		NSTask *sdp = [NSTask launchedTaskWithLaunchPath:@"/usr/bin/sdp"
    81 											   arguments:args];
    81 											   arguments:args];
    82 		[sdp waitUntilExit];
    82 		[sdp waitUntilExit];
    83 		STAssertEquals([sdp terminationStatus], 0, @"sdp didn't exited with status 0");
    83 		XCTAssertEqual([sdp terminationStatus], 0, @"sdp didn't exited with status 0");
    84 		
    84 		
    85 		NSString *scriptTerminologyPath = [scriptTerminologiesFolder stringByAppendingPathComponent:[suiteName stringByAppendingPathExtension:@"scriptTerminology"]];
    85 		NSString *scriptTerminologyPath = [scriptTerminologiesFolder stringByAppendingPathComponent:[suiteName stringByAppendingPathExtension:@"scriptTerminology"]];
    86 		scriptTerminology = [NSDictionary dictionaryWithContentsOfFile:scriptTerminologyPath];
    86 		scriptTerminology = [NSDictionary dictionaryWithContentsOfFile:scriptTerminologyPath];
    87 		STAssertNotNil(scriptTerminology, @"No script suite named %@ in sdef file %@", suiteName, path);
    87 		XCTAssertNotNil(scriptTerminology, @"No script suite named %@ in sdef file %@", suiteName, path);
    88 		
    88 		
    89 		NSLog(@"deleting files in directory: %@", scriptTerminologiesFolder);
    89 		NSLog(@"deleting files in directory: %@", scriptTerminologiesFolder);
    90 		[mgr removeFilesInDirectory:scriptTerminologiesFolder withPrefix:nil movingToTrash:NO];
    90 		[mgr removeFilesInDirectory:scriptTerminologiesFolder withPrefix:nil movingToTrash:NO];
    91 		NSLog(@"deleted files in directory: %@", scriptTerminologiesFolder);
    91 		NSLog(@"deleted files in directory: %@", scriptTerminologiesFolder);
    92 	}
    92 	}
    93 	
    93 	
    94 	return scriptTerminology;
    94 	return scriptTerminology;
    95 }
    95 }
    96 - (NSDictionary *)dictionaryByMergingSuiteDictionary:(NSDictionary *)scriptSuite withTerminologyDictionary:(NSDictionary *)scriptTerminology
    96 - (NSDictionary *)dictionaryByMergingSuiteDictionary:(NSDictionary *)scriptSuite withTerminologyDictionary:(NSDictionary *)scriptTerminology
    97 {
    97 {
    98 	NSMutableDictionary *merged = [[scriptSuite mutableCopy] autorelease];
    98 	NSMutableDictionary *merged = [scriptSuite mutableCopy];
    99 
    99 
   100 	NSMutableDictionary *classes = [[[scriptSuite objectForKey:@"Classes"] mutableCopy] autorelease];
   100 	NSMutableDictionary *classes = [[scriptSuite objectForKey:@"Classes"] mutableCopy];
   101 	NSDictionary *terminologyClasses = [scriptTerminology objectForKey:@"Classes"];
   101 	NSDictionary *terminologyClasses = [scriptTerminology objectForKey:@"Classes"];
   102 
   102 
   103 	NSLog(@"Before add: %@", classes);
   103 	NSLog(@"Before add: %@", classes);
   104 	NSEnumerator *keyEnum = [classes keyEnumerator];
   104 	NSEnumerator *keyEnum = [classes keyEnumerator];
   105 	NSString *className;
   105 	NSString *className;
   106 	while ((className = [keyEnum nextObject])) {
   106 	while ((className = [keyEnum nextObject])) {
   107 		NSMutableDictionary *classDict = [[[classes objectForKey:className] mutableCopy] autorelease];
   107 		NSMutableDictionary *classDict = [[classes objectForKey:className] mutableCopy];
   108 		[classDict translate:nil
   108 		[classDict translate:nil
   109 						 add:[terminologyClasses objectForKey:className]
   109 						 add:[terminologyClasses objectForKey:className]
   110 					  remove:nil];
   110 					  remove:nil];
   111 		[classes setObject:classDict forKey:className];
   111 		[classes setObject:classDict forKey:className];
   112 	}
   112 	}
   151 	NSString *str = @"Too close for missiles; I'm switching to ducks!";
   151 	NSString *str = @"Too close for missiles; I'm switching to ducks!";
   152 
   152 
   153 	NSAttributedString *input = [self giveMeA:[NSAttributedString class] ofThisString:str];
   153 	NSAttributedString *input = [self giveMeA:[NSAttributedString class] ofThisString:str];
   154 	NSString *output = [[NSScriptCoercionHandler sharedCoercionHandler] coerceValue:input toClass:[NSString class]];
   154 	NSString *output = [[NSScriptCoercionHandler sharedCoercionHandler] coerceValue:input toClass:[NSString class]];
   155 
   155 
   156 	STAssertNotNil(output, @"Coercion returned nil");
   156 	XCTAssertNotNil(output, @"Coercion returned nil");
   157 	STAssertTrue([output isKindOfClass:[NSString class]], @"Coercion to NSString must result in an NSString; instead, it resulted in an %@", [output class]);
   157 	XCTAssertTrue([output isKindOfClass:[NSString class]], @"Coercion to NSString must result in an NSString; instead, it resulted in an %@", [output class]);
   158 	STAssertEqualObjects(output, input, @"Coercion must not change the object's value");
   158 	XCTAssertEqualObjects(output, input, @"Coercion must not change the object's value");
   159 }
   159 }
   160 - (void)testMutableAttributedStringToPlainText {
   160 - (void)testMutableAttributedStringToPlainText {
   161 	NSString *str = @"The quack is strong with this one.";
   161 	NSString *str = @"The quack is strong with this one.";
   162 
   162 
   163 	NSMutableAttributedString *input = [self giveMeA:[NSMutableAttributedString class] ofThisString:str];
   163 	NSMutableAttributedString *input = [self giveMeA:[NSMutableAttributedString class] ofThisString:str];
   164 	NSString *output = [[NSScriptCoercionHandler sharedCoercionHandler] coerceValue:input toClass:[NSString class]];
   164 	NSString *output = [[NSScriptCoercionHandler sharedCoercionHandler] coerceValue:input toClass:[NSString class]];
   165 
   165 
   166 	STAssertNotNil(output, @"Coercion returned nil");
   166 	XCTAssertNotNil(output, @"Coercion returned nil");
   167 	STAssertTrue([output isKindOfClass:[NSString class]], @"Coercion to NSString must result in an NSString; instead, it resulted in an %@", [output class]);
   167 	XCTAssertTrue([output isKindOfClass:[NSString class]], @"Coercion to NSString must result in an NSString; instead, it resulted in an %@", [output class]);
   168 	STAssertEqualObjects(output, input, @"Coercion must not change the object's value");
   168 	XCTAssertEqualObjects(output, input, @"Coercion must not change the object's value");
   169 }
   169 }
   170 */
   170 */
   171 - (void)testTextStorageToPlainText {
   171 - (void)testTextStorageToPlainText {
   172 	NSString *str = @"David's gerbil";
   172 	NSString *str = @"David's gerbil";
   173 
   173 
   174 	NSTextStorage *input = [self giveMeA:[NSTextStorage class] ofThisString:str];
   174 	NSTextStorage *input = [self giveMeA:[NSTextStorage class] ofThisString:str];
   175 	NSString *output = [[NSScriptCoercionHandler sharedCoercionHandler] coerceValue:input toClass:[NSString class]];
   175 	NSString *output = [[NSScriptCoercionHandler sharedCoercionHandler] coerceValue:input toClass:[NSString class]];
   176 
   176 
   177 	STAssertNotNil(output, @"Coercion returned nil");
   177 	XCTAssertNotNil(output, @"Coercion returned nil");
   178 	STAssertTrue([output isKindOfClass:[NSString class]], @"Coercion to NSString must result in an NSString; instead, it resulted in an %@", [output class]);
   178 	XCTAssertTrue([output isKindOfClass:[NSString class]], @"Coercion to NSString must result in an NSString; instead, it resulted in an %@", [output class]);
   179 	STAssertEqualObjects(output, [input string], @"Coercion must not change the object's value");
   179 	XCTAssertEqualObjects(output, [input string], @"Coercion must not change the object's value");
   180 }
   180 }
   181 
   181 
   182 #pragma mark -
   182 #pragma mark -
   183 
   183 
   184 /*- (void)testPlainTextToAttributedString {
   184 /*- (void)testPlainTextToAttributedString {
   186 	Class destClass = [NSAttributedString class];
   186 	Class destClass = [NSAttributedString class];
   187 
   187 
   188 	NSString *input = [self giveMeA:[NSString class] ofThisString:str];
   188 	NSString *input = [self giveMeA:[NSString class] ofThisString:str];
   189 	NSAttributedString *output = [[NSScriptCoercionHandler sharedCoercionHandler] coerceValue:input toClass:destClass];
   189 	NSAttributedString *output = [[NSScriptCoercionHandler sharedCoercionHandler] coerceValue:input toClass:destClass];
   190 
   190 
   191 	STAssertNotNil(output, @"Coercion returned nil");
   191 	XCTAssertNotNil(output, @"Coercion returned nil");
   192 	STAssertTrue([output isKindOfClass:destClass], @"Coercion to %@ must result in an %@; instead, it resulted in an %@", destClass, destClass, [output class]);
   192 	XCTAssertTrue([output isKindOfClass:destClass], @"Coercion to %@ must result in an %@; instead, it resulted in an %@", destClass, destClass, [output class]);
   193 	STAssertEqualObjects(output, input, @"Coercion must not change the object's value");
   193 	XCTAssertEqualObjects(output, input, @"Coercion must not change the object's value");
   194 }
   194 }
   195 - (void)testPlainTextToMutableAttributedString {
   195 - (void)testPlainTextToMutableAttributedString {
   196 	NSString *str = @"More fun than a bag of chips.";
   196 	NSString *str = @"More fun than a bag of chips.";
   197 	Class destClass = [NSMutableAttributedString class];
   197 	Class destClass = [NSMutableAttributedString class];
   198 
   198 
   199 	NSString *input = [self giveMeA:[NSString class] ofThisString:str];
   199 	NSString *input = [self giveMeA:[NSString class] ofThisString:str];
   200 	NSMutableAttributedString *output = [[NSScriptCoercionHandler sharedCoercionHandler] coerceValue:input toClass:destClass];
   200 	NSMutableAttributedString *output = [[NSScriptCoercionHandler sharedCoercionHandler] coerceValue:input toClass:destClass];
   201 
   201 
   202 	STAssertNotNil(output, @"Coercion returned nil");
   202 	XCTAssertNotNil(output, @"Coercion returned nil");
   203 	STAssertTrue([output isKindOfClass:destClass], @"Coercion to %@ must result in an %@; instead, it resulted in an %@", destClass, destClass, [output class]);
   203 	XCTAssertTrue([output isKindOfClass:destClass], @"Coercion to %@ must result in an %@; instead, it resulted in an %@", destClass, destClass, [output class]);
   204 	STAssertEqualObjects(output, input, @"Coercion must not change the object's value");
   204 	XCTAssertEqualObjects(output, input, @"Coercion must not change the object's value");
   205 }
   205 }
   206 */
   206 */
   207 - (void)testPlainTextToTextStorage {
   207 - (void)testPlainTextToTextStorage {
   208 	NSString *str = @"Now with 30% more Zing!";
   208 	NSString *str = @"Now with 30% more Zing!";
   209 	Class destClass = [NSTextStorage class];
   209 	Class destClass = [NSTextStorage class];
   210 
   210 
   211 	NSString *input = [self giveMeA:[NSString class] ofThisString:str];
   211 	NSString *input = [self giveMeA:[NSString class] ofThisString:str];
   212 	NSTextStorage *output = [[NSScriptCoercionHandler sharedCoercionHandler] coerceValue:input toClass:destClass];
   212 	NSTextStorage *output = [[NSScriptCoercionHandler sharedCoercionHandler] coerceValue:input toClass:destClass];
   213 
   213 
   214 	STAssertNotNil(output, @"Coercion returned nil");
   214 	XCTAssertNotNil(output, @"Coercion returned nil");
   215 	STAssertTrue([output isKindOfClass:destClass], @"Coercion to %@ must result in an %@; instead, it resulted in an %@", destClass, destClass, [output class]);
   215 	XCTAssertTrue([output isKindOfClass:destClass], @"Coercion to %@ must result in an %@; instead, it resulted in an %@", destClass, destClass, [output class]);
   216 	STAssertEqualObjects([output string], input, @"Coercion must not change the object's value");
   216 	XCTAssertEqualObjects([output string], input, @"Coercion must not change the object's value");
   217 }
   217 }
   218 
   218 
   219 #pragma mark -
   219 #pragma mark -
   220 
   220 
   221 //Coerce the string, then mutate the original.
   221 //Coerce the string, then mutate the original.
   225 	NSMutableAttributedString *input = [self giveMeA:[NSMutableAttributedString class] ofThisString:str];
   225 	NSMutableAttributedString *input = [self giveMeA:[NSMutableAttributedString class] ofThisString:str];
   226 	NSString *output = [[NSScriptCoercionHandler sharedCoercionHandler] coerceValue:input toClass:[NSString class]];
   226 	NSString *output = [[NSScriptCoercionHandler sharedCoercionHandler] coerceValue:input toClass:[NSString class]];
   227 
   227 
   228 	//Mutate the original.
   228 	//Mutate the original.
   229 	NSDictionary *attrs = [NSDictionary dictionaryWithObject:[NSFont fontWithName:@"Courier" size:14.0f] forKey:NSFontAttributeName];
   229 	NSDictionary *attrs = [NSDictionary dictionaryWithObject:[NSFont fontWithName:@"Courier" size:14.0f] forKey:NSFontAttributeName];
   230 	NSAttributedString *replacement = [[[NSAttributedString alloc] initWithString:@" (one of the slogans from the old Adium X site)" attributes:attrs] autorelease];
   230 	NSAttributedString *replacement = [[NSAttributedString alloc] initWithString:@" (one of the slogans from the old Adium X site)" attributes:attrs];
   231 	[input replaceCharactersInRange:(NSRange){ [input length], 0U }
   231 	[input replaceCharactersInRange:(NSRange){ [input length], 0U }
   232 			   withAttributedString:replacement];
   232 			   withAttributedString:replacement];
   233 
   233 
   234 	STAssertNotNil(output, @"Coercion returned nil");
   234 	XCTAssertNotNil(output, @"Coercion returned nil");
   235 	STAssertTrue([output isKindOfClass:[NSString class]], @"Coercion to NSString must result in an NSString; instead, it resulted in an %@", [output class]);
   235 	XCTAssertTrue([output isKindOfClass:[NSString class]], @"Coercion to NSString must result in an NSString; instead, it resulted in an %@", [output class]);
   236 	STAssertEqualObjects(output, str, @"Coercion must not change the object's value");
   236 	XCTAssertEqualObjects(output, str, @"Coercion must not change the object's value");
   237 }
   237 }
   238 */
   238 */
   239 - (void)testTextStorageToPlainTextWithMutations {
   239 - (void)testTextStorageToPlainTextWithMutations {
   240 	NSString *str = @"The only IM client with duck 'n' cover.";
   240 	NSString *str = @"The only IM client with duck 'n' cover.";
   241 
   241 
   242 	NSTextStorage *input = [self giveMeA:[NSTextStorage class] ofThisString:str];
   242 	NSTextStorage *input = [self giveMeA:[NSTextStorage class] ofThisString:str];
   243 	NSString *output = [[NSScriptCoercionHandler sharedCoercionHandler] coerceValue:input toClass:[NSString class]];
   243 	NSString *output = [[NSScriptCoercionHandler sharedCoercionHandler] coerceValue:input toClass:[NSString class]];
   244 
   244 
   245 	//Mutate the original.
   245 	//Mutate the original.
   246 	NSDictionary *attrs = [NSDictionary dictionaryWithObject:[NSFont fontWithName:@"Courier" size:14.0f] forKey:NSFontAttributeName];
   246 	NSDictionary *attrs = [NSDictionary dictionaryWithObject:[NSFont fontWithName:@"Courier" size:14.0f] forKey:NSFontAttributeName];
   247 	NSAttributedString *replacement = [[[NSAttributedString alloc] initWithString:@" (one of the slogans from the old Adium X site)" attributes:attrs] autorelease];
   247 	NSAttributedString *replacement = [[NSAttributedString alloc] initWithString:@" (one of the slogans from the old Adium X site)" attributes:attrs];
   248 	[input replaceCharactersInRange:(NSRange){ [input length], 0U }
   248 	[input replaceCharactersInRange:(NSRange){ [input length], 0U }
   249 			   withAttributedString:replacement];
   249 			   withAttributedString:replacement];
   250 
   250 
   251 	STAssertNotNil(output, @"Coercion returned nil");
   251 	XCTAssertNotNil(output, @"Coercion returned nil");
   252 	STAssertTrue([output isKindOfClass:[NSString class]], @"Coercion to NSString must result in an NSString; instead, it resulted in an %@", [output class]);
   252 	XCTAssertTrue([output isKindOfClass:[NSString class]], @"Coercion to NSString must result in an NSString; instead, it resulted in an %@", [output class]);
   253 	STAssertEqualObjects(output, str, @"Coercion must not change the object's value");
   253 	XCTAssertEqualObjects(output, str, @"Coercion must not change the object's value");
   254 }
   254 }
   255 
   255 
   256 #pragma mark -
   256 #pragma mark -
   257 
   257 
   258 //Run the AppleScript “x as y”, where x is an AS object and y is an AS class.
   258 //Run the AppleScript “x as y”, where x is an AS object and y is an AS class.
   269 		;
   269 		;
   270 
   270 
   271 	NSDictionary *errorInfo = nil;
   271 	NSDictionary *errorInfo = nil;
   272 	[[[[NSAppleScript alloc] initWithSource:source] autorelease] executeAndReturnError:&errorInfo];
   272 	[[[[NSAppleScript alloc] initWithSource:source] autorelease] executeAndReturnError:&errorInfo];
   273 
   273 
   274 	STAssertNil(errorInfo, @"AppleScript returned an error: %@", errorInfo);
   274 	XCTAssertNil(errorInfo, @"AppleScript returned an error: %@", errorInfo);
   275 }
   275 }
   276 
   276 
   277 @end
   277 @end