Skip to content

Commit 9828be7

Browse files
committed
Add basic scalatest matcher
1 parent afd719f commit 9828be7

File tree

4 files changed

+48
-8
lines changed

4 files changed

+48
-8
lines changed

build.sbt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import PgpKeys.{publishLocalSigned, publishSigned}
22

33
organization := "software.purpledragon.xml"
4-
version := "0.0.1-SNAPSHOT"
5-
6-
scalaVersion := "2.11.8"
4+
version := "0.0.1"
75

86
scalaVersion := "2.12.3"
97
crossScalaVersions := Seq("2.11.11", "2.12.3")
@@ -19,6 +17,7 @@ lazy val xmlCompare = project
1917

2018
lazy val xmlScalatest = project
2119
.in(file("xml-scalatest"))
20+
.dependsOn(xmlCompare)
2221

2322
lazy val root = project
2423
.in(file("."))

xml-compare/src/main/scala/software/purpledragon/xml/compare/XmlDiff.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@ package software.purpledragon.xml.compare
22

33
sealed trait XmlDiff {
44
def isEqual: Boolean
5+
def message: String
56
}
67

78
object XmlEqual extends XmlDiff {
89
override def isEqual: Boolean = true
9-
1010
override def toString: String = "XmlEqual"
11+
override def message: String = ""
1112
}
1213

1314
case class XmlDiffers(reason: String, left: Any, right: Any) extends XmlDiff {
1415
override def isEqual: Boolean = false
16+
17+
override def message: String = s"$reason - $left != $right"
1518
}

xml-scalatest/src/main/scala/software/purpledragon/xml/scalatest/XmlMatchers.scala

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,25 @@
1616

1717
package software.purpledragon.xml.scalatest
1818

19-
import org.scalatest.matchers.Matcher
19+
import org.scalatest.matchers.{MatchResult, Matcher}
20+
import software.purpledragon.xml.compare.XmlCompare
2021

2122
import scala.xml.Node
2223

2324
trait XmlMatchers {
24-
def beXml(node: Node): Matcher[Node] = ???
25+
def beXml(expected: Node): Matcher[Node] = new XmlMatcher(expected)
2526

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+
}
2740
}

xml-scalatest/src/test/scala/software/purpledragon/xml/scalatest/XmlMatchersSpec.scala

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,29 @@ package software.purpledragon.xml.scalatest
1818

1919
import org.scalatest.{FlatSpec, Matchers}
2020

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+
}

0 commit comments

Comments
 (0)