Skip to content

Commit

Permalink
Add unit tests for issue #79.
Browse files Browse the repository at this point in the history
  • Loading branch information
davedelong committed Mar 7, 2015
1 parent d321fe1 commit 5a02c85
Showing 1 changed file with 49 additions and 4 deletions.
53 changes: 49 additions & 4 deletions Unit Tests/UnitTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,40 @@ of this software and associated documentation files (the "Software"), to deal

@implementation UnitTests

- (NSURL *)temporaryURLForDelimitedString:(NSString *)string {
static NSURL *temporaryFolder = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSURL *tmp = [NSURL fileURLWithPath:NSTemporaryDirectory()];
tmp = [tmp URLByAppendingPathComponent:@"CHCSVParser" isDirectory:YES];

NSDateFormatter *f = [[NSDateFormatter alloc] init];
f.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
f.calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
f.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
f.dateFormat = @"yyyy.MM.dd.HH.mm.ss.SSSSS";

NSString *folderName = [f stringFromDate:[NSDate date]];
temporaryFolder = [tmp URLByAppendingPathComponent:folderName isDirectory:YES];
XCTAssertNotNil(temporaryFolder, @"Unable to locate temporary directory");

NSError *error = nil;
BOOL created = [[NSFileManager defaultManager] createDirectoryAtURL:temporaryFolder withIntermediateDirectories:YES attributes:nil error:&error];

XCTAssertTrue(created, @"Unable to create temporary directory (%@)", error);

NSLog(@"Writing files to %@", temporaryFolder);
});
NSString *name = [NSUUID UUID].UUIDString;
NSURL *url = [temporaryFolder URLByAppendingPathComponent:name];

NSError *error = nil;
BOOL written = [string writeToURL:url atomically:YES encoding:NSUTF8StringEncoding error:&error];
XCTAssertTrue(written, @"Unable to write string to temporary folder: %@", error);

return url;
}

- (void)testAvailableEncodings {
const CFStringEncoding *encodings = CFStringGetListOfAvailableEncodings();

Expand Down Expand Up @@ -137,10 +171,8 @@ - (void)testGithubIssue50 {
- (void)testGithubIssue50Workaround {
NSString *csv = @"TRẦN,species_code,Scientific name,Author name,Common name,Family,Description,Habitat,\"Leaf size min (cm, 0 decimal digit)\",\"Leaf size max (cm, 0 decimal digit)\",Distribution,Current National Conservation Status,Growth requirements,Horticultural features,Uses,Associated fauna,Reference,species_id";

NSString *file = [NSTemporaryDirectory() stringByAppendingPathComponent:NSStringFromSelector(_cmd)];
[csv writeToFile:file atomically:NO encoding:NSUTF8StringEncoding error:nil];

NSArray *actual = [NSArray arrayWithContentsOfCSVURL:[NSURL fileURLWithPath:file]];
NSURL *url = [self temporaryURLForDelimitedString:csv];
NSArray *actual = [NSArray arrayWithContentsOfCSVURL:url];

NSArray *expected = @[@[@"TRẦN",@"species_code",@"Scientific name",@"Author name",@"Common name",@"Family",@"Description",@"Habitat",@"\"Leaf size min (cm, 0 decimal digit)\"",@"\"Leaf size max (cm, 0 decimal digit)\"",@"Distribution",@"Current National Conservation Status",@"Growth requirements",@"Horticultural features",@"Uses",@"Associated fauna",@"Reference",@"species_id"]];
XCTAssertEqualObjects(actual, expected, @"failed");
Expand Down Expand Up @@ -351,6 +383,19 @@ - (void)testGithubIssue65 {
TEST_ARRAYS(actual, expected);
}

- (void)testGithubIssue79 {
NSString *scsv = @"16681;6;Orehovyj boulevard, ul. Musy Dzhalilja (odd side);20;out;55.6141571054;37.7460757208;800;34;34;0;0;0;0;0;1";
NSArray *parsed = [scsv componentsSeparatedByDelimiter:';'];
NSArray *expected = @[@[@"16681",@"6",@"Orehovyj boulevard, ul. Musy Dzhalilja (odd side)",@"20",@"out",@"55.6141571054",@"37.7460757208",@"800",@"34",@"34",@"0",@"0",@"0",@"0",@"0",@"1"]];

TEST_ARRAYS(parsed, expected);

NSURL *url = [self temporaryURLForDelimitedString:scsv];
parsed = [NSArray arrayWithContentsOfDelimitedURL:url delimiter:';'];

TEST_ARRAYS(parsed, expected);
}

- (void)testEmptyFields {
NSString *csv = COMMA COMMA;
NSArray *expected = @[@[EMPTY, EMPTY, EMPTY]];
Expand Down

0 comments on commit 5a02c85

Please sign in to comment.