summaryrefslogtreecommitdiffstats
path: root/CLI/rust/src/game_of_life.rs
diff options
context:
space:
mode:
authora14m <[email protected]>2021-04-04 01:39:47 +0200
committera14m <[email protected]>2021-04-04 01:39:47 +0200
commit219d79f78c3ebf7fb1e943447c8abda97ed6ac24 (patch)
tree926cfad42dbe1b71b6759866a93fcf69fdedead8 /CLI/rust/src/game_of_life.rs
parent82da71df436fc8650aff7932f736890ce42da72d (diff)
Add the generate method to GoL module
Diffstat (limited to 'CLI/rust/src/game_of_life.rs')
-rw-r--r--CLI/rust/src/game_of_life.rs66
1 files changed, 66 insertions, 0 deletions
diff --git a/CLI/rust/src/game_of_life.rs b/CLI/rust/src/game_of_life.rs
index 18cb1ab..9ad857d 100644
--- a/CLI/rust/src/game_of_life.rs
+++ b/CLI/rust/src/game_of_life.rs
@@ -5,6 +5,13 @@ use clap::{crate_authors, crate_version, App, Arg};
/// Options module for validations and Opts struct
pub(crate) mod opts;
+/// Generators Module to provide interface for generating universes
+/// using Seed or File input
+pub(crate) mod generators;
+
+/// Universe module for running the simulation
+pub(crate) mod universe;
+
/// Define the Clap CLI App for running Game of Life simulation
///
/// Using Clap to define the available options that can be used with game of life
@@ -150,3 +157,62 @@ mod app {
assert_eq!(matches.value_of("dead-cell"), Some("x"));
}
}
+
+/// Generate a universe based on the options
+///
+/// If an input is provided, the input will be used (taking precedence over seed)
+/// Else use the seed option (defaults to random when not provided).
+pub(crate) fn generate(opts: opts::Opts) -> universe::Universe {
+ match opts.input {
+ Some(_) => generators::input::new(opts),
+ None => generators::seed::new(opts),
+ }
+}
+
+#[cfg(test)]
+mod generate {
+ use super::*;
+ use mocktopus::mocking::*;
+
+ fn universe_mock(opts: opts::Opts) -> universe::Universe {
+ universe::Universe {
+ opts,
+ current: vec![vec![]],
+ future: vec![vec![]],
+ }
+ }
+
+ #[test]
+ fn without_input() {
+ let opts = opts::Opts {
+ seed: 1337,
+ input: None,
+ width: 13,
+ height: 13,
+ live_cell: 'L',
+ dead_cell: 'D',
+ delay: 13,
+ };
+
+ generators::seed::new.mock_safe(|opts| MockResult::Return(universe_mock(opts)));
+ generators::input::new.mock_safe(|_| panic!());
+ assert_eq!(generate(opts.clone()), universe_mock(opts.clone()));
+ }
+
+ #[test]
+ fn with_input() {
+ let opts = opts::Opts {
+ seed: 1337,
+ input: Some(String::from("input")),
+ width: 13,
+ height: 13,
+ live_cell: 'L',
+ dead_cell: 'D',
+ delay: 13,
+ };
+
+ generators::seed::new.mock_safe(|_| panic!());
+ generators::input::new.mock_safe(|opts| MockResult::Return(universe_mock(opts)));
+ assert_eq!(generate(opts.clone()), universe_mock(opts.clone()));
+ }
+}