Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix RJITest RSS encoding and decoding #171

Merged
merged 2 commits into from
Apr 9, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 31 additions & 68 deletions Tests/XMLCoderTests/RJITest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]

Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
}