Skip to content

Commit bb6aa44

Browse files
committed
Updated unit tests
1 parent 2d309e1 commit bb6aa44

File tree

3 files changed

+283
-12
lines changed

3 files changed

+283
-12
lines changed

Tests/TextFileTests/Text Files/CSV Tests.swift renamed to Tests/TextFileTests/Text Files/CSV Decode Tests.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// CSV Tests.swift
2+
// CSV Decode Tests.swift
33
// swift-textfile • https://github.com/orchetect/swift-textfile
44
// © 2018-2025 Steffan Andrews • Licensed under MIT License
55
//
@@ -9,7 +9,7 @@ import Testing
99
import TestingExtensions
1010
@testable import TextFile
1111

12-
@Suite struct CSV_Tests {
12+
@Suite struct CSV_Decode_Tests {
1313
@Test func init_Default() async {
1414
let sv = CSV()
1515

@@ -19,7 +19,7 @@ import TestingExtensions
1919

2020
// MARK: - Basic
2121

22-
@Suite struct CSV_Basic_Tests {
22+
@Suite struct CSV_Decode_Basic_Tests {
2323
private let csvRawText_Basic = """
2424
header1,header2,header3
2525
1,2,3
@@ -49,7 +49,7 @@ import TestingExtensions
4949

5050
// MARK: - Single column
5151

52-
@Suite struct CSV_SingleColumn_Tests {
52+
@Suite struct CSV_Decode_SingleColumn_Tests {
5353
private let csvRawText_SingleColumn = """
5454
header1
5555
1
@@ -79,7 +79,7 @@ import TestingExtensions
7979

8080
// MARK: - Quoted fields
8181

82-
@Suite struct CSV_QuotedFields_Tests {
82+
@Suite struct CSV_Decode_QuotedFields_Tests {
8383
private let csvRawText_QuotedFields = #"""
8484
header1,"header, 2",header3
8585
1,2,"3 ""quoted"" here"
@@ -117,7 +117,7 @@ import TestingExtensions
117117

118118
// MARK: - Comma-Containing fields
119119

120-
@Suite struct CSV_CommaContainingFields_Tests {
120+
@Suite struct CSV_Decode_CommaContainingFields_Tests {
121121
private let csvRawText_CommaContainingFields = #"""
122122
header1,header2,header3
123123
data one,"""data, two""",data three
@@ -157,7 +157,7 @@ import TestingExtensions
157157

158158
// MARK: - BOM (Byte Order Mark) Tests
159159

160-
@Suite struct CSV_UTF8_BOM_Tests {
160+
@Suite struct CSV_Decode_UTF8_BOM_Tests {
161161
private static let utf8_BOM_Test_Table: StringTable = [
162162
["Field1", "Field2"],
163163
["Row1A", "Row1B"],
Lines changed: 271 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,271 @@
1+
//
2+
// CSV Encode Tests.swift
3+
// swift-textfile • https://github.com/orchetect/swift-textfile
4+
// © 2018-2025 Steffan Andrews • Licensed under MIT License
5+
//
6+
7+
import class Foundation.Bundle
8+
import struct Foundation.Data
9+
import Testing
10+
import TestingExtensions
11+
@testable import TextFile
12+
13+
@Suite struct CSV_Encode_Tests {
14+
private let csvTable_Basic: StringTable = [
15+
["1", "2", "3"],
16+
["á", "ç", "é"]
17+
]
18+
19+
@Test func data_utf8_withBOM() async throws {
20+
let sv = CSV(table: csvTable_Basic)
21+
22+
let data = try sv.data(encoding: .utf8, includeBOM: true)
23+
24+
let expectedBytes: [UInt8] = [
25+
0xEF, 0xBB, 0xBF, // UTF-8 BOM
26+
0x31, 0x2C, 0x32, 0x2C, 0x33, 0x0A, // "1,2,3" + newline
27+
0xC3, 0xA1, // á
28+
0x2C, // ,
29+
0xC3, 0xA7, // ç
30+
0x2C, // ,
31+
0xC3, 0xA9 // é
32+
]
33+
34+
#expect([UInt8](data) == expectedBytes)
35+
}
36+
37+
@Test func data_utf8_withoutBOM() async throws {
38+
let sv = CSV(table: csvTable_Basic)
39+
40+
let data = try sv.data(encoding: .utf8, includeBOM: false)
41+
42+
let expectedBytes: [UInt8] = [
43+
0x31, 0x2C, 0x32, 0x2C, 0x33, 0x0A, // "1,2,3" + newline
44+
0xC3, 0xA1, // á
45+
0x2C, // ,
46+
0xC3, 0xA7, // ç
47+
0x2C, // ,
48+
0xC3, 0xA9 // é
49+
]
50+
51+
#expect([UInt8](data) == expectedBytes)
52+
}
53+
54+
@Test func data_utf16BE_withBOM() async throws {
55+
let sv = CSV(table: csvTable_Basic)
56+
57+
let data = try sv.data(encoding: .utf16BigEndian, includeBOM: true)
58+
59+
let expectedBytes: [UInt8] = [
60+
0xFE, 0xFF, // UTF-16 big-endian BOM
61+
0x00, 0x31, // 1
62+
0x00, 0x2C, // ,
63+
0x00, 0x32, // 2
64+
0x00, 0x2C, // ,
65+
0x00, 0x33, // 3
66+
0x00, 0x0A, // newline
67+
0x00, 0xE1, // á
68+
0x00, 0x2C, // ,
69+
0x00, 0xE7, // ç
70+
0x00, 0x2C, // ,
71+
0x00, 0xE9 // é
72+
]
73+
74+
#expect([UInt8](data) == expectedBytes)
75+
}
76+
77+
@Test func data_utf16BE_withoutBOM() async throws {
78+
let sv = CSV(table: csvTable_Basic)
79+
80+
let data = try sv.data(encoding: .utf16BigEndian, includeBOM: false)
81+
82+
let expectedBytes: [UInt8] = [
83+
0x00, 0x31, // 1
84+
0x00, 0x2C, // ,
85+
0x00, 0x32, // 2
86+
0x00, 0x2C, // ,
87+
0x00, 0x33, // 3
88+
0x00, 0x0A, // newline
89+
0x00, 0xE1, // á
90+
0x00, 0x2C, // ,
91+
0x00, 0xE7, // ç
92+
0x00, 0x2C, // ,
93+
0x00, 0xE9 // é
94+
]
95+
96+
#expect([UInt8](data) == expectedBytes)
97+
}
98+
99+
@Test func data_utf16LE_withBOM() async throws {
100+
let sv = CSV(table: csvTable_Basic)
101+
102+
let data = try sv.data(encoding: .utf16LittleEndian, includeBOM: true)
103+
104+
let expectedBytes: [UInt8] = [
105+
0xFF, 0xFE, // UTF-16 little-endian BOM
106+
0x31, 0x00, // 1
107+
0x2C, 0x00, // ,
108+
0x32, 0x00, // 2
109+
0x2C, 0x00, // ,
110+
0x33, 0x00, // 3
111+
0x0A, 0x00, // newline
112+
0xE1, 0x00, // á
113+
0x2C, 0x00, // ,
114+
0xE7, 0x00, // ç
115+
0x2C, 0x00, // ,
116+
0xE9, 0x00 // é
117+
]
118+
119+
#expect([UInt8](data) == expectedBytes)
120+
}
121+
122+
@Test func data_utf16LE_withoutBOM() async throws {
123+
let sv = CSV(table: csvTable_Basic)
124+
125+
let data = try sv.data(encoding: .utf16LittleEndian, includeBOM: false)
126+
127+
let expectedBytes: [UInt8] = [
128+
0x31, 0x00, // 1
129+
0x2C, 0x00, // ,
130+
0x32, 0x00, // 2
131+
0x2C, 0x00, // ,
132+
0x33, 0x00, // 3
133+
0x0A, 0x00, // newline
134+
0xE1, 0x00, // á
135+
0x2C, 0x00, // ,
136+
0xE7, 0x00, // ç
137+
0x2C, 0x00, // ,
138+
0xE9, 0x00 // é
139+
]
140+
141+
#expect([UInt8](data) == expectedBytes)
142+
}
143+
144+
@Test func data_utf32BE_withBOM() async throws {
145+
let sv = CSV(table: csvTable_Basic)
146+
147+
let data = try sv.data(encoding: .utf32BigEndian, includeBOM: true)
148+
149+
let expectedBytes: [UInt8] = [
150+
0x00, 0x00, 0xFE, 0xFF, // UTF-32 big-endian BOM
151+
0x00, 0x00, 0x00, 0x31, // 1
152+
0x00, 0x00, 0x00, 0x2C, // ,
153+
0x00, 0x00, 0x00, 0x32, // 2
154+
0x00, 0x00, 0x00, 0x2C, // ,
155+
0x00, 0x00, 0x00, 0x33, // 3
156+
0x00, 0x00, 0x00, 0x0A, // newline
157+
0x00, 0x00, 0x00, 0xE1, // á
158+
0x00, 0x00, 0x00, 0x2C, // ,
159+
0x00, 0x00, 0x00, 0xE7, // ç
160+
0x00, 0x00, 0x00, 0x2C, // ,
161+
0x00, 0x00, 0x00, 0xE9 // é
162+
]
163+
164+
#expect([UInt8](data) == expectedBytes)
165+
}
166+
167+
@Test func data_utf32BE_withoutBOM() async throws {
168+
let sv = CSV(table: csvTable_Basic)
169+
170+
let data = try sv.data(encoding: .utf32BigEndian, includeBOM: false)
171+
172+
let expectedBytes: [UInt8] = [
173+
0x00, 0x00, 0x00, 0x31, // 1
174+
0x00, 0x00, 0x00, 0x2C, // ,
175+
0x00, 0x00, 0x00, 0x32, // 2
176+
0x00, 0x00, 0x00, 0x2C, // ,
177+
0x00, 0x00, 0x00, 0x33, // 3
178+
0x00, 0x00, 0x00, 0x0A, // newline
179+
0x00, 0x00, 0x00, 0xE1, // á
180+
0x00, 0x00, 0x00, 0x2C, // ,
181+
0x00, 0x00, 0x00, 0xE7, // ç
182+
0x00, 0x00, 0x00, 0x2C, // ,
183+
0x00, 0x00, 0x00, 0xE9 // é
184+
]
185+
186+
#expect([UInt8](data) == expectedBytes)
187+
}
188+
189+
@Test func data_utf32LE_withBOM() async throws {
190+
let sv = CSV(table: csvTable_Basic)
191+
192+
let data = try sv.data(encoding: .utf32LittleEndian, includeBOM: true)
193+
194+
let expectedBytes: [UInt8] = [
195+
0xFF, 0xFE, 0x00, 0x00, // UTF-32 little-endian BOM
196+
0x31, 0x00, 0x00, 0x00, // 1
197+
0x2C, 0x00, 0x00, 0x00, // ,
198+
0x32, 0x00, 0x00, 0x00, // 2
199+
0x2C, 0x00, 0x00, 0x00, // ,
200+
0x33, 0x00, 0x00, 0x00, // 3
201+
0x0A, 0x00, 0x00, 0x00, // newline
202+
0xE1, 0x00, 0x00, 0x00, // á
203+
0x2C, 0x00, 0x00, 0x00, // ,
204+
0xE7, 0x00, 0x00, 0x00, // ç
205+
0x2C, 0x00, 0x00, 0x00, // ,
206+
0xE9, 0x00, 0x00, 0x00 // é
207+
]
208+
209+
#expect([UInt8](data) == expectedBytes)
210+
}
211+
212+
@Test func data_utf32LE_withoutBOM() async throws {
213+
let sv = CSV(table: csvTable_Basic)
214+
215+
let data = try sv.data(encoding: .utf32LittleEndian, includeBOM: false)
216+
217+
let expectedBytes: [UInt8] = [
218+
0x31, 0x00, 0x00, 0x00, // 1
219+
0x2C, 0x00, 0x00, 0x00, // ,
220+
0x32, 0x00, 0x00, 0x00, // 2
221+
0x2C, 0x00, 0x00, 0x00, // ,
222+
0x33, 0x00, 0x00, 0x00, // 3
223+
0x0A, 0x00, 0x00, 0x00, // newline
224+
0xE1, 0x00, 0x00, 0x00, // á
225+
0x2C, 0x00, 0x00, 0x00, // ,
226+
0xE7, 0x00, 0x00, 0x00, // ç
227+
0x2C, 0x00, 0x00, 0x00, // ,
228+
0xE9, 0x00, 0x00, 0x00 // é
229+
]
230+
231+
#expect([UInt8](data) == expectedBytes)
232+
}
233+
234+
@Test(arguments: [true, false])
235+
func data_windows1252_withBOM(includeBOM: Bool) async throws {
236+
let sv = CSV(table: csvTable_Basic)
237+
238+
// CP1252 has no BOM, so passing `true` has no effect
239+
let data = try sv.data(encoding: .windowsCP1252, includeBOM: includeBOM)
240+
241+
let expectedBytes: [UInt8] = [
242+
0x31, 0x2C, 0x32, 0x2C, 0x33, 0x0A, // "1,2,3" + newline
243+
0xE1, // á
244+
0x2C, // ,
245+
0xE7, // ç
246+
0x2C, // ,
247+
0xE9 // é
248+
]
249+
250+
#expect([UInt8](data) == expectedBytes)
251+
}
252+
253+
@Test(arguments: [true, false])
254+
func data_macRoman_withBOM(includeBOM: Bool) async throws {
255+
let sv = CSV(table: csvTable_Basic)
256+
257+
// MacRoman has no BOM, so passing `true` has no effect
258+
let data = try sv.data(encoding: .macOSRoman, includeBOM: includeBOM)
259+
260+
let expectedBytes: [UInt8] = [
261+
0x31, 0x2C, 0x32, 0x2C, 0x33, 0x0A, // "1,2,3" + newline
262+
0x87, // á
263+
0x2C, // ,
264+
0x8D, // ç
265+
0x2C, // ,
266+
0x8E // é
267+
]
268+
269+
#expect([UInt8](data) == expectedBytes)
270+
}
271+
}

Tests/TextFileTests/Text Files/TSV Tests.swift renamed to Tests/TextFileTests/Text Files/TSV Decode Tests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
//
2-
// TSV Tests.swift
2+
// TSV Decode Tests.swift
33
// swift-textfile • https://github.com/orchetect/swift-textfile
44
// © 2018-2025 Steffan Andrews • Licensed under MIT License
55
//
66

77
import Testing
88
@testable import TextFile
99

10-
@Suite struct TSV_Tests {
10+
@Suite struct TSV_Decode_Tests {
1111
@Test func init_Default() {
1212
let sv = TSV()
1313

@@ -17,7 +17,7 @@ import Testing
1717

1818
// MARK: - Basic
1919

20-
@Suite struct TSV_Basic_Tests {
20+
@Suite struct TSV_Decode_Basic_Tests {
2121
private let tsvRawText_Basic = """
2222
header1 header2 header3
2323
1 2 3
@@ -47,7 +47,7 @@ import Testing
4747

4848
// MARK: - Single column
4949

50-
@Suite struct TSV_SingleColumn_Tests {
50+
@Suite struct TSV_Decode_SingleColumn_Tests {
5151
private let tsvRawText_SingleColumn = """
5252
header1
5353
1
@@ -77,7 +77,7 @@ import Testing
7777

7878
// MARK: - Quoted fields
7979

80-
@Suite struct TSV_QuotedFields_Tests {
80+
@Suite struct TSV_Decode_QuotedFields_Tests {
8181
private let tsvRawText_QuotedFields = #"""
8282
header1 "header 2" header3
8383
1 2 3 "quoted" here

0 commit comments

Comments
 (0)