From 6c4d00aad58c72015941ab86c042b3db1d3f7df9 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Mon, 7 Jan 2019 22:01:40 -0800 Subject: [PATCH] Print diffs when tests fail --- Cargo.lock | 7 +++++++ cli/Cargo.toml | 1 + cli/src/main.rs | 8 ++------ cli/src/test.rs | 29 +++++++++++++++++++++++++---- 4 files changed, 35 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7a249312..db86e43b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -156,6 +156,11 @@ dependencies = [ "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "difference" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "dirs" version = "1.0.4" @@ -643,6 +648,7 @@ version = "0.1.0" dependencies = [ "ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)", + "difference 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "dirs 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", "hashbrown 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", "ignore 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)", @@ -764,6 +770,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum crossbeam-epoch 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2449aaa4ec7ef96e5fb24db16024b935df718e9ae1cec0a1e68feeca2efca7b8" "checksum crossbeam-utils 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "677d453a17e8bd2b913fa38e8b9cf04bcdbb5be790aa294f2389661d72036015" "checksum crossbeam-utils 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c55913cc2799171a550e307918c0a360e8c16004820291bf3b638969b4a01816" +"checksum difference 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "524cbf6897b527295dff137cec09ecf3a05f4fddffd7dfcd1585403449e74198" "checksum dirs 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "88972de891f6118092b643d85a0b28e0678e0f948d7f879aa32f2d5aafe97d2a" "checksum failure 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "6dd377bcc1b1b7ce911967e3ec24fa19c3224394ec05b54aa7b083d498341ac7" "checksum failure_derive 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "64c2d913fe8ed3b6c6518eedf4538255b989945c14c2a7d5cbff62a5e2120596" diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 200fd2f1..a2f546c4 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -10,6 +10,7 @@ path = "src/main.rs" [dependencies] ansi_term = "0.11" +difference = "2.0" lazy_static = "1.2.0" smallbitvec = "2.3.0" clap = "2.32" diff --git a/cli/src/main.rs b/cli/src/main.rs index 6a0cf9ec..626a729c 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -74,9 +74,7 @@ fn run() -> error::Result<()> { generate::generate_parser_for_grammar(&grammar_path, minimize, state_ids_to_log)?; println!("{}", code); return Ok(()); - } - - if let Some(_matches) = matches.subcommand_matches("test") { + } else if let Some(_matches) = matches.subcommand_matches("test") { let corpus_path = current_dir.join("corpus"); let home_dir = dirs::home_dir().unwrap(); let mut loader = Loader::new(home_dir.join(".tree-sitter")); @@ -85,9 +83,7 @@ fn run() -> error::Result<()> { } else { eprintln!("No language found"); } - } - - if let Some(matches) = matches.subcommand_matches("parse") { + } else if let Some(matches) = matches.subcommand_matches("parse") { loader.find_parsers(&vec![home_dir.join("github")])?; let source_path = Path::new(matches.value_of("path").unwrap()); if let Some((language, _)) = loader.language_for_file_name(source_path)? { diff --git a/cli/src/test.rs b/cli/src/test.rs index 7ef63bb7..a693576d 100644 --- a/cli/src/test.rs +++ b/cli/src/test.rs @@ -1,5 +1,6 @@ use super::error::Result; use ansi_term::Colour; +use difference::{Changeset, Difference}; use regex::bytes::{Regex as ByteRegex, RegexBuilder as ByteRegexBuilder}; use regex::Regex; use std::char; @@ -55,10 +56,30 @@ pub fn run_tests_at_path(language: Language, path: &Path) -> Result<()> { println!("{} failures:", failures.len()) } - for (name, actual, expected) in failures { - println!("\n {}:", name); - println!(" Expected: {}", expected); - println!(" Actual: {}", actual); + println!( + "\n{} / {}", + Colour::Green.paint("expected"), + Colour::Red.paint("actual") + ); + + for (i, (name, actual, expected)) in failures.iter().enumerate() { + println!("\n {}. {}:", i + 1, name); + let changeset = Changeset::new(actual, expected, " "); + print!(" "); + for diff in &changeset.diffs { + match diff { + Difference::Same(part) => { + print!("{}{}", part, changeset.split); + } + Difference::Add(part) => { + print!("{}{}", Colour::Green.paint(part), changeset.split); + } + Difference::Rem(part) => { + print!("{}{}", Colour::Red.paint(part), changeset.split); + } + } + } + println!(""); } }