forked from JohnSundell/Plot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Assertions.swift
221 lines (195 loc) · 4.94 KB
/
Assertions.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/**
* Plot
* Copyright (c) John Sundell 2019
* MIT license, see LICENSE file for details
*/
import XCTest
import Plot
func assertEqualHTMLContent(
_ document: HTML,
_ content: String,
file: StaticString = #file,
line: UInt = #line
) {
let html = document.render()
let expectedPrefix = "<!DOCTYPE html><html>"
let expectedSuffix = "</html>"
XCTAssertTrue(
html.hasPrefix(expectedPrefix),
"""
Invalid HTML prefix.
Expected '\(expectedPrefix)'.
Found '\(html.prefix(expectedPrefix.count))'.
""",
file: file,
line: line
)
XCTAssertTrue(
html.hasSuffix(expectedSuffix),
"""
Invalid HTML suffix.
Expected '\(expectedSuffix)'.
Found '\(html.suffix(expectedSuffix.count))'.
""",
file: file,
line: line
)
let expectedContent = html
.dropFirst(expectedPrefix.count)
.dropLast(expectedSuffix.count)
XCTAssertEqual(
String(expectedContent),
content,
file: file,
line: line
)
}
func assertEqualSiteMapContent(
_ document: SiteMap,
_ content: String,
file: StaticString = #file,
line: UInt = #line
) {
let map = document.render()
let expectedPrefix = XML().render() + """
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" \
xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
"""
let expectedSuffix = "</urlset>"
XCTAssertTrue(
map.hasPrefix(expectedPrefix),
"""
Invalid SiteMap prefix.
Expected '\(expectedPrefix)'.
Found '\(map.prefix(expectedPrefix.count))'.
""",
file: file,
line: line
)
XCTAssertTrue(
map.hasSuffix(expectedSuffix),
"""
Invalid SiteMap suffix.
Expected '\(expectedSuffix)'.
Found '\(map.suffix(expectedSuffix.count))'.
""",
file: file,
line: line
)
let expectedContent = map
.dropFirst(expectedPrefix.count)
.dropLast(expectedSuffix.count)
XCTAssertEqual(
String(expectedContent),
content,
file: file,
line: line
)
}
func assertEqualXMLContent(
_ document: XML,
_ content: String,
file: StaticString = #file,
line: UInt = #line
) {
let xml = document.render()
let declaration = #"<?xml version="1.0" encoding="UTF-8"?>"#
XCTAssertTrue(
xml.hasPrefix(declaration),
"""
Invalid XML declaration.
Expected '\(declaration)'.
Found '\(xml.prefix(declaration.count))'.
""",
file: file,
line: line
)
XCTAssertEqual(
String(xml.dropFirst(declaration.count)),
content,
file: file,
line: line
)
}
func assertEqualPodcastFeedContent(
_ feed: PodcastFeed,
_ content: String,
file: StaticString = #file,
line: UInt = #line
) {
assertEqualRSSFeedContent(
feed,
content,
type: "podcast",
namespaces: [
("atom", "http://www.w3.org/2005/Atom"),
("content", "http://purl.org/rss/1.0/modules/content"),
("itunes", "http://www.itunes.com/dtds/podcast-1.0.dtd"),
("media", "http://www.rssboard.org/media-rss")
],
file: file,
line: line
)
}
func assertEqualRSSFeedContent(
_ feed: RSS,
_ content: String,
file: StaticString = #file,
line: UInt = #line
) {
assertEqualRSSFeedContent(
feed,
content,
type: "RSS",
namespaces: [
("atom", "http://www.w3.org/2005/Atom"),
("content", "http://purl.org/rss/1.0/modules/content")
],
file: file,
line: line
)
}
private func assertEqualRSSFeedContent<R: Renderable>(
_ feed: R,
_ content: String,
type: String,
namespaces: [(name: String, url: String)],
file: StaticString,
line: UInt
) {
let xmlDeclaration = XML().render()
let namespaces = namespaces.map({ name, url in
"xmlns:\(name)=\"\(url)\""
}).joined(separator: " ")
let expectedPrefix = "\(xmlDeclaration)<rss version=\"2.0\" \(namespaces)><channel>"
let expectedSuffix = "</channel></rss>"
let xml = feed.render()
XCTAssertTrue(
xml.hasPrefix(expectedPrefix),
"""
Invalid \(type) feed prefix.
Expected '\(expectedPrefix)'.
Found '\(xml.prefix(expectedPrefix.count))'.
""",
file: file,
line: line
)
XCTAssertTrue(
xml.hasSuffix(expectedSuffix),
"""
\(type.capitalized) feed is not closed with '\(expectedSuffix)'.
Feed: '\(xml)'
""",
file: file,
line: line
)
let expectedContent = xml
.dropFirst(expectedPrefix.count)
.dropLast(expectedSuffix.count)
XCTAssertEqual(
String(expectedContent),
content,
file: file,
line: line
)
}