Skip to content

Commit

Permalink
Add TopLevelEncoder implementation (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxDesiatov authored Apr 13, 2020
1 parent d57b8f2 commit 9eb98ba
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
8 changes: 8 additions & 0 deletions Sources/XMLCoder/Decoder/XMLDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -374,10 +374,18 @@ open class XMLDecoder {

#if canImport(Combine)
import protocol Combine.TopLevelDecoder
import protocol Combine.TopLevelEncoder
#elseif canImport(OpenCombine)
import protocol OpenCombine.TopLevelDecoder
import protocol OpenCombine.TopLevelEncoder
#endif

#if canImport(Combine) || canImport(OpenCombine)
extension XMLDecoder: TopLevelDecoder {}

extension XMLEncoder: TopLevelEncoder {
public func encode<T>(_ value: T) throws -> Data where T: Encodable {
try encode(value, withRootKey: nil, rootAttributes: nil, header: nil)
}
}
#endif
21 changes: 17 additions & 4 deletions Tests/XMLCoderTests/CombineTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,33 @@ private let xml = """
</foo>
""".data(using: .utf8)!

private struct Foo: Decodable {
private struct Foo: Codable {
var name: String
}

@available(iOS 13.0, macOS 10.15.0, tvOS 13.0, watchOS 6.0, *)
final class CombineTests: XCTestCase {
func testDecodeFromXMLDecoder() {
let data = Just(xml)
func testDecode() {
var foo: Foo?
_ = data.decode(type: Foo.self, decoder: XMLDecoder()).sink(
_ = Just(xml).decode(type: Foo.self, decoder: XMLDecoder()).sink(
receiveCompletion: { _ in },
receiveValue: { foo = $0 }
)
XCTAssertEqual(foo?.name, "Foo")
}

func testEncode() {
var foo: Foo?
_ = Just(Foo(name: "Foo"))
.encode(encoder: XMLEncoder())
.decode(type: Foo.self, decoder: XMLDecoder())
.sink(
receiveCompletion: { _ in },
receiveValue: {
foo = $0
}
)
XCTAssertEqual(foo?.name, "Foo")
}
}
#endif
10 changes: 10 additions & 0 deletions Tests/XMLCoderTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,15 @@ extension RootLevelAttributeTest {
]
}

extension RootLevetExtraAttributesTests {
// DO NOT MODIFY: This is autogenerated, use:
// `swift test --generate-linuxmain`
// to regenerate.
static let __allTests__RootLevetExtraAttributesTests = [
("testExtraAttributes", testExtraAttributes),
]
}

extension SharedBoxTests {
// DO NOT MODIFY: This is autogenerated, use:
// `swift test --generate-linuxmain`
Expand Down Expand Up @@ -893,6 +902,7 @@ public func __allTests() -> [XCTestCaseEntry] {
testCase(RJITest.__allTests__RJITest),
testCase(RelationshipsTest.__allTests__RelationshipsTest),
testCase(RootLevelAttributeTest.__allTests__RootLevelAttributeTest),
testCase(RootLevetExtraAttributesTests.__allTests__RootLevetExtraAttributesTests),
testCase(SharedBoxTests.__allTests__SharedBoxTests),
testCase(SimpleChoiceTests.__allTests__SimpleChoiceTests),
testCase(SingleChildTest.__allTests__SingleChildTest),
Expand Down

0 comments on commit 9eb98ba

Please sign in to comment.