johannhof / difference.rs

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

add fuzzy testing #20

Closed colin-kiegel closed 7 years ago

colin-kiegel commented 7 years ago

This adds fuzzy testing via quickcheck :-)

The basic idea is that each sequence set of differences can be asserted

#[test]
fn issue_19() {
    // this should work but it doesn't
    // https://github.com/johannhof/difference.rs/issues/19
    quickcheck(check_changeset("a b : g",
                               "b a : b b : g g",
                               " "));
}

This internally calls Changeset::new("a b : g", "b a : b b : g g", " "), which leads to the differences [Same("") Rem("a") Same("b") Add("a") Same(":") Rem("g") Add("b b") Same(": g") Add("g")].

We can assert them one by one, while stripping our slices accordingly.

Ok, now the last assertion fails with Error: `Same(": g")` does not match `old` at "" for Changeset::new("a b : g", "b a : b b : g g", " ") -> [Same("") Rem("a") Same("b") Add("a") Same(":") Rem("g") Add("b b") Same(": g") Add("g")]

Fuzzy Testing

Now the interesting part is that we can use quickcheck to generate random inputs. These tests pass most of the time, but occasionally quickcheck indeed finds a bogus combination like: Error: `Same("\u{1} ")` does not match `old` at "\u{1}" for Changeset::new("\u{0} \u{0} \u{1}", "\u{1} \u{0}", " ") -> [Same("") Add("\u{1}") Same("\u{0}") Rem("\u{0}") Same("\u{1} ")]

Here the output does not look so nice, because quickcheck tries all that unicode has to offer. Theoretically it would be possible to restrict the tests to latin characters, but that would contradict the general idea of fuzzy testing a little.

colin-kiegel commented 7 years ago

The results look like this: https://travis-ci.org/johannhof/difference.rs/jobs/220621495#L289

I've baked a const DEBUG: bool into the test. Setting it to true will print all assertions to stdout.

Let me know if I should change anything. :-)

PS: The CI tests only fail, because this unveils some edge case bugs.

johannhof commented 7 years ago

Oh, and please rebase on master and run the latest rustfmt :)

Thanks again!

colin-kiegel commented 7 years ago

done :-)

johannhof commented 7 years ago

Great, thanks. I'll open an issue for fixing the fuzzing results.