Skip to content

Commit

Permalink
Merge pull request #93 from yaslab/add-more-unit-tests
Browse files Browse the repository at this point in the history
Add more unit tests
  • Loading branch information
yaslab authored Jun 29, 2019
2 parents ee50dd3 + 7ea1f25 commit b95c949
Show file tree
Hide file tree
Showing 12 changed files with 234 additions and 157 deletions.
2 changes: 1 addition & 1 deletion Sources/CSV/Legacy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
@available(*, deprecated, renamed: "CSVReader")
public typealias CSV = CSVReader

extension CSV: Sequence { }
extension CSVReader: Sequence { }
8 changes: 0 additions & 8 deletions Tests/CSVTests/BinaryReaderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,6 @@ import XCTest

class BinaryReaderTests: XCTestCase {

static let allTests = [
("testReadUInt8WithSmallBuffer", testReadUInt8WithSmallBuffer),
("testReadUInt16BEWithSmallBuffer", testReadUInt16BEWithSmallBuffer),
("testReadUInt16LEWithSmallBuffer", testReadUInt16LEWithSmallBuffer),
("testReadUInt32BEWithSmallBuffer", testReadUInt32BEWithSmallBuffer),
("testReadUInt32LEWithSmallBuffer", testReadUInt32LEWithSmallBuffer)
]

private func random(_ count: Int) -> [UInt8] {
var array = [UInt8]()
for _ in 0 ..< count {
Expand Down
49 changes: 27 additions & 22 deletions Tests/CSVTests/CSVReaderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,6 @@ import XCTest

class CSVReaderTests: XCTestCase {

static let allTests = [
("testOneLine", testOneLine),
("testTwoLines", testTwoLines),
("testLastLineIsEmpty", testLastLineIsEmpty),
("testLastLineIsWhiteSpace", testLastLineIsWhiteSpace),
("testMiddleLineIsEmpty", testMiddleLineIsEmpty),
("testCommaInQuotationMarks", testCommaInQuotationMarks),
("testEscapedQuotationMark1", testEscapedQuotationMark1),
("testEscapedQuotationMark2", testEscapedQuotationMark2),
("testEmptyField", testEmptyField),
("testDoubleQuoteBeforeLineBreak", testDoubleQuoteBeforeLineBreak),
("testCSVState1", testCSVState1),
("testSubscriptInt", testSubscriptInt),
("testHasHeaderRow1", testHasHeaderRow1),
("testHasHeaderRow2", testHasHeaderRow2),
("testHasHeaderRow3", testHasHeaderRow3),
("testSubscript1", testSubscript1),
("testSubscript2", testSubscript2),
("testToArray", testToArray)
]

func testOneLine() {
let csv = "\"abc\",1,2"
var i = 0
Expand Down Expand Up @@ -138,7 +117,7 @@ class CSVReaderTests: XCTestCase {
XCTAssertEqual(record, ["zxcv", "asdf", "qw\"er", ""])
}

func testDoubleQuoteBeforeLineBreak() {
func testDoubleQuoteBeforeLineBreak1() {
let csv = "\"abc\",1,\"2\"\n\n\"cde\",3,\"4\""
var i = 0
for record in AnyIterator(try! CSVReader(string: csv)) {
Expand All @@ -153,6 +132,20 @@ class CSVReaderTests: XCTestCase {
XCTAssertEqual(i, 3)
}

func testDoubleQuoteBeforeLineBreak2() {
let csv = "\"abc\",1,\"2\"\r\n\"cde\",3,\"4\"\r"
var i = 0
for record in AnyIterator(try! CSVReader(string: csv)) {
switch i {
case 0: XCTAssertEqual(record, ["abc", "1", "2"])
case 1: XCTAssertEqual(record, ["cde", "3", "4"])
default: break
}
i += 1
}
XCTAssertEqual(i, 2)
}

func testCSVState1() {
let it = "あ,い1,\"\",えお\n,,x,".unicodeScalars.makeIterator()
let config = CSVReader.Configuration(hasHeaderRow: false,
Expand Down Expand Up @@ -208,6 +201,18 @@ class CSVReaderTests: XCTestCase {
XCTAssertNil(csv.currentRow)
}

func testHasHeaderRow4() {
let csvString = ""
do {
_ = try CSVReader(string: csvString, hasHeaderRow: true)
XCTFail("CSVReader did not throw an error")
} catch CSVError.cannotReadHeaderRow {
// Success
} catch {
XCTFail("\(error)")
}
}

func testSubscript1() {
let csvString = "key1,key2\nvalue1,value2"
let csv = try! CSVReader(string: csvString, hasHeaderRow: true)
Expand Down
24 changes: 0 additions & 24 deletions Tests/CSVTests/CSVRowDecoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,30 +62,6 @@ extension Equatable where Self: DecodableTest {

class CSVRowDecoderTests: XCTestCase {

static let allTests = [
("testNoHeader", testNoHeader),
("testNumberOfFieldsIsSmall", testNumberOfFieldsIsSmall),
("testStringCodingKey", testStringCodingKey),
("testTypeInvalidDateFormat", testTypeInvalidDateFormat),
("testIntCodingKey", testIntCodingKey),
("testIntCodingKeyWhileIgnoringHeaders", testIntCodingKeyWhileIgnoringHeaders),
("testTypeMismatch", testTypeMismatch),
("testUnsupportedDecodableField", testUnsupportedDecodableField),
("testDecodeInteger", testDecodeInteger),
("testDecodeFloat", testDecodeFloat),
("testBoolDecodingStrategy_default", testBoolDecodingStrategy_default),
("testBoolDecodingStrategy_custom", testBoolDecodingStrategy_custom),
("testDateDecodingStrategy_deferredToDate", testDateDecodingStrategy_deferredToDate),
("testDateDecodingStrategy_secondsSince1970", testDateDecodingStrategy_secondsSince1970),
("testDateDecodingStrategy_millisecondsSince1970", testDateDecodingStrategy_millisecondsSince1970),
//("testDateDecodingStrategy_iso8601", testDateDecodingStrategy_iso8601),
("testDateDecodingStrategy_formatted", testDateDecodingStrategy_formatted),
("testDateDecodingStrategy_custom", testDateDecodingStrategy_custom),
("testDataDecodingStrategy_base64", testDataDecodingStrategy_base64),
("testDataDecodingStrategy_custom", testDataDecodingStrategy_custom),
("testFoundationDecoding", testFoundationDecoding),
]

//===----------------------------------------------------------------------===//

fileprivate struct SupportedDecodableExample: Decodable, DecodableTest {
Expand Down
17 changes: 0 additions & 17 deletions Tests/CSVTests/CSVWriterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,6 @@ extension OutputStream {

class CSVWriterTests: XCTestCase {

static let allTests = [
("testSingleFieldSingleRecord", testSingleFieldSingleRecord),
("testSingleFieldMultipleRecord", testSingleFieldMultipleRecord),
("testMultipleFieldSingleRecord", testMultipleFieldSingleRecord),
("testMultipleFieldMultipleRecord", testMultipleFieldMultipleRecord),
("testQuoted", testQuoted),
("testQuotedNewline", testQuotedNewline),
("testEscapeQuote", testEscapeQuote),
("testEscapeQuoteAutomatically", testEscapeQuoteAutomatically),
("testDelimiter", testDelimiter),
("testNewline", testNewline),
("testUTF16BE", testUTF16BE),
("testUTF16LE", testUTF16LE),
("testUTF32BE", testUTF32BE),
("testUTF32LE", testUTF32LE)
]

let str = "TEST-test-1234-😄😆👨‍👩‍👧‍👦"

/// xxxx
Expand Down
15 changes: 0 additions & 15 deletions Tests/CSVTests/LineBreakTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,6 @@ import XCTest

class LineBreakTests: XCTestCase {

static let allTests = [
("testLF", testLF),
("testCRLF", testCRLF),
("testLastCR", testLastCR),
("testLastCRLF", testLastCRLF),
("testLastLF", testLastLF),
("testLFInQuotationMarks", testLFInQuotationMarks),
("testLineBreakLF", testLineBreakLF),
("testLineBreakCR", testLineBreakCR),
("testLineBreakCRLF", testLineBreakCRLF),
("testLineBreakLFLF", testLineBreakLFLF),
("testLineBreakCRCR", testLineBreakCRCR),
("testLineBreakCRLFCRLF", testLineBreakCRLFCRLF)
]

func testLF() {
let csv = "abab,cdcd,efef\nzxcv,asdf,qwer"
let records = parse(csv: csv)
Expand Down
10 changes: 0 additions & 10 deletions Tests/CSVTests/ReadmeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,6 @@ import XCTest

class ReadmeTests: XCTestCase {

static let allTests = [
("testFromCSVString", testFromCSVString),
("testFromFile", testFromFile),
("testGettingTheHeaderRow", testGettingTheHeaderRow),
("testGetTheFieldValueUsingKey", testGetTheFieldValueUsingKey),
("testProvideTheCharacterEncoding", testProvideTheCharacterEncoding),
("testWriteToMemory", testWriteToMemory),
("testWriteToFile", testWriteToFile)
]

// MARK: - Reading

func testFromCSVString() {
Expand Down
21 changes: 0 additions & 21 deletions Tests/CSVTests/TrimFieldsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,6 @@ import XCTest

class TrimFieldsTests: XCTestCase {

static let allTests = [
("testTrimFields1", testTrimFields1),
("testTrimFields2", testTrimFields2),
("testTrimFields3", testTrimFields3),
("testTrimFields4", testTrimFields4),
("testTrimFields5", testTrimFields5),
("testTrimFields6", testTrimFields6),
("testTrimFields7", testTrimFields7),
("testTrimFields8", testTrimFields8),
("testTrimFields9", testTrimFields9),
("testTrimFields10", testTrimFields10),
("testTrimFields11", testTrimFields11),
("testTrimFields12", testTrimFields12),
("testTrimFields13", testTrimFields13),
("testTrimFields14", testTrimFields14),
("testTrimFields15", testTrimFields15),
("testTrimFields16", testTrimFields16),
("testTrimFields17", testTrimFields17),
("testTrimFields18", testTrimFields18)
]

func testTrimFields1() {
let csvString = "abc,def,ghi"
let csv = try! CSVReader(string: csvString, trimFields: true)
Expand Down
10 changes: 0 additions & 10 deletions Tests/CSVTests/UnicodeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,6 @@ import XCTest

class UnicodeTests: XCTestCase {

static let allTests = [
("testUTF8WithBOM", testUTF8WithBOM),
("testUTF16WithNativeEndianBOM", testUTF16WithNativeEndianBOM),
("testUTF16WithBigEndianBOM", testUTF16WithBigEndianBOM),
("testUTF16WithLittleEndianBOM", testUTF16WithLittleEndianBOM),
("testUTF32WithNativeEndianBOM", testUTF32WithNativeEndianBOM),
("testUTF32WithBigEndianBOM", testUTF32WithBigEndianBOM),
("testUTF32WithLittleEndianBOM", testUTF32WithLittleEndianBOM)
]

func testUTF8WithBOM() {
let csvString = "abab,,cdcd,efef\r\nzxcv,asdf,\"qw\"\"er\","
let encoding = String.Encoding.utf8
Expand Down
14 changes: 5 additions & 9 deletions Tests/CSVTests/Version1Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ import CSV

class Version1Tests: XCTestCase {

static let allTests = [
("testV1", testV1)
]

func testV1() {
let str = "a,b,c\n1,2,3"
let data8 = str.data(using: .utf8)!
Expand All @@ -28,7 +24,7 @@ class Version1Tests: XCTestCase {

do {
let stream = InputStream(data: data8)
let csv = try CSV(stream: stream,
let csv = try CSVReader(stream: stream,
codecType: UTF8.self,
hasHeaderRow: true,
trimFields: false,
Expand All @@ -42,7 +38,7 @@ class Version1Tests: XCTestCase {

do {
let stream = InputStream(data: data16)
let csv = try CSV(stream: stream,
let csv = try CSVReader(stream: stream,
codecType: UTF16.self,
endian: .big,
hasHeaderRow: true,
Expand All @@ -57,7 +53,7 @@ class Version1Tests: XCTestCase {

do {
let stream = InputStream(data: data32)
let csv = try CSV(stream: stream,
let csv = try CSVReader(stream: stream,
codecType: UTF32.self,
endian: .big,
hasHeaderRow: true,
Expand All @@ -72,7 +68,7 @@ class Version1Tests: XCTestCase {

do {
let stream = InputStream(data: data8)
let csv = try CSV(stream: stream,
let csv = try CSVReader(stream: stream,
hasHeaderRow: true,
trimFields: false,
delimiter: ",")
Expand All @@ -84,7 +80,7 @@ class Version1Tests: XCTestCase {
}

do {
let csv = try CSV(string: str,
let csv = try CSVReader(string: str,
hasHeaderRow: true,
trimFields: false,
delimiter: ",")
Expand Down
Loading

0 comments on commit b95c949

Please sign in to comment.