summaryrefslogtreecommitdiffstats
path: root/CLI/rust/src/game_of_life/generators/input.rs
blob: c2b2bb008222623d8da24af0ee4fee49b7fd3aa5 (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
use crate::game_of_life::opts::Opts;
use crate::game_of_life::universe::Universe;
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) -> Universe {
    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![]],
        }
    }

    #[test]
    fn with_url() {
        let opts = Opts {
            seed: 1337,
            input: Some(String::from("https://example.com")),
            width: 13,
            height: 13,
            live_cell: 'L',
            dead_cell: 'D',
            delay: 13,
        };

        generate_file_data.mock_safe(|_| panic!());
        generate_url_data.mock_safe(|opts| MockResult::Return(universe_mock(opts)));
        assert_eq!(new(opts.clone()), universe_mock(opts.clone()));
    }

    #[test]
    fn with_file() {
        let opts = Opts {
            seed: 1337,
            input: Some(String::from("./example.txt")),
            width: 13,
            height: 13,
            live_cell: 'L',
            dead_cell: 'D',
            delay: 13,
        };

        generate_file_data.mock_safe(|opts| MockResult::Return(universe_mock(opts)));
        generate_url_data.mock_safe(|_| panic!());
        assert_eq!(new(opts.clone()), universe_mock(opts.clone()));
    }
}

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