Prevent dragging groups into groups. Fixes #8706
[adiumx.git] / UnitTests / TestStringAdditions.m
blob583d6322141648e110ec896147825754e4090d55
1 #import "TestStringAdditions.h"
2 #import "AIUnitTestUtilities.h"
4 #import <AIUtilities/AIStringAdditions.h>
6 @implementation TestStringAdditions
8 - (void)testRandomStringOfLength
10         //Test at least two different lengths, and see what happens when we ask for 0.
11         NSString *str = [NSString randomStringOfLength:6];
12         STAssertEquals([str length], 6U, @"-randomStringOfLength:6 did not return a 6-character string; it returned \"%@\", which is %u characters", str, [str length]);
13         str = [NSString randomStringOfLength:12];
14         STAssertEquals([str length], 12U, @"-randomStringOfLength:12 did not return a 12-character string; it returned \"%@\", which is %u characters", str, [str length]);
15         str = [NSString randomStringOfLength:0];
16         STAssertEquals([str length], 0U, @"-randomStringOfLength:0 did not return a 0-character string; it returned \"%@\", which is %u characters", str, [str length]);
18 - (void)testStringWithContentsOfUTF8File
20         //Our octest file contains a sample file to read in testing this method.
21         NSBundle *bundle = [NSBundle bundleForClass:[self class]];
22         NSString *pathToFile = [bundle pathForResource:@"UTF8Snowman" ofType:@"txt"];
24         char snowmanUTF8[4] = { 0xe2, 0x98, 0x83, 0 };
25         NSString *snowman = [NSString stringWithUTF8String:snowmanUTF8];
26         NSString *snowmanFromFile = [NSString stringWithContentsOfUTF8File:pathToFile];
27         AISimplifiedAssertEqualObjects(snowman, snowmanFromFile, @"-stringWithContentsOfUTF8File: incorrectly read the file");
29 - (void)testEllipsis
31         STAssertEquals([[NSString ellipsis] length], 1U, @"-ellipsis did not return a 1-character string; it returned \"%@\"", [NSString ellipsis]);
32         STAssertEquals((unsigned int)[[NSString ellipsis] characterAtIndex:0U], 0x2026U, @"-ellipsis did not return a horizontal ellipsis (U+2026); it returned \"%@\" instead", [NSString ellipsis]);
34 - (void)testStringByAppendingEllipsis
36         NSString *before = @"Foo";
37         NSString *after  = [before stringByAppendingEllipsis];
38         STAssertEquals(([after length] - [before length]), 1U, @"Appending a single character should result in a string that is one character longer. before is \"%@\"; after is \"%@\"", before, after);
39         STAssertTrue([after hasSuffix:[NSString ellipsis]], @"String formed by appending [NSString ellipsis] should end with [NSString ellipsis]. before is \"%@\"; after is \"%@\"", before, after);
41 - (void)testCompactedString
43         AISimplifiedAssertEqualObjects([@"FOO" compactedString], @"foo", @"-compactedString should lowercase an all-uppercase string");
44         AISimplifiedAssertEqualObjects([@"Foo" compactedString], @"foo", @"-compactedString should lowercase a mixed-case string");
45         AISimplifiedAssertEqualObjects([@"foo" compactedString], @"foo", @"-compactedString should do nothing to an all-lowercase string");
46         AISimplifiedAssertEqualObjects([@"foo bar" compactedString], @"foobar", @"-compactedString should remove spaces");
48 - (void)testStringWithEllipsisByTruncatingToLength
50         NSString *before = @"Foo";
51         NSString *after;
53         //First, try truncating to a greater length.
54         after = [before stringWithEllipsisByTruncatingToLength:[before length] + 1];
55         STAssertEqualObjects(before, after, @"Truncating to a length greater than that of the string being truncated should not change the string. before is \"%@\"; after is \"%@\"", before, after);
57         //Second, try truncating to the same length.
58         after = [before stringWithEllipsisByTruncatingToLength:[before length]];
59         STAssertEqualObjects(before, after, @"Truncating to a length equal to that of the string being truncated should not change the string. before is \"%@\"; after is \"%@\"", before, after);
61         //Third, try truncating to a shorter length. This one should actually truncate the string and append an ellipsis.
62         after = [before stringWithEllipsisByTruncatingToLength:[before length] - 1];
63         STAssertEquals(([before length] - [after length]), 1U, @"Appending a single character should result in a string that is one character longer. before is \"%@\"; after is \"%@\"", before, after);
64         //The part before the ellipsis in after should be equal to the same portion of before.
65         unsigned cutHere = [after length] - 1;
66         STAssertEqualObjects([after  substringToIndex:cutHere - 1],
67                              [before substringToIndex:cutHere - 1],
68                                                  @"Truncating a string should not result in any changes before the truncation point before is \"%@\"; after is \"%@\"", before, after);
69         STAssertTrue([after hasSuffix:[NSString ellipsis]], @"String formed by appending [NSString ellipsis] should end with [NSString ellipsis]. before is \"%@\"; after is \"%@\"", before, after);
71 - (void)testIdentityMethod
73         NSString *str = @"Foo";
74         STAssertEquals([str string], str, @"A method that returns itself must, by definition, return itself.");
76 - (void)testXMLEscaping
78         NSString *originalXMLSource = @"<rel-date><number>Four score</number> &amp; <number>seven</number> years ago</rel-date>";
79         NSString *escaped = [originalXMLSource stringByEscapingForXMLWithEntities:nil];
80         NSString *unescaped = [escaped stringByUnescapingFromXMLWithEntities:nil];
81         STAssertEqualObjects(originalXMLSource, unescaped, @"Round trip through scaping + unescaping did not preserve the original string.");
83 - (void)testEscapingForShell
85         //Whitespace should be replaced by '\' followed by a character (one of [atnfr] for most of them; space simply puts a \ in front of the space).
86         STAssertEqualObjects([@"\a" stringByEscapingForShell], @"\\a", @"stringByEscapingForShell didn't properly escape the alert (bell) character");
87         STAssertEqualObjects([@"\t" stringByEscapingForShell], @"\\t", @"stringByEscapingForShell didn't properly escape the horizontal tab character");
88         STAssertEqualObjects([@"\n" stringByEscapingForShell], @"\\n", @"stringByEscapingForShell didn't properly escape the line-feed character");
89         STAssertEqualObjects([@"\v" stringByEscapingForShell], @"\\v", @"stringByEscapingForShell didn't properly escape the vertical tab character");
90         STAssertEqualObjects([@"\f" stringByEscapingForShell], @"\\f", @"stringByEscapingForShell didn't properly escape the form-feed character");
91         STAssertEqualObjects([@"\r" stringByEscapingForShell], @"\\r", @"stringByEscapingForShell didn't properly escape the carriage-return character");
92         STAssertEqualObjects([@" "  stringByEscapingForShell], @"\\ ", @"stringByEscapingForShell didn't properly escape the space character");
94         //Other unsafe characters are simply backslash-escaped.
95         STAssertEqualObjects([@"\\" stringByEscapingForShell], @"\\\\", @"stringByEscapingForShell didn't properly escape the alert (bell) character");
96         STAssertEqualObjects([@"'" stringByEscapingForShell], @"\\'", @"stringByEscapingForShell didn't properly escape the horizontal tab character");
97         STAssertEqualObjects([@"\"" stringByEscapingForShell], @"\\\"", @"stringByEscapingForShell didn't properly escape the line-feed character");
98         STAssertEqualObjects([@"`" stringByEscapingForShell], @"\\`", @"stringByEscapingForShell didn't properly escape the vertical tab character");
99         STAssertEqualObjects([@"!" stringByEscapingForShell], @"\\!", @"stringByEscapingForShell didn't properly escape the form-feed character");
100         STAssertEqualObjects([@"$" stringByEscapingForShell], @"\\$", @"stringByEscapingForShell didn't properly escape the carriage-return character");
101         STAssertEqualObjects([@"&"  stringByEscapingForShell], @"\\&", @"stringByEscapingForShell didn't properly escape the space character");
102         STAssertEqualObjects([@"|"  stringByEscapingForShell], @"\\|", @"stringByEscapingForShell didn't properly escape the space character");
104 - (void)testVolumePath
106         NSString *homeVolumePath = [NSHomeDirectory() volumePath];
107         STAssertTrue([homeVolumePath isEqualToString:@"/"] || [homeVolumePath isEqualToString:[@"/Users" stringByAppendingPathComponent:NSUserName()]], @"Volume path \"%@\" of home directory %@ is neither / nor /Users/%@", homeVolumePath, NSHomeDirectory, NSUserName());
109         STAssertEqualObjects([@"/" volumePath], @"/", @"Volume path of / is \"%@\", not /", [@"/" volumePath]);
111         //Get the name of the startup volume, so that we can attempt to get the volume path of (what we hope is) a directory on it.
112         OSStatus err;
114         FSRef ref;
115         err = FSPathMakeRef((const UInt8 *)"/", &ref, /*isDirectory*/ NULL);
116         STAssertTrue(err == noErr, @"Error while attempting to determine the path of the startup volume: FSPathMakeRef returned %i", err);
118         struct HFSUniStr255 volumeNameUnicode;
119         err = FSGetCatalogInfo(&ref, /*whichInfo*/ 0, /*catalogInfo*/ NULL, /*outName*/ &volumeNameUnicode, /*fsSpec*/ NULL, /*parentRef*/ NULL);
120         STAssertTrue(err == noErr, @"Error while attempting to determine the path of the startup volume: FSGetCatalogInfo returned %i", err);
122         NSString *volumeName = [[[NSString alloc] initWithCharactersNoCopy:volumeNameUnicode.unicode length:volumeNameUnicode.length freeWhenDone:NO] autorelease];
123         NSLog(@"Volume name from FSGetCatalogInfo is %@", volumeName);
124         NSString *inputPath = [[@"/Volumes" stringByAppendingPathComponent:volumeName] stringByAppendingPathComponent:@"Applications"];
125         NSString *outputPath = [inputPath volumePath];
127         STAssertEqualObjects(outputPath, @"/", @"The volume path of %@ should be /; instead, it was \"%@\"", inputPath, outputPath);
129 - (void)testAllLinesWithSeparator
131         NSString *str = @"Foo\nbar\nbaz";
132         NSArray *linesWithSep = [str allLinesWithSeparator:@"Qux"];
133         NSArray *expectedLines = [NSArray arrayWithObjects:@"Foo", @"Qux", @"bar", @"Qux", @"baz", nil];
134         AISimplifiedAssertEqualObjects(linesWithSep, expectedLines, @"allLinesWithSeparator: did not properly split and splice the array");
136         NSArray *lines = [str allLinesWithSeparator:nil];
137         expectedLines = [NSArray arrayWithObjects:@"Foo", @"bar", @"baz", nil];
138         AISimplifiedAssertEqualObjects(lines, expectedLines, @"allLinesWithSeparator: did not properly split the array");
140 - (void)testAllLines
142         NSString *str = @"Foo\nbar\nbaz";
143         NSArray *lines = [str allLinesWithSeparator:nil];
144         NSArray *expectedLines = [NSArray arrayWithObjects:@"Foo", @"bar", @"baz", nil];
145         AISimplifiedAssertEqualObjects(lines, expectedLines, @"allLines did not properly split the array");
148 @end