diff options
author | Dan Davison <dandavison7@gmail.com> | 2020-06-20 17:04:11 -0400 |
---|---|---|
committer | Dan Davison <dandavison7@gmail.com> | 2020-06-20 17:04:11 -0400 |
commit | be655baa7454c4d0dbc5cb3bb48e40eb5ac4f629 (patch) | |
tree | 35e1b514fa6f58636ba65f429373cfe7953d5401 | |
parent | 9c7c855d7ad72bd2475db0c4e1ff7320898ec011 (diff) |
Refactor: features tests
-rw-r--r-- | src/features/diff_highlight.rs | 76 | ||||
-rw-r--r-- | src/features/diff_so_fancy.rs | 139 | ||||
-rw-r--r-- | src/features/mod.rs | 187 | ||||
-rw-r--r-- | src/features/navigate.rs | 64 |
4 files changed, 240 insertions, 226 deletions
diff --git a/src/features/diff_highlight.rs b/src/features/diff_highlight.rs index 4bda3fa9..e339474b 100644 --- a/src/features/diff_highlight.rs +++ b/src/features/diff_highlight.rs @@ -50,3 +50,79 @@ pub fn _make_feature(bold: bool) -> Vec<(String, FeatureValueFunction)> { ) ]) } + +#[cfg(test)] +mod test_utils { + use std::fs::remove_file; + + use crate::features; + + #[test] + fn test_diff_highlight_defaults() { + let config = features::tests::make_config(&["--features", "diff-highlight"], None, None); + + assert_eq!(config.minus_style, features::tests::make_style("red")); + assert_eq!( + config.minus_non_emph_style, + features::tests::make_style("red") + ); + assert_eq!( + config.minus_emph_style, + features::tests::make_emph_style("red reverse") + ); + assert_eq!(config.zero_style, features::tests::make_style("")); + assert_eq!(config.plus_style, features::tests::make_style("green")); + assert_eq!( + config.plus_non_emph_style, + features::tests::make_style("green") + ); + assert_eq!( + config.plus_emph_style, + features::tests::make_emph_style("green reverse") + ); + } + + #[test] + fn test_diff_highlight_respects_gitconfig() { + let git_config_contents = b" +[color \"diff\"] + old = red bold + new = green bold + +[color \"diff-highlight\"] + oldNormal = ul red bold + oldHighlight = red bold 52 + newNormal = ul green bold + newHighlight = green bold 22 +"; + let git_config_path = "delta__test_diff_highlight.gitconfig"; + + let config = features::tests::make_config( + &["--features", "diff-highlight"], + Some(git_config_contents), + Some(git_config_path), + ); + + assert_eq!(config.minus_style, features::tests::make_style("red bold")); + assert_eq!( + config.minus_non_emph_style, + features::tests::make_style("ul red bold") + ); + assert_eq!( + config.minus_emph_style, + features::tests::make_emph_style("red bold 52") + ); + assert_eq!(config.zero_style, features::tests::make_style("")); + assert_eq!(config.plus_style, features::tests::make_style("green bold")); + assert_eq!( + config.plus_non_emph_style, + features::tests::make_style("ul green bold") + ); + assert_eq!( + config.plus_emph_style, + features::tests::make_emph_style("green bold 22") + ); + + remove_file(git_config_path).unwrap(); + } +} diff --git a/src/features/diff_so_fancy.rs b/src/features/diff_so_fancy.rs index fa5952db..0013ef37 100644 --- a/src/features/diff_so_fancy.rs +++ b/src/features/diff_so_fancy.rs @@ -55,3 +55,142 @@ pub fn make_feature() -> Vec<(String, FeatureValueFunction)> { ])); feature } + +#[cfg(test)] +pub mod tests { + use std::fs::remove_file; + + use crate::features; + + #[test] + fn test_diff_so_fancy_defaults() { + let config = features::tests::make_config(&["--features", "diff-so-fancy"], None, None); + + assert_eq!( + config.commit_style.ansi_term_style, + features::tests::make_style("bold yellow").ansi_term_style + ); + assert_eq!( + config.commit_style.decoration_style, + features::tests::make_decoration_style("none") + ); + + assert_eq!( + config.file_style.ansi_term_style, + features::tests::make_style("11").ansi_term_style + ); + assert_eq!( + config.file_style.decoration_style, + features::tests::make_decoration_style("bold yellow ul ol") + ); + + assert_eq!( + config.hunk_header_style.ansi_term_style, + features::tests::make_style("bold syntax").ansi_term_style + ); + assert_eq!( + config.hunk_header_style.decoration_style, + features::tests::make_decoration_style("magenta box") + ); + } + + #[test] + fn test_diff_so_fancy_respects_git_config() { + let git_config_contents = b" +[color \"diff\"] + meta = 11 + frag = magenta bold + commit = yellow bold + old = red bold + new = green bold + whitespace = red reverse +"; + let git_config_path = "delta__test_diff_so_fancy.gitconfig"; + + let config = features::tests::make_config( + &["--features", "diff-so-fancy some-other-feature"], + Some(git_config_contents), + Some(git_config_path), + ); + + assert_eq!( + config.commit_style.ansi_term_style, + features::tests::make_style("yellow bold").ansi_term_style + ); + assert_eq!( + config.file_style.ansi_term_style, + features::tests::make_style("11").ansi_term_style + ); + assert_eq!( + config.hunk_header_style.ansi_term_style, + features::tests::make_style("magenta bold").ansi_term_style + ); + assert_eq!( + config.commit_style.decoration_style, + features::tests::make_decoration_style("none") + ); + assert_eq!( + config.file_style.decoration_style, + features::tests::make_decoration_style("yellow bold ul ol") + ); + assert_eq!( + config.hunk_header_style.decoration_style, + features::tests::make_decoration_style("magenta box") + ); + + remove_file(git_config_path).unwrap(); + } + + #[test] + fn test_diff_so_fancy_obeys_feature_precedence_rules() { + let git_config_contents = b" +[color \"diff\"] + meta = 11 + frag = magenta bold + commit = yellow bold + old = red bold + new = green bold + whitespace = red reverse + +[delta \"decorations\"] + commit-decoration-style = bold box ul + file-style = bold 19 ul + file-decoration-style = none +"; + let git_config_path = "delta__test_diff_so_fancy_obeys_feature_precedence_rules.gitconfig"; + + let config = features::tests::make_config( + &["--features", "decorations diff-so-fancy"], + Some(git_config_contents), + Some(git_config_path), + ); + + assert_eq!( + config.file_style.ansi_term_style, + features::tests::make_style("11").ansi_term_style + ); + + assert_eq!( + config.file_style.decoration_style, + features::tests::make_decoration_style("yellow bold ul ol") + ); + + let config = features::tests::make_config( + &["--features", "diff-so-fancy decorations"], + Some(git_config_contents), + Some(git_config_path), + ); + + assert_eq!( + config.file_style.ansi_term_style, + features::tests::make_style("ul bold 19").ansi_term_style + ); + + assert_eq!( + config.file_style.decoration_style, + features::tests::make_decoration_style("none") + ); + + remove_file(git_config_path).unwrap(); + } +} diff --git a/src/features/mod.rs b/src/features/mod.rs index 2b025a51..7f7add46 100644 --- a/src/features/mod.rs +++ b/src/features/mod.rs @@ -156,7 +156,7 @@ impl From<OptionValue> for usize { } #[cfg(test)] -mod tests { +pub mod tests { use std::fs::{remove_file, File}; use std::io::Write; use std::path::Path; @@ -340,183 +340,6 @@ mod tests { } #[test] - fn test_diff_highlight_defaults() { - let config = make_config(&["--features", "diff-highlight"], None, None); - - assert_eq!(config.minus_style, make_style("red")); - assert_eq!(config.minus_non_emph_style, make_style("red")); - assert_eq!(config.minus_emph_style, make_emph_style("red reverse")); - assert_eq!(config.zero_style, make_style("")); - assert_eq!(config.plus_style, make_style("green")); - assert_eq!(config.plus_non_emph_style, make_style("green")); - assert_eq!(config.plus_emph_style, make_emph_style("green reverse")); - } - - #[test] - fn test_diff_highlight_respects_gitconfig() { - let git_config_contents = b" -[color \"diff\"] - old = red bold - new = green bold - -[color \"diff-highlight\"] - oldNormal = ul red bold - oldHighlight = red bold 52 - newNormal = ul green bold - newHighlight = green bold 22 -"; - let git_config_path = "delta__test_diff_highlight.gitconfig"; - - let config = make_config( - &["--features", "diff-highlight"], - Some(git_config_contents), - Some(git_config_path), - ); - - assert_eq!(config.minus_style, make_style("red bold")); - assert_eq!(config.minus_non_emph_style, make_style("ul red bold")); - assert_eq!(config.minus_emph_style, make_emph_style("red bold 52")); - assert_eq!(config.zero_style, make_style("")); - assert_eq!(config.plus_style, make_style("green bold")); - assert_eq!(config.plus_non_emph_style, make_style("ul green bold")); - assert_eq!(config.plus_emph_style, make_emph_style("green bold 22")); - - remove_file(git_config_path).unwrap(); - } - - #[test] - fn test_diff_so_fancy_defaults() { - let config = make_config(&["--features", "diff-so-fancy"], None, None); - - assert_eq!( - config.commit_style.ansi_term_style, - make_style("bold yellow").ansi_term_style - ); - assert_eq!( - config.commit_style.decoration_style, - make_decoration_style("none") - ); - - assert_eq!( - config.file_style.ansi_term_style, - make_style("11").ansi_term_style - ); - assert_eq!( - config.file_style.decoration_style, - make_decoration_style("bold yellow ul ol") - ); - - assert_eq!( - config.hunk_header_style.ansi_term_style, - make_style("bold syntax").ansi_term_style - ); - assert_eq!( - config.hunk_header_style.decoration_style, - make_decoration_style("magenta box") - ); - } - - #[test] - fn test_diff_so_fancy_respects_git_config() { - let git_config_contents = b" -[color \"diff\"] - meta = 11 - frag = magenta bold - commit = yellow bold - old = red bold - new = green bold - whitespace = red reverse -"; - let git_config_path = "delta__test_diff_so_fancy.gitconfig"; - - let config = make_config( - &["--features", "diff-so-fancy some-other-feature"], - Some(git_config_contents), - Some(git_config_path), - ); - - assert_eq!( - config.commit_style.ansi_term_style, - make_style("yellow bold").ansi_term_style - ); - assert_eq!( - config.file_style.ansi_term_style, - make_style("11").ansi_term_style - ); - assert_eq!( - config.hunk_header_style.ansi_term_style, - make_style("magenta bold").ansi_term_style - ); - assert_eq!( - config.commit_style.decoration_style, - make_decoration_style("none") - ); - assert_eq!( - config.file_style.decoration_style, - make_decoration_style("yellow bold ul ol") - ); - assert_eq!( - config.hunk_header_style.decoration_style, - make_decoration_style("magenta box") - ); - - remove_file(git_config_path).unwrap(); - } - - #[test] - fn test_diff_so_fancy_obeys_feature_precedence_rules() { - let git_config_contents = b" -[color \"diff\"] - meta = 11 - frag = magenta bold - commit = yellow bold - old = red bold - new = green bold - whitespace = red reverse - -[delta \"decorations\"] - commit-decoration-style = bold box ul - file-style = bold 19 ul - file-decoration-style = none -"; - let git_config_path = "delta__test_diff_so_fancy_obeys_feature_precedence_rules.gitconfig"; - - let config = make_config( - &["--features", "decorations diff-so-fancy"], - Some(git_config_contents), - Some(git_config_path), - ); - - assert_eq!( - config.file_style.ansi_term_style, - make_style("11").ansi_term_style - ); - - assert_eq!( - config.file_style.decoration_style, - make_decoration_style("yellow bold ul ol") - ); - - let config = make_config( - &["--features", "diff-so-fancy decorations"], - Some(git_config_contents), - Some(git_config_path), - ); - - assert_eq!( - config.file_style.ansi_term_style, - make_style("ul bold 19").ansi_term_style - ); - - assert_eq!( - config.file_style.decoration_style, - make_decoration_style("none") - ); - - remove_file(git_config_path).unwrap(); - } - - #[test] fn test_whitespace_error_style() { let git_config_contents = b" [color \"diff\"] @@ -591,11 +414,11 @@ mod tests { remove_file(git_config_path).unwrap(); } - fn make_style(s: &str) -> Style { + pub fn make_style(s: &str) -> Style { _make_style(s, false) } - fn make_emph_style(s: &str) -> Style { + pub fn make_emph_style(s: &str) -> Style { _make_style(s, true) } @@ -603,7 +426,7 @@ mod tests { Style::from_str(s, None, None, None, true, is_emph) } - fn make_decoration_style(s: &str) -> DecorationStyle { + pub fn make_decoration_style(s: &str) -> DecorationStyle { DecorationStyle::from_str(s, true) } @@ -614,7 +437,7 @@ mod tests { GitConfig::from_path(&path) } - fn make_config( + pub fn make_config( args: &[&str], git_config_contents: Option<&[u8]>, path: Option<&str>, diff --git a/src/features/navigate.rs b/src/features/navigate.rs index 612ded6f..ab2ed9a4 100644 --- a/src/features/navigate.rs +++ b/src/features/navigate.rs @@ -22,48 +22,53 @@ pub fn make_feature() -> Vec<(String, FeatureValueFunction)> { #[cfg(test)] mod tests { - use std::fs::{remove_file, File}; - use std::io::Write; - use std::path::Path; + use std::fs::remove_file; - use itertools; - - use crate::config; - use crate::git_config::GitConfig; - use crate::style::Style; + use crate::features; #[test] - fn test_navigate() { + fn test_navigate_with_overriden_key_in_custom_navigate_section() { let git_config_contents = b" [delta] features = navigate + [delta \"navigate\"] file-modified-label = \"modified: \" "; - let git_config_path = "delta__test_file_modified_label.gitconfig"; + let git_config_path = + "delta__test_navigate_with_overriden_key_in_custom_navigate_section.gitconfig"; - assert_eq!(make_config(&[], None, None).file_modified_label, ""); + assert_eq!(features::tests::make_config(&[], None, None).file_modified_label, ""); assert_eq!( - make_config(&["--features", "navigate"], None, None).file_modified_label, + features::tests::make_config(&["--features", "navigate"], None, None) + .file_modified_label, "Δ" ); assert_eq!( - make_config(&[], Some(git_config_contents), Some(git_config_path)).file_modified_label, + features::tests::make_config(&[], Some(git_config_contents), Some(git_config_path)) + .file_modified_label, "modified: " ); + remove_file(git_config_path).unwrap(); + } + + #[test] + fn test_navigate_activated_by_custom_feature() { let git_config_contents = b" [delta \"my-navigate-feature\"] features = navigate file-modified-label = \"modified: \" "; + let git_config_path = "delta__test_navigate_activated_by_custom_feature.gitconfig"; assert_eq!( - make_config(&[], Some(git_config_contents), Some(git_config_path)).file_modified_label, + features::tests::make_config(&[], Some(git_config_contents), Some(git_config_path)) + .file_modified_label, "" ); assert_eq!( - make_config( + features::tests::make_config( &["--features", "my-navigate-feature"], Some(git_config_contents), Some(git_config_path) @@ -74,33 +79,4 @@ mod tests { remove_file(git_config_path).unwrap(); } - - fn _make_style(s: &str, is_emph: bool) -> Style { - Style::from_str(s, None, None, None, true, is_emph) - } - - fn make_git_config(contents: &[u8], path: &str) -> GitConfig { - let path = Path::new(path); - let mut file = File::create(path).unwrap(); - file.write_all(contents).unwrap(); - GitConfig::from_path(&path) - } - - fn make_config( - args: &[&str], - git_config_contents: Option<&[u8]>, - path: Option<&str>, - ) -> config::Config { - let args: Vec<&str> = itertools::chain( - &["/dev/null", "/dev/null", "--24-bit-color", "always"], - args, - ) - .map(|s| *s) - .collect(); - let mut git_config = match (git_config_contents, path) { - (Some(contents), Some(path)) => Some(make_git_config(contents, path)), - _ => None, - }; - config::Config::from_args(&args, &mut git_config) - } } |