Skip to content

Commit

Permalink
Add more cases to AttributedIntrinsicTest (CoreOffice#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxDesiatov authored and Arjun Gupta committed Jun 26, 2020
1 parent 9bffdf1 commit 621da69
Showing 1 changed file with 54 additions and 0 deletions.
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

0 comments on commit 621da69

Please sign in to comment.