johannhof / difference.rs

Rust text diffing and assertion library
https://docs.rs/difference
MIT License
242 stars 33 forks source link

Newlines-only diff are inconsistently reported #28

Open elinorbgr opened 6 years ago

elinorbgr commented 6 years ago

Consider this example:

fn main() {
    let input = r#"
Foo
Bar

Baz

Bam

Meh
"#;

    let output = r#"
Foo
Bar

Baz
Bam

Meh"#;

    let changeset = Changeset::new(input, output, "\n");
    println!("distance = {}", changeset.distance);
    println!("{:#?}", changeset.diffs);
}

It outputs this:

distance = 4
[
    Same(
        "Foo\nBar\n\nBaz\nBam\n\nMeh\n"
    )
]

There is an inconsistency here, there is a distance of 4 yet the diff is only a single Same entry.

I would typically have expected the diff to be something like:

[
    Same("Foo\nBar\n"),
    Rem("\n"),
    Same("Baz\n"),
    Rem("\n"),
    Same("Bam\n"),
    Rem("\n"),
    Same("Meh"),
    Rem("\n")
]
Rulexec commented 6 months ago

Same problem. I have strings count: 0 and count: 0\n, receiving distance = 1 and:

[
    Same(
        "count: 0\n",
    ),
]

UPD: solved it by following hack:

let (distance, diffs) = diff(expected, actual, "\n");

let (distance, diffs) = if distance != 0 && diffs.len() == 1 {
    diff(expected, actual, "")
} else {
    (distance, diffs)
};

And then added hack for formatting diffs, which checks for strings equal to "\n", which can happen only in this case and not printing them, so output will be:

    count: 0
  +