From 84f19e6b16a9ca6db9a6b8833f5dee0a94f55542 Mon Sep 17 00:00:00 2001 From: phoenix Date: Sat, 6 Jun 2026 13:52:00 -0400 Subject: [PATCH] Test cleanup --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/main.rs | 2 +- src/parser/mod.rs | 41 ++++++++++++++++++++++++++++++++--------- 4 files changed, 35 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e1d22b4..2c495cc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -214,7 +214,7 @@ dependencies = [ [[package]] name = "clean_file" -version = "0.8.3" +version = "0.8.4" dependencies = [ "async-std", "futures", diff --git a/Cargo.toml b/Cargo.toml index 57166fe..2394ca2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "clean_file" -version = "0.8.3" +version = "0.8.4" edition = "2024" rust-version = "1.95" diff --git a/src/main.rs b/src/main.rs index 0503ddf..d4fbe0f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,7 +23,7 @@ async fn main() { match prsr.file_dump().await { Ok(contacts) => { prsr.print_phone_numbers(&contacts); - prsr.save_file(contacts).await; + prsr.save_file(contacts, parser::DEFAULT_OUTPUT_FILE).await; } Err(err) => { eprintln!("Error: {err:?}"); diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 28b8a48..45332a6 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -1,5 +1,6 @@ use std::io::BufRead; +pub const DEFAULT_OUTPUT_FILE: &str = "numbers.json"; const LETTERS: &str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; #[derive(Clone, Debug, Default)] @@ -40,9 +41,8 @@ impl NumberParser { } } - pub async fn save_file(&self, numbers: Vec) { - let filename = String::from("numbers.json"); - + pub async fn save_file(&self, numbers: Vec, filename: &str) { + // let filename = String::from("numbers.json"); match serde_json::to_string(&numbers) { Ok(json_string) => match std::fs::write(&filename, json_string) { Ok(_) => { @@ -170,9 +170,11 @@ impl NumberParser { mod tests { use textsender_models::contact::Contact; + const TEST_NUMBERS_0: &str = "tests/numbers-0.txt"; + #[test] fn test_file_dump() { - let filename = String::from("tests/numbers-0.txt"); + let filename = String::from(TEST_NUMBERS_0); let file_exists = { let path = std::path::Path::new(filename.as_str()); path.exists() @@ -181,7 +183,7 @@ mod tests { assert_eq!(true, file_exists, "File does not exists {filename:?}"); let prsr = super::NumberParser { filepath: filename }; - let original: Vec = vec![ + let original: Vec = vec![ Contact { phone_number: String::from("+13055550112"), ..Default::default() @@ -216,9 +218,6 @@ mod tests { }, ]; - - - match async_std::task::block_on(prsr.file_dump()) { Ok(contacts) => { assert!((contacts.len() > 0), "No contacts has been parsed"); @@ -234,5 +233,29 @@ mod tests { } #[test] - fn test_save_file() {} + fn test_save_file() { + let filename = String::from(TEST_NUMBERS_0); + + let prsr = super::NumberParser { filepath: filename }; + let results = async_std::task::block_on(prsr.file_dump()); + assert!(results.is_ok(), "File dump not successful"); + let test_output_file: &str = "test-numbers.json"; + + let contacts = results.unwrap(); + async_std::task::block_on(prsr.save_file(contacts, test_output_file)); + let file_exists = { + let path = std::path::Path::new("numbers.json"); + path.exists() + }; + + assert!(file_exists, "Contacts were not saved"); + match std::fs::remove_file(test_output_file) { + Ok(_) => { + println!("File removed"); + } + Err(err) => { + assert!(false, "Error: {err:?}"); + } + } + } }