diff options
author | Tim Oram <dev@mitmaro.ca> | 2020-11-19 21:20:47 -0330 |
---|---|---|
committer | Tim Oram <dev@mitmaro.ca> | 2020-11-19 22:07:03 -0330 |
commit | b653c0510940b01e04bdfd278d189e8addfb090a (patch) | |
tree | 472197a597eb3b56f86f8648bcbe6560452099e0 | |
parent | f7dc224f231c06fb48ecb85c050f5d7a9f249833 (diff) |
Add process::window_size_error tests
-rw-r--r-- | src/process/window_size_error.rs | 137 |
1 files changed, 132 insertions, 5 deletions
diff --git a/src/process/window_size_error.rs b/src/process/window_size_error.rs index e112e73..5034ac2 100644 --- a/src/process/window_size_error.rs +++ b/src/process/window_size_error.rs @@ -40,12 +40,9 @@ impl ProcessModule for WindowSizeError { if view_width >= MINIMUM_WINDOW_HEIGHT_ERROR_WIDTH { HEIGHT_ERROR_MESSAGE } - else if view_width >= SHORT_ERROR_MESSAGE.len() { - SHORT_ERROR_MESSAGE - } else { - // not much to do if the window gets too narrow - SIZE_ERROR_MESSAGE + // this message will always be safe here + SHORT_ERROR_MESSAGE } } else { @@ -86,3 +83,133 @@ impl WindowSizeError { window_width <= MINIMUM_COMPACT_WINDOW_WIDTH || window_height <= MINIMUM_WINDOW_HEIGHT } } + +#[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 rstest::rstest; + use std::convert::TryFrom; + + #[test] + fn is_window_too_small_width_too_small() { + assert!(WindowSizeError::is_window_too_small( + MINIMUM_COMPACT_WINDOW_WIDTH, + MINIMUM_WINDOW_HEIGHT + 1 + )); + } + + #[test] + fn is_window_too_small_height_too_small() { + assert!(WindowSizeError::is_window_too_small( + MINIMUM_COMPACT_WINDOW_WIDTH + 1, + MINIMUM_WINDOW_HEIGHT + )); + } + + #[test] + fn is_window_too_small_height_and_width_too_small() { + assert!(WindowSizeError::is_window_too_small( + MINIMUM_COMPACT_WINDOW_WIDTH, + MINIMUM_WINDOW_HEIGHT + )); + } + + #[test] + fn is_window_too_small_width_and_height_large() { + assert!(!WindowSizeError::is_window_too_small( + MINIMUM_COMPACT_WINDOW_WIDTH + 1, + MINIMUM_WINDOW_HEIGHT + 1 + )); + } + + #[rstest( + width, height, expected, + case::not_too_small(100, 100, "Bug: window size is invalid!"), + case::width_too_small_long_message(SHORT_ERROR_MESSAGE.len(), MINIMUM_WINDOW_HEIGHT + 1, "Window too small"), + case::width_too_small_short_message(SHORT_ERROR_MESSAGE.len() - 1, MINIMUM_WINDOW_HEIGHT + 1, "Size!"), + case::height_too_small_long_message( + MINIMUM_WINDOW_HEIGHT_ERROR_WIDTH, MINIMUM_WINDOW_HEIGHT, "Window too small, increase height to continue" + ), + case::height_too_small_short_message( + MINIMUM_WINDOW_HEIGHT_ERROR_WIDTH - 1, MINIMUM_WINDOW_HEIGHT, "Window too small" + ) + )] + #[allow(clippy::cast_possible_wrap)] + #[serial_test::serial] + fn build_view_data(width: usize, height: usize, expected: &str) { + process_module_test( + &[], + ViewState { + size: (i32::try_from(width).unwrap(), i32::try_from(height).unwrap()), + ..ViewState::default() + }, + &[], + |test_context: TestContext<'_>| { + let mut module = WindowSizeError::new(); + let view_data = test_context.build_view_data(&mut module); + assert_rendered_output!(view_data, "{BODY}", format!("{{Normal}}{}", expected)); + }, + ); + } + + #[test] + #[serial_test::serial] + fn input_resize_window_still_small() { + process_module_test( + &[], + ViewState { + size: (1, 1), + ..ViewState::default() + }, + &[Input::Resize], + |mut test_context: TestContext<'_>| { + let mut module = WindowSizeError::new(); + module.activate(test_context.rebase_todo_file, State::ConfirmRebase); + assert_process_result!(test_context.handle_input(&mut module), input = Input::Resize); + }, + ); + } + + #[test] + #[serial_test::serial] + fn input_resize_window_no_longer_too_small() { + process_module_test( + &[], + ViewState { + size: (100, 100), + ..ViewState::default() + }, + &[Input::Resize], + |mut test_context: TestContext<'_>| { + let mut module = WindowSizeError::new(); + module.activate(test_context.rebase_todo_file, State::ConfirmRebase); + assert_process_result!( + test_context.handle_input(&mut module), + input = Input::Resize, + state = State::ConfirmRebase + ); + }, + ); + } + + #[test] + #[serial_test::serial] + fn input_other_character() { + process_module_test( + &[], + ViewState { + size: (100, 100), + ..ViewState::default() + }, + &[Input::Character('a')], + |mut test_context: TestContext<'_>| { + let mut module = WindowSizeError::new(); + module.activate(test_context.rebase_todo_file, State::ConfirmRebase); + assert_process_result!(test_context.handle_input(&mut module), input = Input::Character('a')); + }, + ); + } +} |