torrust_tracker/console/clients/checker/
logger.rs

1use std::cell::RefCell;
2
3use super::printer::{Printer, CLEAR_SCREEN};
4
5pub struct Logger {
6    output: RefCell<String>,
7}
8
9impl Default for Logger {
10    fn default() -> Self {
11        Self::new()
12    }
13}
14
15impl Logger {
16    #[must_use]
17    pub fn new() -> Self {
18        Self {
19            output: RefCell::new(String::new()),
20        }
21    }
22
23    pub fn log(&self) -> String {
24        self.output.borrow().clone()
25    }
26}
27
28impl Printer for Logger {
29    fn clear(&self) {
30        self.print(CLEAR_SCREEN);
31    }
32
33    fn print(&self, output: &str) {
34        *self.output.borrow_mut() = format!("{}{}", self.output.borrow(), &output);
35    }
36
37    fn eprint(&self, output: &str) {
38        *self.output.borrow_mut() = format!("{}{}", self.output.borrow(), &output);
39    }
40
41    fn println(&self, output: &str) {
42        self.print(&format!("{}/n", &output));
43    }
44
45    fn eprintln(&self, output: &str) {
46        self.eprint(&format!("{}/n", &output));
47    }
48}
49
50#[cfg(test)]
51mod tests {
52    use crate::console::clients::checker::logger::Logger;
53    use crate::console::clients::checker::printer::{Printer, CLEAR_SCREEN};
54
55    #[test]
56    fn should_capture_the_clear_screen_command() {
57        let console_logger = Logger::new();
58
59        console_logger.clear();
60
61        assert_eq!(CLEAR_SCREEN, console_logger.log());
62    }
63
64    #[test]
65    fn should_capture_the_print_command_output() {
66        let console_logger = Logger::new();
67
68        console_logger.print("OUTPUT");
69
70        assert_eq!("OUTPUT", console_logger.log());
71    }
72}