Add tests (#5)
Rust Build / Check (push) Successful in 33s
Rust Build / Rustfmt (push) Successful in 35s
Rust Build / Test Suite (push) Successful in 38s
Rust Build / Clippy (push) Successful in 29s
Rust Build / build (push) Successful in 42s

Reviewed-on: #5
This commit was merged in pull request #5.
This commit is contained in:
2026-06-06 14:03:42 -04:00
parent cd0ea8a387
commit f5cb2ed892
5 changed files with 384 additions and 7 deletions
+103 -4
View File
@@ -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,11 +41,13 @@ impl NumberParser {
}
}
pub async fn save_file(&self, numbers: Vec<textsender_models::contact::Contact>) {
let filename = String::from("numbers.json");
pub async fn save_file(
&self,
numbers: Vec<textsender_models::contact::Contact>,
filename: &str,
) {
match serde_json::to_string(&numbers) {
Ok(json_string) => match std::fs::write(&filename, json_string) {
Ok(json_string) => match std::fs::write(filename, json_string) {
Ok(_) => {
println!("Saved to: {filename:?}");
}
@@ -166,3 +169,99 @@ impl NumberParser {
parsed
}
}
#[cfg(test)]
mod tests {
use textsender_models::contact::Contact;
const TEST_NUMBERS_0: &str = "tests/numbers-0.txt";
#[test]
fn test_file_dump() {
let file_exists = {
let path = std::path::Path::new(TEST_NUMBERS_0);
path.exists()
};
assert_eq!(true, file_exists, "File does not exists {TEST_NUMBERS_0:?}");
let filename = String::from(TEST_NUMBERS_0);
let prsr = super::NumberParser { filepath: filename };
let original: Vec<Contact> = vec![
Contact {
phone_number: String::from("+13055550112"),
..Default::default()
},
Contact {
phone_number: String::from("+13175550123"),
..Default::default()
},
Contact {
phone_number: String::from("+14155550135"),
..Default::default()
},
Contact {
phone_number: String::from("+16085550190"),
..Default::default()
},
Contact {
phone_number: String::from("+17135550167"),
..Default::default()
},
Contact {
phone_number: String::from("+16025550177"),
..Default::default()
},
Contact {
phone_number: String::from("+13605550158"),
..Default::default()
},
Contact {
phone_number: String::from("+12175550194"),
..Default::default()
},
];
match async_std::task::block_on(prsr.file_dump()) {
Ok(contacts) => {
assert!((contacts.len() > 0), "No contacts has been parsed");
let phone_numbers_1: std::collections::HashSet<_> =
contacts.iter().map(|p| p.phone_number.clone()).collect();
let phone_numbers_2: std::collections::HashSet<_> =
original.iter().map(|p| p.phone_number.clone()).collect();
assert_eq!(phone_numbers_1, phone_numbers_2, "No match");
}
Err(err) => {
assert!(false, "Error: {err:?}");
}
};
}
#[test]
fn test_save_file() {
let prsr = super::NumberParser {
filepath: String::from(TEST_NUMBERS_0),
};
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(test_output_file);
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:?}");
}
}
}
}