forked from davedelong/CHCSVParser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnitTests.m
124 lines (99 loc) · 4.97 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
//
// 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 "CHCSVParser.h"
@implementation UnitTests
- (void) setUp {
testPool = [[NSAutoreleasePool alloc] init];
}
- (void) tearDown {
[testPool drain], testPool = nil;
}
- (NSArray *) expectedFields {
return @[
@[@"This",@"is",@"a",@"simple",@"line"],
@[@"This",@"is",@"a",@"quoted",@"line"],
@[@"This",@"is",@"a",@"mixed",@"line"],
@[@"This",@"has",@"a\nmultiline\nfield"],
@[@"This",@"has",@"single",@"apostrophes",@"ma'am"],
@[@"#This",@"line",@"should",@"not",@"be",@"ignored"],
@[@"This",@"has",@"\"escaped\"",@"quotes"],
@[@"This",@"has",@"\"escaped\"",@"quotes"],
@[@"This",@"has",@"empty",@"fields",@"",@"",@""],
@[@"This",@"has",@"escaped",@"escapes\\"],
@[@"This",@"has",@"escaped",@"commas,"],
@[@"This",@"has",@"quoted",@"commas,"],
@[@"This",@"has",@"empty",@"quoted",@"fields",@"",@""],
@[@"This",@"has",@"mixed",@"\"escaped quotes\""],
@[@" This ",@" line ",@" has ",@" significant ",@" whitespace "],
@[@"This",@"is",@"the",@"last",@"line"],
@[@""]
];
}
- (void) testCSV {
NSString *file = [[NSBundle bundleForClass:[self class]] pathForResource:@"Test" ofType:@"csv"];
NSArray *fields = [NSArray arrayWithContentsOfCSVFile:file options:CHCSVParserOptionsRecognizesBackslashesAsEscapes];
NSLog(@"read: %@", 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);
}
NSString *tempFileName = [NSString stringWithFormat:@"%d-test.csv", arc4random()];
NSString *tempFile = [NSTemporaryDirectory() stringByAppendingPathComponent:tempFileName];
NSLog(@"Writing to file: %@", tempFile);
NSOutputStream *output = [NSOutputStream outputStreamToFileAtPath:tempFile append:NO];
CHCSVWriter *writer = [[CHCSVWriter alloc] initWithOutputStream:output encoding:NSUTF8StringEncoding delimiter:','];
for (NSArray *line in expectedFields) {
[writer writeLineOfFields:line];
}
[writer closeStream];
NSArray *readFromFile = [NSArray arrayWithContentsOfCSVFile:tempFile];
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