From 1a13d563c5a69a9ae05da6fde813db6321a50251 Mon Sep 17 00:00:00 2001 From: a14m Date: Fri, 9 Apr 2021 16:18:08 +0200 Subject: Add a wrapper method to allow mocking terminal size --- CLI/rust/src/game_of_life/opts/validators.rs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/CLI/rust/src/game_of_life/opts/validators.rs b/CLI/rust/src/game_of_life/opts/validators.rs index 0de7b3f..5ea37e2 100644 --- a/CLI/rust/src/game_of_life/opts/validators.rs +++ b/CLI/rust/src/game_of_life/opts/validators.rs @@ -5,11 +5,20 @@ use terminal_size::{terminal_size, Height, Width}; +#[cfg(test)] +use mocktopus::macros::mockable; + +/// An internal method used to allow mocking terminal size in tests +#[cfg_attr(test, mockable)] +fn terminal_size_wrapper() -> (Width, Height) { + terminal_size().unwrap() +} + /// Validator to check if width configuration can be used /// /// The maximum width allowed is the current terminal window width pub(crate) fn is_fitting_term_width(val: &str) -> Result<(), String> { - let (Width(term_width), Height(_)) = terminal_size().unwrap(); + let (Width(term_width), Height(_)) = terminal_size_wrapper(); match val.parse::() { Ok(n) if n > 0 && n <= term_width => Ok(()), Ok(_) => Err(format!( @@ -23,24 +32,29 @@ pub(crate) fn is_fitting_term_width(val: &str) -> Result<(), String> { #[cfg(test)] mod is_fitting_term_width { use super::*; + use mocktopus::mocking::*; #[test] fn positive_value() { + terminal_size_wrapper.mock_safe(|| MockResult::Return((Width(42), Height(42)))); assert!(is_fitting_term_width("13").is_ok()); } #[test] fn value_too_large() { + terminal_size_wrapper.mock_safe(|| MockResult::Return((Width(42), Height(42)))); assert!(is_fitting_term_width("1337").is_err()); } #[test] fn negative_value() { + terminal_size_wrapper.mock_safe(|| MockResult::Return((Width(42), Height(42)))); assert!(is_fitting_term_width("-13").is_err()); } #[test] fn string_value() { + terminal_size_wrapper.mock_safe(|| MockResult::Return((Width(42), Height(42)))); assert!(is_fitting_term_width("not a number").is_err()); } } @@ -51,7 +65,7 @@ mod is_fitting_term_width { /// The last 2 lines are used for status reporting (seed/input) and space /// for ease of use pub(crate) fn is_fitting_term_height(val: &str) -> Result<(), String> { - let (Width(_), Height(term_height)) = terminal_size().unwrap(); + let (Width(_), Height(term_height)) = terminal_size_wrapper(); let term_height = term_height - 2; match val.parse::() { Ok(n) if n > 0 && n <= term_height => Ok(()), @@ -66,24 +80,29 @@ pub(crate) fn is_fitting_term_height(val: &str) -> Result<(), String> { #[cfg(test)] mod is_fitting_term_height { use super::*; + use mocktopus::mocking::*; #[test] fn positive_value() { + terminal_size_wrapper.mock_safe(|| MockResult::Return((Width(42), Height(42)))); assert!(is_fitting_term_height("13").is_ok()); } #[test] fn value_too_large() { + terminal_size_wrapper.mock_safe(|| MockResult::Return((Width(42), Height(42)))); assert!(is_fitting_term_height("1337").is_err()); } #[test] fn negative_value() { + terminal_size_wrapper.mock_safe(|| MockResult::Return((Width(42), Height(42)))); assert!(is_fitting_term_height("-13").is_err()); } #[test] fn string_value() { + terminal_size_wrapper.mock_safe(|| MockResult::Return((Width(42), Height(42)))); assert!(is_fitting_term_height("not a number").is_err()); } } -- cgit v1.2.3