summaryrefslogtreecommitdiffstats
path: root/CLI
diff options
context:
space:
mode:
Diffstat (limited to 'CLI')
-rw-r--r--CLI/rust/src/game_of_life/opts.rs27
-rw-r--r--CLI/rust/src/game_of_life/opts/validators.rs12
2 files changed, 25 insertions, 14 deletions
diff --git a/CLI/rust/src/game_of_life/opts.rs b/CLI/rust/src/game_of_life/opts.rs
index 4a0d009..5427287 100644
--- a/CLI/rust/src/game_of_life/opts.rs
+++ b/CLI/rust/src/game_of_life/opts.rs
@@ -2,8 +2,17 @@ use clap::{ArgMatches, Error};
use rand::{thread_rng, Rng};
use terminal_size::{terminal_size, Height, Width};
+#[cfg(test)]
+use mocktopus::macros::mockable;
+
pub(crate) mod validators;
+/// An internal method used to allow mocking terminal size in tests
+#[cfg_attr(test, mockable)]
+pub(crate) fn terminal_size_wrapper() -> (Width, Height) {
+ terminal_size().unwrap()
+}
+
#[derive(Debug, Clone, PartialEq)]
/// Parsed options for generating a Game of Life simulation
pub(crate) struct Opts {
@@ -40,7 +49,7 @@ impl Opts {
};
// Set the width and height to the maximum terminal size possible when not provided
- let (Width(term_width), Height(term_height)) = terminal_size().unwrap();
+ let (Width(term_width), Height(term_height)) = terminal_size_wrapper();
let max_width: isize = term_width as isize;
let max_height: isize = term_height as isize - 2;
let width = args.value_of_t::<isize>("width").unwrap_or(max_width);
@@ -85,16 +94,17 @@ impl Opts {
mod from {
use super::*;
use crate::game_of_life;
+ use mocktopus::mocking::*;
#[test]
fn no_options() {
- let (Width(term_width), Height(term_height)) = terminal_size().unwrap();
+ terminal_size_wrapper.mock_safe(|| MockResult::Return((Width(42), Height(42))));
let matches = game_of_life::app().try_get_matches_from(vec![""]).unwrap();
let opts = Opts::from(matches).unwrap();
assert!(matches!(opts.seed, 0..=1_000_000));
assert_eq!(opts.input, None);
- assert_eq!(opts.width, term_width as isize);
- assert_eq!(opts.height, term_height as isize - 2);
+ assert_eq!(opts.width, 42);
+ assert_eq!(opts.height, 40);
assert_eq!(opts.live_cell, '█');
assert_eq!(opts.dead_cell, ' ');
assert_eq!(opts.delay, 50);
@@ -102,6 +112,7 @@ mod from {
#[test]
fn invalid_seed() {
+ terminal_size_wrapper.mock_safe(|| MockResult::Return((Width(42), Height(42))));
let matches = game_of_life::app()
.try_get_matches_from(vec!["", "--seed=leet"])
.unwrap();
@@ -111,6 +122,7 @@ mod from {
#[test]
fn seed() {
+ terminal_size_wrapper.mock_safe(|| MockResult::Return((Width(42), Height(42))));
let matches = game_of_life::app()
.try_get_matches_from(vec!["", "--seed=1337"])
.unwrap();
@@ -120,6 +132,7 @@ mod from {
#[test]
fn input() {
+ terminal_size_wrapper.mock_safe(|| MockResult::Return((Width(42), Height(42))));
let matches = game_of_life::app()
.try_get_matches_from(vec!["", "--input=file"])
.unwrap();
@@ -129,6 +142,7 @@ mod from {
#[test]
fn width() {
+ terminal_size_wrapper.mock_safe(|| MockResult::Return((Width(42), Height(42))));
let matches = game_of_life::app()
.try_get_matches_from(vec!["", "--width=13"])
.unwrap();
@@ -138,6 +152,7 @@ mod from {
#[test]
fn height() {
+ terminal_size_wrapper.mock_safe(|| MockResult::Return((Width(42), Height(42))));
let matches = game_of_life::app()
.try_get_matches_from(vec!["", "--height=13"])
.unwrap();
@@ -147,6 +162,7 @@ mod from {
#[test]
fn live_cell() {
+ terminal_size_wrapper.mock_safe(|| MockResult::Return((Width(42), Height(42))));
let matches = game_of_life::app()
.try_get_matches_from(vec!["", "--live-cell=l"])
.unwrap();
@@ -156,6 +172,7 @@ mod from {
#[test]
fn dead_cell() {
+ terminal_size_wrapper.mock_safe(|| MockResult::Return((Width(42), Height(42))));
let matches = game_of_life::app()
.try_get_matches_from(vec!["", "--dead-cell=d"])
.unwrap();
@@ -165,6 +182,7 @@ mod from {
#[test]
fn same_live_and_dead_cells() {
+ terminal_size_wrapper.mock_safe(|| MockResult::Return((Width(42), Height(42))));
let matches = game_of_life::app()
.try_get_matches_from(vec!["", "--live-cell=x", "--dead-cell=x"])
.unwrap();
@@ -174,6 +192,7 @@ mod from {
#[test]
fn delay() {
+ terminal_size_wrapper.mock_safe(|| MockResult::Return((Width(42), Height(42))));
let matches = game_of_life::app()
.try_get_matches_from(vec!["", "--delay=13"])
.unwrap();
diff --git a/CLI/rust/src/game_of_life/opts/validators.rs b/CLI/rust/src/game_of_life/opts/validators.rs
index 5ea37e2..4abcc19 100644
--- a/CLI/rust/src/game_of_life/opts/validators.rs
+++ b/CLI/rust/src/game_of_life/opts/validators.rs
@@ -3,16 +3,8 @@
//! Used for validating different CLI args (like width, height and delay)
//! Ref: https://docs.rs/clap/3.0.0-beta.2/clap/struct.Arg.html#method.validator
-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()
-}
+use terminal_size::{Height, Width};
+use super::terminal_size_wrapper;
/// Validator to check if width configuration can be used
///