-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
98 additions
and
8 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,61 @@ | ||
// | ||
// RootLevetExtraAttributesTests.swift | ||
// XMLCoderTests | ||
// | ||
// Created by Luís Portela Afonso on 19/03/2020. | ||
// | ||
|
||
import XCTest | ||
@testable import XMLCoder | ||
|
||
class RootLevetExtraAttributesTests: XCTestCase { | ||
|
||
private let encoder = XMLEncoder() | ||
|
||
func testExtraAttributes() { | ||
let policy = Policy(name: "test", initial: "extra root attributes") | ||
|
||
let extraRootAttributes = [ | ||
"xmlns" : "http://www.nrf-arts.org/IXRetail/namespace", | ||
"xmlns:xsd" : "http://www.w3.org/2001/XMLSchema", | ||
"xmlns:xsi" : "http://www.w3.org/2001/XMLSchema-instance" | ||
] | ||
|
||
encoder.keyEncodingStrategy = .lowercased | ||
encoder.outputFormatting = [.prettyPrinted, .sortedKeys] | ||
|
||
do { | ||
let data = try encoder.encode(policy, | ||
rootAttributes: extraRootAttributes) | ||
|
||
let dataString = String(data: data, encoding: .utf8) | ||
XCTAssertNotNil(dataString, "failed to encode object") | ||
|
||
let expected = """ | ||
<policy name="test" xmlns="http://www.nrf-arts.org/IXRetail/namespace" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | ||
<initial>extra root attributes</initial> | ||
</policy> | ||
""" | ||
|
||
XCTAssertEqual(dataString!, expected, "") | ||
} catch { | ||
XCTAssertThrowsError(error) | ||
} | ||
} | ||
} | ||
|
||
private struct Policy: Encodable, DynamicNodeEncoding { | ||
var name: String | ||
var initial: String | ||
|
||
enum CodingKeys: String, CodingKey { | ||
case name, initial//, rule | ||
} | ||
|
||
static func nodeEncoding(for key: CodingKey) -> XMLEncoder.NodeEncoding { | ||
switch key { | ||
case Policy.CodingKeys.name: return .attribute | ||
default: return .element | ||
} | ||
} | ||
} |
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