New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Differ Enhancement #2177
Merged
bvenners
merged 6 commits into
scalatest:3.2.x-new
from
cheeseng:feature-differ-enhancement
Nov 30, 2022
Merged
Differ Enhancement #2177
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ce94c93
Enhanced GenSeqDiffer, GenSetDiffer and GenMapDiffer to use the passe…
cheeseng f81da3e
Enhanced GenSeqDiffer to support find diff elements according to limi…
cheeseng 381a406
Enhanced GenSeqDiffer to support find diff elements according to limi…
cheeseng 4f13337
Enhanced GenMapDiffer to support find diff elements according to limi…
cheeseng df6a3e2
Removed println for debugging.
cheeseng 55f9f6e
Refactored the code to get prettifier limit as a function.
cheeseng File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ package org.scalactic | |
// SKIP-SCALATESTNATIVE-START | ||
import org.scalactic.source.ObjectMeta | ||
// SKIP-SCALATESTNATIVE-END | ||
import scala.annotation.tailrec | ||
|
||
private[scalactic] trait Differ { | ||
|
||
|
@@ -115,31 +116,36 @@ private[scalactic] object StringDiffer extends StringDiffer | |
|
||
private[scalactic] class GenSeqDiffer extends Differ { | ||
|
||
@tailrec | ||
private def recurDiff(prettifier: Prettifier, aSeq: scala.collection.GenSeq[_], bSeq: scala.collection.GenSeq[_], limit: Int, idx: Int = 0, result: Vector[String] = Vector.empty): Vector[String] = | ||
if (result.length <= limit) | ||
(aSeq.headOption, bSeq.headOption) match { | ||
case (Some(leftEl), Some(rightEl)) => | ||
recurDiff(prettifier, aSeq.tail, bSeq.tail, limit, idx + 1, | ||
result ++ (if (leftEl != rightEl) Vector(idx + ": " + prettifier(leftEl) + " -> " + prettifier(rightEl)) else Vector.empty)) | ||
case (Some(leftEl), None) => | ||
recurDiff(prettifier, aSeq.tail, bSeq, limit, idx + 1, result :+ (idx + ": " + prettifier(leftEl) + " -> ")) | ||
case (None, Some(rightEl)) => | ||
recurDiff(prettifier, aSeq, bSeq.tail, limit, idx + 1, result :+ (idx + ": -> " + prettifier(rightEl))) | ||
case (None, None) => | ||
result | ||
} | ||
else result.dropRight(1) :+ "..." | ||
|
||
def difference(a: Any, b: Any, prettifier: Prettifier): PrettyPair = { | ||
(a, b) match { | ||
case (aSeq: scala.collection.GenSeq[_], bSeq: scala.collection.GenSeq[_]) => | ||
val diffSet = | ||
((0 until aSeq.length) flatMap { i => | ||
val leftEl = aSeq(i) | ||
if (bSeq.isDefinedAt(i)) { | ||
val rightEl = bSeq(i) | ||
if (leftEl != rightEl) | ||
Some(i + ": " + leftEl + " -> " + rightEl) | ||
else | ||
None | ||
} | ||
else | ||
Some(i + ": " + leftEl + " -> ") | ||
}).toSet ++ | ||
((aSeq.length until bSeq.length) flatMap { i => | ||
Some(i + ": -> " + bSeq(i)) | ||
}).toSet | ||
|
||
val limit = | ||
prettifier match { | ||
case tp: TruncatingPrettifier => tp.sizeLimit.value | ||
case _ => math.max(aSeq.length, bSeq.length) | ||
} | ||
val diffs = recurDiff(prettifier, aSeq, bSeq, limit) | ||
val shortName = Differ.simpleClassName(aSeq) | ||
if (diffSet.isEmpty) | ||
if (diffs.isEmpty) | ||
PrettyPair(prettifier(a), prettifier(b), None) | ||
else | ||
PrettyPair(prettifier(a), prettifier(b), Some(shortName + "(" + diffSet.toList.sorted.mkString(", ") + ")")) | ||
PrettyPair(prettifier(a), prettifier(b), Some(shortName + "(" + diffs.mkString(", ") + ")")) | ||
|
||
case _ => PrettyPair(prettifier(a), prettifier(b), None) | ||
} | ||
|
@@ -154,17 +160,27 @@ private[scalactic] class GenSetDiffer extends Differ { | |
def difference(a: Any, b: Any, prettifier: Prettifier): PrettyPair = { | ||
(a, b) match { | ||
case (aSet: scala.collection.GenSet[_], bSet: scala.collection.GenSet[_]) => | ||
val missingInRight = aSet.toList.diff(bSet.toList) | ||
val missingInLeft = bSet.toList.diff(aSet.toList) | ||
val missingInRight = aSet.toList.diff(bSet.toList).map(prettifier.apply) | ||
val missingInLeft = bSet.toList.diff(aSet.toList).map(prettifier.apply) | ||
|
||
val limit = | ||
prettifier match { | ||
case tp: TruncatingPrettifier => tp.sizeLimit.value | ||
case _ => math.max(aSet.size, bSet.size) | ||
} | ||
|
||
val limitedMissingInRight: List[String] = if (missingInRight.length > limit) missingInRight.take(limit) :+ "..." else missingInRight | ||
val limitedMissingInLeft: List[String] = if (missingInLeft.length > limit) missingInLeft.take(limit) :+ "..." else missingInLeft | ||
|
||
val shortName = Differ.simpleClassName(aSet) | ||
println("####shortName: " + shortName + ", ") | ||
if (missingInLeft.isEmpty && missingInRight.isEmpty) | ||
PrettyPair(prettifier(a), prettifier(b), None) | ||
else { | ||
val diffList = | ||
List( | ||
if (missingInLeft.isEmpty) "" else "missingInLeft: [" + missingInLeft.mkString(", ") + "]", | ||
if (missingInRight.isEmpty) "" else "missingInRight: [" + missingInRight.mkString(", ") + "]" | ||
if (limitedMissingInLeft.isEmpty) "" else "missingInLeft: [" + limitedMissingInLeft.mkString(", ") + "]", | ||
if (limitedMissingInRight.isEmpty) "" else "missingInRight: [" + limitedMissingInRight.mkString(", ") + "]" | ||
).filter(_.nonEmpty) | ||
PrettyPair(prettifier(a), prettifier(b), Some(shortName + "(" + diffList.mkString(", ") + ")")) | ||
} | ||
|
@@ -179,43 +195,46 @@ private[scalactic] object GenSetDiffer extends GenSetDiffer | |
|
||
private[scalactic] class GenMapDiffer[K, V] extends Differ { | ||
|
||
@tailrec | ||
private def recurDiff[AK, AV, BK, BV](prettifier: Prettifier, aMap: scala.collection.GenMap[AK, AV], bMap: scala.collection.GenMap[BK, BV], limit: Int, result: Vector[String] = Vector.empty): Vector[String] = | ||
if (result.length <= limit) | ||
aMap.headOption match { | ||
case Some((aKey, aValue)) => | ||
// This gives a compiler error due to k being a wildcard type: | ||
// val rightValue = bMap(k) | ||
// Not sure why aMap(k) doesn't give the same error, but regardless, fixing it | ||
// by pulling the value out for the key using a == comparison, which for now | ||
// works because of universal equality, then assuming the value exists, just | ||
// as bMap(k) previously was assuming. | ||
bMap.collect { case (nextK, nextV) if nextK == aKey => nextV }.headOption match { | ||
case Some(bValue) => | ||
recurDiff(prettifier, aMap.tail, bMap.filter(_._1 != aKey), limit, | ||
result ++ (if (aValue != bValue) Vector(prettifier(aKey) + ": " + prettifier(aValue) + " -> " + prettifier(bValue)) else Vector.empty)) | ||
case None => | ||
recurDiff(prettifier, aMap.tail, bMap, limit, result :+ (prettifier(aKey) + ": " + prettifier(aValue) + " -> ")) | ||
} | ||
case None => | ||
result ++ bMap.map { case (bKey, bValue) => | ||
prettifier(bKey) + ": -> " + prettifier(bValue) | ||
} | ||
} | ||
else result.dropRight(1) :+ "..." | ||
|
||
def difference(a: Any, b: Any, prettifier: Prettifier): PrettyPair = | ||
(a, b) match { | ||
case (aMap: scala.collection.GenMap[_, _], bMap: scala.collection.GenMap[_, _]) => | ||
val leftKeySet = aMap.keySet | ||
val rightKeySet = bMap.keySet | ||
val missingKeyInRight = leftKeySet.toList.diff(rightKeySet.toList) | ||
val missingKeyInLeft = rightKeySet.toList.diff(leftKeySet.toList) | ||
val intersectKeys = leftKeySet.toList.intersect(rightKeySet.toList) | ||
val diffSet = | ||
intersectKeys.flatMap { k => | ||
val leftValue = aMap(k) | ||
// This gives a compiler error due to k being a wildcard type: | ||
// val rightValue = bMap(k) | ||
// Not sure why aMap(k) doesn't give the same error, but regardless, fixing it | ||
// by pulling the value out for the key using a == comparison, which for now | ||
// works because of universal equality, then assuming the value exists, just | ||
// as bMap(k) previously was assuming. | ||
val rightValue = bMap.collect { case (nextK, nextV) if nextK == k => nextV }.head | ||
if (leftValue != rightValue) | ||
Some(k.toString + ": " + leftValue + " -> " + rightValue) | ||
else | ||
None | ||
}.toSet ++ | ||
missingKeyInLeft.flatMap { k => | ||
val rightValue = bMap(k) | ||
Option(k.toString + ": -> " + rightValue) | ||
}.toSet ++ | ||
missingKeyInRight.flatMap { k => | ||
val leftValue = aMap(k) | ||
Option(k.toString + ": " + leftValue + " -> ") | ||
}.toSet | ||
val limit = | ||
prettifier match { | ||
case tp: TruncatingPrettifier => tp.sizeLimit.value | ||
case _ => math.max(aMap.size, bMap.size) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This match seems to be repeated 3 times. Perhaps factor out into a method? |
||
val diffs = recurDiff(prettifier, aMap, bMap, limit) | ||
|
||
val shortName = Differ.simpleClassName(aMap) | ||
if (diffSet.isEmpty) | ||
if (diffs.isEmpty) | ||
PrettyPair(prettifier(a), prettifier(b), None) | ||
else | ||
PrettyPair(prettifier(a), prettifier(b), Some(shortName + "(" + diffSet.toList.sorted.mkString(", ") + ")")) | ||
PrettyPair(prettifier(a), prettifier(b), Some(shortName + "(" + diffs.mkString(", ") + ")")) | ||
|
||
case _ => | ||
PrettyPair(prettifier(a), prettifier(b), None) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you intend to remove this println?