mirror of
https://github.com/gnustep/libs-base.git
synced 2025-05-30 08:21:25 +00:00
Implement -[NSString enumerateLinesUsingBlock:] (#407)
* Implement -[NSString enumerateLinesUsingBlock:] * Fix formatting * Use GNUstep CALL_BLOCK macro
This commit is contained in:
parent
c498475110
commit
4d3926d250
5 changed files with 133 additions and 1 deletions
89
Tests/base/NSString/enumerateLinesUsingBlock.m
Normal file
89
Tests/base/NSString/enumerateLinesUsingBlock.m
Normal file
|
@ -0,0 +1,89 @@
|
|||
#import <Foundation/NSAutoreleasePool.h>
|
||||
#import <Foundation/NSString.h>
|
||||
#import <Foundation/NSArray.h>
|
||||
|
||||
#import "Testing.h"
|
||||
|
||||
#if defined(__has_extension) && __has_extension(blocks)
|
||||
|
||||
BOOL testEnumerateSimpleLines() {
|
||||
NSString *testString = @"First line\nSecond line\nThird line";
|
||||
NSMutableArray *collectedLines = [NSMutableArray array];
|
||||
NSArray *expectedLines = @[@"First line", @"Second line", @"Third line"];
|
||||
|
||||
[testString enumerateLinesUsingBlock: ^(NSString *line, BOOL *stop) {
|
||||
[collectedLines addObject: line];
|
||||
}];
|
||||
|
||||
return [collectedLines isEqualToArray: expectedLines];
|
||||
}
|
||||
|
||||
BOOL testEnumerateCRLFLines() {
|
||||
NSString *testString = @"First line\r\nSecond line\r\nThird line";
|
||||
NSMutableArray *collectedLines = [NSMutableArray array];
|
||||
NSArray *expectedLines = @[@"First line", @"Second line", @"Third line"];
|
||||
|
||||
[testString enumerateLinesUsingBlock: ^(NSString *line, BOOL *stop) {
|
||||
[collectedLines addObject: line];
|
||||
}];
|
||||
|
||||
return [collectedLines isEqualToArray: expectedLines];
|
||||
}
|
||||
|
||||
BOOL testStopEarly() {
|
||||
NSString *testString = @"First line\nSecond line\nThird line";
|
||||
__block int lineCount = 0;
|
||||
|
||||
[testString enumerateLinesUsingBlock:^(NSString *line, BOOL *stop) {
|
||||
lineCount++;
|
||||
if ([line isEqualToString:@"Second line"]) {
|
||||
*stop = YES;
|
||||
}
|
||||
}];
|
||||
|
||||
return lineCount == 2; // Should stop after the second line
|
||||
}
|
||||
|
||||
BOOL testEmptyString() {
|
||||
NSString *testString = @"";
|
||||
__block BOOL blockCalled = NO;
|
||||
|
||||
[testString enumerateLinesUsingBlock:^(NSString *line, BOOL *stop) {
|
||||
blockCalled = YES;
|
||||
}];
|
||||
|
||||
return !blockCalled; // Block should not be called
|
||||
}
|
||||
|
||||
BOOL testSingleLineNoBreaks() {
|
||||
NSString *testString = @"Single line without line breaks";
|
||||
__block NSString *receivedLine = nil;
|
||||
|
||||
[testString enumerateLinesUsingBlock:^(NSString *line, BOOL *stop) {
|
||||
receivedLine = line;
|
||||
}];
|
||||
|
||||
return [receivedLine isEqualToString:testString];
|
||||
}
|
||||
|
||||
int main() {
|
||||
NSAutoreleasePool *arp = [NSAutoreleasePool new];
|
||||
|
||||
PASS(testEnumerateSimpleLines(), "Should enumerate all lines correctly.");
|
||||
PASS(testEnumerateCRLFLines(), "Should enumerate all CRLF lines correctly.");
|
||||
PASS(testStopEarly(), "Should stop enumeration early as directed.");
|
||||
PASS(testEmptyString(), "Should not call block for empty string.");
|
||||
PASS(testSingleLineNoBreaks(), "Should handle single line without line breaks correctly.");
|
||||
|
||||
[arp release];
|
||||
return 0;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
int main (int argc, const char * argv[])
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue