Skip to content

Commit bfa51f0

Browse files
committed
Initial commit
0 parents  commit bfa51f0

File tree

5 files changed

+168
-0
lines changed

5 files changed

+168
-0
lines changed

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.DS_Store
2+
/.build
3+
/.swiftpm
4+
/Packages
5+
/*.xcodeproj
6+
/Package.resolved

LICENSE

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
This is free and unencumbered software released into the public domain.
2+
3+
Anyone is free to copy, modify, publish, use, compile, sell, or
4+
distribute this software, either in source code form or as a compiled
5+
binary, for any purpose, commercial or non-commercial, and by any
6+
means.
7+
8+
In jurisdictions that recognize copyright laws, the author or authors
9+
of this software dedicate any and all copyright interest in the
10+
software to the public domain. We make this dedication for the benefit
11+
of the public at large and to the detriment of our heirs and
12+
successors. We intend this dedication to be an overt act of
13+
relinquishment in perpetuity of all present and future rights to this
14+
software under copyright law.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19+
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22+
OTHER DEALINGS IN THE SOFTWARE.
23+
24+
For more information, please refer to <http://unlicense.org>

Package.swift

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// swift-tools-version:4.0
2+
import PackageDescription
3+
4+
let package = Package(
5+
name: "XML",
6+
products: [
7+
.library(name: "XML", targets: ["XML"]),
8+
],
9+
dependencies: [
10+
.package(
11+
url: "https://github.com/swift-stack/test.git",
12+
.branch("master"))
13+
],
14+
targets: [
15+
.target(
16+
name: "XML",
17+
dependencies: []),
18+
.testTarget(
19+
name: "XMLTests",
20+
dependencies: ["XML", "Test"]),
21+
]
22+
)

Sources/XML/XML.swift

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
public struct XML {
2+
public enum Node {
3+
case element(Element)
4+
case text(String)
5+
}
6+
7+
public enum Encoding {
8+
case utf8
9+
}
10+
11+
public enum Standalone {
12+
case yes
13+
case no
14+
}
15+
16+
public struct Document {
17+
public var version: String? = nil
18+
public var encoding: Encoding? = nil
19+
public var standalone: Standalone? = nil
20+
21+
public var root: Element? = nil
22+
23+
public init(
24+
root: Element? = nil,
25+
version: String? = "1.0",
26+
encoding: Encoding? = .utf8,
27+
standalone: Standalone? = .no
28+
) {
29+
self.root = root
30+
self.version = version
31+
self.encoding = encoding
32+
self.standalone = standalone
33+
}
34+
}
35+
36+
public struct Element {
37+
public var name: String
38+
public var attributes: [String : String]
39+
public var children: [Node]
40+
41+
public init(
42+
name: String,
43+
attributes: [String : String] = [:],
44+
children: [Node] = []
45+
) {
46+
self.name = name
47+
self.attributes = attributes
48+
self.children = children
49+
}
50+
51+
public var value: String {
52+
var result = ""
53+
for child in children {
54+
if case .text(let string) = child {
55+
result += string
56+
}
57+
}
58+
return result
59+
}
60+
}
61+
}
62+
63+
extension XML.Element: Equatable {
64+
public static func == (lhs: XML.Element, rhs: XML.Element) -> Bool {
65+
return lhs.name == rhs.name && lhs.children == rhs.children
66+
}
67+
}
68+
69+
extension XML.Node: Equatable {
70+
public static func == (lhs: XML.Node, rhs: XML.Node) -> Bool {
71+
switch (lhs, rhs) {
72+
case let (.element(lhs), .element(rhs)): return lhs == rhs
73+
case let (.text(lhs), .text(rhs)): return lhs == rhs
74+
default: return false
75+
}
76+
}
77+
}

Tests/XMLTests/XMLTests.swift

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import Test
2+
@testable import XML
3+
4+
class XMLTests: TestCase {
5+
func testDocument() {
6+
let document = XML.Document()
7+
assertEqual(document.version, "1.0")
8+
assertEqual(document.encoding, .utf8)
9+
assertEqual(document.standalone, .no)
10+
assertNil(document.root)
11+
}
12+
13+
func testElement() {
14+
let element = XML.Element(name: "root")
15+
assertEqual(element.name, "root")
16+
assertEqual(element.attributes, [:])
17+
assertEqual(element.children, [])
18+
}
19+
20+
func testElementNode() {
21+
let node = XML.Node.element(XML.Element(name: "root"))
22+
assertEqual(node, .element(XML.Element(name: "root")))
23+
}
24+
25+
func testTextNode() {
26+
let node = XML.Node.text("text")
27+
assertEqual(node, .text("text"))
28+
}
29+
30+
func testElementChildren() {
31+
let element = XML.Element(name: "root", children: [.text("text")])
32+
assertEqual(element.children, [.text("text")])
33+
}
34+
35+
func testNodeValue() {
36+
let element = XML.Element(name: "root", children: [.text("text")])
37+
assertEqual(element.value, "text")
38+
}
39+
}

0 commit comments

Comments
 (0)