Skip to content

Instantly share code, notes, and snippets.

@ppazos
Forked from sujeet100/MapDiff.groovy
Created March 2, 2021 23:06
Show Gist options
  • Save ppazos/78b85f5db9da0e525388aedc480bfce7 to your computer and use it in GitHub Desktop.
Save ppazos/78b85f5db9da0e525388aedc480bfce7 to your computer and use it in GitHub Desktop.
Groovy script to find out the difference between two maps. Can be useful to debug failing unit tests when the objects in comparison are big jsons.
def mapDiff(Map m1, Map m2, String path="") { m1.each{k,v->
if(m2[k] != m1[k]) {
if(m1[k] in Map) {
mapDiff(m1[k] as Map, m2[k] as Map, "${path}${k}.")
}
else {
println("${path}${k} ->");
println("\texpected: ${m1[k]}")
println("\tactual: ${m2[k]}")
if (m1[k] in List) {
print("\texpected but missing elements: ")
m1[k].each {
if(!m2[k].any{it1->return it1==it}) {
print(it + ", ")
}
}
print("\tnot expected but found: ")
m2[k].each {
if(!m1[k].any{it1->return it1==it}) {
print(it + ", ")
}
}
}
println("")
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment