forked from CoreOffice/XMLCoder
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test root level attribute encoding (CoreOffice#134)
Resolves CoreOffice#127. I think the plist encoding/decoding should be independent of the XML encoding phase. I have tried to reproduce just the relevant encoding phase and it seems to work.
- Loading branch information
1 parent
d5c42cd
commit ac32e6b
Showing
2 changed files
with
49 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// | ||
// RootLevelAttributeTest.swift | ||
// XMLCoderTests | ||
// | ||
// Created by Benjamin Wetherfield on 9/28/19. | ||
// | ||
|
||
import Foundation | ||
import XCTest | ||
@testable import XMLCoder | ||
|
||
private struct Policy: Encodable, DynamicNodeEncoding { | ||
var name: String | ||
var initial: String | ||
|
||
enum CodingKeys: String, CodingKey { | ||
case name, initial | ||
} | ||
|
||
public static func nodeEncoding(for key: CodingKey) -> XMLEncoder.NodeEncoding { | ||
switch key { | ||
case Policy.CodingKeys.name: return .attribute | ||
default: return .element | ||
} | ||
} | ||
} | ||
|
||
let expected = """ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<policy name="generic"> | ||
<initial>more xml here</initial> | ||
</policy> | ||
""" | ||
|
||
class RootLevelAttributeTest: XCTestCase { | ||
func testPolicyEncodingAtRoot() throws { | ||
let encoder = XMLEncoder() | ||
encoder.outputFormatting = [.prettyPrinted] | ||
let policy = Policy(name: "generic", initial: "more xml here") | ||
let data = try encoder.encode(policy, | ||
withRootKey: "policy", | ||
header: XMLHeader(version: 1.0, encoding: "UTF-8")) | ||
XCTAssertEqual(String(data: data, encoding: .utf8)!, expected) | ||
} | ||
} |
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