Skip to content

Commit

Permalink
update diffx for argonaut
Browse files Browse the repository at this point in the history
  • Loading branch information
stephennancekivell committed Jul 24, 2021
1 parent d46a3f1 commit c99a31b
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 54 deletions.
2 changes: 1 addition & 1 deletion argonaut/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ name := "scalatest-argonaut"
libraryDependencies ++= Seq(
"org.scalatest" %% "scalatest" % "3.2.9",
"io.argonaut" %% "argonaut" % "6.3.6",
"com.softwaremill.diffx" %% "diffx-core" % "0.3.30"
"com.softwaremill.diffx" %% "diffx-core" % "0.5.3"
)
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package com.softwaremill.diffx

case class DiffResultJsArray(diffs: List[DiffResult])
extends DiffResultDifferent {
override private[diffx] def showIndented(indent: Int)(implicit c: ConsoleColorConfig): String = {
val showFields = diffs.map(f => s"${f.showIndented(indent)}")
case class DiffResultJsArray(diffs: List[DiffResult]) extends DiffResult {
override private[diffx] def showIndented(
indent: Int,
renderIdentical: Boolean
)(implicit c: ConsoleColorConfig): String = {
val showFields =
diffs.map(f => s"${f.showIndented(indent, renderIdentical)}")
showFields.mkString(s"[\n${i(indent)}", s",\n${i(indent)}", "]")
}

override def isIdentical: Boolean = diffs.forall(_.isIdentical)
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package com.softwaremill.diffx

case class DiffResultJson(fields: Map[DiffResult, DiffResult])
extends DiffResultDifferent {
override private[diffx] def showIndented(indent: Int)(implicit c: ConsoleColorConfig): String = {
extends DiffResult {
override private[diffx] def showIndented(
indent: Int,
renderIdentical: Boolean
)(implicit c: ConsoleColorConfig): String = {
val showFields =
fields.map(
f => s"""${i(indent)}"${f._1.show}": ${f._2.showIndented(indent + 5)}"""
fields.map(f =>
s"""${i(indent)}"${f._1
.show()}": ${f._2.showIndented(indent + 5, renderIdentical)}"""
)
showFields.mkString("{\n", ",\n", "}")
}

override def isIdentical: Boolean = fields.forall { case (k, v) =>
k.isIdentical && v.isIdentical
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@ trait JsonMatchers {

private implicit val diffJson: Diff[Json] = new Diff[Json] {
def apply(
left: Json,
right: Json,
toIgnore: List[_root_.com.softwaremill.diffx.FieldPath]
left: Json,
right: Json,
context: DiffContext
): DiffResult = {

def diffPrimative[A](leftA: Option[A], rightA: Option[A])(
show: A => String
show: A => String
): Option[DiffResult] =
diffWith(leftA, rightA) { (leftValue, rightValue) =>
if (leftValue == rightValue)
Identical(show(leftValue))
IdenticalValue(show(leftValue))
else
DiffResultValue(show(leftValue), show(rightValue))
}

def diffWith[A](leftA: Option[A], rightA: Option[A])(
fn: (A, A) => DiffResult
fn: (A, A) => DiffResult
): Option[DiffResult] = {
for {
leftValue <- leftA
Expand All @@ -46,16 +46,15 @@ trait JsonMatchers {
.orElse(diffPrimative(left.bool, right.bool)(_.asJson.nospaces))
.orElse({
if (left.isNull && right.isNull)
Some(Identical(left.asJson.nospaces))
Some(IdenticalValue(left.asJson.nospaces))
else
None
})
.orElse(diffWith(left.array, right.array) { (leftArr, rightArr) =>
val zipped: immutable.Seq[(Json, Json)] = leftArr.zip(rightArr)
val diffs: immutable.Seq[DiffResult] = zipped.map {
case (a, b) =>
val d: DiffResult = diffJson(a, b)
d
val diffs: immutable.Seq[DiffResult] = zipped.map { case (a, b) =>
val d: DiffResult = diffJson(a, b)
d
}

val missing = leftArr.drop(rightArr.size).map(DiffResultMissing.apply)
Expand All @@ -65,49 +64,45 @@ trait JsonMatchers {
val all = (diffs ++ missing ++ additional).toList

if (all.forall(_.isIdentical)) {
Identical(all)
IdenticalValue(all)
} else {
DiffResultJsArray(all)
}
})
.orElse(diffWith(left.obj, right.obj) {
case (leftObj, rightObj) =>
val leftMap = leftObj.toMap
val m: List[(DiffResult, DiffResult)] = leftObj.toList.map {
case (key, leftValue) =>
rightObj(key) match {
case Some(rightValue) =>
val d: DiffResult = diffJson(leftValue, rightValue)
Identical(key) -> d
case None =>
DiffResultMissing(key) -> DiffResultMissing(leftValue)
}
.orElse(diffWith(left.obj, right.obj) { case (leftObj, rightObj) =>
val leftMap = leftObj.toMap
val m: List[(DiffResult, DiffResult)] =
leftObj.toList.map { case (key, leftValue) =>
rightObj(key) match {
case Some(rightValue) =>
val d: DiffResult = diffJson(leftValue, rightValue)
IdenticalValue(key) -> d
case None =>
DiffResultMissing(key) -> DiffResultMissing(leftValue)
}
}

val rightKeys = rightObj.toMap.keySet
val leftKeys = leftMap.keySet
val additional: List[(DiffResult, DiffResult)] = rightKeys.toList
.filterNot(leftKeys.contains)
.flatMap(
k =>
rightObj(k).map(
v => DiffResultAdditional(k) -> DiffResultAdditional(v)
)
)

val all = m ++ additional
if (all.forall { case (k, v) => k.isIdentical && v.isIdentical }) {
Identical(leftObj)
} else {
DiffResultJson(all.toMap)
}
val rightKeys = rightObj.toMap.keySet
val leftKeys = leftMap.keySet
val additional: List[(DiffResult, DiffResult)] = rightKeys.toList
.filterNot(leftKeys.contains)
.flatMap(k =>
rightObj(k)
.map(v => DiffResultAdditional(k) -> DiffResultAdditional(v))
)

val all = m ++ additional
if (all.forall { case (k, v) => k.isIdentical && v.isIdentical }) {
IdenticalValue(leftObj)
} else {
DiffResultJson(all.toMap)
}
})
.getOrElse(DiffResultValue(left, right))
}
}

/**
* Checks if the given json objects are equivalent.
/** Checks if the given json objects are equivalent.
*/
def matchJson(right: String): Matcher[String] =
Matcher[String] { left =>
Expand All @@ -128,7 +123,7 @@ trait JsonMatchers {
)
case (Right(leftJs), Right(rightJs)) =>
val diffResult = Diff.compare(leftJs, rightJs)
val s = diffResult.show
val s = diffResult.show()
MatchResult(
matches = diffResult.isIdentical,
rawFailureMessage = "Json did not match.\n" + s,
Expand Down Expand Up @@ -159,7 +154,7 @@ trait JsonMatchers {
}

def matchJsonGolden[T: EncodeJson: DecodeJson: ClassTag](
value: T
value: T
): Matcher[String] = {
Matcher[String] { jsonString: String =>
val valueAsJson = value.asJson
Expand Down

0 comments on commit c99a31b

Please sign in to comment.