/
StreamlinedXml.scala
201 lines (193 loc) · 6.98 KB
/
StreamlinedXml.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
/*
* Copyright 2001-2014 Artima, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.scalatest
import scala.xml.{Text, Node, Elem, NodeSeq}
import org.scalactic.Uniformity
/**
* Trait providing a <code>streamlined</code> method that returns a <a href="../scalactic/Uniformity.html"><code>Uniformity[T]</code></a>
* instance for any subtype of <code>scala.xml.NodeSeq</code> that will normalize the XML by removing empty text nodes and trimming
* non-empty text nodes.
*
* <p>
* Here's an example of some unnormalized XML:
* </p>
*
* <pre class="stHighlighted">
* <summer>
* <day></day>
* <night>
* <span class="stReserved">with</span> lots of stars
* </night>
* </summer>
* </pre>
*
* <p>
* The <code>Uniformity</code> returned by this trait's <code>streamlined</code> method would transform
* the above XML to:
* </p>
*
* <pre class="stHighlighted">
* <summer><day></day><night><span class="stReserved">with</span> lots of stars</night></summer>
* </pre>
*
* <p>
* The <code>streamlined</code> method can be used with the <a href="../scalactic/Explicitly.html"><code>Explicitly</code></a> DSL, like this:
* </p>
*
* <pre class="stHighlighted">
* xmlElem should equal (
* <summer>
* <day></day>
* <night>
* <span class="stReserved">with</span> lots of stars
* </night>
* </summer>
* ) (after being streamlined[<span class="stType">Elem</span>])
* </pre>
*
* <p>
* The goal of this trait is to provide a normalization for XML that makes it easier to test XML objects for equality.
* White space is significant in XML, and is taken into account by the default equality for XML, accessed
* by invoking the <code>==</code> method on an XML <code>NodeSeq</code>. Here's an example:
* </p>
*
* <pre class="stREPL">
* scala> val xmlElem = <summer><day></day><night>with lots of stars</night></summer>
* xmlElem: scala.xml.Elem = <summer><day></day><night>with lots of stars</night></summer>
*
* scala> xmlElem == <summer>
* | <day></day>
* | <night>
* | with lots of stars
* | </night>
* | </summer>
* res1: Boolean = false
* </pre>
*
* <p>
* The above equality comparison produces false because of whitespace differences in the XML.
* When such whitespace differences are unimportant to the actual application, it can make it
* easier to write readable test code if you can compare XML for equality without taking
* into account empty text nodes, or leading and trailing whitespace in nonempty text nodes.
* The <code>streamlined</code> method of this trait provides a <code>Uniformity</code>
* instance that does just that:
* </p>
*
* <pre class="stREPL">
* scala> import org.scalactic._
* import org.scalactic._
*
* scala> import Explicitly._
* import Explicitly._
*
* scala> import TripleEquals._
* import TripleEquals._
*
* scala> import org.scalatest.StreamlinedXml._
* import StreamlinedXml._
*
* scala> import scala.xml.Elem
* import scala.xml.Elem *
*
* scala> (xmlElem === <summer>
* | <day></day>
* | <night>
* | with lots of stars
* | </night>
* | </summer>) (after being streamlined[Elem])
* res9: Boolean = true
* </pre>
*
* @author Bill Venners
*/
trait StreamlinedXml {
/**
* Provides a <a href="../scalactic/Uniformity.html"><code>Uniformity[T]</code></a>
* instance for any subtype of <code>scala.xml.NodeSeq</code> that will normalize the XML by removing empty text nodes and trimming
* non-empty text nodes.
*
* <p>
* The purpose of this <code>Uniformity</code> is to make it easier to write readable test
* code that compares XML for equality. See the main documentation for this trait for more
* details and examples.
* </p>
*
* @return a <code>Uniformity[T]</code> instance that normalizes XML for testing
*/
def streamlined[T <: NodeSeq]: Uniformity[T] = {
def trimTextZappingEmpty(node: Node): Seq[Node] =
node match {
case Text(text) if (text.trim.isEmpty) => Nil
case Text(text) => List(Text(text.trim))
case Elem(pre, lab, md, scp, children @ _*) =>
Elem(pre, lab, md, scp, false, (children.flatMap(trimTextZappingEmpty)):_*)
case _ => List(node)
}
new Uniformity[T] {
def normalized(nodeSeq: T): T =
nodeSeq match {
case Elem(pre, lab, md, scp, children @ _*) =>
val mergedTextNodes = // Merge adjacent text nodes
children.foldLeft(Nil: List[Node]) { (acc, ele) =>
ele match {
case eleTxt: Text =>
acc.headOption match {
case Some(accTxt: Text) =>
Text(accTxt.text + eleTxt.text) :: acc.tail
case _ => ele :: acc
}
case _ => ele :: acc
}
}
Elem(pre, lab, md, scp, false, (mergedTextNodes.flatMap(trimTextZappingEmpty)):_*).asInstanceOf[T]
case _ => nodeSeq
}
/**
* Returns true if the passed <code>Any</code> is a <code>Elem</code>.
*
* @return true if the passed <code>Any</code> is a <code>Elem</code>.
*/
final def normalizedCanHandle(b: Any): Boolean = b.isInstanceOf[NodeSeq]
/**
* Normalizes the passed object if it is a <code>Elem</code>.
*
* <p>
* This method returns either:
* </p>
*
* <ul>
* <li>if the passed object is a <code>Elem</code>, the result of passing that string to <code>normalized</code></li>
* <li>else, the same exact object that was passed
* </p>
*
* @return a normalized form of any passed <code>Elem</code>, or the same object if not a <code>Elem</code>.
*/
final def normalizedOrSame(b: Any): Any =
b match {
case s: NodeSeq => StreamlinedXml.streamlined[NodeSeq].normalized(s)
case _ => b
}
}
}
}
/**
* Companion object that facilitates the importing of <code>StreamlinedXml</code> members as
* an alternative to mixing it the trait. One use case is to import <code>StreamlinedXml</code> members so you can use
* them in the Scala interpreter.
*
* @author Bill Venners
*/
object StreamlinedXml extends StreamlinedXml