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
|
use crate::game_of_life::opts::Opts;
use crate::game_of_life::universe::Universe;
use clap::Error;
use regex::Regex;
#[cfg(test)]
use mocktopus::macros::mockable;
/// Generate a new Universe/Board with a parsed local file or remote URL/file
///
/// If a URL is provided as an input (starting with http/ftp) the URL/File will be downloaded
/// and parsed to generate a seeding input for the simulation
/// otherwise, it'll be assumed that it's a path to a local file that will be parsed, and
/// used as the seeding input
#[cfg_attr(test, mockable)]
pub(crate) fn new(opts: Opts) -> Result<Universe, Error> {
let input = opts.input.clone().unwrap();
match Regex::new("^(ht|f)?tp(s)?://*").unwrap().is_match(&input) {
true => generate_url_data(opts),
false => generate_file_data(opts),
}
}
#[cfg(test)]
mod new {
use super::*;
use mocktopus::mocking::*;
fn universe_mock(opts: Opts) -> Universe {
Universe {
opts,
current: vec![vec![]],
future: vec![vec![]],
}
}
fn opts(input: Option<String>) -> Opts {
Opts {
input,
width: 13,
height: 13,
seed: 1337,
live_cell: 'L',
dead_cell: 'D',
delay: 13,
}
}
#[test]
fn with_url() {
let opts = opts(Some(String::from("https://example.com")));
generate_file_data.mock_safe(|_| panic!());
generate_url_data.mock_safe(|opts| MockResult::Return(Ok(universe_mock(opts))));
let universe = new(opts.clone()).unwrap();
assert_eq!(universe, universe_mock(opts));
}
#[test]
fn with_file() {
let opts = opts(Some(String::from("./example.txt")));
generate_file_data.mock_safe(|opts| MockResult::Return(Ok(universe_mock(opts))));
generate_url_data.mock_safe(|_| panic!());
let universe = new(opts.clone()).unwrap();
assert_eq!(universe, universe_mock(opts));
}
}
/// Reads a local file and parses it, converting alpha-numerical chars into living cells
/// and generate a new universe instance in the initial state from file
#[cfg_attr(test, mockable)]
fn generate_file_data(opts: Opts) -> Result<Universe, Error> {
let file = std::fs::read_to_string(opts.input.clone().unwrap())?;
let data: Vec<Vec<bool>> = file
.split("\n")
.map(|s| {
s.to_string()
.chars()
.map(|c| c.is_ascii_alphanumeric())
.collect()
})
.collect();
Ok(populate(opts, data))
}
#[cfg(test)]
mod generate_file_data {
use super::*;
use std::env;
use std::fs::File;
use std::io::Write;
fn opts(input: Option<String>) -> Opts {
Opts {
input,
width: 4,
height: 3,
seed: 1337,
live_cell: 'L',
dead_cell: 'D',
delay: 13,
}
}
#[test]
fn file_exists() {
let temp_file = env::temp_dir().join("file.txt");
let path = temp_file.clone().into_os_string().into_string().unwrap();
let mut file = File::create(temp_file).unwrap();
writeln!(&mut file, "--Learning Rust--").unwrap();
writeln!(&mut file, "--First attempt--").unwrap();
let opts = opts(Some(path));
let universe = generate_file_data(opts.clone()).unwrap();
assert_eq!(universe, Universe {
opts,
future: vec![vec![false; 4]; 3],
current: vec![
vec![false, false, true, true],
vec![false, false, true, true],
vec![false, false, false, false],
],
})
}
#[test]
fn file_does_not_exist() {
assert!(generate_file_data(opts(Some(String::from("./not-real.txt")),)).is_err());
}
}
/// Reads a URL and parses it, converting alpha-numerical chars into living cells
/// in a newly generated universe simulation
#[cfg_attr(test, mockable)]
fn generate_url_data(_opts: Opts) -> Result<Universe, Error> {
unimplemented!()
}
|