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
use std::sync::Mutex;

use crate::{error::Error, traits::Speaker, WaitFuture};

/// Dummy Speaker for debug or tests.
#[derive(Debug, Default)]
pub struct DummySpeaker {
    pub message: Mutex<String>,
}

impl DummySpeaker {
    /// Creates a new `DummySpeaker`.
    pub fn new() -> Self {
        Self::default()
    }

    pub fn current_message(&self) -> String {
        self.message.lock().unwrap().clone()
    }
}

impl Speaker for DummySpeaker {
    fn speak(&self, message: &str) -> Result<WaitFuture, Error> {
        *self.message.lock().unwrap() = message.to_string();
        Ok(WaitFuture::ready())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_set() {
        let speaker = DummySpeaker::new();

        assert_eq!(speaker.current_message(), "");
        speaker.speak("abc").unwrap().await.unwrap();
        assert_eq!(speaker.current_message(), "abc");
    }

    #[test]
    fn test_set_no_wait() {
        let speaker = DummySpeaker::new();

        assert_eq!(speaker.current_message(), "");
        drop(speaker.speak("abc").unwrap());
        assert_eq!(speaker.current_message(), "abc");
    }
}