diff options
author | Tim Oram <dev@mitmaro.ca> | 2024-02-11 21:19:48 -0330 |
---|---|---|
committer | Tim Oram <dev@mitmaro.ca> | 2024-02-15 20:27:06 -0330 |
commit | b9569b975f304c188e12e3d216daa6051e4e970c (patch) | |
tree | 1b542aaa60daff8bdb53cf4691d4ab09bc9d6139 | |
parent | cf89e9a5c4e3925fbad2935567619beb3e767f1e (diff) |
Remove assert_output test util
-rw-r--r-- | src/display/testutil.rs | 11 | ||||
-rw-r--r-- | src/display/testutil/mockcrossterm.rs | 114 | ||||
-rw-r--r-- | src/view/tests.rs | 9 |
3 files changed, 70 insertions, 64 deletions
diff --git a/src/display/testutil.rs b/src/display/testutil.rs index 2c986dd..3289b70 100644 --- a/src/display/testutil.rs +++ b/src/display/testutil.rs @@ -8,14 +8,3 @@ pub(crate) use self::{ mockcrossterm::CrossTerm, state::State, }; -use crate::display::Display; - -/// Assert the the content of the Display is an expected value. -/// -/// # Panics -/// -/// Will panic is the expected output does not match the rendered output. -#[allow(clippy::missing_assert_message)] // not sure why this is triggering -pub(crate) fn assert_output(display: &Display<CrossTerm>, expected: &[&str]) { - assert_eq!(display.tui.get_output().join(""), format!("{}\n", expected.join("\n"))); -} diff --git a/src/display/testutil/mockcrossterm.rs b/src/display/testutil/mockcrossterm.rs index 5372765..85051dc 100644 --- a/src/display/testutil/mockcrossterm.rs +++ b/src/display/testutil/mockcrossterm.rs @@ -1,4 +1,7 @@ +use std::{ops::Deref, sync::Arc}; + use ::crossterm::style::{Attribute, Attributes, Color, Colors}; +use parking_lot::RwLock; use crate::display::{ testutil::{MockableTui, State}, @@ -7,9 +10,8 @@ use crate::display::{ Size, }; -/// A mocked version of `CrossTerm`, useful for testing. #[derive(Debug)] -pub(crate) struct CrossTerm { +struct CrossTermInternalState { attributes: Attributes, color_mode: ColorMode, colors: Colors, @@ -20,87 +22,95 @@ pub(crate) struct CrossTerm { state: State, } +/// A mocked version of `CrossTerm`, useful for testing. +#[derive(Debug)] +pub(crate) struct CrossTerm { + state: Arc<RwLock<CrossTermInternalState>>, +} + impl MockableTui for CrossTerm { fn get_color_mode(&self) -> ColorMode { - self.color_mode + self.state.read().color_mode } fn reset(&mut self) -> Result<(), DisplayError> { - self.attributes = Attributes::from(Attribute::Reset); - self.colors = Colors::new(Color::Reset, Color::Reset); - self.output.clear(); - self.state = State::Normal; + let mut state = self.state.write(); + state.attributes = Attributes::from(Attribute::Reset); + state.colors = Colors::new(Color::Reset, Color::Reset); + state.output.clear(); + state.state = State::Normal; Ok(()) } fn flush(&mut self) -> Result<(), DisplayError> { - self.dirty = false; + self.state.write().dirty = false; Ok(()) } fn print(&mut self, s: &str) -> Result<(), DisplayError> { - self.output.push(String::from(s)); + self.state.write().output.push(String::from(s)); Ok(()) } fn set_color(&mut self, colors: Colors) -> Result<(), DisplayError> { - self.colors = colors; + self.state.write().colors = colors; Ok(()) } fn set_dim(&mut self, dim: bool) -> Result<(), DisplayError> { if dim { - self.attributes.set(Attribute::Dim); + self.state.write().attributes.set(Attribute::Dim); } else { - self.attributes.set(Attribute::NormalIntensity); + self.state.write().attributes.set(Attribute::NormalIntensity); } Ok(()) } fn set_underline(&mut self, dim: bool) -> Result<(), DisplayError> { if dim { - self.attributes.set(Attribute::Underlined); + self.state.write().attributes.set(Attribute::Underlined); } else { - self.attributes.set(Attribute::NoUnderline); + self.state.write().attributes.set(Attribute::NoUnderline); } Ok(()) } fn set_reverse(&mut self, dim: bool) -> Result<(), DisplayError> { if dim { - self.attributes.set(Attribute::Reverse); + self.state.write().attributes.set(Attribute::Reverse); } else { - self.attributes.set(Attribute::NoReverse); + self.state.write().attributes.set(Attribute::NoReverse); } Ok(()) } fn get_size(&self) -> Size { - self.size + self.state.read().size } fn move_to_column(&mut self, x: u16) -> Result<(), DisplayError> { - self.position.0 = x; + self.state.write().position.0 = x; Ok(()) } fn move_next_line(&mut self) -> Result<(), DisplayError> { - self.output.push(String::from("\n")); - self.position.0 = 0; - self.position.1 += 1; + let mut state = self.state.write(); + state.output.push(String::from("\n")); + state.position.0 = 0; + state.position.1 += 1; Ok(()) } fn start(&mut self) -> Result<(), DisplayError> { - self.state = State::Normal; + self.state.write().state = State::Normal; Ok(()) } fn end(&mut self) -> Result<(), DisplayError> { - self.state = State::Ended; + self.state.write().state = State::Ended; Ok(()) } } @@ -110,67 +120,77 @@ impl CrossTerm { #[must_use] pub(crate) fn new() -> Self { Self { - attributes: Attributes::from(Attribute::Reset), - color_mode: ColorMode::FourBit, - colors: Colors::new(Color::Reset, Color::Reset), - dirty: true, - output: vec![], - position: (0, 0), - size: Size::new(10, 10), - state: State::New, + state: Arc::new(RwLock::new(CrossTermInternalState { + attributes: Attributes::from(Attribute::Reset), + color_mode: ColorMode::FourBit, + colors: Colors::new(Color::Reset, Color::Reset), + dirty: true, + output: vec![], + position: (0, 0), + size: Size::new(10, 10), + state: State::New, + })), } } /// Get a representation of the rendered output. #[must_use] - pub(crate) const fn get_output(&self) -> &Vec<String> { - &self.output + pub(crate) fn get_output(&self) -> Vec<String> { + self.state.read().output.clone() } /// Get the current state. #[must_use] - pub(crate) const fn get_state(&self) -> State { - self.state + pub(crate) fn get_state(&self) -> State { + self.state.read().state } /// Are colors enabled. #[must_use] pub(crate) fn is_colors_enabled(&self, colors: Colors) -> bool { - self.colors == colors + self.state.read().colors == colors } /// Does the current style attributes contained dimmed. #[must_use] - pub(crate) const fn is_dimmed(&self) -> bool { - self.attributes.has(Attribute::Dim) + pub(crate) fn is_dimmed(&self) -> bool { + self.state.read().attributes.has(Attribute::Dim) } /// Does the current style attributes contained reverse. #[must_use] - pub(crate) const fn is_reverse(&self) -> bool { - self.attributes.has(Attribute::Reverse) + pub(crate) fn is_reverse(&self) -> bool { + self.state.read().attributes.has(Attribute::Reverse) } /// Does the current style attributes contained underlined. #[must_use] - pub(crate) const fn is_underline(&self) -> bool { - self.attributes.has(Attribute::Underlined) + pub(crate) fn is_underline(&self) -> bool { + self.state.read().attributes.has(Attribute::Underlined) } /// Update the size. pub(crate) fn set_size(&mut self, size: Size) { - self.size = size; + self.state.write().size = size; } /// Get the current cursor position. #[must_use] - pub(crate) const fn get_position(&self) -> (u16, u16) { - self.position + pub(crate) fn get_position(&self) -> (u16, u16) { + self.state.read().position } /// Has the output been flushed. #[must_use] - pub(crate) const fn is_dirty(&self) -> bool { - self.dirty + pub(crate) fn is_dirty(&self) -> bool { + self.state.read().dirty + } +} + +impl Clone for CrossTerm { + fn clone(&self) -> Self { + CrossTerm { + state: Arc::clone(&self.state), + } } } diff --git a/src/view/tests.rs b/src/view/tests.rs index c55e81e..660ed41 100644 --- a/src/view/tests.rs +++ b/src/view/tests.rs @@ -1,15 +1,13 @@ use super::*; use crate::{ config::Theme, - display::{ - testutil::{assert_output, CrossTerm}, - Size, - }, + display::{testutil::CrossTerm, Size}, }; fn assert_render(width: usize, height: usize, view_data: &ViewData, expected: &[&str]) { let theme = Theme::new(); let mut crossterm = CrossTerm::new(); + let readonly_tui = crossterm.clone(); crossterm.set_size(Size::new(width, height)); let display = Display::new(crossterm, &theme); let mut view = View::new(display, "~", "?"); @@ -18,8 +16,7 @@ fn assert_render(width: usize, height: usize, view_data: &ViewData, expected: &[ render_slice.record_resize(width, height); render_slice.sync_view_data(view_data); view.render(&render_slice).unwrap(); - - assert_output(&view.display, expected); + assert_eq!(readonly_tui.get_output().join(""), format!("{}\n", expected.join("\n"))); } #[test] |