diff options
author | Dan Davison <dandavison7@gmail.com> | 2020-05-20 10:52:39 -0400 |
---|---|---|
committer | Dan Davison <dandavison7@gmail.com> | 2020-05-20 11:55:34 -0400 |
commit | eb6518cad198ca7215ef0118eca50dd580b0518d (patch) | |
tree | dec1273ea4a5e8efabb8fa2c1cc29406a1b8c130 /src/tests | |
parent | bbbd722687fe8f916f1aaf7bda413fec74df1ba5 (diff) |
Refactor: consume options when creating config
Diffstat (limited to 'src/tests')
-rw-r--r-- | src/tests/ansi_test_utils.rs | 17 | ||||
-rw-r--r-- | src/tests/integration_test_utils.rs | 21 | ||||
-rw-r--r-- | src/tests/test_example_diffs.rs | 106 | ||||
-rw-r--r-- | src/tests/test_hunk_highlighting.rs | 12 |
4 files changed, 58 insertions, 98 deletions
diff --git a/src/tests/ansi_test_utils.rs b/src/tests/ansi_test_utils.rs index 1dddc460..5167999c 100644 --- a/src/tests/ansi_test_utils.rs +++ b/src/tests/ansi_test_utils.rs @@ -5,8 +5,6 @@ pub mod ansi_test_utils { use itertools::Itertools; use syntect::highlighting::StyleModifier; - use crate::bat::assets::HighlightingAssets; - use crate::cli; use crate::config::{ColorLayer::*, Config}; use crate::delta::State; use crate::paint; @@ -88,29 +86,26 @@ pub mod ansi_test_utils { } } - pub fn assert_has_color_other_than_plus_color(string: &str, options: &cli::Opt) { + 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, &options); + get_color_variants(string, config); assert_ne!(string, string_without_any_color); assert_ne!(string, string_with_plus_color_only); } - pub fn assert_has_plus_color_only(string: &str, options: &cli::Opt) { + pub fn assert_has_plus_color_only(string: &str, config: &Config) { let (string_without_any_color, string_with_plus_color_only) = - get_color_variants(string, &options); + get_color_variants(string, config); assert_ne!(string, string_without_any_color); assert_eq!(string, string_with_plus_color_only); } - pub fn get_color_variants(string: &str, options: &cli::Opt) -> (String, String) { - let assets = HighlightingAssets::new(); - let config = cli::process_command_line_arguments(&assets, &options); - + pub fn get_color_variants(string: &str, config: &Config) -> (String, String) { let string_without_any_color = strip_ansi_codes(string).to_string(); let string_with_plus_color_only = paint_text( &string_without_any_color, config.plus_style_modifier, - &config, + config, ); (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 2e941757..6836d85d 100644 --- a/src/tests/integration_test_utils.rs +++ b/src/tests/integration_test_utils.rs @@ -4,8 +4,8 @@ pub mod integration_test_utils { use console::strip_ansi_codes; use std::io::BufReader; - use crate::bat::assets::HighlightingAssets; use crate::cli; + use crate::config; use crate::delta::delta; pub fn get_command_line_options() -> cli::Opt { @@ -43,26 +43,27 @@ pub mod integration_test_utils { } } - pub fn get_line_of_code_from_delta(input: &str, options: &cli::Opt) -> String { - let output = run_delta(&input, &options); + pub fn get_line_of_code_from_delta<'a>( + input: &str, + options: cli::Opt, + ) -> (String, config::Config<'a>) { + let (output, config) = run_delta(&input, options); let line_of_code = output.lines().nth(12).unwrap(); assert!(strip_ansi_codes(line_of_code) == " class X:"); - line_of_code.to_string() + (line_of_code.to_string(), config) } - pub fn run_delta(input: &str, options: &cli::Opt) -> String { + pub fn run_delta<'a>(input: &str, options: cli::Opt) -> (String, config::Config<'a>) { let mut writer: Vec<u8> = Vec::new(); - let assets = HighlightingAssets::new(); - let config = cli::process_command_line_arguments(&assets, &options); + let config = cli::process_command_line_arguments(options); delta( ByteLines::new(BufReader::new(input.as_bytes())), - &config, - &assets, &mut writer, + &config, ) .unwrap(); - String::from_utf8(writer).unwrap() + (String::from_utf8(writer).unwrap(), config) } } diff --git a/src/tests/test_example_diffs.rs b/src/tests/test_example_diffs.rs index 2d5faa22..f46e2169 100644 --- a/src/tests/test_example_diffs.rs +++ b/src/tests/test_example_diffs.rs @@ -7,11 +7,8 @@ mod tests { #[test] fn test_added_file() { let options = integration_test_utils::get_command_line_options(); - let output = strip_ansi_codes(&integration_test_utils::run_delta( - ADDED_FILE_INPUT, - &options, - )) - .to_string(); + let (output, _) = integration_test_utils::run_delta(ADDED_FILE_INPUT, options); + let output = strip_ansi_codes(&output); assert!(output.contains("\nadded: a.py\n")); if false { // TODO: hline width @@ -23,22 +20,17 @@ mod tests { #[ignore] // #128 fn test_added_empty_file() { let options = integration_test_utils::get_command_line_options(); - let output = strip_ansi_codes(&integration_test_utils::run_delta( - ADDED_EMPTY_FILE, - &options, - )) - .to_string(); + let (output, _) = integration_test_utils::run_delta(ADDED_EMPTY_FILE, options); + let output = strip_ansi_codes(&output); assert!(output.contains("\nadded: file\n")); } #[test] fn test_added_file_directory_path_containing_space() { let options = integration_test_utils::get_command_line_options(); - let output = strip_ansi_codes(&integration_test_utils::run_delta( - ADDED_FILES_DIRECTORY_PATH_CONTAINING_SPACE, - &options, - )) - .to_string(); + let (output, _) = + integration_test_utils::run_delta(ADDED_FILES_DIRECTORY_PATH_CONTAINING_SPACE, options); + let output = strip_ansi_codes(&output); assert!(output.contains("\nadded: with space/file1\n")); assert!(output.contains("\nadded: nospace/file2\n")); } @@ -46,11 +38,8 @@ mod tests { #[test] fn test_renamed_file() { let options = integration_test_utils::get_command_line_options(); - let output = strip_ansi_codes(&integration_test_utils::run_delta( - RENAMED_FILE_INPUT, - &options, - )) - .to_string(); + let (output, _) = integration_test_utils::run_delta(RENAMED_FILE_INPUT, options); + let output = strip_ansi_codes(&output); assert!(output.contains("\nrenamed: a.py ⟶ b.py\n")); } @@ -58,9 +47,9 @@ mod tests { fn test_recognized_file_type() { // In addition to the background color, the code has language syntax highlighting. let options = integration_test_utils::get_command_line_options(); - let input = ADDED_FILE_INPUT; - let output = integration_test_utils::get_line_of_code_from_delta(&input, &options); - ansi_test_utils::assert_has_color_other_than_plus_color(&output, &options); + let (output, config) = + integration_test_utils::get_line_of_code_from_delta(&ADDED_FILE_INPUT, options); + ansi_test_utils::assert_has_color_other_than_plus_color(&output, &config); } #[test] @@ -69,8 +58,8 @@ mod tests { // .txt syntax under the theme. let options = integration_test_utils::get_command_line_options(); let input = ADDED_FILE_INPUT.replace("a.py", "a"); - let output = integration_test_utils::get_line_of_code_from_delta(&input, &options); - ansi_test_utils::assert_has_color_other_than_plus_color(&output, &options); + let (output, config) = integration_test_utils::get_line_of_code_from_delta(&input, options); + ansi_test_utils::assert_has_color_other_than_plus_color(&output, &config); } #[test] @@ -80,18 +69,15 @@ mod tests { let mut options = integration_test_utils::get_command_line_options(); options.theme = Some("none".to_string()); let input = ADDED_FILE_INPUT.replace("a.py", "a"); - let output = integration_test_utils::get_line_of_code_from_delta(&input, &options); - ansi_test_utils::assert_has_plus_color_only(&output, &options); + let (output, config) = integration_test_utils::get_line_of_code_from_delta(&input, options); + ansi_test_utils::assert_has_plus_color_only(&output, &config); } #[test] fn test_diff_unified_two_files() { let options = integration_test_utils::get_command_line_options(); - let output = strip_ansi_codes(&integration_test_utils::run_delta( - DIFF_UNIFIED_TWO_FILES, - &options, - )) - .to_string(); + let (output, _) = integration_test_utils::run_delta(DIFF_UNIFIED_TWO_FILES, options); + let output = strip_ansi_codes(&output); let mut lines = output.split('\n'); // Header @@ -109,11 +95,8 @@ mod tests { #[test] fn test_diff_unified_two_directories() { let options = integration_test_utils::get_command_line_options(); - let output = strip_ansi_codes(&integration_test_utils::run_delta( - DIFF_UNIFIED_TWO_DIRECTORIES, - &options, - )) - .to_string(); + let (output, _) = integration_test_utils::run_delta(DIFF_UNIFIED_TWO_DIRECTORIES, options); + let output = strip_ansi_codes(&output); let mut lines = output.split('\n'); // Header @@ -140,11 +123,8 @@ mod tests { #[ignore] // Ideally, delta would make this test pass. See #121. fn test_delta_ignores_non_diff_input() { let options = integration_test_utils::get_command_line_options(); - let output = strip_ansi_codes(&integration_test_utils::run_delta( - NOT_A_DIFF_OUTPUT, - &options, - )) - .to_string(); + let (output, _) = integration_test_utils::run_delta(NOT_A_DIFF_OUTPUT, options); + let output = strip_ansi_codes(&output); assert_eq!(output, NOT_A_DIFF_OUTPUT.to_owned() + "\n"); } @@ -156,16 +136,16 @@ mod tests { ] { let mut options = integration_test_utils::get_command_line_options(); options.color_only = true; - let output = integration_test_utils::run_delta(input, &options); - assert_eq!(strip_ansi_codes(&output).to_string(), input.to_owned()); - assert_ne!(output, input.to_owned()); + let (output, _) = integration_test_utils::run_delta(input, options); + assert_eq!(strip_ansi_codes(&output), input); + assert_ne!(output, input); } } #[test] fn test_diff_with_merge_conflict_is_not_truncated() { let options = integration_test_utils::get_command_line_options(); - let output = integration_test_utils::run_delta(DIFF_WITH_MERGE_CONFLICT, &options); + let (output, _) = integration_test_utils::run_delta(DIFF_WITH_MERGE_CONFLICT, options); // TODO: The + in the first column is being removed. assert!(strip_ansi_codes(&output).contains("+>>>>>>> Stashed changes")); assert_eq!(output.split('\n').count(), 46); @@ -175,32 +155,25 @@ mod tests { fn test_diff_with_merge_conflict_is_passed_on_unchanged_under_color_only() { let mut options = integration_test_utils::get_command_line_options(); options.color_only = true; - let output = integration_test_utils::run_delta(DIFF_WITH_MERGE_CONFLICT, &options); - assert_eq!( - strip_ansi_codes(&output).to_string(), - DIFF_WITH_MERGE_CONFLICT.to_owned() - ); + let (output, _) = integration_test_utils::run_delta(DIFF_WITH_MERGE_CONFLICT, options); + assert_eq!(strip_ansi_codes(&output), DIFF_WITH_MERGE_CONFLICT); } #[test] fn test_submodule_contains_untracked_content() { let options = integration_test_utils::get_command_line_options(); - let output = strip_ansi_codes(&integration_test_utils::run_delta( - SUBMODULE_CONTAINS_UNTRACKED_CONTENT_INPUT, - &options, - )) - .to_string(); + let (output, _) = + integration_test_utils::run_delta(SUBMODULE_CONTAINS_UNTRACKED_CONTENT_INPUT, options); + let output = strip_ansi_codes(&output); assert!(output.contains("\nSubmodule x/y/z contains untracked content\n")); } #[test] fn test_triple_dash_at_beginning_of_line_in_code() { let options = integration_test_utils::get_command_line_options(); - let output = strip_ansi_codes(&integration_test_utils::run_delta( - TRIPLE_DASH_AT_BEGINNING_OF_LINE_IN_CODE, - &options, - )) - .to_string(); + let (output, _) = + integration_test_utils::run_delta(TRIPLE_DASH_AT_BEGINNING_OF_LINE_IN_CODE, options); + let output = strip_ansi_codes(&output); assert!( output.contains(" -- instance (Category p, Category q) => Category (p ∧ q) where\n") ); @@ -209,19 +182,16 @@ mod tests { #[test] fn test_binary_files_differ() { let options = integration_test_utils::get_command_line_options(); - let output = strip_ansi_codes(&integration_test_utils::run_delta( - BINARY_FILES_DIFFER, - &options, - )) - .to_string(); + let (output, _) = integration_test_utils::run_delta(BINARY_FILES_DIFFER, options); + let output = strip_ansi_codes(&output); assert!(output.contains("Binary files /dev/null and b/foo differ\n")); } #[test] fn test_diff_in_diff() { let options = integration_test_utils::get_command_line_options(); - let output = strip_ansi_codes(&integration_test_utils::run_delta(DIFF_IN_DIFF, &options)) - .to_string(); + let (output, _) = integration_test_utils::run_delta(DIFF_IN_DIFF, options); + let output = strip_ansi_codes(&output); assert!(output.contains("\n ---\n")); assert!(output.contains("\n Subject: [PATCH] Init\n")); } diff --git a/src/tests/test_hunk_highlighting.rs b/src/tests/test_hunk_highlighting.rs index 605bba32..730218c0 100644 --- a/src/tests/test_hunk_highlighting.rs +++ b/src/tests/test_hunk_highlighting.rs @@ -2,7 +2,6 @@ mod tests { use itertools::Itertools; - use crate::bat::assets::HighlightingAssets; use crate::cli; use crate::config::ColorLayer::*; use crate::delta::State; @@ -54,7 +53,7 @@ mod tests { ); println!(); } - _do_hunk_color_test(&options); + _do_hunk_color_test(options.clone()); } } } @@ -62,18 +61,13 @@ mod tests { } } - fn _do_hunk_color_test(options: &cli::Opt) { - let output = integration_test_utils::run_delta( + 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); - // Hack: The config has been built once already in run_delta - let assets = HighlightingAssets::new(); - let config = cli::process_command_line_arguments(&assets, &options); - let minus = paint::paint_text_background("", config.minus_style_modifier.background.unwrap(), true) .trim_end_matches(paint::ANSI_SGR_RESET) |