forked from CoreOffice/XMLCoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
XMLElementTests.swift
133 lines (115 loc) · 5.45 KB
/
XMLElementTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright (c) 2018-2020 XMLCoder contributors
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
//
// Created by Vincent Esche on 12/24/18.
//
import XCTest
@testable import XMLCoder
class XMLElementTests: XCTestCase {
func testInitNull() {
let null = XMLCoderElement(key: "foo")
XCTAssertEqual(null.key, "foo")
XCTAssertNil(null.stringValue)
XCTAssertEqual(null.elements, [])
XCTAssertEqual(null.attributes, [])
}
func testInitUnkeyed() {
let keyed = XMLCoderElement(key: "foo", isStringBoxCDATA: false, box: UnkeyedBox())
XCTAssertEqual(keyed.key, "foo")
XCTAssertNil(keyed.stringValue)
XCTAssertEqual(keyed.elements, [])
XCTAssertEqual(keyed.attributes, [])
}
func testInitKeyed() {
let keyed = XMLCoderElement(key: "foo", isStringBoxCDATA: false, box: KeyedBox(
elements: [] as [(String, Box)],
attributes: [("baz", NullBox()), ("blee", IntBox(42))] as [(String, SimpleBox)]
))
XCTAssertEqual(keyed.key, "foo")
XCTAssertNil(keyed.stringValue)
XCTAssertEqual(keyed.elements, [])
XCTAssertEqual(keyed.attributes, [XMLCoderElement.Attribute(key: "blee", value: "42")])
}
func testInitSimple() {
let keyed = XMLCoderElement(key: "foo", isStringBoxCDATA: false, box: StringBox("bar"))
let element = XMLCoderElement(stringValue: "bar")
XCTAssertEqual(keyed.key, "foo")
XCTAssertNil(keyed.stringValue)
XCTAssertEqual(keyed.elements, [element])
XCTAssertEqual(keyed.attributes, [])
}
func testWhitespaceWithNoElements_keyed() {
let keyed = XMLCoderElement(key: "foo", isStringBoxCDATA: false, box: StringBox("bar"))
XCTAssertFalse(keyed.isWhitespaceWithNoElements())
}
func testWhitespaceWithNoElements_whitespace() {
let whitespaceElement1 = XMLCoderElement(stringValue: "\n ")
let whitespaceElement2 = XMLCoderElement(stringValue: "\n")
let whitespaceElement3 = XMLCoderElement(stringValue: " ")
XCTAssert(whitespaceElement1.isWhitespaceWithNoElements())
XCTAssert(whitespaceElement2.isWhitespaceWithNoElements())
XCTAssert(whitespaceElement3.isWhitespaceWithNoElements())
}
func testNestedElementWith_Namespace_Attribute() {
typealias Attribute = XMLCoderElement.Attribute
typealias Element = XMLCoderElement
let nested = Element(key: "Nested",
elements: [
Element(key: "",
elements: [],
attributes: [
Attribute(key: "xsi:someName", value: "nestedAttrValue")
]
)],
attributes: [
Attribute(key: "xmlns:xsi", value: "https://example.com")
])
let inputNamespace = Attribute(key: "xmlns", value: "https://example.com")
let input = Element(key: "Input",
elements: [nested],
attributes: [inputNamespace])
let result = input.toXMLString(
escapedCharacters: (elements: XMLEncoder().charactersEscapedInElements,
attributes: XMLEncoder().charactersEscapedInAttributes),
formatting: [],
indentation: .spaces(4)
)
XCTAssertEqual(result, """
<Input xmlns="https://example.com"><Nested xmlns:xsi="https://example.com" xsi:someName="nestedAttrValue"></Nested></Input>
""")
}
func testNestedElementWith_Namespace_Attribute_Element() {
typealias Attribute = XMLCoderElement.Attribute
typealias Element = XMLCoderElement
let nested = Element(key: "Nested",
elements: [
Element(key: "",
elements: [
Element(key: "nonAttrField",
elements: [Element(key: "", stringValue: "hello")],
attributes: [])
],
attributes: [
Attribute(key: "xsi:someName", value: "nestedAttrValue")
]
)],
attributes: [
Attribute(key: "xmlns:xsi", value: "https://example.com")
])
let inputNamespace = Attribute(key: "xmlns", value: "https://example.com")
let input = Element(key: "Input",
elements: [nested],
attributes: [inputNamespace])
let result = input.toXMLString(
escapedCharacters: (elements: XMLEncoder().charactersEscapedInElements,
attributes: XMLEncoder().charactersEscapedInAttributes),
formatting: [],
indentation: .spaces(4)
)
XCTAssertEqual(result, """
<Input xmlns="https://example.com"><Nested xmlns:xsi="https://example.com" xsi:someName="nestedAttrValue"><nonAttrField>hello</nonAttrField></Nested></Input>
""")
}
}