This repository has been archived on 2024-10-16. You can view files and clone it, but cannot push or open issues or pull requests.
pt/tests/test_logger_struct.rs

93 lines
3.0 KiB
Rust
Raw Normal View History

2023-07-07 21:05:37 +02:00
//// IMPORTS ///////////////////////////////////////////////////////////////////////////////////////
//// HELPERS ///////////////////////////////////////////////////////////////////////////////////////
/// ## checks if the expected thing was printed to stdout
///
/// Source: [users.rust-lang.org](https://users.rust-lang.org/t/how-to-test-functions-that-use-
/// println/67188/5)
2023-07-07 23:36:55 +02:00
macro_rules! get_stdout_for {
($test:expr) => {{
2023-07-07 21:05:37 +02:00
use gag::BufferRedirect;
use std::io::Read;
let mut buf = BufferRedirect::stdout().unwrap();
$test;
let mut output = String::new();
buf.read_to_string(&mut output).unwrap();
drop(buf);
2023-07-07 23:36:55 +02:00
output
2023-07-07 21:05:37 +02:00
}};
}
//// IMPLEMENTATION ////////////////////////////////////////////////////////////////////////////////
/// ## Tests for basic logging functionality
mod test_logger_struct {
use libpt::logger::*;
2023-07-07 23:36:55 +02:00
use regex::Regex;
/// ## setup that's needed before testing the logger struct
2023-07-07 21:05:37 +02:00
fn setup() {
// we don't want to log messages during our tests!
2023-07-07 23:36:55 +02:00
Logger::init_specialized(false, false, env_logger::Target::Stdout);
println!()
2023-07-07 21:05:37 +02:00
}
/// ## Tests for basic logging
///
/// This test tests if the loggers basic logging functionality works, that is it's methods:
///
/// - [`Logger::trace`]
/// - [`Logger::debug`]
/// - [`Logger::info`]
/// - [`Logger::warn`]
/// - [`Logger::error`]
///
/// After those methods have Successfully been executed, their outputs gets stored in a single
/// [`String`] and a [`Regex`] checks if we have five correctly formatted messages.
2023-07-07 21:05:37 +02:00
#[test]
fn test_log_basic() {
std::env::set_var(LOGGER_ENV_KEY, "Trace");
setup();
let l = Logger::new();
2023-07-07 23:36:55 +02:00
let trace_out = get_stdout_for!(l.trace("MSG"));
let debug_out = get_stdout_for!(l.debug("MSG"));
let info_out = get_stdout_for!(l.info("MSG"));
let warn_out = get_stdout_for!(l.warn("MSG"));
let error_out = get_stdout_for!(l.error("MSG"));
let combined = format!(
"{}{}{}{}{}",
trace_out, debug_out, info_out, warn_out, error_out
);
print!("{}", combined);
// too long, so i split into two lines.
// this matches the format of the env_logger perfectly, but make sure that color is off,
// else the ANSI escape sequences break this test
2023-07-07 23:36:55 +02:00
let regex = Regex::new(concat!(
r"(?m)\[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z ",
r"(TRACE|DEBUG|INFO|WARN|ERROR) +libpt::logger\] MSG"
))
.unwrap();
// we have 5 log levels, therefore we should have 5 captures
2023-07-07 23:36:55 +02:00
assert_eq!(regex.captures_iter(&combined).count(), 5);
2023-07-07 21:05:37 +02:00
}
#[test]
fn test_multi_initialize() {
setup();
let l = Logger::new();
// these should be ignored due to the global flag
Logger::init();
Logger::init();
Logger::init();
Logger::init();
l.info("Successfully ignored extra init");
}
}