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 11import PgpKeys .{publishLocalSigned , publishSigned }
22
33organization := " software.purpledragon.xml"
4- version := " 0.0.1-SNAPSHOT"
5-
6- scalaVersion := " 2.11.8"
4+ version := " 0.0.1"
75
86scalaVersion := " 2.12.3"
97crossScalaVersions := Seq (" 2.11.11" , " 2.12.3" )
@@ -19,6 +17,7 @@ lazy val xmlCompare = project
1917
2018lazy val xmlScalatest = project
2119 .in(file(" xml-scalatest" ))
20+ .dependsOn(xmlCompare)
2221
2322lazy val root = project
2423 .in(file(" ." ))
Original file line number Diff line number Diff line change @@ -2,14 +2,17 @@ package software.purpledragon.xml.compare
22
33sealed trait XmlDiff {
44 def isEqual : Boolean
5+ def message : String
56}
67
78object XmlEqual extends XmlDiff {
89 override def isEqual : Boolean = true
9-
1010 override def toString : String = " XmlEqual"
11+ override def message : String = " "
1112}
1213
1314case 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}
Original file line number Diff line number Diff line change 1616
1717package 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
2122import scala .xml .Node
2223
2324trait 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}
Original file line number Diff line number Diff line change @@ -18,4 +18,29 @@ package software.purpledragon.xml.scalatest
1818
1919import 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+ }
You can’t perform that action at this time.
0 commit comments