summaryrefslogtreecommitdiffstats
path: root/CLI/rust
diff options
context:
space:
mode:
authora14m <[email protected]>2021-04-09 16:18:08 +0200
committera14m <[email protected]>2021-04-09 16:18:08 +0200
commit1a13d563c5a69a9ae05da6fde813db6321a50251 (patch)
treeba9160043ccd094ec5f0ad63ad55fb7145307722 /CLI/rust
parent29a2501f074772a94fc6897e14c8bc52c85830af (diff)
Add a wrapper method to allow mocking terminal size
Diffstat (limited to 'CLI/rust')
-rw-r--r--CLI/rust/src/game_of_life/opts/validators.rs23
1 files 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::<u16>() {
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::<u16>() {
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());
}
}