Skip to content

Commit

Permalink
Add always ignore differ
Browse files Browse the repository at this point in the history
  • Loading branch information
jatcwang committed Jul 27, 2021
1 parent f5f7b10 commit 62914c6
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ projectFilesBackup/
version.properties
RUNNING_PID
.metals
metals.sbt
.bsp
TempGo.scala
27 changes: 27 additions & 0 deletions modules/core/src/main/scala/difflicious/AlwaysIgnoreDiffer.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package difflicious

import difflicious.DiffResult.ValueResult

/** A Differ that always return an Ignored result.
* Useful when you can't really diff a type */
final class AlwaysIgnoreDiffer[T] extends Differ[T] {
override type R = ValueResult

override def diff(inputs: DiffInput[T]): ValueResult =
ValueResult.Both("[ALWAYS IGNORED]", "[ALWAYS IGNORED]", isSame = true, isIgnored = true)

override protected def configureIgnored(newIgnored: Boolean): Differ[T] = this

override protected def configurePath(
step: String,
nextPath: ConfigurePath,
op: ConfigureOp,
): Either[ConfigureError, Differ[T]] = Left(ConfigureError.PathTooLong(nextPath))

override protected def configurePairBy(
path: ConfigurePath,
op: ConfigureOp.PairBy[_],
): Either[ConfigureError, Differ[T]] = {
Left(ConfigureError.InvalidConfigureOp(path, op, "AlwaysIgnoreDiffer"))
}
}
3 changes: 3 additions & 0 deletions modules/core/src/main/scala/difflicious/Differ.scala
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ object Differ extends DifferTupleInstances with DifferGen with DifferTimeInstanc
def useEquals[T](valueToString: T => String): EqualsDiffer[T] =
new EqualsDiffer[T](isIgnored = false, valueToString = valueToString)

/** A Differ that always return an Ignored result. Useful when you can't really diff something */
def alwaysIgnore[T]: AlwaysIgnoreDiffer[T] = new AlwaysIgnoreDiffer[T]

// TODO: better string diff (edit distance and a description of how to get there?
// this can help especially in cases like extra space or special char)
implicit val stringDiffer: ValueDiffer[String] = useEquals[String](str => s""""$str"""")
Expand Down
40 changes: 40 additions & 0 deletions modules/coretest/src/test/scala/difflicious/DifferSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -830,4 +830,44 @@ class DifferSpec extends ScalaCheckSuite {
assertIsOkIfIgnoredProp(NewInt.differ)
}

test("Differ.alwaysIgnore: Always returns ignored result") {
assertEquals(
AlwaysIgnoreClass.differ.diff(AlwaysIgnoreClass(1), AlwaysIgnoreClass(2)),
DiffResult.ValueResult.Both(
"[ALWAYS IGNORED]",
"[ALWAYS IGNORED]",
isSame = true,
isIgnored = true,
),
)
}

test("Differ.alwaysIgnore: still return ignored result after unignore") {
assertEquals(
AlwaysIgnoreClass.differ.unignore.diff(AlwaysIgnoreClass(1), AlwaysIgnoreClass(2)): DiffResult,
DiffResult.ValueResult.Both(
"[ALWAYS IGNORED]",
"[ALWAYS IGNORED]",
isSame = true,
isIgnored = true,
),
)
}

test("Differ.alwaysIgnore: configurePath returns PathTooLong error") {
assertEquals(
intercept[ConfigureError](
AlwaysIgnoreClass.differ.configure(_.i)(_.ignore),
),
ConfigureError.PathTooLong(ConfigurePath(Vector("i"), Nil)),
)
}

test("Differ.alwaysIgnore: configurePairBy returns InvalidConfigureOp error") {
assertEquals(
AlwaysIgnoreClass.differ.configureRaw(ConfigurePath.current, ConfigureOp.PairBy.Index),
Left(ConfigureError.InvalidConfigureOp(ConfigurePath.current, ConfigureOp.PairBy.Index, "AlwaysIgnoreDiffer")),
)
}

}
6 changes: 6 additions & 0 deletions modules/coretest/src/test/scala/difflicious/testtypes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,10 @@ object testtypes {

final case class OpenSub(i: Int) extends OpenSuperType

case class AlwaysIgnoreClass(i: Int)

object AlwaysIgnoreClass {
implicit val differ: AlwaysIgnoreDiffer[AlwaysIgnoreClass] = Differ.alwaysIgnore
}

}

0 comments on commit 62914c6

Please sign in to comment.