diff --git a/Tests/XMLCoderTests/RJITest.swift b/Tests/XMLCoderTests/RJITest.swift index f86d317d..535fa8fd 100644 --- a/Tests/XMLCoderTests/RJITest.swift +++ b/Tests/XMLCoderTests/RJITest.swift @@ -28,6 +28,14 @@ private struct RSS: Codable, Equatable { } } +private struct GeneratorAgent: Codable, Equatable { + let resource: URL + + enum CodingKeys: String, CodingKey { + case resource = "rdf:resource" + } +} + private struct Channel: Codable, Equatable { let title: String let link: URL @@ -36,7 +44,7 @@ private struct Channel: Codable, Equatable { let creator: String let rights: String let date: Date - let generatorAgentResource: URL + let generatorAgent: GeneratorAgent let image: Image let items: [Item] @@ -47,51 +55,9 @@ private struct Channel: Codable, Equatable { case creator = "dc:creator" case rights = "dc:rights" case date = "dc:date" - case generatorAgentResource = "admin:generatorAgent" + case generatorAgent = "admin:generatorAgent" case items = "item" } - - enum GeneratorAgentKeys: String, CodingKey { - case resource = "rdf:resource" - } - - init(title: String, link: URL, - description: String, - language: String, - creator: String, - rights: String, - date: Date, - generatorAgentResource: URL, - image: Image, - items: [Item]) { - self.title = title - self.link = link - self.description = description - self.language = language - self.creator = creator - self.rights = rights - self.date = date - self.generatorAgentResource = generatorAgentResource - self.image = image - self.items = items - } - - init(from decoder: Decoder) throws { - let values = try decoder.container(keyedBy: CodingKeys.self) - title = try values.decode(String.self, forKey: .title) - link = try values.decode(URL.self, forKey: .link) - description = try values.decode(String.self, forKey: .description) - language = try values.decode(String.self, forKey: .language) - creator = try values.decode(String.self, forKey: .creator) - rights = try values.decode(String.self, forKey: .rights) - date = try values.decode(Date.self, forKey: .date) - - let generatorAgentValues = try values.nestedContainer(keyedBy: GeneratorAgentKeys.self, forKey: .generatorAgentResource) - generatorAgentResource = try generatorAgentValues.decode(URL.self, forKey: .resource) - - image = try values.decode(Image.self, forKey: .image) - items = try values.decode([Item].self, forKey: .items) - } } private struct Image: Codable, Equatable { @@ -119,19 +85,6 @@ private struct Item: Codable, Equatable { case date = "dc:date" case author } - - init(from decoder: Decoder) throws { - let values = try decoder.container(keyedBy: CodingKeys.self) - - title = try values.decode(String.self, forKey: .title) - link = try values.decode(URL.self, forKey: .link) - guid = try values.decode(URL.self, forKey: .guid) - enclosure = try values.decodeIfPresent(Enclosure.self, forKey: .enclosure) - description = try values.decode(String.self, forKey: .description) - subject = try values.decode(String.self, forKey: .subject) - date = try values.decode(Date.self, forKey: .date) - author = try values.decodeIfPresent(String.self, forKey: .author) - } } private struct Enclosure: Codable, Equatable { @@ -143,20 +96,30 @@ private struct Enclosure: Codable, Equatable { class RJITest: XCTestCase { func testRSS() throws { let decoder = XMLDecoder() -// let encoder = XMLEncoder() + let encoder = XMLEncoder() let dateFormatter = DateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ" decoder.dateDecodingStrategy = .formatted(dateFormatter) - -// let rss1 - _ = try decoder.decode(RSS.self, from: rjiSampleXML) - // this is a very peculiar case of `XMLCoder` not being able to decode an XML - // that it itself encoded -// data = try encoder.encode(rss1, withRootKey: "note", -// header: XMLHeader(version: 1.0, -// encoding: "UTF-8")) -// let rss2 = try decoder.decode(RSS.self, from: data) -// XCTAssertEqual(rss1, rss2) + decoder.trimValueWhitespaces = false + encoder.dateEncodingStrategy = .formatted(dateFormatter) + + let rss1 = try decoder.decode(RSS.self, from: rjiSampleXML) + let data = try encoder.encode(rss1, withRootKey: "note", + header: XMLHeader(version: 1.0, + encoding: "UTF-8")) + + let rss2 = try decoder.decode(RSS.self, from: data) + XCTAssertEqual(rss1.channel.items.count, 100) + XCTAssertEqual(rss2.channel.items.count, 100) + + for (i1, i2) in zip(rss1.channel.items, rss2.channel.items) { + guard i1 == i2 else { + XCTFail("items in \(#function) aren't equal") + continue + } + } + + XCTAssertEqual(rss1, rss2) } }