Skip to content
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

Fixes #24 crash when keys were removed from a map #29

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions src/main/scala/diff.scala
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ abstract class DiffShowInstances2 extends DiffShowInstancesLowPriority {
val identical = left.keys.toList intersect right.keys.toList
val removed = left.keys.toList diff right.keys.toList
val added = right.keys.toList diff left.keys.toList
def show( keys: List[K] ) = keys.map( k => DiffShow.show( k ) -> DiffShow.show( right( k ) ) )
def show( keys: List[K], m: Map[K, V] ) = keys.map( k => DiffShow.show( k ) -> DiffShow.show( m( k ) ) )
val changed = for {
key <- left.keys.toList diff removed
Different( s ) <- DiffShow.diff( left( key ), right( key ) ) :: Nil
Expand All @@ -316,8 +316,8 @@ abstract class DiffShowInstances2 extends DiffShowInstancesLowPriority {
"Map",
identical.map( _ => None ) ++ Seq(
changed,
show( removed ).map( ( arrow _ ).tupled ).map( red ),
show( added ).map( ( arrow _ ).tupled ).map( green )
show( removed, left ).map( ( arrow _ ).tupled ).map( red ),
show( added, right ).map( ( arrow _ ).tupled ).map( green )
).flatten.map( s => Option( ( "", s ) ) )
)
if ( removed.isEmpty && added.isEmpty && changed.isEmpty )
Expand Down
17 changes: 17 additions & 0 deletions src/test/scala/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,23 @@ class AllTests extends FunSuite {
assertIdentical( Foo1.Foo2.Foo3, Foo1.Foo2.Foo3 )
}

{
// testing issue when comparing Maps (https://github.com/xdotai/diff/issues/24)
println(
DiffShow[Map[String, String]](DiffShow.mapDiffShow).diff(
Map("a" -> "b"),
Map("a" -> "b", "c" -> "d")
).string
)

println(
DiffShow[Map[String, String]](DiffShow.mapDiffShow).diff(
Map("a" -> "b", "c" -> "d"),
Map("a" -> "b")
).string
)
}

/*

//import pprint.Config.Defaults._
Expand Down