summaryrefslogtreecommitdiffstats
path: root/CLI/rust/src/game_of_life.rs
blob: 18cb1abdde852e8411750390e28a2b3cdeba4c57 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//! Game of Life module for generating the CLI app, validating the options
//! and starting the simulation
use clap::{crate_authors, crate_version, App, Arg};

/// Options module for validations and Opts struct
pub(crate) mod opts;

/// 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 app() -> App<'static> {
    App::new("Game of Life")
        .author(crate_authors!())
        .version(crate_version!())
        .arg(
            Arg::new("seed")
                .about("Specify the seed number to use as an initial state [default: random]")
                .takes_value(true)
                .long("seed")
                .short('s')
                .conflicts_with("input")
                .display_order(0)
        )
        .arg(
            Arg::new("input")
                .about("Specify the path/URL for the file to use as an initial state. (used instead of seed)")
                .takes_value(true)
                .long("input")
                .short('i')
                .display_order(1)
        )
        .arg(
            Arg::new("width")
                .about("Specify the width of generated universe. [default: terminal width]")
                .takes_value(true)
                .long("width")
                .validator(opts::validators::is_fitting_term_width)
                .display_order(2)
        )
        .arg(
            Arg::new("height")
                .about("Specify the width of generated universe. [default: terminal height]")
                .takes_value(true)
                .long("height")
                .validator(opts::validators::is_fitting_term_height)
                .display_order(3)
        )
        .arg(
            Arg::new("live-cell")
                .about("Specify the live-cell representation")
                .takes_value(true)
                .long("live-cell")
                .default_value("█")
                .display_order(4)
        )
        .arg(
            Arg::new("dead-cell")
                .about("Specify the dead-cell representation")
                .takes_value(true)
                .long("dead-cell")
                .default_value(" ")
                .display_order(5)
        )
        .arg(
            Arg::new("delay")
                .about("Specify the introduced delay between each generation")
                .takes_value(true)
                .short('d')
                .long("delay")
                .default_value("50")
                .validator(opts::validators::is_positive)
                .display_order(6)
        )
}

#[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());
    }

    #[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"));
    }
}