diff options
author | Dan Davison <dandavison7@gmail.com> | 2020-05-20 22:46:08 -0400 |
---|---|---|
committer | Dan Davison <dandavison7@gmail.com> | 2020-05-22 22:45:37 -0400 |
commit | 2245582fbef18d94b0bef2fec3389f710dadc8d9 (patch) | |
tree | c3b70c9ec42e4ce95e0e26f807278eb636e70987 /src/tests | |
parent | 291d2fb7fa8cfc2a0886af895972326d00fb4dbe (diff) |
Implement hunk styles using style string arguments
- Do not apply foreground syntax style if it is "null syntect style"
This isn't really correct. We should find either a valid sentinel
value, or a way to only do the superimposing when we're doing
syntax-highlighting.
- Add --zero-style option (style for unchanged hunk lines)
- Implement --color-only using an option rewrite rule
Diffstat (limited to 'src/tests')
-rw-r--r-- | src/tests/ansi_test_utils.rs | 80 | ||||
-rw-r--r-- | src/tests/integration_test_utils.rs | 21 | ||||
-rw-r--r-- | src/tests/mod.rs | 1 | ||||
-rw-r--r-- | src/tests/test_hunk_highlighting.rs | 171 |
4 files changed, 13 insertions, 260 deletions
diff --git a/src/tests/ansi_test_utils.rs b/src/tests/ansi_test_utils.rs index ccfbab6f..7e2ce6bd 100644 --- a/src/tests/ansi_test_utils.rs +++ b/src/tests/ansi_test_utils.rs @@ -1,86 +1,8 @@ #[cfg(test)] pub mod ansi_test_utils { - use ansi_parser::{self, AnsiParser}; use console::strip_ansi_codes; - use itertools::Itertools; - use crate::config::{ColorLayer::*, Config}; - use crate::delta::State; - use crate::paint; - - use ansi_parser::AnsiSequence::*; - use ansi_parser::Output::*; - - // Note that ansi_parser seems to be parsing 24-bit sequences as TextBlock - // rather than Escape(SetGraphicsMode). As a workaround, we examime the - // TextBlock string value using functions such as - // string_has_some_background_color and string_has_some_foreground_color. - - pub fn is_syntax_highlighted(line: &str) -> bool { - line.ansi_parse() - .filter(|tok| match tok { - TextBlock(s) => string_has_some_foreground_color(s), - Escape(SetGraphicsMode(parameters)) => parameters[0] == 38, - _ => false, - }) - .next() - .is_some() - } - - pub fn line_has_background_color(line: &str, state: &State, config: &Config) -> bool { - line.ansi_parse() - .filter(|tok| match tok { - TextBlock(s) => string_has_background_color(s, state, config), - _ => false, - }) - .next() - .is_some() - } - - pub fn line_has_no_background_color(line: &str) -> bool { - line.ansi_parse() - .filter(|tok| match tok { - TextBlock(s) => string_has_some_background_color(s), - _ => false, - }) - .next() - .is_none() - } - - fn string_has_background_color(string: &str, state: &State, config: &Config) -> bool { - let painted = config - .get_color(state, Background) - .unwrap_or_else(|| panic!("state {:?} does not have a background color", state)) - .paint(""); - let ansi_sequence = painted - .trim_end_matches(paint::ANSI_SGR_RESET) - .trim_end_matches("m"); - string.starts_with(ansi_sequence) - } - - fn string_has_some_background_color(s: &str) -> bool { - s.starts_with("\x1b[48;") - } - - fn string_has_some_foreground_color(s: &str) -> bool { - s.starts_with("\x1b[38;") - } - - pub fn assert_line_has_expected_ansi_sequences(line: &str, expected: &Vec<(&str, &str)>) { - let parsed_line = line.ansi_parse().filter(|token| match token { - Escape(SetGraphicsMode(parameters)) if parameters == &vec![0 as u32] => false, - _ => true, - }); - for ((expected_ansi_sequence, _), ref token) in expected.iter().zip_eq(parsed_line) { - match token { - TextBlock(s) => { - assert!(s.starts_with(*expected_ansi_sequence)); - } - Escape(SetGraphicsMode(parameters)) => assert_eq!(parameters, &vec![0 as u32]), - _ => panic!("Unexpected token: {:?}", token), - } - } - } + use crate::config::Config; 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/integration_test_utils.rs b/src/tests/integration_test_utils.rs index cc4dc22b..1421ec56 100644 --- a/src/tests/integration_test_utils.rs +++ b/src/tests/integration_test_utils.rs @@ -8,22 +8,25 @@ pub mod integration_test_utils { use crate::config; use crate::delta::delta; + // TODO: These should be set in a more principled way, based on the default + // argument values. pub fn get_command_line_options() -> cli::Opt { cli::Opt { light: false, dark: false, - minus_style: None, - minus_emph_style: None, - plus_style: None, - plus_emph_style: None, - _deprecated_minus_color: None, - _deprecated_minus_emph_color: None, - _deprecated_plus_color: None, - _deprecated_plus_emph_color: None, + minus_style: "normal auto".to_string(), + minus_emph_style: "normal auto".to_string(), + zero_style: "syntax normal".to_string(), + plus_style: "syntax auto".to_string(), + plus_emph_style: "syntax auto".to_string(), + deprecated_minus_background_color: None, + deprecated_minus_emph_background_color: None, + deprecated_plus_background_color: None, + deprecated_plus_emph_background_color: None, color_only: false, keep_plus_minus_markers: false, theme: None, - highlight_minus_lines: false, + deprecated_highlight_minus_lines: false, commit_style: cli::SectionStyle::Plain, commit_color: "Yellow".to_string(), file_style: cli::SectionStyle::Underline, diff --git a/src/tests/mod.rs b/src/tests/mod.rs index afa09977..f95a9ef5 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -1,4 +1,3 @@ pub mod ansi_test_utils; pub mod integration_test_utils; pub mod test_example_diffs; -pub mod test_hunk_highlighting; diff --git a/src/tests/test_hunk_highlighting.rs b/src/tests/test_hunk_highlighting.rs deleted file mode 100644 index fdc5cfc5..00000000 --- a/src/tests/test_hunk_highlighting.rs +++ /dev/null @@ -1,171 +0,0 @@ -#[cfg(test)] -mod tests { - use itertools::Itertools; - - use ansi_term::Style; - - use crate::cli; - use crate::config::ColorLayer::*; - use crate::delta::State; - use crate::paint; - use crate::tests::ansi_test_utils::ansi_test_utils; - use crate::tests::integration_test_utils::integration_test_utils; - - const VERBOSE: bool = false; - - #[test] - fn test_hunk_highlighting() { - let mut options = integration_test_utils::get_command_line_options(); - options.theme = Some("GitHub".to_string()); - options.max_line_distance = 1.0; - let minus_emph_background = "#ffa0a0"; - let plus_emph_background = "#80ef80"; - for minus_foreground in vec!["none", "green"] { - for minus_emph_foreground in vec!["none", "#80ef80"] { - for plus_foreground in vec!["none", "red"] { - for plus_emph_foreground in vec!["none", "#ffa0a0"] { - options.minus_style = Some(minus_foreground.to_string()); - options.minus_emph_style = Some(format!( - "{} {}", - minus_emph_foreground, minus_emph_background - )); - options.plus_style = Some(plus_foreground.to_string()); - options.plus_emph_style = - Some(format!("{} {}", plus_emph_foreground, plus_emph_background)); - if VERBOSE { - println!(); - print!(" --minus-style {:?}", options.minus_style); - print!(" --minus-emph-style {:?}", options.minus_emph_style); - print!(" --plus-style {:?}", options.plus_style); - print!(" --plus-emph-style {:?}", options.plus_emph_style); - println!(); - } - _do_hunk_color_test(options.clone()); - } - } - } - } - } - - fn _do_hunk_color_test(options: cli::Opt) { - let (output, config) = integration_test_utils::run_delta( - DIFF_YIELDING_ALL_HUNK_LINE_COLOR_CATEGORIES, - options, - ); - let lines = output.trim().split("\n").skip(4); - - let minus = Style::new() - .on(config.minus_style.background.unwrap()) - .paint("") - .trim_end_matches(paint::ANSI_SGR_RESET) - .trim_end_matches("m") - .to_string(); - let minus_emph = Style::new() - .on(config.minus_emph_style.background.unwrap()) - .paint("") - .trim_end_matches(paint::ANSI_SGR_RESET) - .trim_end_matches("m") - .to_string(); - let plus = Style::new() - .on(config.plus_style.background.unwrap()) - .paint("") - .trim_end_matches(paint::ANSI_SGR_RESET) - .trim_end_matches("m") - .to_string(); - let plus_emph = Style::new() - .on(config.plus_emph_style.background.unwrap()) - .paint("") - .trim_end_matches(paint::ANSI_SGR_RESET) - .trim_end_matches("m") - .to_string(); - - let expectation = vec![ - // line 1: unchanged - ( - State::HunkZero, - vec![("", "(11111111, 11111111, 11111111)")], - ), - // line 2: removed, final token is minus-emph - ( - State::HunkMinus, - vec![ - (minus.as_str(), "(22222222, 22222222"), - (minus_emph.as_str(), ", 22222222"), - (minus.as_str(), ")"), - ], - ), - // line 3: removed - ( - State::HunkMinus, - vec![(minus.as_str(), "(33333333, 33333333, 33333333)")], - ), - // line 4: removed - ( - State::HunkMinus, - vec![(minus.as_str(), "(44444444, 44444444, 44444444)")], - ), - // line 5: added, and syntax-higlighted. - ( - State::HunkPlus, - vec![(plus.as_str(), "(22222222, 22222222)")], - ), - // line 6: added, and syntax-highlighted. First is plus-emph. - ( - State::HunkPlus, - vec![ - (plus.as_str(), "("), - (plus_emph.as_str(), "33333333, "), - (plus.as_str(), "33333333, 33333333, 33333333)"), - ], - ), - // line 7: unchanged - ( - State::HunkZero, - vec![("", "(55555555, 55555555, 55555555)")], - ), - // line 8: added, and syntax-highlighted. - ( - State::HunkPlus, - vec![(plus.as_str(), "(66666666, 66666666, 66666666)")], - ), - ]; - - // TODO: check same length - for ((state, assertion), line) in expectation.iter().zip_eq(lines) { - if VERBOSE { - println!("{}", line) - }; - if config.should_syntax_highlight(state) { - assert!(ansi_test_utils::is_syntax_highlighted(line)); - } else { - // An explicit assertion about the ANSI sequences should be available (when there's - // syntax highlighting the pattern of ANSI sequences is too complex to make the - // assertion). - ansi_test_utils::assert_line_has_expected_ansi_sequences(line, &assertion) - } - // Background color should match the line's state. - match config.get_color(state, Background) { - Some(_color) => assert!(ansi_test_utils::line_has_background_color( - line, state, &config - )), - None => assert!(ansi_test_utils::line_has_no_background_color(line)), - } - } - } - - const DIFF_YIELDING_ALL_HUNK_LINE_COLOR_CATEGORIES: &str = r" -diff --git a/file.py b/file.py -index 15c0fa2..dc2254c 100644 ---- a/file.py -+++ b/file.py -@@ -1,6 +1,6 @@ - (11111111, 11111111, 11111111) --(22222222, 22222222, 22222222) --(33333333, 33333333, 33333333) --(44444444, 44444444, 44444444) -+(22222222, 22222222) -+(33333333, 33333333, 33333333, 33333333) - (55555555, 55555555, 55555555) -+(66666666, 66666666, 66666666) -"; -} |