I'm writing some tests for a language tokenizer and I'm comparing a JSON-serialized version of the tokenization produced by the tokenizer with a serialization of a known-good tokenization. So I have some tests like this:
#[test]
fn test_tokenize() {
let actual_token_json = /* string */;
let expected_token_json = /* string */;
assert_eq!(actual_token_json, expected_token_json);
}
However, when those tests fail, cargo test
just says that the JSON strings weren't equal and prints them both out, which isn't that useful. Since the JSON strings are both pretty-printed, it would be more helpful if cargo test
printed out a line diff of the two strings. Is there any way I could do this?
Update: Someone edited the title of this post but deleted an important piece of information: I'm looking for a line diff when two strings aren't equal. One of the answers I'm seeing here is for the more general question of getting a line and character diff of the debug representations of two values when they're not equal. That's either more than I need or not what I need. For example, the diff of the debug representations of two strings won't give me a line diff but a large, convoluted character diff (since the lines show up in the debug representation as escaped "\n" tokens).