diff options
| author | a14m <[email protected]> | 2021-04-05 23:01:57 +0200 |
|---|---|---|
| committer | a14m <[email protected]> | 2021-04-05 23:01:57 +0200 |
| commit | 95621ff29646604e99edf66e48f1d9b7a147164c (patch) | |
| tree | c50891ef4b6844477c06ff6c6e49727c309fb750 /CLI | |
| parent | acb43997d5581b9c72d4a27db28f11406b86ed7a (diff) | |
Add generate_file_data implementation w/testing
Diffstat (limited to 'CLI')
| -rw-r--r-- | CLI/rust/src/game_of_life/generators/input.rs | 61 |
1 files changed, 58 insertions, 3 deletions
diff --git a/CLI/rust/src/game_of_life/generators/input.rs b/CLI/rust/src/game_of_life/generators/input.rs index 07ab43a..e22a9fc 100644 --- a/CLI/rust/src/game_of_life/generators/input.rs +++ b/CLI/rust/src/game_of_life/generators/input.rs @@ -66,10 +66,65 @@ mod new { } /// Reads a local file and parses it, converting alpha-numerical chars into living cells -/// in a newly generated universe simulation +/// 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> { - unimplemented!() +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 |
