From c1743ad8c51ed02f04b2526180285df6c0795a6e Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sat, 24 Feb 2018 02:46:52 -0500 Subject: more tests --- src/prompt.rs | 247 +++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 192 insertions(+), 55 deletions(-) diff --git a/src/prompt.rs b/src/prompt.rs index fcb2f0a..940029f 100644 --- a/src/prompt.rs +++ b/src/prompt.rs @@ -195,61 +195,7 @@ impl Prompt { } fn format_vcs(&self) -> Option { - self.data.vcs_info.as_ref().map(|vcs_info| { - let mut vcs = String::new(); - - write!(vcs, "{}", vcs_id(vcs_info.vcs())).unwrap(); - - if vcs_info.has_modified_files() { - write!(vcs, "*").unwrap(); - } - if vcs_info.has_staged_files() { - write!(vcs, "+").unwrap(); - } - if vcs_info.has_new_files() { - write!(vcs, "?").unwrap(); - } - if !vcs_info.has_commits() { - write!(vcs, "!").unwrap(); - } - - let branch = vcs_info.branch().map(|branch| { - if branch == "master" { - String::new() - } - else { - branch - } - }).unwrap_or_else(|| String::from("???")); - if branch != "" { - write!(vcs, ":").unwrap(); - } - write!(vcs, "{}", branch).unwrap(); - - if let Some((local, remote)) = vcs_info.remote_branch_diff() { - if local > 0 || remote > 0 { - write!(vcs, ":").unwrap(); - } - if local > 0 { - write!(vcs, "+{}", local).unwrap(); - } - if remote > 0 { - write!(vcs, "-{}", remote).unwrap(); - } - } - else { - write!(vcs, ":-").unwrap(); - } - - match vcs_info.active_operation() { - vcs::ActiveOperation::None => {}, - op => { - write!(vcs, "({})", active_operation_id(op)).unwrap(); - } - } - - vcs - }) + format_vcs(&self.data.vcs_info) } fn vcs_color(&self) -> String { @@ -320,6 +266,64 @@ fn path_color(path: &Option) -> String }).unwrap_or_else(|| String::from("path_not_exist")) } +fn format_vcs(vcs_info: &Option>) -> Option { + vcs_info.as_ref().map(|vcs_info| { + let mut vcs = String::new(); + + write!(vcs, "{}", vcs_id(vcs_info.vcs())).unwrap(); + + if vcs_info.has_modified_files() { + write!(vcs, "*").unwrap(); + } + if vcs_info.has_staged_files() { + write!(vcs, "+").unwrap(); + } + if vcs_info.has_new_files() { + write!(vcs, "?").unwrap(); + } + if !vcs_info.has_commits() { + write!(vcs, "!").unwrap(); + } + + let branch = vcs_info.branch().map(|branch| { + if branch == "master" { + String::new() + } + else { + branch + } + }).unwrap_or_else(|| String::from("???")); + if branch != "" { + write!(vcs, ":").unwrap(); + } + write!(vcs, "{}", branch).unwrap(); + + if let Some((local, remote)) = vcs_info.remote_branch_diff() { + if local > 0 || remote > 0 { + write!(vcs, ":").unwrap(); + } + if local > 0 { + write!(vcs, "+{}", local).unwrap(); + } + if remote > 0 { + write!(vcs, "-{}", remote).unwrap(); + } + } + else { + write!(vcs, ":-").unwrap(); + } + + match vcs_info.active_operation() { + vcs::ActiveOperation::None => {}, + op => { + write!(vcs, "({})", active_operation_id(op)).unwrap(); + } + } + + vcs + }) +} + fn compress_path( path: &Option, home: &Option, @@ -416,6 +420,44 @@ fn active_operation_id(op: vcs::ActiveOperation) -> String { mod test { use super::*; + struct TestVcs { + vcs: vcs::VcsType, + has_modified_files: bool, + has_staged_files: bool, + has_new_files: bool, + has_commits: bool, + active_operation: vcs::ActiveOperation, + branch: Option, + remote_branch_diff: Option<(usize, usize)>, + } + + impl vcs::VcsInfo for TestVcs { + fn vcs(&self) -> vcs::VcsType { + self.vcs + } + fn has_modified_files(&self) -> bool { + self.has_modified_files + } + fn has_staged_files(&self) -> bool { + self.has_staged_files + } + fn has_new_files(&self) -> bool { + self.has_new_files + } + fn has_commits(&self) -> bool { + self.has_commits + } + fn active_operation(&self) -> vcs::ActiveOperation { + self.active_operation + } + fn branch(&self) -> Option { + self.branch.clone() + } + fn remote_branch_diff(&self) -> Option<(usize, usize)> { + self.remote_branch_diff + } + } + #[test] fn test_compress_path() { { @@ -520,4 +562,99 @@ mod test { } } } + + #[test] + fn test_format_vcs() { + { + assert_eq!( + format_vcs(&None), + None + ) + } + { + let test_vcs = TestVcs { + vcs: vcs::VcsType::Git, + has_modified_files: false, + has_staged_files: false, + has_new_files: false, + has_commits: true, + active_operation: vcs::ActiveOperation::None, + branch: Some(String::from("master")), + remote_branch_diff: Some((0, 0)), + }; + + assert_eq!( + format_vcs(&Some(Box::new(test_vcs))), + Some(String::from("g")) + ) + } + { + let test_vcs = TestVcs { + vcs: vcs::VcsType::Git, + has_modified_files: false, + has_staged_files: false, + has_new_files: false, + has_commits: true, + active_operation: vcs::ActiveOperation::None, + branch: Some(String::from("master")), + remote_branch_diff: None, + }; + + assert_eq!( + format_vcs(&Some(Box::new(test_vcs))), + Some(String::from("g:-")) + ) + } + { + let test_vcs = TestVcs { + vcs: vcs::VcsType::Git, + has_modified_files: false, + has_staged_files: false, + has_new_files: false, + has_commits: true, + active_operation: vcs::ActiveOperation::None, + branch: Some(String::from("dev")), + remote_branch_diff: None, + }; + + assert_eq!( + format_vcs(&Some(Box::new(test_vcs))), + Some(String::from("g:dev:-")) + ) + } + { + let test_vcs = TestVcs { + vcs: vcs::VcsType::Git, + has_modified_files: true, + has_staged_files: true, + has_new_files: true, + has_commits: true, + active_operation: vcs::ActiveOperation::None, + branch: Some(String::from("master")), + remote_branch_diff: None, + }; + + assert_eq!( + format_vcs(&Some(Box::new(test_vcs))), + Some(String::from("g*+?:-")) + ) + } + { + let test_vcs = TestVcs { + vcs: vcs::VcsType::Git, + has_modified_files: true, + has_staged_files: true, + has_new_files: true, + has_commits: true, + active_operation: vcs::ActiveOperation::None, + branch: Some(String::from("dev")), + remote_branch_diff: None, + }; + + assert_eq!( + format_vcs(&Some(Box::new(test_vcs))), + Some(String::from("g*+?:dev:-")) + ) + } + } } -- cgit v1.2.3