diff options
author | Tim Oram <dev@mitmaro.ca> | 2020-11-17 10:08:10 -0330 |
---|---|---|
committer | Tim Oram <dev@mitmaro.ca> | 2020-11-17 10:22:04 -0330 |
commit | 6c0e6e3085467b04c1b14336ffd878322d59ee60 (patch) | |
tree | 7ed7fd76925a57a8c503996782bbfc5b3ddb521a | |
parent | 916d6ccbdf0a8364ab80deedd9b56a9da9aee741 (diff) |
Add process::error tests
Also fix bug caused by multi-line error rendering.
-rw-r--r-- | src/process/error.rs | 136 | ||||
-rw-r--r-- | src/process/testutil.rs | 24 |
2 files changed, 143 insertions, 17 deletions
diff --git a/src/process/error.rs b/src/process/error.rs index 9ee50a7..632ec8f 100644 --- a/src/process/error.rs +++ b/src/process/error.rs @@ -51,7 +51,10 @@ impl Error { self.view_data.reset(); self.view_data.set_show_title(true); for cause in error.chain() { - self.view_data.push_line(ViewLine::from(format!("{:#}", cause))); + let error_text = format!("{:#}", cause); + for err in error_text.split('\n') { + self.view_data.push_line(ViewLine::from(err)); + } } self.view_data .push_trailing_line(ViewLine::from(LineSegment::new_with_color( @@ -60,3 +63,134 @@ impl Error { ))); } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::assert_process_result; + use crate::assert_rendered_output; + use crate::process::testutil::{process_module_test, TestContext, ViewState}; + use anyhow::anyhow; + + #[test] + #[serial_test::serial] + fn simple_error() { + process_module_test(&[], ViewState::default(), &[], |test_context: TestContext<'_>| { + let mut module = Error::new(); + module.set_error_message(&anyhow!("Test Error")); + let view_data = test_context.build_view_data(&mut module); + assert_rendered_output!( + view_data, + "{TITLE}", + "{BODY}", + "{Normal}Test Error", + "{TRAILING}", + "{IndicatorColor}Press any key to continue" + ); + }); + } + + #[test] + #[serial_test::serial] + fn error_with_contest() { + process_module_test(&[], ViewState::default(), &[], |test_context: TestContext<'_>| { + let mut module = Error::new(); + module.set_error_message(&anyhow!("Test Error").context("Context")); + let view_data = test_context.build_view_data(&mut module); + assert_rendered_output!( + view_data, + "{TITLE}", + "{BODY}", + "{Normal}Context", + "{Normal}Test Error", + "{TRAILING}", + "{IndicatorColor}Press any key to continue" + ); + }); + } + + #[test] + #[serial_test::serial] + fn error_with_newlines() { + process_module_test(&[], ViewState::default(), &[], |test_context: TestContext<'_>| { + let mut module = Error::new(); + module.set_error_message(&anyhow!("Test\nError").context("With\nContext")); + let view_data = test_context.build_view_data(&mut module); + assert_rendered_output!( + view_data, + "{TITLE}", + "{BODY}", + "{Normal}With", + "{Normal}Context", + "{Normal}Test", + "{Normal}Error", + "{TRAILING}", + "{IndicatorColor}Press any key to continue" + ); + }); + } + + #[test] + #[serial_test::serial] + fn return_state() { + process_module_test( + &[], + ViewState::default(), + &[Input::Character('a')], + |mut test_context: TestContext<'_>| { + let mut module = Error::new(); + module.activate(test_context.rebase_todo_file, State::ConfirmRebase); + module.set_error_message(&anyhow!("Test Error")); + assert_process_result!( + test_context.handle_input(&mut module), + input = Input::Character('a'), + state = State::ConfirmRebase + ) + }, + ); + } + + #[test] + #[serial_test::serial] + fn resize() { + process_module_test( + &[], + ViewState::default(), + &[Input::Resize], + |mut test_context: TestContext<'_>| { + let mut module = Error::new(); + module.activate(test_context.rebase_todo_file, State::ConfirmRebase); + module.set_error_message(&anyhow!("Test Error")); + assert_process_result!(test_context.handle_input(&mut module), input = Input::Resize) + }, + ); + } + + #[test] + #[serial_test::serial] + fn scroll_events() { + process_module_test( + &[], + ViewState::default(), + &[ + Input::ScrollLeft, + Input::ScrollRight, + Input::ScrollDown, + Input::ScrollUp, + Input::ScrollJumpDown, + Input::ScrollJumpUp, + ], + |mut test_context: TestContext<'_>| { + let mut module = Error::new(); + module.activate(test_context.rebase_todo_file, State::ConfirmRebase); + module.set_error_message(&anyhow!("Test Error")); + assert_process_result!(test_context.handle_input(&mut module), input = Input::ScrollLeft); + assert_process_result!(test_context.handle_input(&mut module), input = Input::ScrollRight); + assert_process_result!(test_context.handle_input(&mut module), input = Input::ScrollDown); + assert_process_result!(test_context.handle_input(&mut module), input = Input::ScrollUp); + assert_process_result!(test_context.handle_input(&mut module), input = Input::ScrollJumpDown); + assert_process_result!(test_context.handle_input(&mut module), input = Input::ScrollJumpUp); + }, + ); + } +} diff --git a/src/process/testutil.rs b/src/process/testutil.rs index f6057f5..276f07c 100644 --- a/src/process/testutil.rs +++ b/src/process/testutil.rs @@ -164,9 +164,9 @@ fn map_input_to_curses(key_bindings: &KeyBindings, input: Input) -> PancursesInp Input::Backspace => map_input_str_to_curses("Backspace"), Input::Character(c) => map_input_str_to_curses(c.to_string().as_str()), Input::Delete => map_input_str_to_curses("Delete"), - Input::Down => map_input_str_to_curses("Down"), + Input::Down | Input::ScrollDown => map_input_str_to_curses("Down"), Input::Edit => map_input_str_to_curses(key_bindings.edit.as_str()), - Input::End => map_input_str_to_curses("End"), + Input::End | Input::ScrollBottom => map_input_str_to_curses("End"), Input::Enter => map_input_str_to_curses("Enter"), Input::F0 => map_input_str_to_curses("F0"), Input::F1 => map_input_str_to_curses("F1"), @@ -187,14 +187,14 @@ fn map_input_to_curses(key_bindings: &KeyBindings, input: Input) -> PancursesInp Input::ForceAbort => map_input_str_to_curses(key_bindings.force_abort.as_str()), Input::ForceRebase => map_input_str_to_curses(key_bindings.force_rebase.as_str()), Input::Help => map_input_str_to_curses(key_bindings.help.as_str()), - Input::Home => map_input_str_to_curses("Home"), + Input::Home | Input::ScrollTop => map_input_str_to_curses("Home"), Input::Insert => map_input_str_to_curses("Insert"), Input::KeypadCenter => map_input_str_to_curses("KeypadCenter"), Input::KeypadLowerLeft => map_input_str_to_curses("KeypadLowerLeft"), Input::KeypadLowerRight => map_input_str_to_curses("KeypadLowerRight"), Input::KeypadUpperLeft => map_input_str_to_curses("KeypadUpperLeft"), Input::KeypadUpperRight => map_input_str_to_curses("KeypadUpperRight"), - Input::Left => map_input_str_to_curses("Left"), + Input::Left | Input::ScrollLeft => map_input_str_to_curses("Left"), Input::MoveCursorDown => map_input_str_to_curses(key_bindings.move_down.as_str()), Input::MoveCursorLeft => map_input_str_to_curses(key_bindings.move_left.as_str()), Input::MoveCursorPageDown => map_input_str_to_curses(key_bindings.move_down_step.as_str()), @@ -204,20 +204,12 @@ fn map_input_to_curses(key_bindings: &KeyBindings, input: Input) -> PancursesInp Input::No => map_input_str_to_curses(key_bindings.confirm_no.as_str()), Input::OpenInEditor => map_input_str_to_curses(key_bindings.open_in_external_editor.as_str()), Input::Other => map_input_str_to_curses("Other"), - Input::PageDown => map_input_str_to_curses("PageDown"), - Input::PageUp => map_input_str_to_curses("PageUp"), + Input::PageDown | Input::ScrollJumpDown => map_input_str_to_curses("PageDown"), + Input::PageUp | Input::ScrollJumpUp => map_input_str_to_curses("PageUp"), Input::Print => map_input_str_to_curses("Print"), Input::Rebase => map_input_str_to_curses(key_bindings.rebase.as_str()), Input::Resize => map_input_str_to_curses("Resize"), - Input::Right => map_input_str_to_curses("Right"), - Input::ScrollBottom => map_input_str_to_curses("ScrollBottom"), - Input::ScrollDown => map_input_str_to_curses("ScrollDown"), - Input::ScrollJumpDown => map_input_str_to_curses("ScrollJumpDown"), - Input::ScrollJumpUp => map_input_str_to_curses("ScrollJumpUp"), - Input::ScrollLeft => map_input_str_to_curses("ScrollLeft"), - Input::ScrollRight => map_input_str_to_curses("ScrollRight"), - Input::ScrollTop => map_input_str_to_curses("ScrollTop"), - Input::ScrollUp => map_input_str_to_curses("ScrollUp"), + Input::Right | Input::ScrollRight => map_input_str_to_curses("Right"), Input::ShiftDelete => map_input_str_to_curses("ShiftDelete"), Input::ShiftDown => map_input_str_to_curses("ShiftDown"), Input::ShiftEnd => map_input_str_to_curses("ShiftEnd"), @@ -235,7 +227,7 @@ fn map_input_to_curses(key_bindings: &KeyBindings, input: Input) -> PancursesInp Input::SwapSelectedUp => map_input_str_to_curses(key_bindings.move_selection_up.as_str()), Input::Tab => map_input_str_to_curses("Tab"), Input::ToggleVisualMode => map_input_str_to_curses(key_bindings.toggle_visual_mode.as_str()), - Input::Up => map_input_str_to_curses("Up"), + Input::Up | Input::ScrollUp => map_input_str_to_curses("Up"), Input::Yes => map_input_str_to_curses(key_bindings.confirm_yes.as_str()), } } |