splitwise / super_diff

A more helpful way to view differences between complex data structures in RSpec.
https://splitwise.github.io/super_diff/
MIT License
970 stars 50 forks source link

Difference of arrays should be smarter #5

Closed mcmire closed 5 years ago

mcmire commented 13 years ago

Currently if you have two arrays:

a = %w(a b c d)
b = %w(1 2 a b)

and you diff them, the differ will tell you this, basically:

0: "a" != "1"
1: "b" != "2"
2: "c" != "a"
3: "d" != "b"

Now, ideally, this is not very helpful. We should be able to figure out that "1" and "2" were inserted before "a" and that "c" and "d" were removed. So maybe the output would be exactly that:

- "1", "2" inserted before "a"
- "b", "c" deleted after "b"

Notice that we aren't listing the array indices anymore, since those are no longer meaningful.

This actually sounds like a proper use case for the LCS algorithm.

mcmire commented 13 years ago

Conceptually, we need to extend Diff::LCS so that if a value was changed (i.e. a deletion and addition occurred at the same position), and that value is an array, we need to descend into that array and perform LCS on it. If it's a hash, we need to do what we normally do.