summaryrefslogtreecommitdiffstats
path: root/CLI/rust/src/game_of_life/generators/input.rs
blob: 07ab43af92b035cea00ed4af735093482d0f4b76 (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
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
/// in a newly generated universe simulation
#[cfg_attr(test, mockable)]
fn generate_file_data(_opts: Opts) -> Result<Universe, Error> {
    unimplemented!()
}

/// 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!()
}