diff --git a/Tests/CSVTests/Data.swift b/Tests/CSVTests/Data.swift new file mode 100644 index 0000000..26eec88 --- /dev/null +++ b/Tests/CSVTests/Data.swift @@ -0,0 +1,59 @@ +let orderedData: OrderedKeyedCollection = [ + "first name": ["Caleb", "Benjamin", "Doc", "Grace", "Anne", "TinTin"], + "last_name": ["Kleveter", "Franklin", "Holliday", "Hopper", "Shirley", nil], + "age": ["18", "269", "174", "119", "141", "16"], + "gender": ["M", "M", "M", "F", "F", "M"], + "tagLine": [ + "😜", "A penny saved is a penny earned", "Bang", nil, + "God's in His heaven,\nall's right with the world", "Great snakes!" + ] +] + +let orderedChunks: [OrderedKeyedCollection>] = [ + ["first name": ["Caleb"], "last_name": ["Kleveter"], "age": ["18"], "gender": ["M"], "tagLine": ["😜"]], + [ + "first name": ["Benjamin"], "last_name": ["Franklin"], "age": ["269"], "gender": ["M"], + "tagLine": ["A penny saved is a penny earned"] + ], + ["first name": ["Doc"], "last_name": ["Holliday"], "age": ["174"], "gender": ["M"], "tagLine": ["Bang"]], + ["first name": ["Grace"], "last_name": ["Hopper"], "age": ["119"], "gender": ["F"], "tagLine": [nil]], + [ + "first name": ["Anne"], "last_name": ["Shirley"], "age": ["141"], "gender": ["F"], + "tagLine": ["God's in His heaven,\nall's right with the world"] + ], + ["first name": ["TinTin"], "last_name": [nil], "age": ["16"], "gender": ["M"], "tagLine": ["Great snakes!"]] +] + +let chunks: [String] = [ + "first name,last_name,age", + ",gender,tagLine\nCaleb,Kleveter,18,M,", + "😜\r\nBenjamin,Franklin,269,M,A penny saved is a ", + "penny earned\n\"", + #"Doc","Holliday","174","M",Bang\#r\#n"#, + "Grace,Hopper,119,F,", + #"\#nAnne,Shirley,141,F,"God's in His heaven,\#n"#, + #"all's right with the world""#, + "\nTinTin,,16,M,Great snakes!" +] + +let data = """ +first name,last_name,age,gender,tagLine +Caleb,Kleveter,18,M,😜\r +Benjamin,Franklin,269,M,A penny saved is a penny earned +"Doc","Holliday","174","M",Bang\r +Grace,Hopper,119,F, +Anne,Shirley,141,F,"God's in His heaven, +all's right with the world" +TinTin,,16,M,Great snakes! +""" + +let expected = """ +"first name","last_name","age","gender","tagLine" +"Caleb","Kleveter","18","M","😜" +"Benjamin","Franklin","269","M","A penny saved is a penny earned" +"Doc","Holliday","174","M","Bang" +"Grace","Hopper","119","F","" +"Anne","Shirley","141","F","God's in His heaven, +all's right with the world" +"TinTin","","16","M","Great snakes!" +""" diff --git a/Tests/CSVTests/DecoderTests.swift b/Tests/CSVTests/DecoderTests.swift index 1d5f190..f3a4a6f 100644 --- a/Tests/CSVTests/DecoderTests.swift +++ b/Tests/CSVTests/DecoderTests.swift @@ -123,26 +123,3 @@ fileprivate struct Person: Codable, Equatable { case tagLine } } - -fileprivate let data = """ -first name,last_name,age,gender,tagLine -Caleb,Kleveter,18,M,😜\r -Benjamin,Franklin,269,M,A penny saved is a penny earned -"Doc","Holliday","174","M",Bang\r -Grace,Hopper,119,F, -Anne,Shirley,141,F,"God's in His heaven, -all's right with the world" -TinTin,,16,M,Great snakes! -""" - -fileprivate let chunks: [String] = [ - "first name,last_name,age", - ",gender,tagLine\nCaleb,Kleveter,18,M,", - "😜\r\nBenjamin,Franklin,269,M,A penny saved is a ", - "penny earned\n\"", - #"Doc","Holliday","174","M",Bang\#r\#n"#, - "Grace,Hopper,119,F,", - #"\#nAnne,Shirley,141,F,"God's in His heaven,\#n"#, - #"all's right with the world""#, - "\nTinTin,,16,M,Great snakes!" -] diff --git a/Tests/CSVTests/EncoderTests.swift b/Tests/CSVTests/EncoderTests.swift index 27a2130..ed176cb 100644 --- a/Tests/CSVTests/EncoderTests.swift +++ b/Tests/CSVTests/EncoderTests.swift @@ -35,8 +35,8 @@ final class EncoderTests: XCTestCase { func testSyncEncode() throws { let encoder = CSVEncoder().sync - let data = try encoder.encode(people) - let string = String(decoding: data, as: UTF8.self) + let encoded = try encoder.encode(people) + let string = String(decoding: encoded, as: UTF8.self) XCTAssertEqual(string, expected) } @@ -93,14 +93,3 @@ fileprivate let people = [ ), Person(firstName: "TinTin", lastName: nil, age: 16, gender: .male, tagLine: "Great snakes!") ] - -fileprivate let expected = """ -"first name","last_name","age","gender","tagLine" -"Caleb","Kleveter","18","M","😜" -"Benjamin","Franklin","269","M","A penny saved is a penny earned" -"Doc","Holliday","174","M","Bang" -"Grace","Hopper","119","F","" -"Anne","Shirley","141","F","God's in His heaven, -all's right with the world" -"TinTin","","16","M","Great snakes!" -""" diff --git a/Tests/CSVTests/ParserTests.swift b/Tests/CSVTests/ParserTests.swift index f4cf741..6643b30 100644 --- a/Tests/CSVTests/ParserTests.swift +++ b/Tests/CSVTests/ParserTests.swift @@ -110,26 +110,3 @@ final class ParserTests: XCTestCase { } } } - -fileprivate let data = """ -first name,last_name,age,gender,tagLine -Caleb,Kleveter,18,M,😜\r -Benjamin,Franklin,269,M,A penny saved is a penny earned -"Doc","Holliday","174","M",Bang\r -Grace,Hopper,119,F, -Anne,Shirley,141,F,"God's in His heaven, -all's right with the world" -TinTin,,16,M,Great snakes! -""" - -fileprivate let chunks: [String] = [ - "first name,last_name,age", - ",gender,tagLine\nCaleb,Kleveter,18,M,", - "😜\r\nBenjamin,Franklin,269,M,A penny saved is a ", - "penny earned\n\"", - #"Doc","Holliday","174","M",Bang\#r\#n"#, - "Grace,Hopper,119,F,", - #"\#nAnne,Shirley,141,F,"God's in His heaven,\#n"#, - #"all's right with the world""#, - "\nTinTin,,16,M,Great snakes!" -] diff --git a/Tests/CSVTests/SerializerTests.swift b/Tests/CSVTests/SerializerTests.swift index 12bb204..a6f129e 100644 --- a/Tests/CSVTests/SerializerTests.swift +++ b/Tests/CSVTests/SerializerTests.swift @@ -16,7 +16,7 @@ final class SerializerTests: XCTestCase { // 6.679 measure { for _ in 0..<100_000 { - _ = serializer.serialize(data) + _ = serializer.serialize(orderedData) } } } @@ -38,75 +38,12 @@ final class SerializerTests: XCTestCase { // 5.896 measure { for _ in 0..<100_000 { - chunks.forEach { chunk in serializer.serialize(chunk) } + orderedChunks.forEach { chunk in serializer.serialize(chunk) } } } } } -fileprivate let orderedData: OrderedKeyedCollection = [ - "first name": ["Caleb", "Benjamin", "Doc", "Grace", "Anne", "TinTin"], - "last_name": ["Kleveter", "Franklin", "Holliday", "Hopper", "Shirley", nil], - "age": ["18", "269", "174", "119", "141", "16"], - "gender": ["M", "M", "M", "F", "F", "M"], - "tagLine": [ - "😜", "A penny saved is a penny earned", "Bang", nil, - "God's in His heaven,\nall's right with the world", "Great snakes!" - ] -] - -fileprivate let data = [ - "first name": ["Caleb", "Benjamin", "Doc", "Grace", "Anne", "TinTin"], - "last_name": ["Kleveter", "Franklin", "Holliday", "Hopper", "Shirley", nil], - "age": ["18", "269", "174", "119", "141", "16"], - "gender": ["M", "M", "M", "F", "F", "M"], - "tagLine": [ - "😜", "A penny saved is a penny earned", "Bang", nil, - "God's in His heaven,\nall's right with the world", "Great snakes!" - ] -] - -fileprivate let orderedChunks: [OrderedKeyedCollection>] = [ - ["first name": ["Caleb"], "last_name": ["Kleveter"], "age": ["18"], "gender": ["M"], "tagLine": ["😜"]], - [ - "first name": ["Benjamin"], "last_name": ["Franklin"], "age": ["269"], "gender": ["M"], - "tagLine": ["A penny saved is a penny earned"] - ], - ["first name": ["Doc"], "last_name": ["Holliday"], "age": ["174"], "gender": ["M"], "tagLine": ["Bang"]], - ["first name": ["Grace"], "last_name": ["Hopper"], "age": ["119"], "gender": ["F"], "tagLine": [nil]], - [ - "first name": ["Anne"], "last_name": ["Shirley"], "age": ["141"], "gender": ["F"], - "tagLine": ["God's in His heaven,\nall's right with the world"] - ], - ["first name": ["TinTin"], "last_name": [nil], "age": ["16"], "gender": ["M"], "tagLine": ["Great snakes!"]] -] - -fileprivate let chunks = [ - ["first name": ["Caleb"], "last_name": ["Kleveter"], "age": ["18"], "gender": ["M"], "tagLine": ["😜"]], - [ - "first name": ["Benjamin"], "last_name": ["Franklin"], "age": ["269"], "gender": ["M"], - "tagLine": ["A penny saved is a penny earned"] - ], - ["first name": ["Doc"], "last_name": ["Holliday"], "age": ["174"], "gender": ["M"], "tagLine": ["Bang"]], - ["first name": ["Grace"], "last_name": ["Hopper"], "age": ["119"], "gender": ["F"], "tagLine": [nil]], - [ - "first name": ["Anne"], "last_name": ["Shirley"], "age": ["141"], "gender": ["F"], - "tagLine": ["God's in His heaven,\nall's right with the world"] - ], - ["first name": ["TinTin"], "last_name": [nil], "age": ["16"], "gender": ["M"], "tagLine": ["Great snakes!"]] -] - -fileprivate let expected = """ -"first name","last_name","age","gender","tagLine" -"Caleb","Kleveter","18","M","😜" -"Benjamin","Franklin","269","M","A penny saved is a penny earned" -"Doc","Holliday","174","M","Bang" -"Grace","Hopper","119","F","" -"Anne","Shirley","141","F","God's in His heaven, -all's right with the world" -"TinTin","","16","M","Great snakes!" -""" - internal struct OrderedKeyedCollection: KeyedCollection, ExpressibleByDictionaryLiteral where K: Hashable { typealias Index = Int typealias Element = (key: Key, value: Value)