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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
//! 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;
/// 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
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"));
}
}
/// 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()));
}
}
|