forked from cezheng/Fuzi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXMLTests.swift
122 lines (105 loc) · 4.55 KB
/
XMLTests.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
// XMLTests.swift
// Copyright (c) 2015 Ce Zheng
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import XCTest
import Fuzi
class XMLTests: XCTestCase {
var document: Fuzi.XMLDocument!
override func setUpWithError() throws {
try super.setUpWithError()
let data = try loadData(filename: "xml", extension: "xml")
document = try XMLDocument(data: data)
}
func testXMLVersion() {
XCTAssertEqual(document.version, "1.0", "XML version should be 1.0")
}
func testXMLEncoding() {
XCTAssertEqual(document.encoding, String.Encoding.utf8, "XML encoding should be UTF-8")
}
func testRoot() {
XCTAssertEqual(document.root!.tag, "spec", "root tag should be spec")
XCTAssertEqual(document.root!.attributes["w3c-doctype"], "rec", "w3c-doctype should be rec")
XCTAssertEqual(document.root!.attributes["lang"], "en", "lang should be en")
}
func testTitle() {
let titleElement = document.root!.firstChild(tag: "header")?.firstChild(tag: "title")
XCTAssertNotNil(titleElement, "title element should not be nil")
XCTAssertEqual(titleElement?.tag, "title", "tag should be `title`")
XCTAssertEqual(titleElement?.stringValue, "Extensible Markup Language (XML)", "title string value should be 'Extensible Markup Language (XML)'")
}
func testXPath() {
let path = "/spec/header/title"
let elts = document.xpath(path)
var counter = 0
for elt in elts {
XCTAssertEqual("title", elt.tag, "tag should be `title`")
counter += 1
}
XCTAssertEqual(1, counter, "at least one element should have been found at element path '\(path)'")
}
func testTryXpathThrowsError() {
do {
_ = try document.tryXPath("////")
XCTAssertFalse(true, "error should have been thrown")
} catch XMLError.libXMLError(code: 1207, message: "Invalid expression") {
} catch {
XCTAssertFalse(true, "error type should be libXMLError \(error)")
}
}
func testTryXpathFunctionThrowsError() {
do {
_ = try document.tryXPath("//*[unknown()]")
XCTAssertFalse(true, "error should have been thrown")
} catch XMLError.libXMLError(code: 1209, message: "Unregistered function") {
// On Linux >= 5.7
} catch XMLError.libXMLError(code: 1223, message: "Stack usage error") {
// On Linux < 5.7
} catch {
XCTAssertFalse(true, "error type should be libXMLError \(error)")
}
}
func testLineNumber() throws {
let headerElement = document.root!.firstChild(tag: "header")
XCTAssertNotNil(headerElement, "header element should not be nil")
switch headerElement?.lineNumber {
case 120:
break // all good
case 123:
throw XCTSkip("For some reason this sometimes returns 123, even though it is 120, if you inspect the file. However, this was in the test like this for ages.")
default:
XCTAssertEqual(headerElement?.lineNumber, 120, "header line number should be correct")
}
}
func testThrowsError() {
do {
document = try XMLDocument(cChars: [CChar]())
XCTAssertFalse(true, "error should have been thrown")
} catch XMLError.parserFailure {
} catch {
XCTAssertFalse(true, "error type should be ParserFailure")
}
}
func testAuthorsByStaticTag() {
let authlistElement = document.root!.firstChild(staticTag: "header")?.firstChild(staticTag: "authlist")
XCTAssertNotNil(authlistElement, "authorlist element should not be nil")
let authorElements = authlistElement?.children(staticTag: "author")
XCTAssertEqual(authorElements?.count, 5, "should have 5 elements")
}
}