|
| 1 | +import XCTest |
| 2 | +import CodableCSV |
| 3 | + |
| 4 | +/// Tests checking the regular encoding usage. |
| 5 | +final class EncodingOptionalsTests: XCTestCase { |
| 6 | + override func setUp() { |
| 7 | + self.continueAfterFailure = false |
| 8 | + } |
| 9 | +} |
| 10 | + |
| 11 | +extension EncodingOptionalsTests { |
| 12 | + /// Tests writting `nil` values on a keyed containers with named coding keys. |
| 13 | + func testOptionalNamedFields() throws { |
| 14 | + struct Student: Encodable { |
| 15 | + let name: String, age: Int?, country: String?, hasPet: Bool? |
| 16 | + } |
| 17 | + |
| 18 | + let students: [Student] = [ |
| 19 | + .init(name: "Marcos", age: 1, country: "Spain", hasPet: true), |
| 20 | + .init(name: "Anaïs", age: nil, country: "France", hasPet: false), |
| 21 | + .init(name: "Alex", age: 3, country: nil, hasPet: false), |
| 22 | + .init(name: "家豪", age: nil, country: "China", hasPet: nil), |
| 23 | + .init(name: "Дэниел", age: 5, country: nil, hasPet: nil), |
| 24 | + .init(name: "ももこ", age: nil, country: nil, hasPet: nil) |
| 25 | + ] |
| 26 | + |
| 27 | + let encoder = CSVEncoder { $0.headers = ["name", "age", "country", "hasPet"] } |
| 28 | + let result = try encoder.encode(students, into: String.self) |
| 29 | + XCTAssertFalse(result.isEmpty) |
| 30 | + } |
| 31 | + |
| 32 | + /// Tests writting `nil` values on a keyed containers with integer coding keys. |
| 33 | + func testOptionalIntegerFields() throws { |
| 34 | + struct Student: Encodable { |
| 35 | + let name: String, age: Int?, country: String?, hasPet: Bool? |
| 36 | + private enum CodingKeys: Int, CodingKey { |
| 37 | + case name=0, age, country, hasPet |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + let students: [Student] = [ |
| 42 | + .init(name: "Marcos", age: 1, country: "Spain", hasPet: true), |
| 43 | + .init(name: "Anaïs", age: nil, country: "France", hasPet: false), |
| 44 | + .init(name: "Alex", age: 3, country: nil, hasPet: false), |
| 45 | + .init(name: "家豪", age: nil, country: "China", hasPet: nil), |
| 46 | + .init(name: "Дэниел", age: 5, country: nil, hasPet: nil), |
| 47 | + .init(name: "ももこ", age: nil, country: nil, hasPet: nil) |
| 48 | + ] |
| 49 | + |
| 50 | + let encoder = CSVEncoder() |
| 51 | + let result = try encoder.encode(students, into: String.self) |
| 52 | + XCTAssertFalse(result.isEmpty) |
| 53 | + XCTAssertTrue(result.hasPrefix("Marcos,1,Spain,true\nAnaïs,,France,false")) |
| 54 | + } |
| 55 | + |
| 56 | + /// Tests writting `nil` values on a keyed containers with named coding keys. |
| 57 | + func testOptionalNamedFieldsWithCustomStrategy() throws { |
| 58 | + struct Student: Encodable { |
| 59 | + let name: String, age: Int?, country: String?, hasPet: Bool? |
| 60 | + } |
| 61 | + |
| 62 | + let students: [Student] = [ |
| 63 | + .init(name: "Marcos", age: 1, country: "Spain", hasPet: true), |
| 64 | + .init(name: "Anaïs", age: nil, country: "France", hasPet: false), |
| 65 | + .init(name: "Alex", age: 3, country: nil, hasPet: false), |
| 66 | + .init(name: "家豪", age: nil, country: "China", hasPet: nil), |
| 67 | + .init(name: "Дэниел", age: 5, country: nil, hasPet: nil), |
| 68 | + .init(name: "ももこ", age: nil, country: nil, hasPet: nil) |
| 69 | + ] |
| 70 | + |
| 71 | + let encoder = CSVEncoder { |
| 72 | + $0.headers = ["name", "age", "country", "hasPet"] |
| 73 | + $0.nilStrategy = .custom({ |
| 74 | + var container = $0.singleValueContainer() |
| 75 | + try container.encode("null") |
| 76 | + }) |
| 77 | + } |
| 78 | + let result = try encoder.encode(students, into: String.self) |
| 79 | + XCTAssertFalse(result.isEmpty) |
| 80 | + XCTAssertTrue(result.contains("null")) |
| 81 | + } |
| 82 | +} |
0 commit comments