diff --git a/Tests/PenguinCSVTests/CSVInterpreterTests.swift b/Tests/PenguinCSVTests/CSVInterpreterTests.swift index 27df837c..7aaeffd5 100644 --- a/Tests/PenguinCSVTests/CSVInterpreterTests.swift +++ b/Tests/PenguinCSVTests/CSVInterpreterTests.swift @@ -290,6 +290,37 @@ final class CSVInterpreterTests: XCTestCase { ] } +#if swift(>=5.3) +fileprivate func assertCompatible( + _ cell: String, with type: CSVType, file: StaticString = #filePath, line: UInt = #line +) { + XCTAssert( + type.isCompatibleWith(cell), "\(type) should be compatible with \(cell)", file: file, line: line + ) +} + +fileprivate func assertNotCompatible( + _ cell: String, with type: CSVType, file: StaticString = #filePath, line: UInt = #line +) { + XCTAssertFalse( + type.isCompatibleWith(cell), "\(type) should not be compatible with \(cell)", file: file, + line: line) +} + +fileprivate func assertParsedHeuristics( + _ text: String, _ heuristics: [SeparatorHeuristics], file: StaticString = #file, + line: UInt = #line +) { + var text2 = text // Make a mutable copy, as withUTF8 might modify the string. + text2.withUTF8 { body in + let lines = body.split(separator: UInt8(ascii: "\n")) + precondition(lines.count > 2) + let fullLines = lines[0..=5.3) +fileprivate func checkColumnGuesser( + expected: [CSVType], best: CSVType, cells: String..., file: StaticString = #filePath, + line: UInt = #line +) { + var guesser = CSVColumnGuesser() + for cell in cells { + guesser.updateCompatibilities(cell: cell) + } + XCTAssertEqual(Set(expected), guesser.possibleTypes, "Cells: \(cells)", file: file, line: line) + XCTAssertEqual(best, guesser.bestGuess, "Cells: \(cells)", file: file, line: line) +} + +fileprivate func checkColumnSniffing( + withoutFirstRow: [CSVType], + withFirstRow: [CSVType], + _ contents: String, + separator: Unicode.Scalar = ",", + file: StaticString = #filePath, + line: UInt = #line +) { + precondition( + withoutFirstRow.count == withFirstRow.count, + "Mismatched counts: \(withoutFirstRow.count) and \(withFirstRow.count)") + var c = contents // Must make a mutable copy first. :-( + c.withUTF8 { contents in + let lines = contents.split(separator: UInt8(ascii: "\n")) + let allLines = lines[0..=5.3) +fileprivate func assertMetadataNotNil( + _ reader: CSVReader, file: StaticString = #filePath, line: UInt = #line +) throws -> CSVGuess { + XCTAssertNotNil(reader.metadata, file: file, line: line) + guard let metadata = reader.metadata else { throw TestError.missingMetadata } + return metadata +} +#else fileprivate func assertMetadataNotNil( _ reader: CSVReader, file: StaticString = #file, line: UInt = #line ) throws -> CSVGuess { @@ -265,3 +274,4 @@ fileprivate func assertMetadataNotNil( guard let metadata = reader.metadata else { throw TestError.missingMetadata } return metadata } +#endif diff --git a/Tests/PenguinParallelTests/NonBlockingThreadPoolTests.swift b/Tests/PenguinParallelTests/NonBlockingThreadPoolTests.swift index 450d6951..81ca845e 100644 --- a/Tests/PenguinParallelTests/NonBlockingThreadPoolTests.swift +++ b/Tests/PenguinParallelTests/NonBlockingThreadPoolTests.swift @@ -110,6 +110,17 @@ final class NonBlockingThreadPoolTests: XCTestCase { } // Overload for 2-tuple +#if swift(>=5.3) +fileprivate func XCTAssertEqual( + _ lhs: (Int, Int), _ rhs: (Int, Int), _ msg: String = "", file: StaticString = #filePath, + line: UInt = #line +) { + XCTAssertEqual( + lhs.0, rhs.0, "items 0 did not agree: \(lhs) vs \(rhs) \(msg)", file: file, line: line) + XCTAssertEqual( + lhs.1, rhs.1, "items 1 did not agree: \(lhs) vs \(rhs) \(msg)", file: file, line: line) +} +#else fileprivate func XCTAssertEqual( _ lhs: (Int, Int), _ rhs: (Int, Int), _ msg: String = "", file: StaticString = #file, line: UInt = #line @@ -119,6 +130,7 @@ fileprivate func XCTAssertEqual( XCTAssertEqual( lhs.1, rhs.1, "items 1 did not agree: \(lhs) vs \(rhs) \(msg)", file: file, line: line) } +#endif /// A platform to count threads and to ensure deallocation. /// diff --git a/Tests/PenguinTablesTests/CSVParsibleTests.swift b/Tests/PenguinTablesTests/CSVParsibleTests.swift index 25b377c9..36bed794 100644 --- a/Tests/PenguinTablesTests/CSVParsibleTests.swift +++ b/Tests/PenguinTablesTests/CSVParsibleTests.swift @@ -30,6 +30,20 @@ final class CSVParsibleTests: XCTestCase { ] } +#if swift(>=5.3) +fileprivate func assertParse( + _ bytes: String, + as val: T, + file: StaticString = #filePath, + line: UInt = #line +) { + var s = bytes + s.withUTF8 { s in + let parsed = T(CSVCell.raw(s)) + XCTAssertEqual(parsed, val, file: file, line: line) + } +} +#else fileprivate func assertParse( _ bytes: String, as val: T, @@ -42,3 +56,4 @@ fileprivate func assertParse( XCTAssertEqual(parsed, val, file: file, line: line) } } +#endif diff --git a/Tests/PenguinTablesTests/StringParsibleTests.swift b/Tests/PenguinTablesTests/StringParsibleTests.swift index 9655fcdb..b0d020f1 100644 --- a/Tests/PenguinTablesTests/StringParsibleTests.swift +++ b/Tests/PenguinTablesTests/StringParsibleTests.swift @@ -67,6 +67,37 @@ final class StringParsibleTests: XCTestCase { ] } +#if swift(>=5.3) +fileprivate func assertParses( + expected: T, source: String, file: StaticString = #filePath, line: UInt = #line +) { + let result = T(parsing: source) + XCTAssertEqual(expected, result, file: file, line: line) +} + +fileprivate func assertParseFailure( + a type: T.Type, + from source: String, + reason: String? = nil, + file: StaticString = #filePath, + line: UInt = #line +) { + do { + let unexpected = try T(parseOrThrow: source) + XCTFail( + "\"\(source)\" parsed as \(type) unexpectedly as \(unexpected).", file: file, line: line) + } catch { + let msg = String(describing: error) + if let reason = reason { + XCTAssert( + msg.contains(reason), + "Error message \"\(msg)\" did not contain expected string \"\(reason)\".", + file: file, + line: line) + } + } +} +#else fileprivate func assertParses( expected: T, source: String, file: StaticString = #file, line: UInt = #line ) { @@ -96,3 +127,4 @@ fileprivate func assertParseFailure( } } } +#endif diff --git a/Tests/PenguinTablesTests/TableTests.swift b/Tests/PenguinTablesTests/TableTests.swift index 0573ce9d..13177b95 100644 --- a/Tests/PenguinTablesTests/TableTests.swift +++ b/Tests/PenguinTablesTests/TableTests.swift @@ -277,8 +277,10 @@ final class TableTests: XCTestCase { ] } +#if swift(>=5.3) fileprivate func assertPColumnsEqual( - _ lhs: PColumn?, _ rhs: PColumn?, dtype: T.Type, file: StaticString = #file, line: UInt = #line + _ lhs: PColumn?, _ rhs: PColumn?, dtype: T.Type, + file: StaticString = #filePath, line: UInt = #line ) { if lhs == nil && rhs == nil { return } @@ -296,3 +298,25 @@ fileprivate func assertPColumnsEqual( } XCTAssertEqual(lhsT, rhsT, file: file, line: line) } +#else +fileprivate func assertPColumnsEqual( + _ lhs: PColumn?, _ rhs: PColumn?, dtype: T.Type, + file: StaticString = #file, line: UInt = #line +) { + if lhs == nil && rhs == nil { return } + + guard let lhsT: PTypedColumn = try? lhs?.asDType() else { + XCTFail( + "lhs could not be interpreted as dtype \(dtype): \(String(describing: lhs))", + file: file, line: line) + return + } + guard let rhsT: PTypedColumn = try? rhs?.asDType() else { + XCTFail( + "rhs could not be interpreted as dtype \(dtype): \(String(describing: rhs))", + file: file, line: line) + return + } + XCTAssertEqual(lhsT, rhsT, file: file, line: line) +} +#endif