File tree Expand file tree Collapse file tree 4 files changed +48
-8
lines changed
xml-compare/src/main/scala/software/purpledragon/xml/compare
main/scala/software/purpledragon/xml/scalatest
test/scala/software/purpledragon/xml/scalatest Expand file tree Collapse file tree 4 files changed +48
-8
lines changed Original file line number Diff line number Diff line change 1
1
import PgpKeys .{publishLocalSigned , publishSigned }
2
2
3
3
organization := " software.purpledragon.xml"
4
- version := " 0.0.1-SNAPSHOT"
5
-
6
- scalaVersion := " 2.11.8"
4
+ version := " 0.0.1"
7
5
8
6
scalaVersion := " 2.12.3"
9
7
crossScalaVersions := Seq (" 2.11.11" , " 2.12.3" )
@@ -19,6 +17,7 @@ lazy val xmlCompare = project
19
17
20
18
lazy val xmlScalatest = project
21
19
.in(file(" xml-scalatest" ))
20
+ .dependsOn(xmlCompare)
22
21
23
22
lazy val root = project
24
23
.in(file(" ." ))
Original file line number Diff line number Diff line change @@ -2,14 +2,17 @@ package software.purpledragon.xml.compare
2
2
3
3
sealed trait XmlDiff {
4
4
def isEqual : Boolean
5
+ def message : String
5
6
}
6
7
7
8
object XmlEqual extends XmlDiff {
8
9
override def isEqual : Boolean = true
9
-
10
10
override def toString : String = " XmlEqual"
11
+ override def message : String = " "
11
12
}
12
13
13
14
case class XmlDiffers (reason : String , left : Any , right : Any ) extends XmlDiff {
14
15
override def isEqual : Boolean = false
16
+
17
+ override def message : String = s " $reason - $left != $right"
15
18
}
Original file line number Diff line number Diff line change 16
16
17
17
package software .purpledragon .xml .scalatest
18
18
19
- import org .scalatest .matchers .Matcher
19
+ import org .scalatest .matchers .{MatchResult , Matcher }
20
+ import software .purpledragon .xml .compare .XmlCompare
20
21
21
22
import scala .xml .Node
22
23
23
24
trait XmlMatchers {
24
- def beXml (node : Node ): Matcher [Node ] = ???
25
+ def beXml (expected : Node ): Matcher [Node ] = new XmlMatcher (expected)
25
26
26
- def beExactXml (node : Node ): Matcher [Node ] = ???
27
+ // def beExactXml(node: Node): Matcher[Node] = ???
28
+
29
+ class XmlMatcher (expected : Node ) extends Matcher [Node ] {
30
+ override def apply (actual : Node ): MatchResult = {
31
+ val diff = XmlCompare .compare(expected, actual)
32
+
33
+ MatchResult (
34
+ diff.isEqual,
35
+ s " XML did not match - ${diff.message}" ,
36
+ " XML matched"
37
+ )
38
+ }
39
+ }
27
40
}
Original file line number Diff line number Diff line change @@ -18,4 +18,29 @@ package software.purpledragon.xml.scalatest
18
18
19
19
import org .scalatest .{FlatSpec , Matchers }
20
20
21
- class XmlMatchersSpec extends FlatSpec with Matchers with XmlMatchers {}
21
+ class XmlMatchersSpec extends FlatSpec with Matchers with XmlMatchers {
22
+ " beXml" should " match identical XML" in {
23
+ val matcher = beXml(<test >text</test >)
24
+
25
+ val matchResult = matcher(<test >text</test >)
26
+ matchResult.matches shouldBe true
27
+ }
28
+
29
+ it should " match XML with different whitespace" in {
30
+ val matcher = beXml(<test >text</test >)
31
+
32
+ val matchResult = matcher(
33
+ <test >
34
+ text
35
+ </test >)
36
+ matchResult.matches shouldBe true
37
+ }
38
+
39
+ it should " not match different XML" in {
40
+ val matcher = beXml(<test >text</test >)
41
+
42
+ val matchResult = matcher(<test >different</test >)
43
+ matchResult.matches shouldBe false
44
+ matchResult.failureMessage shouldBe " XML did not match - different text - text != different"
45
+ }
46
+ }
You can’t perform that action at this time.
0 commit comments