diff options
author | Dan Davison <dandavison7@gmail.com> | 2020-05-31 13:32:59 -0400 |
---|---|---|
committer | Dan Davison <dandavison7@gmail.com> | 2020-05-31 13:38:10 -0400 |
commit | 20eeaba4b3ad6dddff50a7d8f1864d68a1810c49 (patch) | |
tree | aaa12aedc29e3e953d3075f98eea476123f01bb7 /src/tests | |
parent | cfdd4492d4e9d88f0ce7f7f5d29118dcda67e609 (diff) |
Add support for 4-bit foreground colors in test utilities
4-bit color ANSI escape sequences look like: ^[1;31m
For foreground, they are the 8 color codes 30-37 and the 8 bright color codes 90-97
Delta does not actually emit them (they are converted to
ansi_term::Color::Fixed(n) by to_ansi_color), but git does by default,
so they are needed for testing delta's handling of colors received
from git.
Diffstat (limited to 'src/tests')
-rw-r--r-- | src/tests/ansi_test_utils.rs | 85 |
1 files changed, 72 insertions, 13 deletions
diff --git a/src/tests/ansi_test_utils.rs b/src/tests/ansi_test_utils.rs index 6e20fb1c..33ee5d0f 100644 --- a/src/tests/ansi_test_utils.rs +++ b/src/tests/ansi_test_utils.rs @@ -7,27 +7,38 @@ pub mod ansi_test_utils { use crate::paint; use crate::style::Style; - 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_style: &str, + config: &Config, + ) { + _assert_line_has_foreground_color( + output, + line_number, + expected_prefix, + expected_style, + config, + false, + ); } - pub fn assert_line_has_foreground_color( + pub fn assert_line_has_4_bit_foreground_color( output: &str, line_number: usize, expected_prefix: &str, expected_style: &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, - Style::from_str(expected_style, None, None, None, config.true_color, false) - .ansi_term_style - .foreground - .unwrap() - )); + _assert_line_has_foreground_color( + output, + line_number, + expected_prefix, + expected_style, + config, + true, + ); } pub fn assert_line_has_no_color(output: &str, line_number: usize, expected_prefix: &str) { @@ -105,4 +116,52 @@ pub mod ansi_test_utils { ); output_buffer } + + fn has_foreground_color(string: &str, color: ansi_term::Color, _4_bit_color: bool) -> bool { + let color = if _4_bit_color { + ansi_term_fixed_foreground_to_4_bit_color(color) + } else { + color + }; + let style = ansi_term::Style::default().fg(color); + string.starts_with(&style.prefix().to_string()) + } + + fn _assert_line_has_foreground_color( + output: &str, + line_number: usize, + expected_prefix: &str, + expected_style: &str, + config: &Config, + _4_bit_color: bool, + ) { + let line = output.lines().nth(line_number).unwrap(); + assert!(strip_ansi_codes(line).starts_with(expected_prefix)); + assert!(has_foreground_color( + line, + Style::from_str(expected_style, None, None, None, config.true_color, false) + .ansi_term_style + .foreground + .unwrap(), + _4_bit_color + )); + } + + fn ansi_term_fixed_foreground_to_4_bit_color(color: ansi_term::Color) -> ansi_term::Color { + match color { + ansi_term::Color::Fixed(30) => ansi_term::Color::Black, + ansi_term::Color::Fixed(31) => ansi_term::Color::Red, + ansi_term::Color::Fixed(32) => ansi_term::Color::Green, + ansi_term::Color::Fixed(33) => ansi_term::Color::Yellow, + ansi_term::Color::Fixed(34) => ansi_term::Color::Blue, + ansi_term::Color::Fixed(35) => ansi_term::Color::Purple, + ansi_term::Color::Fixed(36) => ansi_term::Color::Cyan, + ansi_term::Color::Fixed(37) => ansi_term::Color::White, + color => panic!( + "Invalid 4-bit color: {:?}. \ + (Add bright color entries to this map if needed for tests.", + color + ), + } + } } |