Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix a bug with throws on
Encodable
encoding nothing (#31)
By encoding via `func encode(_:withRootKey:header:)` we always get an XML root node with label `rootKey`, so not encoding anything within `func encode(to:)` is fine in XML (unlike JSON). * Fixed a bug that would cause `XMLEncoder` to throw on `Encodable`s encoding nothing * Fix merge conflict * Fix indentation and xcodeproj file
- Loading branch information
1 parent
1f0cb9d
commit 099272e
Showing
3 changed files
with
68 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// | ||
// EmptyTests.swift | ||
// XMLCoderTests | ||
// | ||
// Created by Vincent Esche on 12/19/18. | ||
// | ||
|
||
import XCTest | ||
@testable import XMLCoder | ||
|
||
class EmptyTests: XCTestCase { | ||
struct Container: Codable, Equatable { | ||
// empty | ||
|
||
func encode(to encoder: Encoder) throws { | ||
// do nothing | ||
} | ||
} | ||
|
||
func testAttribute() throws { | ||
let decoder = XMLDecoder() | ||
let encoder = XMLEncoder() | ||
|
||
encoder.nodeEncodingStrategy = .custom { codableType, _ in | ||
return { _ in .attribute } | ||
} | ||
|
||
XCTAssertThrowsError(try decoder.decode(Container.self, from: Data())) | ||
|
||
let encoded = try encoder.encode(Container(), withRootKey: "container") | ||
XCTAssertEqual(String(data: encoded, encoding: .utf8)!, "<container />") | ||
} | ||
|
||
func testElement() throws { | ||
let decoder = XMLDecoder() | ||
let encoder = XMLEncoder() | ||
|
||
encoder.outputFormatting = [.prettyPrinted] | ||
|
||
XCTAssertThrowsError(try decoder.decode(Container.self, from: Data())) | ||
|
||
let encoded = try encoder.encode(Container(), withRootKey: "container") | ||
XCTAssertEqual(String(data: encoded, encoding: .utf8)!, "<container />") | ||
} | ||
|
||
static var allTests = [ | ||
("testAttribute", testAttribute), | ||
("testElement", testElement), | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters