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

Add more cases to AttributedIntrinsicTest #95

Merged
merged 3 commits into from May 1, 2019
Merged
Changes from all commits
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
54 changes: 54 additions & 0 deletions Tests/XMLCoderTests/AttributedIntrinsicTest.swift
Expand Up @@ -40,6 +40,42 @@ let fooValueXML = """
</container>
""".data(using: .utf8)!

let fooValueAttributeXML = """
<foo value="blah">456</foo>
""".data(using: .utf8)!

let fooValueElementXML = """
<foo><value>blah</value></foo>
""".data(using: .utf8)!

private struct FooValueAttribute: Codable, DynamicNodeDecoding {
let valueAttribute: String
let value: Int

enum CodingKeys: String, CodingKey {
case valueAttribute = "value"
case value = ""
}

static func nodeDecoding(for key: CodingKey) -> XMLDecoder.NodeDecoding {
guard key.stringValue == CodingKeys.valueAttribute.stringValue else {
return .element
}

return .attribute
}
}

private struct FooValueElement: Codable {
let valueElement: String
let value: Int?

enum CodingKeys: String, CodingKey {
case valueElement = "value"
case value = ""
}
}

private struct Foo: Codable, DynamicNodeEncoding, Equatable {
let id: String
let value: String
Expand Down Expand Up @@ -247,6 +283,24 @@ final class AttributedIntrinsicTest: XCTestCase {
]))
}

func testFooValueAttribute() throws {
let foo = try XMLDecoder().decode(
FooValueAttribute.self,
from: fooValueAttributeXML
)
XCTAssertEqual(foo.valueAttribute, "blah")
XCTAssertEqual(foo.value, 456)
}

func testFooValueElement() throws {
let foo = try XMLDecoder().decode(
FooValueElement.self,
from: fooValueElementXML
)
XCTAssertEqual(foo.valueElement, "blah")
XCTAssertNil(foo.value)
}

static var allTests = [
("testEncode", testEncode),
("testDecode", testDecode),
Expand Down