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 nested attributed intrinsic #79

Merged
merged 2 commits into from Feb 23, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion Sources/XMLCoder/Auxiliaries/XMLCoderElement.swift
Expand Up @@ -44,6 +44,8 @@ struct XMLCoderElement: Equatable {
elements.append(element)
}

// FIXME: this should be split into separate functions and

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Todo Violation: FIXMEs should be resolved (this should be split into sepa...). (todo)

// thoroughtly tested
func flatten() -> KeyedBox {
let attributes = self.attributes.mapValues { StringBox($0) }

Expand Down Expand Up @@ -73,7 +75,7 @@ struct XMLCoderElement: Equatable {
case var unkeyedBox as UnkeyedBox:
unkeyedBox.append(content)
result[key] = unkeyedBox
case let box?:
case let box? where !hasValue:
result[key] = UnkeyedBox([box, content])
default:
result[key] = content
Expand Down
47 changes: 47 additions & 0 deletions Tests/XMLCoderTests/AttributedIntrinsicTest.swift
Expand Up @@ -52,6 +52,45 @@ private struct FooEmptyKeyed: Codable, DynamicNodeEncoding {
}
}

private let previewXML =
"""
<?xml version="1.0" encoding="UTF-8"?>
<app_preview display_target="iOS-6.5-in" position="1">
<preview_image_time format="24/999 1000/nonDrop">00:00:17:01</preview_image_time>
</app_preview>
""".data(using: .utf8)!

private struct AppPreview: Codable {
var displayTarget: String
var position: Int
var previewImageTime: PreviewImageTime

enum CodingKeys: String, CodingKey {
case displayTarget = "display_target"
case position
case previewImageTime = "preview_image_time"
}
}

private struct PreviewImageTime: Codable, DynamicNodeEncoding {
var format: String
var value: String

enum CodingKeys: String, CodingKey {
case format
case value
}

static func nodeEncoding(forKey key: CodingKey) -> XMLEncoder.NodeEncoding {
switch key {
case CodingKeys.format:
return .attribute
default:
return .element
}
}
}

final class AttributedIntrinsicTest: XCTestCase {
func testEncode() throws {
let encoder = XMLEncoder()
Expand Down Expand Up @@ -83,9 +122,17 @@ final class AttributedIntrinsicTest: XCTestCase {
XCTAssertEqual(foo2.unkeyedValue, 456)
}

func testDecodePreview() throws {
let decoder = XMLDecoder()

let preview = try decoder.decode(AppPreview.self, from: previewXML)
print(preview)
}

static var allTests = [
("testEncode", testEncode),
("testDecode", testDecode),
("testDecodePreview", testDecodePreview),
]
}

Expand Down