src/handlers/mock_handler.rs

Lines

100.00 %

Functions

100.00 %

Regions

100.00 %

LineCountSource
1
//
2
// File Name:    mock_handler.rs
3
// Project Name: flogging
4
//
5
// Copyright (C) 2025 Bradley Willcott
6
//
7
// SPDX-License-Identifier: GPL-3.0-or-later
8
//
9
// This library (crate) is free software: you can redistribute it and/or modify
10
// it under the terms of the GNU General Public License as published by
11
// the Free Software Foundation, either version 3 of the License, or
12
// (at your option) any later version.
13
//
14
// This library (crate) is distributed in the hope that it will be useful,
15
// but WITHOUT ANY WARRANTY; without even the implied warranty of
16
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
// GNU General Public License for more details.
18
//
19
// You should have received a copy of the GNU General Public License
20
// along with this library (crate).  If not, see <https://www.gnu.org/licenses/>.
21
//
22
23
//!
24
//! # Mock Handler
25
//!
26
//! Returned as Default for Custom handler that is missing.
27
//!
28
29
use crate::*;
30
use std::{
31
    fmt,
32
    io::{self, Error},
33
};
34
35
///
36
/// This is used as a _fake_ or _mock_ handler.
37
///
38
/// It is a filler for `Handler::Custom(label).create()`. It is also used
39
/// in examples for custom handlers.
40
///
41
#[derive(Debug, Default)]
42
pub struct MockHandler {}
43
44
impl fmt::Display for MockHandler {
451
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
461
        write!(f, "MockHandler!!")
471
    }
48
}
49
50
impl HandlerTrait for MockHandler {
511
    fn create(_name: &str) -> Result<Self, Error>
521
    where
531
        Self: Sized,
54
    {
551
        Ok(Default::default())
561
    }
57
58
    ///
59
    /// This is a 'NoOp' fn.
60
    ///
611
    fn close(&mut self) {}
62
63
    ///
64
    /// This is a 'NoOp' fn.
65
    ///
661
    fn flush(&mut self) {}
67
681
    fn get_formatter(&self) -> Formatter {
691
        Default::default()
701
    }
71
722
    fn get_log(&self) -> String {
732
        Default::default()
742
    }
75
76
    ///
77
    /// This is always `false`.
78
    ///
791
    fn is_open(&self) -> bool {
801
        false
811
    }
82
83
    #[allow(private_interfaces)]
842
    fn publish(&mut self, _log_entry: &LogEntry) {}
85
861
    fn set_formatter(&mut self, _formatter: Formatter) {}
87
88
    ///
89
    /// This is a 'NoOp' fn.
90
    ///
911
    fn set_test_mode(&mut self, _state: bool) {}
92
}
93
94
#[cfg(test)]
95
mod tests {
96
    use super::*;
97
98
    use crate::{Handler, Logger, logger};
99
100
    #[test]
1011
    fn handler_trait() {
1021
        let mut log = Logger::custom_logger(
1031
            module_path!(),
1041
            "Mock",
1051
            Box::new(MockHandler::create(module_path!()).unwrap()),
106
        );
107
108
        // println!("{log}");
1091
        assert_eq!("flogging::handlers::mock_handler::tests:: - [INFO]\n\nHandler::Custom(Mock): MockHandler!!\n\n".to_string(), log.to_string());
1101
        log.info("trait methods");
1111
        log.warning("The sky is falling!");
112
1131
        let handler = log
1141
            .get_handler(crate::Handler::Custom("Mock".to_string()))
1151
            .unwrap();
116
1171
        handler.set_test_mode(true);
118
1191
        assert!(!handler.is_open());
120
1211
        handler.set_formatter(FormatType::Simple.create(None));
122
1231
        assert_eq!(
1241
            handler.get_formatter().to_string(),
1251
            "dt_fmt: \"\" - fmt_string: \"{mod_path}->{fn_name} [{level:7}] {message}\""
1261
                .to_string()
127
        );
128
1291
        assert_eq!(handler.get_log(), "".to_string());
130
1311
        handler.flush();
1321
        assert_eq!(handler.get_log(), "".to_string());
1331
        handler.close();
1341
    }
135
}