From b08bf4f9181f0a5e146663c5a7dd6fec5bd3d1f6 Mon Sep 17 00:00:00 2001 From: Stadnik Andrii Date: Thu, 5 Oct 2023 12:24:08 +0300 Subject: [PATCH] Update tests, log time as well --- Cargo.lock | 13 +++++++++++ bot/src/main.rs | 2 +- core/Cargo.toml | 4 ++++ core/src/model/finite_state.rs | 3 +++ core/tests/test_data.rs | 40 ++++++++++++++++++++-------------- 5 files changed, 45 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 37b5efb8..aab0d888 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -433,11 +433,13 @@ dependencies = [ "bytes", "const_format", "csv", + "env_logger", "indexmap", "log", "regex", "reqwest", "serde", + "test-log", "tokio", ] @@ -1630,6 +1632,17 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "test-log" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9601d162c1d77e62c1ea0bc8116cd1caf143ce3af947536c3c9052a1677fe0c" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "thiserror" version = "1.0.48" diff --git a/bot/src/main.rs b/bot/src/main.rs index 13dc0fd1..105a6550 100644 --- a/bot/src/main.rs +++ b/bot/src/main.rs @@ -15,7 +15,7 @@ static DATA: OnceLock = OnceLock::new(); #[tokio::main] async fn main() -> Result<()> { - pretty_env_logger::init(); + pretty_env_logger::init_timed(); let data = if cfg!(debug_assertions) { Data::dynamic() } else { diff --git a/core/Cargo.toml b/core/Cargo.toml index bf18b4d2..19dff011 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -23,3 +23,7 @@ anyhow = "1.0" tokio = { version = "1.32" , features = ["macros", "rt-multi-thread" ] } const_format = "0.2" regex = "1.9.5" + +[dev-dependencies] +env_logger = "0.10" +test-log = "0.2" diff --git a/core/src/model/finite_state.rs b/core/src/model/finite_state.rs index 0013bfb5..0fb2d46c 100644 --- a/core/src/model/finite_state.rs +++ b/core/src/model/finite_state.rs @@ -59,4 +59,7 @@ impl Fs { next_states: options, }) } + pub fn num_nodes(&self) -> usize { + 1 + self.next_states.values().map(Fs::num_nodes).sum::() + } } diff --git a/core/tests/test_data.rs b/core/tests/test_data.rs index 43559cc0..149179e0 100644 --- a/core/tests/test_data.rs +++ b/core/tests/test_data.rs @@ -29,7 +29,11 @@ fn test_fs(fs: Fs) -> Result<()> { if fs.message.chars().all(char::is_whitespace) { bail!("Empty message"); } - if fs.link.as_ref().is_some_and(|s| s.chars().all(char::is_whitespace)) { + if fs + .link + .as_ref() + .is_some_and(|s| s.chars().all(char::is_whitespace)) + { bail!("Empty link"); } test_md(&fs.message)?; @@ -39,27 +43,13 @@ fn test_fs(fs: Fs) -> Result<()> { Ok(()) } -#[tokio::test] -async fn test_table() -> Result<()> { - let data = if cfg!(debug_assertions) { - get_data_from_web().await? - } else { - get_data_from_file("../table.csv")? - }; - for fs in data.into_values() { - test_fs(fs)?; - } - - Ok(()) -} - // Add tests using cfg #[cfg(test)] mod tests { use super::*; - #[test] + #[test_log::test] fn test_example() -> Result<()> { let s = r" *bold \*text* _italic \*text_ @@ -76,4 +66,22 @@ pre-formatted fixed-width code block written in the Python programming language ``` "; test_md(s) } + + #[test_log::test(tokio::test)] + async fn test_table() -> Result<()> { + let data = if cfg!(debug_assertions) { + get_data_from_web().await? + } else { + get_data_from_file("../table.csv")? + }; + data.iter() + .for_each(|(lang, fs)| log::info!("Testing {lang} with {} nodes", fs.num_nodes())); + for (lang, fs) in data.into_iter() { + let n_nodes = fs.num_nodes(); + test_fs(fs)?; + log::info!("All tests passed for {lang} with {n_nodes} nodes"); + } + + Ok(()) + } }