Skip to content

Commit

Permalink
Add specs2 matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
stringbean committed Aug 23, 2017
1 parent 610cc28 commit 8fc61d3
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 9 deletions.
17 changes: 9 additions & 8 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,30 @@ import PgpKeys.{publishLocalSigned, publishSigned}
import com.typesafe.sbt.SbtGit.GitKeys._

organization := "software.purpledragon.xml"
version := "0.0.1"
version := "0.0.2"

scalaVersion := "2.12.3"
crossScalaVersions := Seq("2.11.11", "2.12.3")

// dependencies common for all sub-projects
libraryDependencies ++= Seq(
"org.scala-lang.modules" %% "scala-xml" % "1.0.6",
"org.scalatest" %% "scalatest" % "3.0.4" % "test"
"org.scala-lang.modules" %% "scala-xml" % "1.0.6"
)

lazy val xmlCompare = project
.in(file("xml-compare"))
lazy val xmlCompare = Project("xml-compare", file("xml-compare"))

lazy val xmlScalatest = project
.in(file("xml-scalatest"))
lazy val xmlScalatest = Project("xml-scalatest", file("xml-scalatest"))
.dependsOn(xmlCompare)

lazy val xmlSpecs2 = Project("xml-specs2", file("xml-specs2"))
.dependsOn(xmlCompare)

lazy val root = project
.in(file("."))
.aggregate(
xmlCompare,
xmlScalatest
xmlScalatest,
xmlSpecs2
)
.settings(
publish := {},
Expand Down
4 changes: 4 additions & 0 deletions xml-compare/build.sbt
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
name := "xml-compare"

libraryDependencies ++= Seq(
"org.scalatest" %% "scalatest" % "3.0.4" % "test"
)
1 change: 0 additions & 1 deletion xml-scalatest/build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
name := "xml-scalatest"

// need to build against scalatest
libraryDependencies ++= Seq(
"org.scalatest" %% "scalatest" % "3.0.4" % "provided"
)
5 changes: 5 additions & 0 deletions xml-specs2/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name := "xml-specs2"

libraryDependencies ++= Seq(
"org.specs2" %% "specs2-core" % "3.9.4" % "provided"
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package software.purpledragon.xml.specs2

import org.specs2.matcher.Matcher
import org.specs2.matcher.MatchersImplicits._
import software.purpledragon.xml.compare.XmlCompare

import scala.xml.Node

trait XmlMatchers {
def beXml(expected: Node): Matcher[Node] = { actual: Node =>
val diff = XmlCompare.compare(expected, actual)

(diff.isEqual, "XML matched", s"XML did not match - ${diff.message}")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2017 Michael Stringer
*
* 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 software.purpledragon.xml.specs2

import org.specs2.matcher.Expectations
import org.specs2.mutable.Specification

class XmlMatchersSpec extends Specification with XmlMatchers with Expectations {

"beXml" should {
"match identical XML" in {
val matcher = beXml(<test>text</test>)

val matchResult = matcher(createExpectable(<test>text</test>))
matchResult.isSuccess === true
}

"match XML with different whitespace" in {
val matcher = beXml(<test>text</test>)

val matchResult = matcher(createExpectable(
<test>
text
</test>))
matchResult.isSuccess === true
}

"not match different XML" in {
val matcher = beXml(<test>text</test>)

val matchResult = matcher(createExpectable(<test>different</test>))
matchResult.isSuccess === false
matchResult.message === "XML did not match - different text - text != different"
}
}
}

0 comments on commit 8fc61d3

Please sign in to comment.