way-lib-rust 0.0.3

foundation for all Ampliway services
Documentation
use crate::id;
use std::sync::Arc;

pub trait App {
    fn new() -> Self;

    fn id(&self) -> Arc<dyn id::v1::base::V1>;
}

pub struct AppModule {
    id: Arc<dyn id::v1::base::V1>,
}

impl AppModule {
    #[cfg(test)]
    fn id_mock(&mut self) -> Arc<id::v1::mock::Mock> {
        let id_mock = Arc::new(id::v1::mock::Mock::new());

        self.id = id_mock.clone();

        id_mock.clone()
    }
}

impl App for AppModule {
    fn new() -> Self {
        AppModule {
            id: Arc::new(id::v1::id::ID::new()),
        }
    }

    fn id(&self) -> Arc<dyn id::v1::base::V1> {
        self.id.clone()
    }
}

#[cfg(test)]
mod tests {
    use rand::distributions::Alphanumeric;
    use rand::thread_rng;
    use rand::Rng;

    use super::App;
    use super::AppModule;

    #[test]
    fn id() {
        let app = AppModule::new();
        let mut ids = vec![];

        for _ in 1..100 {
            ids.push(app.id().make());
        }

        for id in ids {
            assert!(app.id().is_valid(id))
        }
    }

    #[test]
    fn id_mock() {
        let mut app = AppModule::new();
        let id_mock = app.id_mock();

        let expected_id: String = thread_rng()
            .sample_iter(&Alphanumeric)
            .take(26)
            .map(char::from)
            .collect();

        let expected_id = expected_id.to_lowercase();

        id_mock.expected_make(expected_id.clone());

        let result_id = app.id().make();

        assert_eq!(expected_id, result_id.clone());
    }
}