johannhof / difference.rs

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

The assert_diff function is hard to debug if used as a test. #7

Closed paul-english closed 8 years ago

paul-english commented 8 years ago

I tried using the assert_diff function to make some tests with long strings easier to debug. The problem with the function is the tests will fail and reference the line in the difference crate, not the tests where its used. This can be solved with a macro and makes using this function a lot more useful. This is my version of assert_diff!,

#[macro_export]
macro_rules! assert_diff {
    ($orig:expr , $edit:expr, $split: expr, $expected: expr) => ({
        use difference::{diff, print_diff};

        let orig = $orig;
        let edit = $edit;

        let (d, _) = diff(orig, edit, &($split));
        if d != $expected {
            print_diff(orig, edit, &($split));
            panic!("assertion failed: edit distance between {:?} and {:?} is {} and not {}, see \
                    diffset above",
                   orig,
                   edit,
                   d,
                   &($expected))
        }
    })
}
johannhof commented 8 years ago

Yeah, good point. Feel free to make a pull request! :)

paul-english commented 8 years ago

Been busy, I've added this in PR #9