diff options
-rw-r--r-- | src/config.rs | 2 | ||||
-rw-r--r-- | src/tests/ansi_test_utils.rs | 30 | ||||
-rw-r--r-- | src/tests/test_example_diffs.rs | 272 |
3 files changed, 301 insertions, 3 deletions
diff --git a/src/config.rs b/src/config.rs index f57d5a52..28420824 100644 --- a/src/config.rs +++ b/src/config.rs @@ -321,7 +321,7 @@ pub fn parse_style_string( style } -fn color_from_rgb_or_ansi_code(s: &str, true_color: bool) -> Color { +pub fn color_from_rgb_or_ansi_code(s: &str, true_color: bool) -> Color { let die = || { eprintln!("Invalid color: {}", s); process::exit(1); diff --git a/src/tests/ansi_test_utils.rs b/src/tests/ansi_test_utils.rs index b1fc10d8..bb7d52d8 100644 --- a/src/tests/ansi_test_utils.rs +++ b/src/tests/ansi_test_utils.rs @@ -1,8 +1,36 @@ #[cfg(test)] pub mod ansi_test_utils { + use ansi_term; use console::strip_ansi_codes; - use crate::config::Config; + use crate::config::{color_from_rgb_or_ansi_code, Config}; + + pub fn has_foreground_color(string: &str, color: ansi_term::Color) -> bool { + let style = ansi_term::Style::default().fg(color); + string.starts_with(&style.prefix().to_string()) + } + + pub fn assert_line_has_foreground_color( + output: &str, + line_number: usize, + expected_prefix: &str, + expected_color: &str, + config: &Config, + ) { + let line = output.lines().nth(line_number).unwrap(); + assert!(strip_ansi_codes(line).starts_with(expected_prefix)); + assert!(has_foreground_color( + line, + color_from_rgb_or_ansi_code(expected_color, config.true_color) + )); + } + + pub fn assert_line_has_no_color(output: &str, line_number: usize, expected_prefix: &str) { + let line = output.lines().nth(line_number).unwrap(); + let stripped_line = strip_ansi_codes(line); + assert!(stripped_line.starts_with(expected_prefix)); + assert_eq!(line, stripped_line); + } pub fn assert_has_color_other_than_plus_color(string: &str, config: &Config) { let (string_without_any_color, string_with_plus_color_only) = diff --git a/src/tests/test_example_diffs.rs b/src/tests/test_example_diffs.rs index a01c94ce..fc68f4e9 100644 --- a/src/tests/test_example_diffs.rs +++ b/src/tests/test_example_diffs.rs @@ -1,8 +1,10 @@ #[cfg(test)] mod tests { + use console::strip_ansi_codes; + + use crate::cli::SectionStyle; use crate::tests::ansi_test_utils::ansi_test_utils; use crate::tests::integration_test_utils::integration_test_utils; - use console::strip_ansi_codes; #[test] fn test_added_file() { @@ -202,6 +204,274 @@ mod tests { assert!(output.contains("\n Subject: [PATCH] Init\n")); } + #[test] + fn test_commit_style_plain() { + let mut options = integration_test_utils::get_command_line_options(); + options.commit_style = SectionStyle::Plain; + // TODO: --commit-color has no effect in conjunction with --commit-style plain + let (output, _) = integration_test_utils::run_delta(GIT_DIFF_SINGLE_HUNK, options); + ansi_test_utils::assert_line_has_no_color( + &output, + 0, + "commit 94907c0f136f46dc46ffae2dc92dca9af7eb7c2e", + ); + let output = strip_ansi_codes(&output); + assert!(output.contains( + "\ +commit 94907c0f136f46dc46ffae2dc92dca9af7eb7c2e +" + )); + } + + #[test] + fn test_commit_style_box() { + let mut options = integration_test_utils::get_command_line_options(); + options.commit_style = SectionStyle::Box; + options.commit_color = "blue".to_string(); + let (output, config) = integration_test_utils::run_delta(GIT_DIFF_SINGLE_HUNK, options); + ansi_test_utils::assert_line_has_foreground_color( + &output, + 0, + "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓", + "blue", + &config, + ); + ansi_test_utils::assert_line_has_foreground_color( + &output, + 1, + "commit 94907c0f136f46dc46ffae2dc92dca9af7eb7c2e ┃", + "blue", + &config, + ); + ansi_test_utils::assert_line_has_foreground_color( + &output, + 2, + "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┻━", + "blue", + &config, + ); + let output = strip_ansi_codes(&output); + assert!(output.contains( + "\ +━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ +commit 94907c0f136f46dc46ffae2dc92dca9af7eb7c2e ┃ +━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┻━" + )); + } + + #[test] + fn test_commit_style_underline() { + let mut options = integration_test_utils::get_command_line_options(); + options.commit_style = SectionStyle::Underline; + options.commit_color = "yellow".to_string(); + let (output, config) = integration_test_utils::run_delta(GIT_DIFF_SINGLE_HUNK, options); + ansi_test_utils::assert_line_has_foreground_color( + &output, + 0, + "commit 94907c0f136f46dc46ffae2dc92dca9af7eb7c2e", + "yellow", + &config, + ); + ansi_test_utils::assert_line_has_foreground_color( + &output, + 1, + "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━", + "yellow", + &config, + ); + let output = strip_ansi_codes(&output); + assert!(output.contains( + "\ +commit 94907c0f136f46dc46ffae2dc92dca9af7eb7c2e +━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" + )); + } + + #[test] + fn test_file_style_plain() { + let mut options = integration_test_utils::get_command_line_options(); + options.file_style = SectionStyle::Plain; + // TODO: --file-color has no effect in conjunction with --file-style plain + let (output, _) = integration_test_utils::run_delta(GIT_DIFF_SINGLE_HUNK, options); + for (i, line) in vec![ + "diff --git a/src/align.rs b/src/align.rs", + "index 8e37a9e..6ce4863 100644", + "--- a/src/align.rs", + "+++ b/src/align.rs", + ] + .iter() + .enumerate() + { + ansi_test_utils::assert_line_has_no_color(&output, 6 + i, line); + } + let output = strip_ansi_codes(&output); + assert!(output.contains( + " +diff --git a/src/align.rs b/src/align.rs +index 8e37a9e..6ce4863 100644 +--- a/src/align.rs ++++ b/src/align.rs +" + )); + } + + #[test] + fn test_file_style_box() { + let mut options = integration_test_utils::get_command_line_options(); + options.file_style = SectionStyle::Box; + options.file_color = "green".to_string(); + let (output, config) = integration_test_utils::run_delta(GIT_DIFF_SINGLE_HUNK, options); + ansi_test_utils::assert_line_has_foreground_color( + &output, + 7, + "─────────────┐", + "green", + &config, + ); + ansi_test_utils::assert_line_has_foreground_color( + &output, + 8, + "src/align.rs │", + "green", + &config, + ); + ansi_test_utils::assert_line_has_foreground_color( + &output, + 9, + "─────────────┴─", + "green", + &config, + ); + let output = strip_ansi_codes(&output); + assert!(output.contains( + " +─────────────┐ +src/align.rs │ +─────────────┴─" + )); + } + + #[test] + fn test_file_style_underline() { + let mut options = integration_test_utils::get_command_line_options(); + options.file_style = SectionStyle::Underline; + options.file_color = "magenta".to_string(); + let (output, config) = integration_test_utils::run_delta(GIT_DIFF_SINGLE_HUNK, options); + ansi_test_utils::assert_line_has_foreground_color( + &output, + 7, + "src/align.rs", + "magenta", + &config, + ); + ansi_test_utils::assert_line_has_foreground_color( + &output, + 8, + "────────────", + "magenta", + &config, + ); + let output = strip_ansi_codes(&output); + assert!(output.contains( + " +src/align.rs +────────────" + )); + } + + #[test] + fn test_hunk_style_plain() { + let mut options = integration_test_utils::get_command_line_options(); + options.hunk_style = SectionStyle::Plain; + // TODO: --hunk-color has no effect in conjunction with --hunk-style plain + let (output, _) = integration_test_utils::run_delta(GIT_DIFF_SINGLE_HUNK, options); + ansi_test_utils::assert_line_has_no_color( + &output, + 9, + "@@ -71,11 +71,8 @@ impl<'a> Alignment<'a> {", + ); + let output = strip_ansi_codes(&output); + assert!(output.contains("@@ -71,11 +71,8 @@ impl<'a> Alignment<'a> {")); + } + + #[test] + fn test_hunk_style_box() { + let mut options = integration_test_utils::get_command_line_options(); + options.hunk_style = SectionStyle::Box; + options.hunk_color = "white".to_string(); + let (output, config) = integration_test_utils::run_delta(GIT_DIFF_SINGLE_HUNK, options); + ansi_test_utils::assert_line_has_foreground_color( + &output, + 9, + "──────────────────────────┐", + "white", + &config, + ); + ansi_test_utils::assert_line_has_foreground_color( + &output, + 11, + "──────────────────────────┘", + "white", + &config, + ); + let output = strip_ansi_codes(&output); + assert!(output.contains( + " +──────────────────────────┐ + impl<'a> Alignment<'a> { │ +──────────────────────────┘ +" + )); + } + + #[test] + fn test_hunk_style_underline() { + let mut options = integration_test_utils::get_command_line_options(); + options.hunk_style = SectionStyle::Underline; + options.hunk_color = "black".to_string(); + let (output, config) = integration_test_utils::run_delta(GIT_DIFF_SINGLE_HUNK, options); + ansi_test_utils::assert_line_has_foreground_color( + &output, + 10, + "─────────────────────────", + "black", + &config, + ); + let output = strip_ansi_codes(&output); + assert!(output.contains( + " + impl<'a> Alignment<'a> { +─────────────────────────" + )); + } + + const GIT_DIFF_SINGLE_HUNK: &str = "\ +commit 94907c0f136f46dc46ffae2dc92dca9af7eb7c2e +Author: Dan Davison <dandavison7@gmail.com> +Date: Thu May 14 11:13:17 2020 -0400 + + rustfmt + +diff --git a/src/align.rs b/src/align.rs +index 8e37a9e..6ce4863 100644 +--- a/src/align.rs ++++ b/src/align.rs +@@ -71,11 +71,8 @@ impl<'a> Alignment<'a> { + + for (i, x_i) in self.x.iter().enumerate() { + for (j, y_j) in self.y.iter().enumerate() { +- let (left, diag, up) = ( +- self.index(i, j + 1), +- self.index(i, j), +- self.index(i + 1, j), +- ); ++ let (left, diag, up) = ++ (self.index(i, j + 1), self.index(i, j), self.index(i + 1, j)); + let candidates = [ + Cell { + parent: left, +"; + const DIFF_IN_DIFF: &str = "\ diff --git a/0001-Init.patch b/0001-Init.patch deleted file mode 100644 |