From d0839bf47171d4e554ab9a3eb101c82cbfbec28d Mon Sep 17 00:00:00 2001 From: "Yifei.Hu" Date: Mon, 15 Jul 2024 15:01:46 +0800 Subject: [PATCH] Fixed the problems and confirm the conformance --- SwiftCSV/Array+duplicates.swift | 2 +- SwiftCSV/Parser.swift | 2 +- SwiftCSV/ParsingState.swift | 6 +++++- SwiftCSVTests/DuplicateColumnNameHandlingTests.swift | 7 ++++++- 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/SwiftCSV/Array+duplicates.swift b/SwiftCSV/Array+duplicates.swift index aa2049c..63bc027 100644 --- a/SwiftCSV/Array+duplicates.swift +++ b/SwiftCSV/Array+duplicates.swift @@ -1,5 +1,5 @@ // -// Extension.swift +// Array+duplicates.swift // // // Created by 胡逸飞 on 2024/4/26. diff --git a/SwiftCSV/Parser.swift b/SwiftCSV/Parser.swift index 1406da4..1bfc336 100644 --- a/SwiftCSV/Parser.swift +++ b/SwiftCSV/Parser.swift @@ -130,7 +130,7 @@ enum Parser { // Check for duplicate column names let duplicateColumns = header.duplicates() if !duplicateColumns.isEmpty { - throw CSVParseError.generic(message: "Duplicate column names found: \(duplicateColumns.joined(separator: ", "))") + throw CSVParseError.duplicateColumns(columnNames: duplicateColumns) } // Start after the header diff --git a/SwiftCSV/ParsingState.swift b/SwiftCSV/ParsingState.swift index 068498d..30e2b92 100644 --- a/SwiftCSV/ParsingState.swift +++ b/SwiftCSV/ParsingState.swift @@ -6,9 +6,11 @@ // Copyright © 2016 Naoto Kaneko. All rights reserved. // -public enum CSVParseError: Error, Equatable { +public enum CSVParseError: Error { case generic(message: String) case quotation(message: String) + case duplicateColumns(columnNames: [String]) + public static func == (lhs: CSVParseError, rhs: CSVParseError) -> Bool { switch (lhs, rhs) { @@ -16,6 +18,8 @@ public enum CSVParseError: Error, Equatable { return message1 == message2 case (.quotation(let message1), .quotation(let message2)): return message1 == message2 + case (.duplicateColumns(let columns1), .duplicateColumns(let columns2)): + return columns1 == columns2 default: return false } diff --git a/SwiftCSVTests/DuplicateColumnNameHandlingTests.swift b/SwiftCSVTests/DuplicateColumnNameHandlingTests.swift index adf622f..6e539ee 100644 --- a/SwiftCSVTests/DuplicateColumnNameHandlingTests.swift +++ b/SwiftCSVTests/DuplicateColumnNameHandlingTests.swift @@ -19,7 +19,12 @@ class DuplicateColumnNameHandlingTests: XCTestCase { """ XCTAssertThrowsError(try CSV(string: csvString)) { error in - XCTAssertEqual(error as? CSVParseError, CSVParseError.generic(message: "Duplicate column names found: name")) + switch error as? CSVParseError { + case .duplicateColumns(let columnNames): + XCTAssertEqual(["name"], columnNames) + default: + XCTFail("Expected CSVParseError.duplicateColumns") + } } }