Skip to content

Commit

Permalink
Fixed the problems and confirm the conformance
Browse files Browse the repository at this point in the history
  • Loading branch information
TomorrowMC committed Jul 15, 2024
1 parent 67b115e commit d0839bf
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
2 changes: 1 addition & 1 deletion SwiftCSV/Array+duplicates.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Extension.swift
// Array+duplicates.swift
//
//
// Created by 胡逸飞 on 2024/4/26.
Expand Down
2 changes: 1 addition & 1 deletion SwiftCSV/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion SwiftCSV/ParsingState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@
// 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) {
case (.generic(let message1), .generic(let message2)):
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
}
Expand Down
7 changes: 6 additions & 1 deletion SwiftCSVTests/DuplicateColumnNameHandlingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ class DuplicateColumnNameHandlingTests: XCTestCase {
"""

XCTAssertThrowsError(try CSV<Named>(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")
}
}
}

Expand Down

0 comments on commit d0839bf

Please sign in to comment.