forked from davedelong/CHCSVParser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnitTests.m
130 lines (102 loc) · 5.67 KB
/
UnitTests.m
1
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
//
// UnitTests.m
// CHCSVParser
/**
Copyright (c) 2010 Dave DeLong
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
**/
#import "UnitTests.h"
#import "NSArray+CHCSVAdditions.h"
#import "NSString+CHCSVAdditions.h"
@implementation UnitTests
- (void) setUp {
testPool = [[NSAutoreleasePool alloc] init];
}
- (void) tearDown {
[testPool drain], testPool = nil;
}
- (NSArray *) expectedFields {
return [NSArray arrayWithObjects:
[NSArray arrayWithObjects:@"This",@"is",@"a",@"simple",@"line",nil],
[NSArray arrayWithObjects:@"This",@"is",@"a",@"quoted",@"line",nil],
[NSArray arrayWithObjects:@"This",@"is",@"a",@"mixed",@"line",nil],
[NSArray arrayWithObjects:@"This",@"has",@"a\nmultiline\nfield",nil],
[NSArray arrayWithObjects:@"This",@"has",@"single",@"apostrophes",@"ma'am",nil],
[NSArray arrayWithObjects:@"#This",@"line",@"should",@"not",@"be",@"ignored",nil],
[NSArray arrayWithObjects:@"This",@"has",@"\"escaped\"",@"quotes",nil],
[NSArray arrayWithObjects:@"This",@"has",@"\"escaped\"",@"quotes",nil],
[NSArray arrayWithObjects:@"This",@"has",@"empty",@"fields",@"",@"",@"",nil],
[NSArray arrayWithObjects:@"This",@"has",@"escaped",@"escapes\\",nil],
[NSArray arrayWithObjects:@"This",@"has",@"escaped",@"commas,",nil],
[NSArray arrayWithObjects:@"This",@"has",@"quoted",@"commas,",nil],
[NSArray arrayWithObjects:@"This",@"has",@"empty",@"quoted",@"fields",@"",@"",nil],
[NSArray arrayWithObjects:@"This",@"has",@"mixed",@"\"escaped quotes\"", nil],
[NSArray arrayWithObjects:@"This",@"is",@"the",@"last",@"line",nil],
nil];
}
- (void) testCSV {
NSString * file = [[NSBundle bundleForClass:[self class]] pathForResource:@"Test" ofType:@"csv"];
NSStringEncoding encoding = 0;
NSError * error = nil;
NSArray * fields = [NSArray arrayWithContentsOfCSVFile:file usedEncoding:&encoding error:&error];
NSLog(@"read: %@", fields);
STAssertTrue(encoding == NSUTF8StringEncoding, @"Wrong encoding; given %@ (%lu)", CFStringGetNameOfEncoding(CFStringConvertNSStringEncodingToEncoding(encoding)), encoding);
STAssertNil(error, @"Unexpected error: %@", error);
NSArray * expectedFields = [self expectedFields];
NSUInteger expectedCount = [expectedFields count];
NSUInteger actualCount = [fields count];
STAssertTrue(expectedCount == actualCount, @"incorrect number of lines parsed. expected %lu, given %lu", expectedCount, actualCount);
for (int i = 0; i < MIN(expectedCount, actualCount); ++i) {
NSArray * actualLine = [fields objectAtIndex:i];
NSArray * expectedLine = [expectedFields objectAtIndex:i];
STAssertTrue([actualLine isEqualToArray:expectedLine], @"lines differ. Expected %@, given %@", expectedLine, actualLine);
}
NSString * tempFileName = [NSString stringWithFormat:@"%d-test.csv", arc4random()];
NSString * tempFile = [NSTemporaryDirectory() stringByAppendingPathComponent:tempFileName];
NSLog(@"Writing to file: %@", tempFile);
error = nil;
BOOL writtenToFile = [expectedFields writeToCSVFile:tempFile atomically:YES error:&error];
STAssertNil(error, @"Unexpected error writing to temporary file: %@", error);
STAssertTrue(writtenToFile, @"Unable to write to temporary file");
error = nil;
NSArray * readFromFile = [NSArray arrayWithContentsOfCSVFile:tempFile encoding:encoding error:&error];
STAssertNil(error, @"Unexpected error reading from temporary file: %@", error);
NSUInteger readCount = [readFromFile count];
STAssertTrue(readCount == expectedCount, @"Incorrect number of lines read. Expected %lu, read %lu", expectedCount, readCount);
for (int i = 0; i < MIN(expectedCount, readCount); ++i) {
NSArray * readLine = [readFromFile objectAtIndex:i];
NSArray * expectedLine = [expectedFields objectAtIndex:i];
STAssertTrue([expectedLine isEqualToArray:readLine], @"lines differ. Expected %@, read %@", expectedLine, readLine);
}
}
- (void) testCSVString {
NSString * file = [[NSBundle bundleForClass:[self class]] pathForResource:@"Test" ofType:@"csv"];
NSStringEncoding encoding = 0;
NSString * csv = [NSString stringWithContentsOfFile:file usedEncoding:&encoding error:nil];
NSArray * fields = [csv CSVComponents];
NSLog(@"fields: %@", fields);
NSArray * expectedFields = [self expectedFields];
NSUInteger expectedCount = [expectedFields count];
NSUInteger actualCount = [fields count];
STAssertTrue(expectedCount == actualCount, @"incorrect number of lines parsed. expected %lu, given %lu", expectedCount, actualCount);
for (int i = 0; i < MIN(expectedCount, actualCount); ++i) {
NSArray * actualLine = [fields objectAtIndex:i];
NSArray * expectedLine = [expectedFields objectAtIndex:i];
STAssertTrue([actualLine isEqualToArray:expectedLine], @"lines differ. Expected %@, given %@", expectedLine, actualLine);
}
}
@end