From 2e5b4dec37f5c1518edf506047d533e9523194db Mon Sep 17 00:00:00 2001 From: Steffan Andrews Date: Fri, 3 Jan 2025 01:15:37 -0800 Subject: [PATCH] Updated unit tests --- Package.swift | 12 ++++++- .../TestResource/TestResource.swift | 20 +++++++++++ .../TestResource/Text Files/utf8-bom-test.csv | 3 ++ .../TextFileKitTests/TextFile CSV Tests.swift | 35 +++++++++++++++++++ 4 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 Tests/TextFileKitTests/TestResource/TestResource.swift create mode 100644 Tests/TextFileKitTests/TestResource/Text Files/utf8-bom-test.csv diff --git a/Package.swift b/Package.swift index c1bcdd6..ba9f80d 100644 --- a/Package.swift +++ b/Package.swift @@ -10,8 +10,18 @@ let package = Package( products: [ .library(name: "TextFileKit", targets: ["TextFileKit"]) ], + dependencies: [ + .package(url: "https://github.com/orchetect/swift-testing-extensions", .upToNextMajor(from: "0.2.0")), + ], targets: [ .target(name: "TextFileKit"), - .testTarget(name: "TextFileKitTests", dependencies: ["TextFileKit"]) + .testTarget( + name: "TextFileKitTests", + dependencies: [ + "TextFileKit", + .product(name: "TestingExtensions", package: "swift-testing-extensions") + ], + resources: [.copy("TestResource/Text Files")] + ) ] ) diff --git a/Tests/TextFileKitTests/TestResource/TestResource.swift b/Tests/TextFileKitTests/TestResource/TestResource.swift new file mode 100644 index 0000000..bff0426 --- /dev/null +++ b/Tests/TextFileKitTests/TestResource/TestResource.swift @@ -0,0 +1,20 @@ +// +// TestResource.swift +// TextFileKit • https://github.com/orchetect/TextFileKit +// © 2021-2024 Steffan Andrews • Licensed under MIT License +// + +import Foundation +import Testing +import TestingExtensions + +// NOTE: DO NOT name any folders "Resources". Xcode will fail to build iOS targets. + +/// Resources files on disk used for unit testing. +extension TestResource { + enum TextFiles { + static let utf8_BOM_Test_csv = TestResource.File( + name: "utf8-bom-test", ext: "csv", subFolder: "Text Files" + ) + } +} diff --git a/Tests/TextFileKitTests/TestResource/Text Files/utf8-bom-test.csv b/Tests/TextFileKitTests/TestResource/Text Files/utf8-bom-test.csv new file mode 100644 index 0000000..4511aa6 --- /dev/null +++ b/Tests/TextFileKitTests/TestResource/Text Files/utf8-bom-test.csv @@ -0,0 +1,3 @@ +"Field1","Field2" +"Row1A","Row1B" +"Row2A","Row2B" diff --git a/Tests/TextFileKitTests/TextFile CSV Tests.swift b/Tests/TextFileKitTests/TextFile CSV Tests.swift index ac2880f..e05f2e8 100644 --- a/Tests/TextFileKitTests/TextFile CSV Tests.swift +++ b/Tests/TextFileKitTests/TextFile CSV Tests.swift @@ -6,6 +6,7 @@ @testable import TextFileKit import Testing +import TestingExtensions @Suite struct CSV_Tests { @Test func init_Default() { @@ -137,3 +138,37 @@ extension CSV_Tests { #expect(sv.rawText == csvRawText_CommaContainingFields) } } + +// MARK: - BOM (Byte Order Mark) Tests + +extension CSV_Tests { + static let utf8_BOM_Test_Table: StringTable = [ + ["Field1", "Field2"], + ["Row1A", "Row1B"], + ["Row2A", "Row2B"] + ] + + @Test func utf8BOM_initURL() throws { + let url = try #require(try TestResource.TextFiles.utf8_BOM_Test_csv.url()) + + let sv = try TextFile.CSV(file: url) + + let table = sv.table + try #require(table.count == 3) + #expect(table[0] == Self.utf8_BOM_Test_Table[0]) + #expect(table[1] == Self.utf8_BOM_Test_Table[1]) + #expect(table[2] == Self.utf8_BOM_Test_Table[2]) + } + + @Test func utf8BOM_initRawData() throws { + let data = try #require(try TestResource.TextFiles.utf8_BOM_Test_csv.data()) + + let sv = try TextFile.CSV(rawData: data) + + let table = sv.table + try #require(table.count == 3) + #expect(table[0] == Self.utf8_BOM_Test_Table[0]) + #expect(table[1] == Self.utf8_BOM_Test_Table[1]) + #expect(table[2] == Self.utf8_BOM_Test_Table[2]) + } +}