summaryrefslogtreecommitdiffstats
path: root/src/tests
diff options
context:
space:
mode:
authorDan Davison <dandavison7@gmail.com>2020-05-16 10:54:01 -0400
committerDan Davison <dandavison7@gmail.com>2020-05-17 11:07:44 -0400
commit271e30cbdcc7fb480a443b3043776127cba810d3 (patch)
tree5b6ea81a5e5249e77a223015bf43baf2f3049536 /src/tests
parent253bc3ac0fe34a8f08c2bf625fcc8bb78de01a4c (diff)
Add new command-line options controlling foreground colors
- Revert "Do not expose new foreground color options yet" This reverts commit 253bc3ac0fe34a8f08c2bf625fcc8bb78de01a4c. - Make test assertions for minus_foreground_color - Fix test: fix detection of foreground color - Add diagnostic output to hunk highlighting test - Disable syntax-highlighting if the user has set foreground colors - Eliminate redundant style modifiers in config - Revert fixed_foreground_style implementation The syntax_style_sections will just contain syntect styles, or else a dummy no-op style. All delta styles will be done in diff_style_sections. - Add more dimensions to grid in hunk highlighting test - Support "none" as a value for background colors - Set foreground colors in a way that's consistent with background colors - Bug fix: don't highlight when there is no theme
Diffstat (limited to 'src/tests')
-rw-r--r--src/tests/ansi_test_utils.rs33
-rw-r--r--src/tests/integration_test_utils.rs4
-rw-r--r--src/tests/test_hunk_highlighting.rs79
3 files changed, 83 insertions, 33 deletions
diff --git a/src/tests/ansi_test_utils.rs b/src/tests/ansi_test_utils.rs
index ad3c1084..1dddc460 100644
--- a/src/tests/ansi_test_utils.rs
+++ b/src/tests/ansi_test_utils.rs
@@ -11,10 +11,19 @@ pub mod ansi_test_utils {
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 {
- ansi_parser::Output::TextBlock(s) => string_has_some_foreground_color(s),
+ TextBlock(s) => string_has_some_foreground_color(s),
+ Escape(SetGraphicsMode(parameters)) => parameters[0] == 38,
_ => false,
})
.next()
@@ -24,7 +33,7 @@ pub mod ansi_test_utils {
pub fn line_has_background_color(line: &str, state: &State, config: &Config) -> bool {
line.ansi_parse()
.filter(|tok| match tok {
- ansi_parser::Output::TextBlock(s) => string_has_background_color(s, state, config),
+ TextBlock(s) => string_has_background_color(s, state, config),
_ => false,
})
.next()
@@ -34,7 +43,7 @@ pub mod ansi_test_utils {
pub fn line_has_no_background_color(line: &str) -> bool {
line.ansi_parse()
.filter(|tok| match tok {
- ansi_parser::Output::TextBlock(s) => string_has_some_background_color(s),
+ TextBlock(s) => string_has_some_background_color(s),
_ => false,
})
.next()
@@ -49,7 +58,9 @@ pub mod ansi_test_utils {
.unwrap_or_else(|| panic!("state {:?} does not have a background color", state)),
true,
);
- let ansi_sequence = painted.trim_end_matches(paint::ANSI_SGR_RESET);
+ let ansi_sequence = painted
+ .trim_end_matches(paint::ANSI_SGR_RESET)
+ .trim_end_matches("m");
string.starts_with(ansi_sequence)
}
@@ -62,15 +73,17 @@ pub mod ansi_test_utils {
}
pub fn assert_line_has_expected_ansi_sequences(line: &str, expected: &Vec<(&str, &str)>) {
- assert_eq!(line.ansi_parse().count(), expected.len());
- for ((expected_ansi_sequence, _), ref token) in expected.iter().zip_eq(line.ansi_parse()) {
+ 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 {
- ansi_parser::Output::TextBlock(s) => {
+ TextBlock(s) => {
assert!(s.starts_with(*expected_ansi_sequence));
}
- ansi_parser::Output::Escape(_) => {
- assert_eq!(expected_ansi_sequence, &paint::ANSI_SGR_RESET);
- }
+ Escape(SetGraphicsMode(parameters)) => assert_eq!(parameters, &vec![0 as u32]),
+ _ => panic!("Unexpected token: {:?}", token),
}
}
}
diff --git a/src/tests/integration_test_utils.rs b/src/tests/integration_test_utils.rs
index 0d10f185..2e941757 100644
--- a/src/tests/integration_test_utils.rs
+++ b/src/tests/integration_test_utils.rs
@@ -14,8 +14,12 @@ pub mod integration_test_utils {
dark: false,
minus_color: None,
minus_emph_color: None,
+ minus_foreground_color: None,
+ minus_emph_foreground_color: None,
plus_color: None,
plus_emph_color: None,
+ plus_foreground_color: None,
+ plus_emph_foreground_color: None,
color_only: false,
keep_plus_minus_markers: false,
theme: None,
diff --git a/src/tests/test_hunk_highlighting.rs b/src/tests/test_hunk_highlighting.rs
index 5edcf3a0..605bba32 100644
--- a/src/tests/test_hunk_highlighting.rs
+++ b/src/tests/test_hunk_highlighting.rs
@@ -10,15 +10,55 @@ mod tests {
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;
- for lines_to_be_syntax_highlighted in
- vec!["none", "all", "+", "0", "0+", "-", "-+", "-0", "-0+"]
- {
- options.lines_to_be_syntax_highlighted = lines_to_be_syntax_highlighted.to_string();
- _do_hunk_color_test(&options);
+ options.minus_emph_color = Some("#ffa0a0".to_string());
+ options.plus_emph_color = Some("#80ef80".to_string());
+ for minus_foreground_color in vec![None, Some("green".to_string())] {
+ options.minus_foreground_color = minus_foreground_color;
+ for minus_emph_foreground_color in vec![None, Some("#80ef80".to_string())] {
+ options.minus_emph_foreground_color = minus_emph_foreground_color;
+ for plus_foreground_color in vec![None, Some("red".to_string())] {
+ options.plus_foreground_color = plus_foreground_color;
+ for plus_emph_foreground_color in vec![None, Some("#ffa0a0".to_string())] {
+ options.plus_emph_foreground_color = plus_emph_foreground_color;
+ for lines_to_be_syntax_highlighted in vec!["none", "all"] {
+ options.lines_to_be_syntax_highlighted =
+ lines_to_be_syntax_highlighted.to_string();
+ if VERBOSE {
+ println!();
+ print!(
+ " --syntax-highlight {:?}",
+ options.lines_to_be_syntax_highlighted
+ );
+ print!(
+ " --minus-foreground-color {:?}",
+ options.minus_foreground_color
+ );
+ print!(
+ " --minus-emph-foreground-color {:?}",
+ options.minus_emph_foreground_color
+ );
+ print!(
+ " --plus-foreground-color {:?}",
+ options.plus_foreground_color
+ );
+ print!(
+ " --plus-emph-foreground-color {:?}",
+ options.plus_emph_foreground_color
+ );
+ println!();
+ }
+ _do_hunk_color_test(&options);
+ }
+ }
+ }
+ }
}
}
@@ -37,6 +77,7 @@ mod tests {
let minus =
paint::paint_text_background("", config.minus_style_modifier.background.unwrap(), true)
.trim_end_matches(paint::ANSI_SGR_RESET)
+ .trim_end_matches("m")
.to_string();
let minus_emph = paint::paint_text_background(
"",
@@ -44,10 +85,12 @@ mod tests {
true,
)
.trim_end_matches(paint::ANSI_SGR_RESET)
+ .trim_end_matches("m")
.to_string();
let plus =
paint::paint_text_background("", config.plus_style_modifier.background.unwrap(), true)
.trim_end_matches(paint::ANSI_SGR_RESET)
+ .trim_end_matches("m")
.to_string();
let plus_emph = paint::paint_text_background(
"",
@@ -55,6 +98,7 @@ mod tests {
true,
)
.trim_end_matches(paint::ANSI_SGR_RESET)
+ .trim_end_matches("m")
.to_string();
let expectation = vec![
@@ -70,32 +114,22 @@ mod tests {
(minus.as_str(), "(22222222, 22222222"),
(minus_emph.as_str(), ", 22222222"),
(minus.as_str(), ")"),
- (paint::ANSI_SGR_RESET, ""),
],
),
// line 3: removed
(
State::HunkMinus,
- vec![
- (minus.as_str(), "(33333333, 33333333, 33333333)"),
- (paint::ANSI_SGR_RESET, ""),
- ],
+ vec![(minus.as_str(), "(33333333, 33333333, 33333333)")],
),
// line 4: removed
(
State::HunkMinus,
- vec![
- (minus.as_str(), "(44444444, 44444444, 44444444)"),
- (paint::ANSI_SGR_RESET, ""),
- ],
+ vec![(minus.as_str(), "(44444444, 44444444, 44444444)")],
),
// line 5: added, and syntax-higlighted.
(
State::HunkPlus,
- vec![
- (plus.as_str(), "(22222222, 22222222)"),
- (paint::ANSI_SGR_RESET, ""),
- ],
+ vec![(plus.as_str(), "(22222222, 22222222)")],
),
// line 6: added, and syntax-highlighted. First is plus-emph.
(
@@ -104,7 +138,6 @@ mod tests {
(plus.as_str(), "("),
(plus_emph.as_str(), "33333333, "),
(plus.as_str(), "33333333, 33333333, 33333333)"),
- (paint::ANSI_SGR_RESET, ""),
],
),
// line 7: unchanged
@@ -115,15 +148,15 @@ mod tests {
// line 8: added, and syntax-highlighted.
(
State::HunkPlus,
- vec![
- (plus.as_str(), "(66666666, 66666666, 66666666)"),
- (paint::ANSI_SGR_RESET, ""),
- ],
+ 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 {