summaryrefslogtreecommitdiffstats
path: root/src/tests
diff options
context:
space:
mode:
authorDan Davison <dandavison7@gmail.com>2020-05-25 12:24:06 -0400
committerDan Davison <dandavison7@gmail.com>2020-05-26 09:21:49 -0400
commit53f0de7b1ef168adb2a86f06b2682bacb79ff6b6 (patch)
treebfbef0b5778d19f980e516c72691a050301158c2 /src/tests
parent83ee16b03f9f45f7f9bd1851606d8329f4976dec (diff)
Update tests after implementing decoration styles
Diffstat (limited to 'src/tests')
-rw-r--r--src/tests/ansi_test_utils.rs45
-rw-r--r--src/tests/test_example_diffs.rs55
2 files changed, 85 insertions, 15 deletions
diff --git a/src/tests/ansi_test_utils.rs b/src/tests/ansi_test_utils.rs
index bb7d52d8..04e0eeda 100644
--- a/src/tests/ansi_test_utils.rs
+++ b/src/tests/ansi_test_utils.rs
@@ -4,6 +4,8 @@ pub mod ansi_test_utils {
use console::strip_ansi_codes;
use crate::config::{color_from_rgb_or_ansi_code, Config};
+ 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);
@@ -32,6 +34,24 @@ pub mod ansi_test_utils {
assert_eq!(line, stripped_line);
}
+ /// Assert that the specified line number of output (a) matches
+ /// `expected_prefix` and (b) for the length of expected_prefix is
+ /// syntax-highlighted according to `language_extension`.
+ pub fn assert_line_is_syntax_highlighted(
+ output: &str,
+ line_number: usize,
+ expected_prefix: &str,
+ language_extension: &str,
+ config: &Config,
+ ) {
+ let line = output.lines().nth(line_number).unwrap();
+ let stripped_line = &strip_ansi_codes(line);
+ assert!(stripped_line.starts_with(expected_prefix));
+ let painted_line = paint_line(expected_prefix, language_extension, config);
+ // remove trailing newline appended by paint::paint_lines.
+ assert!(line.starts_with(painted_line.trim_end()));
+ }
+
pub fn assert_has_color_other_than_plus_color(string: &str, config: &Config) {
let (string_without_any_color, string_with_plus_color_only) =
get_color_variants(string, config);
@@ -57,4 +77,29 @@ pub mod ansi_test_utils {
string_with_plus_color_only.to_string(),
)
}
+
+ pub fn paint_line(line: &str, language_extension: &str, config: &Config) -> String {
+ let mut output_buffer = String::new();
+ let mut unused_writer = Vec::<u8>::new();
+ let mut painter = paint::Painter::new(&mut unused_writer, config);
+ let syntax_highlighted_style = Style {
+ is_syntax_highlighted: true,
+ ..Style::new()
+ };
+ painter.set_syntax(Some(language_extension));
+ painter.set_highlighter();
+ let lines = vec![line];
+ let syntax_style_sections = painter.highlighter.highlight(line, &config.syntax_set);
+ paint::Painter::paint_lines(
+ vec![syntax_style_sections],
+ vec![vec![(syntax_highlighted_style, lines[0])]],
+ &mut output_buffer,
+ config,
+ "",
+ config.null_style,
+ config.null_style,
+ None,
+ );
+ output_buffer
+ }
}
diff --git a/src/tests/test_example_diffs.rs b/src/tests/test_example_diffs.rs
index ea98b0b1..592fa4c0 100644
--- a/src/tests/test_example_diffs.rs
+++ b/src/tests/test_example_diffs.rs
@@ -206,7 +206,7 @@ mod tests {
#[test]
fn test_commit_style_plain() {
let mut options = integration_test_utils::get_command_line_options();
- options.commit_style = "plain".to_string();
+ options.commit_decoration_style = "".to_string();
// 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(
@@ -225,8 +225,8 @@ commit 94907c0f136f46dc46ffae2dc92dca9af7eb7c2e
#[test]
fn test_commit_style_box() {
let mut options = integration_test_utils::get_command_line_options();
- options.commit_style = "box".to_string();
- options.deprecated_commit_color = Some("blue".to_string());
+ options.commit_style = "blue".to_string();
+ options.commit_decoration_style = "blue box".to_string();
let (output, config) = integration_test_utils::run_delta(GIT_DIFF_SINGLE_HUNK, options);
ansi_test_utils::assert_line_has_foreground_color(
&output,
@@ -261,8 +261,8 @@ commit 94907c0f136f46dc46ffae2dc92dca9af7eb7c2e ┃
#[test]
fn test_commit_style_underline() {
let mut options = integration_test_utils::get_command_line_options();
- options.commit_style = "underline".to_string();
- options.deprecated_commit_color = Some("yellow".to_string());
+ options.commit_style = "yellow".to_string();
+ options.commit_decoration_style = "yellow underline".to_string();
let (output, config) = integration_test_utils::run_delta(GIT_DIFF_SINGLE_HUNK, options);
ansi_test_utils::assert_line_has_foreground_color(
&output,
@@ -289,7 +289,7 @@ commit 94907c0f136f46dc46ffae2dc92dca9af7eb7c2e
#[test]
fn test_file_style_plain() {
let mut options = integration_test_utils::get_command_line_options();
- options.file_style = "plain".to_string();
+ options.file_decoration_style = "".to_string();
// 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![
@@ -317,8 +317,8 @@ index 8e37a9e..6ce4863 100644
#[test]
fn test_file_style_box() {
let mut options = integration_test_utils::get_command_line_options();
- options.file_style = "box".to_string();
- options.deprecated_file_color = Some("green".to_string());
+ options.file_style = "green".to_string();
+ options.file_decoration_style = "green box".to_string();
let (output, config) = integration_test_utils::run_delta(GIT_DIFF_SINGLE_HUNK, options);
ansi_test_utils::assert_line_has_foreground_color(
&output,
@@ -353,8 +353,8 @@ src/align.rs │
#[test]
fn test_file_style_underline() {
let mut options = integration_test_utils::get_command_line_options();
- options.file_style = "underline".to_string();
- options.deprecated_file_color = Some("magenta".to_string());
+ options.file_style = "magenta".to_string();
+ options.file_decoration_style = "magenta underline".to_string();
let (output, config) = integration_test_utils::run_delta(GIT_DIFF_SINGLE_HUNK, options);
ansi_test_utils::assert_line_has_foreground_color(
&output,
@@ -381,7 +381,7 @@ src/align.rs
#[test]
fn test_hunk_style_plain() {
let mut options = integration_test_utils::get_command_line_options();
- options.deprecated_hunk_style = Some("plain".to_string());
+ options.hunk_header_decoration_style = "".to_string();
// 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(
@@ -396,8 +396,7 @@ src/align.rs
#[test]
fn test_hunk_style_box() {
let mut options = integration_test_utils::get_command_line_options();
- options.deprecated_hunk_style = Some("box".to_string());
- options.deprecated_hunk_color = Some("white".to_string());
+ options.hunk_header_decoration_style = "white box".to_string();
let (output, config) = integration_test_utils::run_delta(GIT_DIFF_SINGLE_HUNK, options);
ansi_test_utils::assert_line_has_foreground_color(
&output,
@@ -426,8 +425,7 @@ src/align.rs
#[test]
fn test_hunk_style_underline() {
let mut options = integration_test_utils::get_command_line_options();
- options.deprecated_hunk_style = Some("underline".to_string());
- options.deprecated_hunk_color = Some("black".to_string());
+ options.hunk_header_decoration_style = "black underline".to_string();
let (output, config) = integration_test_utils::run_delta(GIT_DIFF_SINGLE_HUNK, options);
ansi_test_utils::assert_line_has_foreground_color(
&output,
@@ -444,6 +442,33 @@ src/align.rs
));
}
+ #[test]
+ fn test_hunk_style_box_with_syntax_highlighting() {
+ let mut options = integration_test_utils::get_command_line_options();
+ options.hunk_header_style = "syntax".to_string();
+ // For this test we are currently forced to disable styling of the decoration, since
+ // otherwise it will confuse assert_line_is_syntax_highlighted.
+ options.hunk_header_decoration_style = "box".to_string();
+ let (output, config) = integration_test_utils::run_delta(GIT_DIFF_SINGLE_HUNK, options);
+ ansi_test_utils::assert_line_has_no_color(&output, 9, "──────────────────────────┐");
+ ansi_test_utils::assert_line_is_syntax_highlighted(
+ &output,
+ 10,
+ " impl<'a> Alignment<'a> {",
+ "rs",
+ &config,
+ );
+ ansi_test_utils::assert_line_has_no_color(&output, 11, "──────────────────────────┘");
+ 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>