andyglow / scala-xml-diff

Scala XML Diff
GNU Lesser General Public License v3.0
9 stars 7 forks source link

Scala XML Diff

Build Status codecov mvn mvn

Tool to compare scala.xml.Nodes with detailed comparison result

Usage

build.sbt

libraryDependencies += "com.github.andyglow" %% "scala-xml-diff" % ${LATEST_VERSION} % Compile

Import

import com.github.andyglow.xml.diff._

REPL example

scala> <foo/> =?= <foo/>
res0: com.github.andyglow.xml.diff.package.XmlDiff = Eq

scala> <foo/> =?= <bar/>
res1: com.github.andyglow.xml.diff.package.XmlDiff = Neq(List(UnequalElem(foo,List(UnequalName(foo,bar)))))

scala> <foo x="a"/> =?= <foo x="b"/>
res2: com.github.andyglow.xml.diff.package.XmlDiff = Neq(List(UnequalElem(foo,List(UnequalAttribute(x,a,b)))))

scala> <foo><bar key="val1" key2="val2"/></foo> =?= <foo><bar key="val2" key3="val3"/></foo>
res3: com.github.andyglow.xml.diff.package.XmlDiff = Neq(List(
  UnequalElem(foo,List(
    UnequalElem(bar,List(
      UnequalAttribute(key,val1,val2),
      AbsentAttribute(key2,val2),
      RedundantAttribute(key3,val3)))))))

// not ignoring whitespace      
scala> <jaz>foo</jaz> =?= <jaz>foo </jaz>
res4: com.github.andyglow.xml.diff.package.XmlDiff = Neq(List(
  UnequalElem(jaz,List(
    AbsentNode(foo),
    RedundantNode(foo )))))

// ignoring whitespace      
scala> <jaz>foo</jaz> =#= <jaz>foo </jaz>
res5: com.github.andyglow.xml.diff.package.XmlDiff = Eq

Scalatest example

import org.scalatest.xml.XmlMatchers._
"MyRestService" must {
  "generate proper xml" in {
    val document: xml.NodeSeq = service.call(...)
    document should beXml(
      <document>
        <title>Invoice</title>
        <version>0.6.2</version>
        <header></header>
        <content>
          <line id="...">...</line>
        </content>
      </document>
    )
  }

  // it is also possible to match negative scenarios
  "be able to distinguish <bar/> from <foo/>" in {
    <bar/> should not beXml <foo/>
  }

  // you then can match ignoring whitespace
  "match not counting leading/trailing whitespaces" in {
    <foo>x</foo> should beXml (<foo> x </foo>, ignoreWhitespaces = true)
  }

  // the same could be done using not beXml

}