Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import sbtrelease.ReleasePlugin.autoImport._
inThisBuild(
Seq(
organization := "software.purpledragon.xml",
scalaVersion := "2.13.1",
crossScalaVersions := Seq(scalaVersion.value, "2.11.12", "2.12.10"),
scalaVersion := "2.13.2",
crossScalaVersions := Seq(scalaVersion.value, "2.11.12", "2.12.11"),
licenses += ("Apache-2.0", url("https://opensource.org/licenses/Apache-2.0")),
developers := List(
Developer("stringbean", "Michael Stringer", "@the_stringbean", url("https://github.com/stringbean"))
Expand All @@ -24,7 +24,7 @@ inThisBuild(
scalacOptions ++= Seq(s"-target:jvm-$javaVersion", "-deprecation", "-feature", "-unchecked"),
// dependencies common for all sub-projects
libraryDependencies ++= Seq(
"org.scala-lang.modules" %% "scala-xml" % "1.2.0"
"org.scala-lang.modules" %% "scala-xml" % "1.3.0"
),
))

Expand Down Expand Up @@ -70,6 +70,7 @@ lazy val root = Project("scala-xml-compare", file("."))
),
// sbt-release settings
releaseCrossBuild := true,
releaseVcsSign := true,
releaseProcess := Seq[ReleaseStep](
checkSnapshotDependencies,
inquireVersions,
Expand Down
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.3.2
sbt.version=1.3.12
16 changes: 8 additions & 8 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// code style
addSbtPlugin("org.scalastyle" %% "scalastyle-sbt-plugin" % "1.0.0")
addSbtPlugin("org.scoverage" % "sbt-scoverage" % "1.6.0")
addSbtPlugin("com.lucidchart" % "sbt-scalafmt" % "1.15")
addSbtPlugin("de.heikoseeberger" % "sbt-header" % "5.0.0")
addSbtPlugin("org.scoverage" % "sbt-scoverage" % "1.6.1")
addSbtPlugin("com.lucidchart" % "sbt-scalafmt" % "1.16")
addSbtPlugin("de.heikoseeberger" % "sbt-header" % "5.6.0")

// artifact publishing
addSbtPlugin("org.foundweekends" % "sbt-bintray" % "0.5.4")
addSbtPlugin("com.jsuereth" % "sbt-pgp" % "2.0.0")
addSbtPlugin("com.github.gseitz" % "sbt-release" % "1.0.11")
addSbtPlugin("org.foundweekends" % "sbt-bintray" % "0.5.6")
addSbtPlugin("com.jsuereth" % "sbt-pgp" % "2.0.1")
addSbtPlugin("com.github.gseitz" % "sbt-release" % "1.0.13")

// documentation
addSbtPlugin("com.eed3si9n" % "sbt-unidoc" % "0.4.2")
addSbtPlugin("com.eed3si9n" % "sbt-unidoc" % "0.4.3")
addSbtPlugin("com.typesafe.sbt" % "sbt-ghpages" % "0.6.3")
addSbtPlugin("com.typesafe.sbt" % "sbt-site" % "1.4.0")
addSbtPlugin("com.lightbend.paradox" % "sbt-paradox" % "0.6.5")
addSbtPlugin("com.lightbend.paradox" % "sbt-paradox" % "0.8.0")
addSbtPlugin("com.thoughtworks.sbt-api-mappings" % "sbt-api-mappings" % "3.0.0")
2 changes: 1 addition & 1 deletion xml-compare/build.sbt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name := "xml-compare"

libraryDependencies ++= Seq(
"org.scalatest" %% "scalatest" % "3.0.8" % Test
"org.scalatest" %% "scalatest" % "3.1.2" % Test
)
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,10 @@ import scala.xml._
object XmlCompare {
private type Check = (Node, Node, DiffOptions, Seq[String]) => XmlDiff

private implicit val NodeOrdering = NormalisedNodeOrdering
private implicit val NodeOrdering: NormalisedNodeOrdering.type = NormalisedNodeOrdering

/**
* Default [[software.purpledragon.xml.compare.options.DiffOption.DiffOption DiffOption]]s to use during XML comparison.
*
* Currently these are:
* - [[software.purpledragon.xml.compare.options.DiffOption.IgnoreNamespacePrefix IgnoreNamespacePrefix]]
*/
val DefaultOptions: DiffOptions = Set(IgnoreNamespacePrefix)
@deprecated(message = "use DiffOptions.Default", since = "2.0.0")
val DefaultOptions: DiffOptions = DiffOptions.default

/**
* Compares two XML documents. This will perform a recursive scan of all the nodes in each document, checking each
Expand All @@ -47,7 +42,7 @@ object XmlCompare {
* @param options configuration options to control the way the comparison is performed.
* @return results of the XML comparison.
*/
def compare(left: Node, right: Node, options: DiffOptions = DefaultOptions): XmlDiff = {
def compare(left: Node, right: Node, options: DiffOptions = DiffOptions.default): XmlDiff = {
compareNodes(left, right, options, Nil)
}

Expand All @@ -72,10 +67,10 @@ object XmlCompare {
private def compareNamespace(left: Node, right: Node, options: DiffOptions, path: Seq[String]): XmlDiff = {
if (left.label != right.label) {
XmlDiffers("different label", left.label, right.label, extendPath(path, left))
} else if (left.namespace != right.namespace && !options.contains(IgnoreNamespace)) {
} else if (left.namespace != right.namespace && options.isDisabled(IgnoreNamespace)) {
XmlDiffers("different namespace", left.namespace, right.namespace, extendPath(path, left))
} else if (left.prefix != right.prefix && !options.contains(IgnoreNamespacePrefix) &&
!options.contains(IgnoreNamespace)) {
} else if (left.prefix != right.prefix && options.isDisabled(IgnoreNamespacePrefix) &&
options.isDisabled(IgnoreNamespace)) {
XmlDiffers("different namespace prefix", left.prefix, right.prefix, extendPath(path, left))
} else {
XmlEqual
Expand All @@ -88,7 +83,7 @@ object XmlCompare {

if (leftKeys.sorted != rightKeys.sorted) {
XmlDiffers("different attribute names", leftKeys.sorted, rightKeys.sorted, extendPath(path, left))
} else if (options.contains(StrictAttributeOrdering) && leftKeys != rightKeys) {
} else if (options.isEnabled(StrictAttributeOrdering) && leftKeys != rightKeys) {
XmlDiffers("different attribute ordering", leftKeys, rightKeys, extendPath(path, left))
} else {
leftKeys.sorted collectFirst {
Expand Down Expand Up @@ -134,7 +129,7 @@ object XmlCompare {
}

private def normalise(nodes: Seq[Node], options: DiffOptions): Seq[Node] = {
val sort = options.contains(DiffOption.IgnoreChildOrder)
val sort = options.isEnabled(DiffOption.IgnoreChildOrder)
val filtered = nodes.filterNot(n => n.isInstanceOf[Atom[_]])

if (sort) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.compare.options

object DiffOptions {

/**
* Default [[DiffOption DiffOption]]s to use during XML comparison.
*
* Currently these are:
* - [[DiffOption.IgnoreNamespacePrefix IgnoreNamespacePrefix]]
*/
val default: DiffOptions = DiffOptions(DiffOption.IgnoreNamespacePrefix)
val empty: DiffOptions = DiffOptions()

def apply(options: DiffOption.DiffOption*): DiffOptions = DiffOptions(options.toSet)
}

case class DiffOptions(options: Set[DiffOption.DiffOption]) {
def &(option: DiffOption.DiffOption): DiffOptions = copy(options = options + option)
def &!(option: DiffOption.DiffOption): DiffOptions = copy(options = options - option)

def +(option: DiffOption.DiffOption): DiffOptions = copy(options = options + option)
def -(option: DiffOption.DiffOption): DiffOptions = copy(options = options - option)

def isEnabled(option: DiffOption.DiffOption): Boolean = options.contains(option)
def isDisabled(option: DiffOption.DiffOption): Boolean = !options.contains(option)
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@

package software.purpledragon.xml

import org.scalatest.{FlatSpec, Matchers}
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

class XmlUtilsSpec extends FlatSpec with Matchers {
class XmlUtilsSpec extends AnyFlatSpec with Matchers {
"XmlUtils.extractAttributes" should "return empty values for no attributes" in {
XmlUtils.extractAttributes(<empty/>) shouldBe (Nil, Map.empty[String, String])
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@

package software.purpledragon.xml.compare

import org.scalatest.{FlatSpec, Matchers}
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

import scala.xml.{Comment, Node, PCData, Text}

class NormalisedNodeOrderingSpec extends FlatSpec with Matchers {
class NormalisedNodeOrderingSpec extends AnyFlatSpec with Matchers {
private implicit val ordering: Ordering[Node] = NormalisedNodeOrdering

"NormalisedNodeOrdering" should "order nodes by type" in {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@

package software.purpledragon.xml.compare

import org.scalatest.{FlatSpec, Matchers}
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
import software.purpledragon.xml.compare.options.DiffOption._
import software.purpledragon.xml.compare.options.DiffOptions

class XmlCompareSpec extends FlatSpec with Matchers {
class XmlCompareSpec extends AnyFlatSpec with Matchers {

"compare with defaults" should "match same empty element" in {
XmlCompare.compare(<test/>, <test/>) shouldBe XmlEqual
Expand Down Expand Up @@ -152,47 +154,47 @@ class XmlCompareSpec extends FlatSpec with Matchers {
XmlCompare.compare(
<t:test xmlns:t="http://example.com"/>,
<e:test xmlns:e="http://example.com"/>,
Set.empty
DiffOptions.empty
) shouldBe XmlDiffers("different namespace prefix", "t", "e", Seq("t:test"))
}

it should "match same namespaces" in {
XmlCompare.compare(
<t:test xmlns:t="http://example.com"/>,
<t:test xmlns:t="http://example.com"/>,
Set.empty
DiffOptions.empty
) shouldBe XmlEqual
}

"compare with IgnoreNamespace" should "match different namespace prefix" in {
XmlCompare.compare(
<t:test xmlns:t="http://example.com"/>,
<e:test xmlns:e="http://example.com"/>,
Set(IgnoreNamespace)
DiffOptions(IgnoreNamespace)
) shouldBe XmlEqual
}

it should "match different namespace" in {
XmlCompare.compare(
<t:test xmlns:t="http://example.com"/>,
<t:test xmlns:e="http://example.org"/>,
Set(IgnoreNamespace)
DiffOptions(IgnoreNamespace)
) shouldBe XmlEqual
}

"compare with StrictAttributeOrder" should "match with same attributes" in {
XmlCompare.compare(
<test first="a" second="b" />,
<test first="a" second="b" />,
Set(StrictAttributeOrdering)
DiffOptions(StrictAttributeOrdering)
) shouldBe XmlEqual
}

it should "not-match with attributes in different order" in {
XmlCompare.compare(
<test first="a" second="b"/>,
<test second="b" first="a"/>,
Set(StrictAttributeOrdering)
DiffOptions(StrictAttributeOrdering)
) shouldBe XmlDiffers(
"different attribute ordering",
Seq("first", "second"),
Expand All @@ -205,31 +207,31 @@ class XmlCompareSpec extends FlatSpec with Matchers {
XmlCompare.compare(
<test><child-a/><child-b/><child-a/></test>,
<test><child-a/><child-b/><child-a/></test>,
Set(IgnoreChildOrder)
DiffOptions(IgnoreChildOrder)
) shouldBe XmlEqual
}

it should "match XML with children in different order" in {
XmlCompare.compare(
<test><child-a/><child-b/><child-a/></test>,
<test><child-b/><child-a/><child-a/></test>,
Set(IgnoreChildOrder)
DiffOptions(IgnoreChildOrder)
) shouldBe XmlEqual
}

it should "match XML with children in different order with attributes" in {
XmlCompare.compare(
<test><child value="a"/><child value="b"/><child value="c"/></test>,
<test><child value="b"/><child value="a"/><child value="c"/></test>,
Set(IgnoreChildOrder)
DiffOptions(IgnoreChildOrder)
) shouldBe XmlEqual
}

it should "match XML with children in different order with multiple attributes" in {
XmlCompare.compare(
<test><child first="a" second="b"/><child second="1" first="2"/></test>,
<test><child second="1" first="2"/><child second="b" first="a"/></test>,
Set(IgnoreChildOrder)
DiffOptions(IgnoreChildOrder)
) shouldBe XmlEqual
}
}
Loading