From b399bc46261d90300750349a389bf1b8fd45cd9b Mon Sep 17 00:00:00 2001 From: Erik Kundt Date: Wed, 11 Dec 2024 13:27:32 +0100 Subject: [PATCH] bin(patch): Extend review tests --- bin/commands/patch/review.rs | 69 +++++++++++++++++++++++++++++++++--- bin/test.rs | 5 +++ 2 files changed, 70 insertions(+), 4 deletions(-) diff --git a/bin/commands/patch/review.rs b/bin/commands/patch/review.rs index a0d21f29..4a2c6b11 100644 --- a/bin/commands/patch/review.rs +++ b/bin/commands/patch/review.rs @@ -684,9 +684,24 @@ mod test { } #[test] - fn app_can_be_constructed() -> Result<()> { + fn app_with_single_hunk_can_be_constructed() -> Result<()> { let alice = test::fixtures::node_with_repo(); - let branch = test::fixtures::branch(&alice); + let branch = test::fixtures::branch_with_line_added(&alice); + + let mut patches = Cache::no_cache(&alice.repo.repo).unwrap(); + let patch = test::fixtures::patch(&alice, &branch, &mut patches)?; + + let app = fixtures::app(&alice, patch)?; + + assert_eq!(app.hunks().len(), 1); + + Ok(()) + } + + #[test] + fn app_with_single_file_multiple_hunks_can_be_constructed() -> Result<()> { + let alice = test::fixtures::node_with_repo(); + let branch = test::fixtures::branch_with_eof_removed(&alice); let mut patches = Cache::no_cache(&alice.repo.repo).unwrap(); let patch = test::fixtures::patch(&alice, &branch, &mut patches)?; @@ -701,7 +716,7 @@ mod test { #[test] fn first_hunk_is_selected_by_default() -> Result<()> { let alice = test::fixtures::node_with_repo(); - let branch = test::fixtures::branch(&alice); + let branch = test::fixtures::branch_with_line_added(&alice); let mut patches = Cache::no_cache(&alice.repo.repo).unwrap(); let patch = test::fixtures::patch(&alice, &branch, &mut patches)?; @@ -716,7 +731,7 @@ mod test { #[test] fn hunk_can_be_selected() -> Result<()> { let alice = test::fixtures::node_with_repo(); - let branch = test::fixtures::branch(&alice); + let branch = test::fixtures::branch_with_eof_removed(&alice); let mut patches = Cache::no_cache(&alice.repo.repo).unwrap(); let patch = test::fixtures::patch(&alice, &branch, &mut patches)?; @@ -730,4 +745,50 @@ mod test { Ok(()) } + + #[test] + fn single_file_single_hunk_can_be_accepted() -> Result<()> { + let alice = test::fixtures::node_with_repo(); + let branch = test::fixtures::branch_with_line_added(&alice); + + let mut patches = Cache::no_cache(&alice.repo.repo).unwrap(); + let patch = test::fixtures::patch(&alice, &branch, &mut patches)?; + + let mut app = fixtures::app(&alice, patch)?; + app.update(Message::Accept); + + let hunks = app.hunks(); + let state = &hunks.get(0).unwrap().inner.state(); + + assert_eq!(**state, HunkState::Accepted); + + Ok(()) + } + + #[test] + fn multiple_files_single_hunk_can_be_accepted() -> Result<()> { + let alice = test::fixtures::node_with_repo(); + let branch = test::fixtures::branch_with_files_added(&alice); + + let mut patches = Cache::no_cache(&alice.repo.repo).unwrap(); + let patch = test::fixtures::patch(&alice, &branch, &mut patches)?; + + let mut app = fixtures::app(&alice, patch)?; + app.update(Message::Accept); + + app.update(Message::HunkChanged { + state: TableState::new(Some(1)), + }); + app.update(Message::Accept); + + let hunks = app.hunks(); + let states = [ + hunks.get(0).unwrap().inner.state(), + hunks.get(1).unwrap().inner.state(), + ]; + + assert_eq!(states, [&HunkState::Accepted, &HunkState::Accepted]); + + Ok(()) + } } diff --git a/bin/test.rs b/bin/test.rs index 2fd1b0b2..2fb734a0 100644 --- a/bin/test.rs +++ b/bin/test.rs @@ -20,6 +20,11 @@ pub mod fixtures { checkout.branch_with([("README", b"Hello World!\nHello World!\n")]) } + pub fn branch_with_files_added(node: &NodeWithRepo) -> BranchWith { + let checkout = node.repo.checkout(); + checkout.branch_with([("CONTRIBUTE", b"TBD\n"), ("LICENSE", b"TBD\n")]) + } + pub fn patch<'a, 'g>( node: &'a NodeWithRepo, branch: &BranchWith,