-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsome.test.scala
219 lines (199 loc) · 7.31 KB
/
some.test.scala
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
package cats_parse_xml
import cats.parse.Parser
import org.scalatest.Inside.inside
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec
class XmlParserTests extends AnyWordSpec with Matchers:
private def selectNodes(segments: List[Segment]): List[XmlNode] =
segments.collect { case Segment.Node(n) => n }
private def selectComments(segments: List[Segment]): List[String] =
segments.collect { case Segment.Comment(c) => c }
private def selectTexts(segments: List[Segment]): List[String] =
segments.collect { case Segment.Text(t) => t }
"xmlHeader" should:
"succeed" in:
val source = """ <?xml version="1.0" encoding="UTF-8"?> """
val res = XmlParser.xmlFileHeader.parseAll(source)
inside(res):
case Right(header) =>
header shouldBe source.trim
"xmlNode - simple" should:
"succeed with no attributes" in:
val source = """<Root />"""
val res = XmlParser.xmlNode.parseAll(source)
inside(res):
case Right(xmlNode) =>
xmlNode.tagName shouldBe "Root"
xmlNode.attributes shouldBe Map.empty
"succeed with attributes" in:
val source =
"""<Root Id="foo" />"""
val res = XmlParser.xmlNode.parseAll(source)
inside(res):
case Right(xmlNode) =>
xmlNode.tagName shouldBe "Root"
xmlNode.attributes shouldBe Map("Id" -> "foo")
"andBody" should:
"succeed with correct closing tag" in:
val source =
""" > </Root>"""
val pre = ("Root", List.empty)
val res = XmlParser.andBody(pre).parseAll(source)
inside(res):
case Right(xmlNode) =>
xmlNode.segments should have size 1
"fail with incorrect closing tag" in:
val source =
""" > </B> """
val pre = ("Root", List.empty)
val res = XmlParser.andBody(pre).parseAll(source)
inside(res):
case Left(error) =>
"xmlNode" should:
"succeed with empty body" in:
val source =
"""<Root Id="foo" ></Root>"""
val res = XmlParser.xmlNode.parseAll(source)
inside(res):
case Right(xmlNode) =>
xmlNode.tagName shouldBe "Root"
xmlNode.attributes shouldBe Map("Id" -> "foo")
xmlNode.segments shouldBe Nil
"succeed with body" in:
val source =
"""<A> <B /> </A>"""
val res = XmlParser.xmlNode.parseAll(source)
inside(res):
case Right(xmlNode) =>
xmlNode.tagName shouldBe "A"
xmlNode.attributes shouldBe Map.empty
selectComments(xmlNode.segments) shouldBe List.empty
xmlNode.segments should have size 3
val subNodes = selectNodes(xmlNode.segments)
subNodes.head.tagName shouldBe "B"
"succeed with multiple-level body" in:
val source =
"""<B Id="x"> <A> </A> </B>"""
val res = XmlParser.xmlNode.parseAll(source)
inside(res):
case Right(xmlNode) =>
xmlNode.tagName shouldBe "B"
xmlNode.attributes shouldBe Map("Id" -> "x")
xmlNode.segments should have size 3
selectComments(xmlNode.segments) shouldBe List.empty
val subNodes = selectNodes(xmlNode.segments)
subNodes should have size 1
subNodes.head.tagName shouldBe "A"
"succeed with text segments" in:
val source =
"""<Quz>
| Some text
| <!-- comment -->
| <Arg Name="abcdf" />
| <Minute/>
|</Quz>""".stripMargin
val res = XmlParser.xmlNode.parseAll(source)
inside(res):
case Right(xmlNode) =>
xmlNode.tagName shouldBe "Quz"
xmlNode.attributes shouldBe Map.empty
selectComments(xmlNode.segments) shouldBe List(" comment ")
selectTexts(xmlNode.segments) shouldBe List(
"\n Some text\n ",
"\n ",
"\n ",
"\n"
)
"succeed with namespace declarations" in:
val source =
"""<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
| xmlns="Tethys"
| xmlns:Units="Tethys/Units"
| xsi:schemaLocation="Tethys https://foo/Tethys.xsd
| Tethys/Units https://foo/Units.xsd"
| Id="Default">
| <DefineArg Name="abcdf"><Minute/><Value>0</Value></DefineArg>
|</Root>""".stripMargin
val res = XmlParser.xmlNode.parseAll(source)
inside(res):
case Right(xmlNode) =>
xmlNode.tagName shouldBe "Root"
xmlNode.attributes.keys should contain theSameElementsAs List(
"xmlns:xsi",
"xmlns",
"xmlns:Units",
"xsi:schemaLocation",
"Id"
)
xmlNode.attributes(
"xmlns:xsi"
) shouldBe "http://www.w3.org/2001/XMLSchema-instance"
xmlNode.attributes("xmlns") shouldBe "Tethys"
xmlNode.attributes("xmlns:Units") shouldBe "Tethys/Units"
xmlNode
.attributes("xsi:schemaLocation")
.replaceAll("\\s+", " ") shouldBe
"""Tethys https://foo/Tethys.xsd Tethys/Units https://foo/Units.xsd"""
xmlNode.attributes("Id") shouldBe "Default"
"xmlDoc" should:
val noFileHeaderSource =
"""<!-- file level comment -->
|<B Id="x">
| <!-- comment in B -->
| <A>
| <!-- comment in A -->
| <C foo="quz" />
| <D />
| <!-- post-comment in A -->
| </A>
| <!-- post-comment in B -->
|</B>
|<!--final file-->
|<!--comments-->
|""".stripMargin
def checks(xmlContents: XmlDoc) =
xmlContents.preComments shouldBe List(" file level comment ")
xmlContents.postComments shouldBe List("final file", "comments")
val xmlNode = xmlContents.xmlNode
// B:
xmlNode.tagName shouldBe "B"
xmlNode.attributes shouldBe Map("Id" -> "x")
selectComments(xmlNode.segments) shouldBe List(
" comment in B ",
" post-comment in B "
)
val subNodes = selectNodes(xmlNode.segments)
subNodes should have size 1
// A:
subNodes.head.tagName shouldBe "A"
selectComments(subNodes.head.segments) shouldBe List(
" comment in A ",
" post-comment in A "
)
val subSubNodes = selectNodes(subNodes.head.segments)
subSubNodes should have size 2
// C:
subSubNodes.head.tagName shouldBe "C"
subSubNodes.head.attributes shouldBe Map("foo" -> "quz")
selectComments(subSubNodes.head.segments) shouldBe Nil
// D:
subSubNodes(1).tagName shouldBe "D"
subSubNodes(1).attributes shouldBe Map.empty
selectComments(subSubNodes(1).segments) shouldBe Nil
"succeed with no file header" in:
val res = XmlParser.xmlDoc.parseAll(noFileHeaderSource)
inside(res):
case Right(xmlContents) =>
xmlContents.header shouldBe None
checks(xmlContents)
"succeed with file header" in:
val source =
s""" <?xml version="1.0" encoding="UTF-8"?>
|$noFileHeaderSource
|""".stripMargin
val res = XmlParser.xmlDoc.parseAll(source)
inside(res):
case Right(xmlContents) =>
xmlContents.header shouldBe Some:
"""<?xml version="1.0" encoding="UTF-8"?>"""
checks(xmlContents)