diff options
author | Tim Oram <dev@mitmaro.ca> | 2024-02-11 21:37:18 -0330 |
---|---|---|
committer | Tim Oram <dev@mitmaro.ca> | 2024-02-15 20:27:06 -0330 |
commit | f3784a9f671e4a21cd99b5a48caaefd3843f129c (patch) | |
tree | 902213be8c44bef16554d62ecbdedbc0439b6e74 | |
parent | b9569b975f304c188e12e3d216daa6051e4e970c (diff) |
Refactor Crossterm mock and move to test helpers
-rw-r--r-- | src/application.rs | 3 | ||||
-rw-r--r-- | src/display.rs | 4 | ||||
-rw-r--r-- | src/display/testutil.rs | 10 | ||||
-rw-r--r-- | src/display/testutil/mockable_tui.rs | 123 | ||||
-rw-r--r-- | src/display/testutil/state.rs | 11 | ||||
-rw-r--r-- | src/editor.rs | 4 | ||||
-rw-r--r-- | src/test_helpers.rs | 1 | ||||
-rw-r--r-- | src/test_helpers/mocks.rs | 1 | ||||
-rw-r--r-- | src/test_helpers/mocks/crossterm.rs (renamed from src/display/testutil/mockcrossterm.rs) | 133 | ||||
-rw-r--r-- | src/view/tests.rs | 5 | ||||
-rw-r--r-- | src/view/thread.rs | 13 |
11 files changed, 141 insertions, 167 deletions
diff --git a/src/application.rs b/src/application.rs index a640b51..1b8bbb2 100644 --- a/src/application.rs +++ b/src/application.rs @@ -195,11 +195,12 @@ mod tests { use super::*; use crate::{ - display::{testutil::CrossTerm, Size}, + display::Size, events::Event, input::{KeyCode, KeyEvent, KeyModifiers}, module::Modules, runtime::{Installer, RuntimeError}, + test_helpers::mocks::crossterm::CrossTerm, testutil::{create_event_reader, set_git_directory, DefaultTestModule, TestModuleProvider}, }; diff --git a/src/display.rs b/src/display.rs index b80da19..c132bcf 100644 --- a/src/display.rs +++ b/src/display.rs @@ -14,8 +14,6 @@ mod crossterm; mod display_color; mod error; mod size; -#[cfg(not(tarpaulin_include))] -pub(crate) mod testutil; mod tui; mod utils; @@ -369,7 +367,7 @@ mod tests { use rstest::rstest; use super::*; - use crate::display::testutil::{CrossTerm, State}; + use crate::test_helpers::mocks::crossterm::{CrossTerm, State}; #[test] fn draw_str() { diff --git a/src/display/testutil.rs b/src/display/testutil.rs deleted file mode 100644 index 3289b70..0000000 --- a/src/display/testutil.rs +++ /dev/null @@ -1,10 +0,0 @@ -//! Utilities for writing tests that interact with the display. -mod mockable_tui; -mod mockcrossterm; -mod state; - -pub(crate) use self::{ - mockable_tui::{create_unexpected_error, MockableTui}, - mockcrossterm::CrossTerm, - state::State, -}; diff --git a/src/display/testutil/mockable_tui.rs b/src/display/testutil/mockable_tui.rs deleted file mode 100644 index 906d930..0000000 --- a/src/display/testutil/mockable_tui.rs +++ /dev/null @@ -1,123 +0,0 @@ -use std::io; - -use crossterm::style::Colors; - -use crate::display::{ColorMode, DisplayError, Size, Tui}; - -/// Create an instance of a `DisplayError::Unexpected` error with an other IO error. -#[must_use] -pub(crate) fn create_unexpected_error() -> DisplayError { - DisplayError::Unexpected(io::Error::from(io::ErrorKind::Other)) -} - -/// A version of the `TUI` that provides defaults for all trait methods. This can be used to create -/// mocked versions of the `TUI` interface, without needing to define all methods provided by the -/// interface. -#[allow(missing_docs, clippy::missing_errors_doc)] -pub(crate) trait MockableTui: Tui { - fn get_color_mode(&self) -> ColorMode { - ColorMode::TwoTone - } - - fn reset(&mut self) -> Result<(), DisplayError> { - Ok(()) - } - - fn flush(&mut self) -> Result<(), DisplayError> { - Ok(()) - } - - fn print(&mut self, _s: &str) -> Result<(), DisplayError> { - Ok(()) - } - - fn set_color(&mut self, _colors: Colors) -> Result<(), DisplayError> { - Ok(()) - } - - fn set_dim(&mut self, _dim: bool) -> Result<(), DisplayError> { - Ok(()) - } - - fn set_underline(&mut self, _underline: bool) -> Result<(), DisplayError> { - Ok(()) - } - - fn set_reverse(&mut self, _reverse: bool) -> Result<(), DisplayError> { - Ok(()) - } - - fn get_size(&self) -> Size { - Size::new(100, 100) - } - - fn move_to_column(&mut self, _x: u16) -> Result<(), DisplayError> { - Ok(()) - } - - fn move_next_line(&mut self) -> Result<(), DisplayError> { - Ok(()) - } - - fn start(&mut self) -> Result<(), DisplayError> { - Ok(()) - } - - fn end(&mut self) -> Result<(), DisplayError> { - Ok(()) - } -} - -impl<T: MockableTui> Tui for T { - fn get_color_mode(&self) -> ColorMode { - <T as MockableTui>::get_color_mode(self) - } - - fn reset(&mut self) -> Result<(), DisplayError> { - <T as MockableTui>::reset(self) - } - - fn flush(&mut self) -> Result<(), DisplayError> { - <T as MockableTui>::flush(self) - } - - fn print(&mut self, s: &str) -> Result<(), DisplayError> { - <T as MockableTui>::print(self, s) - } - - fn set_color(&mut self, colors: Colors) -> Result<(), DisplayError> { - <T as MockableTui>::set_color(self, colors) - } - - fn set_dim(&mut self, dim: bool) -> Result<(), DisplayError> { - <T as MockableTui>::set_dim(self, dim) - } - - fn set_underline(&mut self, underline: bool) -> Result<(), DisplayError> { - <T as MockableTui>::set_underline(self, underline) - } - - fn set_reverse(&mut self, reverse: bool) -> Result<(), DisplayError> { - <T as MockableTui>::set_reverse(self, reverse) - } - - fn get_size(&self) -> Size { - <T as MockableTui>::get_size(self) - } - - fn move_to_column(&mut self, x: u16) -> Result<(), DisplayError> { - <T as MockableTui>::move_to_column(self, x) - } - - fn move_next_line(&mut self) -> Result<(), DisplayError> { - <T as MockableTui>::move_next_line(self) - } - - fn start(&mut self) -> Result<(), DisplayError> { - <T as MockableTui>::start(self) - } - - fn end(&mut self) -> Result<(), DisplayError> { - <T as MockableTui>::end(self) - } -} diff --git a/src/display/testutil/state.rs b/src/display/testutil/state.rs deleted file mode 100644 index 0a66c3a..0000000 --- a/src/display/testutil/state.rs +++ /dev/null @@ -1,11 +0,0 @@ -/// The state of the `CrossTerm` instance. -#[derive(Copy, Clone, Debug, PartialEq, Eq)] -#[allow(clippy::exhaustive_enums)] -pub(crate) enum State { - /// The TUI is new and unchanged. - New, - /// The TUI is in the normal mode. - Normal, - /// The TUI has ended. - Ended, -} diff --git a/src/editor.rs b/src/editor.rs index ca0ffcd..da36b5a 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -1,7 +1,7 @@ -#[cfg(test)] -use crate::display::testutil::CrossTerm; #[cfg(not(test))] use crate::display::CrossTerm; +#[cfg(test)] +use crate::test_helpers::mocks::crossterm::CrossTerm; use crate::{ application::Application, arguments::Args, diff --git a/src/test_helpers.rs b/src/test_helpers.rs index ea0af6b..a353ef4 100644 --- a/src/test_helpers.rs +++ b/src/test_helpers.rs @@ -1,4 +1,5 @@ mod create_invalid_utf; +pub(crate) mod mocks; mod with_git_config; pub(crate) use self::{create_invalid_utf::invalid_utf, with_git_config::with_git_config}; diff --git a/src/test_helpers/mocks.rs b/src/test_helpers/mocks.rs new file mode 100644 index 0000000..5d17e48 --- /dev/null +++ b/src/test_helpers/mocks.rs @@ -0,0 +1 @@ +pub(crate) mod crossterm; diff --git a/src/display/testutil/mockcrossterm.rs b/src/test_helpers/mocks/crossterm.rs index 85051dc..134a562 100644 --- a/src/display/testutil/mockcrossterm.rs +++ b/src/test_helpers/mocks/crossterm.rs @@ -1,14 +1,133 @@ -use std::{ops::Deref, sync::Arc}; +use std::{io, ops::Deref, sync::Arc}; use ::crossterm::style::{Attribute, Attributes, Color, Colors}; use parking_lot::RwLock; -use crate::display::{ - testutil::{MockableTui, State}, - ColorMode, - DisplayError, - Size, -}; +use crate::display::{ColorMode, DisplayError, Size, Tui}; + +/// The state of the `CrossTerm` instance. +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +#[allow(clippy::exhaustive_enums)] +pub(crate) enum State { + /// The TUI is new and unchanged. + New, + /// The TUI is in the normal mode. + Normal, + /// The TUI has ended. + Ended, +} + +/// A version of the `TUI` that provides defaults for all trait methods. This can be used to create +/// mocked versions of the `TUI` interface, without needing to define all methods provided by the +/// interface. +#[allow(missing_docs, clippy::missing_errors_doc)] +pub(crate) trait MockableTui: Tui { + fn get_color_mode(&self) -> ColorMode { + ColorMode::TwoTone + } + + fn reset(&mut self) -> Result<(), DisplayError> { + Ok(()) + } + + fn flush(&mut self) -> Result<(), DisplayError> { + Ok(()) + } + + fn print(&mut self, _s: &str) -> Result<(), DisplayError> { + Ok(()) + } + + fn set_color(&mut self, _colors: Colors) -> Result<(), DisplayError> { + Ok(()) + } + + fn set_dim(&mut self, _dim: bool) -> Result<(), DisplayError> { + Ok(()) + } + + fn set_underline(&mut self, _underline: bool) -> Result<(), DisplayError> { + Ok(()) + } + + fn set_reverse(&mut self, _reverse: bool) -> Result<(), DisplayError> { + Ok(()) + } + + fn get_size(&self) -> Size { + Size::new(100, 100) + } + + fn move_to_column(&mut self, _x: u16) -> Result<(), DisplayError> { + Ok(()) + } + + fn move_next_line(&mut self) -> Result<(), DisplayError> { + Ok(()) + } + + fn start(&mut self) -> Result<(), DisplayError> { + Ok(()) + } + + fn end(&mut self) -> Result<(), DisplayError> { + Ok(()) + } +} + +impl<T: MockableTui> Tui for T { + fn get_color_mode(&self) -> ColorMode { + <T as MockableTui>::get_color_mode(self) + } + + fn reset(&mut self) -> Result<(), DisplayError> { + <T as MockableTui>::reset(self) + } + + fn flush(&mut self) -> Result<(), DisplayError> { + <T as MockableTui>::flush(self) + } + + fn print(&mut self, s: &str) -> Result<(), DisplayError> { + <T as MockableTui>::print(self, s) + } + + fn set_color(&mut self, colors: Colors) -> Result<(), DisplayError> { + <T as MockableTui>::set_color(self, colors) + } + + fn set_dim(&mut self, dim: bool) -> Result<(), DisplayError> { + <T as MockableTui>::set_dim(self, dim) + } + + fn set_underline(&mut self, underline: bool) -> Result<(), DisplayError> { + <T as MockableTui>::set_underline(self, underline) + } + + fn set_reverse(&mut self, reverse: bool) -> Result<(), DisplayError> { + <T as MockableTui>::set_reverse(self, reverse) + } + + fn get_size(&self) -> Size { + <T as MockableTui>::get_size(self) + } + + fn move_to_column(&mut self, x: u16) -> Result<(), DisplayError> { + <T as MockableTui>::move_to_column(self, x) + } + + fn move_next_line(&mut self) -> Result<(), DisplayError> { + <T as MockableTui>::move_next_line(self) + } + + fn start(&mut self) -> Result<(), DisplayError> { + <T as MockableTui>::start(self) + } + + fn end(&mut self) -> Result<(), DisplayError> { + <T as MockableTui>::end(self) + } +} #[derive(Debug)] struct CrossTermInternalState { diff --git a/src/view/tests.rs b/src/view/tests.rs index 660ed41..515068d 100644 --- a/src/view/tests.rs +++ b/src/view/tests.rs @@ -1,8 +1,5 @@ use super::*; -use crate::{ - config::Theme, - display::{testutil::CrossTerm, Size}, -}; +use crate::{config::Theme, display::Size, test_helpers::mocks::crossterm::CrossTerm}; fn assert_render(width: usize, height: usize, view_data: &ViewData, expected: &[&str]) { let theme = Theme::new(); diff --git a/src/view/thread.rs b/src/view/thread.rs index b382263..33f373d 100644 --- a/src/view/thread.rs +++ b/src/view/thread.rs @@ -155,24 +155,25 @@ impl<ViewTui: Tui + Send + 'static> Thread<ViewTui> { #[cfg(test)] mod tests { - use std::borrow::BorrowMut; + use std::{borrow::BorrowMut, io}; use claims::assert_ok; use super::*; use crate::{ config::Theme, - display::{ - testutil::{create_unexpected_error, CrossTerm, MockableTui}, - Display, - DisplayError, - }, + display::{Display, DisplayError}, runtime::{testutils::ThreadableTester, Status}, + test_helpers::mocks::crossterm::{CrossTerm, MockableTui}, view::ViewData, }; const READ_MESSAGE_TIMEOUT: Duration = Duration::from_secs(1); + fn create_unexpected_error() -> DisplayError { + DisplayError::Unexpected(io::Error::from(io::ErrorKind::Other)) + } + fn with_view<C, CT: MockableTui>(tui: CT, callback: C) where C: FnOnce(View<CT>) { let theme = Theme::new(); |