way_lib_rust/
app.rs

1use crate::id;
2use std::sync::Arc;
3
4pub trait App {
5    fn new() -> Self;
6
7    fn id(&self) -> Arc<dyn id::v1::base::V1>;
8}
9
10pub struct AppModule {
11    id: Arc<dyn id::v1::base::V1>,
12}
13
14impl AppModule {
15    #[cfg(test)]
16    fn id_mock(&mut self) -> Arc<id::v1::mock::Mock> {
17        let id_mock = Arc::new(id::v1::mock::Mock::new());
18
19        self.id = id_mock.clone();
20
21        id_mock.clone()
22    }
23}
24
25impl App for AppModule {
26    fn new() -> Self {
27        AppModule {
28            id: Arc::new(id::v1::id::ID::new()),
29        }
30    }
31
32    fn id(&self) -> Arc<dyn id::v1::base::V1> {
33        self.id.clone()
34    }
35}
36
37#[cfg(test)]
38mod tests {
39    use rand::distributions::Alphanumeric;
40    use rand::thread_rng;
41    use rand::Rng;
42
43    use super::App;
44    use super::AppModule;
45
46    #[test]
47    fn id() {
48        let app = AppModule::new();
49        let mut ids = vec![];
50
51        for _ in 1..100 {
52            ids.push(app.id().make());
53        }
54
55        for id in ids {
56            assert!(app.id().is_valid(id))
57        }
58    }
59
60    #[test]
61    fn id_mock() {
62        let mut app = AppModule::new();
63        let id_mock = app.id_mock();
64
65        let expected_id: String = thread_rng()
66            .sample_iter(&Alphanumeric)
67            .take(26)
68            .map(char::from)
69            .collect();
70
71        let expected_id = expected_id.to_lowercase();
72
73        id_mock.expected_make(expected_id.clone());
74
75        let result_id = app.id().make();
76
77        assert_eq!(expected_id, result_id.clone());
78    }
79}