summaryrefslogtreecommitdiffstats
path: root/CLI/rust
diff options
context:
space:
mode:
authora14m <[email protected]>2021-04-03 00:28:43 +0200
committera14m <[email protected]>2021-04-03 00:28:43 +0200
commit0d76d91184df9d62f68fd7f4e4a1fa72bb370b48 (patch)
tree326cbfb3b94db4592320204ec7a87523689c0945 /CLI/rust
parent7bca115698e720378d0359a416d54a360a7bce39 (diff)
Refactor options method to allow easier testing
And add the testing of the refactored app method
Diffstat (limited to 'CLI/rust')
-rw-r--r--CLI/rust/src/game_of_life.rs87
1 files changed, 81 insertions, 6 deletions
diff --git a/CLI/rust/src/game_of_life.rs b/CLI/rust/src/game_of_life.rs
index 4b1e979..2534bd0 100644
--- a/CLI/rust/src/game_of_life.rs
+++ b/CLI/rust/src/game_of_life.rs
@@ -1,13 +1,13 @@
-use clap::{crate_authors, crate_version, App, Arg, ArgMatches, Error};
+use clap::{crate_authors, crate_version, App, Arg};
/// Options module for validations and Opts struct
pub(crate) mod opts;
-/// Define the Clap CLI for running Game of Life simulation
+/// 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
-pub(crate) fn options() -> Result<ArgMatches, Error> {
- let matches = App::new("Game of Life")
+pub(crate) fn app() -> App<'static> {
+ App::new("Game of Life")
.author(crate_authors!())
.version(crate_version!())
.arg(
@@ -69,7 +69,82 @@ pub(crate) fn options() -> Result<ArgMatches, Error> {
.validator(opts::validators::is_positive)
.display_order(6)
)
- .get_matches();
+}
+
+#[cfg(test)]
+mod app {
+ use super::*;
+
+ #[test]
+ fn no_options() {
+ let matches = app().get_matches_from(vec![""]);
+ assert_eq!(matches.value_of("seed"), None);
+ assert_eq!(matches.value_of("input"), None);
+ assert_eq!(matches.value_of("width"), None);
+ assert_eq!(matches.value_of("height"), None);
+ assert_eq!(matches.value_of("live-cell"), Some("█"));
+ assert_eq!(matches.value_of("dead-cell"), Some(" "));
+ assert_eq!(matches.value_of("delay"), Some("50"));
+ }
+
+ #[test]
+ fn seed() {
+ let matches = app().get_matches_from(vec!["", "--seed=1337"]);
+ assert_eq!(matches.value_of("seed"), Some("1337"));
+ }
+
+ #[test]
+ fn input() {
+ let matches = app().get_matches_from(vec!["", "--input=file"]);
+ assert_eq!(matches.value_of("input"), Some("file"));
+ }
+
+ #[test]
+ fn seed_and_input() {
+ let matches = app().try_get_matches_from(vec!["", "--seed=1337", "--input=file"]);
+ assert!(matches.is_err());
+ }
+
+ #[test]
+ fn valid_width() {
+ let matches = app().get_matches_from(vec!["", "--width=13"]);
+ assert_eq!(matches.value_of("width"), Some("13"));
+ }
+
+ #[test]
+ fn valid_height() {
+ let matches = app().get_matches_from(vec!["", "--height=13"]);
+ assert_eq!(matches.value_of("height"), Some("13"));
+ }
+
+ #[test]
+ fn invalid_width() {
+ let matches = app().try_get_matches_from(vec!["", "--width=1337"]);
+ assert!(matches.is_err());
+ }
+
+ #[test]
+ fn invalid_height() {
+ let matches = app().try_get_matches_from(vec!["", "--height=1337"]);
+ assert!(matches.is_err());
+ }
+
+ #[test]
+ fn valid_delay() {
+ let matches = app().get_matches_from(vec!["", "--delay=13"]);
+ assert_eq!(matches.value_of("delay"), Some("13"));
+ }
+
+ #[test]
+ fn invalid_delay() {
+ let matches = app().try_get_matches_from(vec!["", "--delay=-13"]);
+ assert!(matches.is_err());
+ }
- Ok(matches)
+ #[test]
+ fn live_and_dead_cells() {
+ let matches = app().get_matches_from(vec!["", "--live-cell=x", "--dead-cell=x"]);
+ assert_eq!(matches.value_of("live-cell"), Some("x"));
+ assert_eq!(matches.value_of("dead-cell"), Some("x"));
+ }
}