From acaffbc03b3a7040364d8bffa2d6419f61e4f76c Mon Sep 17 00:00:00 2001 From: AndreMacedo88 Date: Mon, 6 May 2024 13:04:05 +0200 Subject: [PATCH] test: :white_check_mark: make test functions return a Return<(), E> and replace all unwraps with ? --- src/file_handling.rs | 53 +++++++++++++-------- src/front_matter_styles.rs | 53 +++++++++++++-------- src/front_matter_styles/article_bio_like.rs | 4 +- src/process_metadata.rs | 9 ++-- tests/integration_tests.rs | 25 +++++----- 5 files changed, 86 insertions(+), 58 deletions(-) diff --git a/src/file_handling.rs b/src/file_handling.rs index a705dca..3bbc630 100644 --- a/src/file_handling.rs +++ b/src/file_handling.rs @@ -19,58 +19,71 @@ pub fn create_open_output_file(output: &str, overwrite: bool) -> File { mod tests_create_open_output_file { use super::*; use pretty_assertions::assert_eq; + use std::error::Error; use std::fs; use std::io::Write; #[test] - fn test_output_created() { + fn test_output_created() -> Result<(), Box> { let output_path_str: &str = "test1.bib"; let file: File = create_open_output_file(output_path_str, true); - // test if file was created - assert!(file.metadata().is_ok()); + + // read file to an object + let condition: bool = file.metadata().is_ok(); + // remove test file - fs::remove_file(output_path_str).unwrap(); + fs::remove_file(output_path_str)?; + + // test if file was created + assert!(condition); + Ok(()) } #[test] - fn test_output_appended() { + fn test_output_appended() -> Result<(), Box> { let output_path_str: &str = "test2.bib"; // create a file to append - let mut file: File = File::create(output_path_str).unwrap(); - file.write("Test. ".as_bytes()).unwrap(); + let mut file: File = File::create(output_path_str)?; + file.write("Test. ".as_bytes())?; drop(file); // append more bytes to that file let mut file2: File = create_open_output_file(output_path_str, false); - file2.write("Test2.".as_bytes()).unwrap(); + file2.write("Test2.".as_bytes())?; - // test if the text was appended - let contents: String = fs::read_to_string(output_path_str).unwrap(); - assert_eq!(contents, String::from("Test. Test2.")); + // read file to an object + let contents: String = fs::read_to_string(output_path_str)?; // remove test file - fs::remove_file(output_path_str).unwrap(); + fs::remove_file(output_path_str)?; + + // test if the text was appended + assert_eq!(contents, String::from("Test. Test2.")); + Ok(()) } #[test] - fn test_output_overwritten() { + fn test_output_overwritten() -> Result<(), Box> { let output_path_str: &str = "test3.bib"; // create a file and write some text - let mut file: File = File::create(output_path_str).unwrap(); - file.write("Test. ".as_bytes()).unwrap(); + let mut file: File = File::create(output_path_str)?; + file.write("Test. ".as_bytes())?; drop(file); // overwrite some bytes on that file let mut file2: File = create_open_output_file(output_path_str, true); - file2.write("Test2.".as_bytes()).unwrap(); + file2.write("Test2.".as_bytes())?; - // test if the text was overwritten - let contents: String = fs::read_to_string(output_path_str).unwrap(); - assert_eq!(contents, String::from("Test2.")); + // read file to an object + let contents: String = fs::read_to_string(output_path_str)?; // remove test file - fs::remove_file(output_path_str).unwrap(); + fs::remove_file(output_path_str)?; + + // test if the text was overwritten + assert_eq!(contents, String::from("Test2.")); + Ok(()) } } diff --git a/src/front_matter_styles.rs b/src/front_matter_styles.rs index b820fcc..1753cca 100644 --- a/src/front_matter_styles.rs +++ b/src/front_matter_styles.rs @@ -21,6 +21,7 @@ pub fn get_yaml_front_matter( mod tests_parse_document_bio { use super::*; use pretty_assertions::assert_eq; + use std::error::Error; fn test_file_bio_data() -> String { let test_f: String = String::from( @@ -43,58 +44,67 @@ this is a test } #[test] - fn test_title() { + fn test_title() -> Result<(), Box> { let test_f: String = test_file_bio_data(); - let result: Document = parse_document_bio(test_f).unwrap(); + let result: Document = parse_document_bio(test_f)?; assert_eq!(result.metadata.title, String::from("1")); + Ok(()) } #[test] - fn test_author() { + fn test_author() -> Result<(), Box> { let test_f: String = test_file_bio_data(); - let result: Document = parse_document_bio(test_f).unwrap(); + let result: Document = parse_document_bio(test_f)?; assert_eq!(result.metadata.author, String::from("2")); + Ok(()) } #[test] - fn test_journal() { + fn test_journal() -> Result<(), Box> { let test_f: String = test_file_bio_data(); - let result: Document = parse_document_bio(test_f).unwrap(); + let result: Document = parse_document_bio(test_f)?; assert_eq!(result.metadata.journal, String::from("3")); + Ok(()) } #[test] - fn test_year() { + fn test_year() -> Result<(), Box> { let test_f: String = test_file_bio_data(); - let result: Document = parse_document_bio(test_f).unwrap(); + let result: Document = parse_document_bio(test_f)?; assert_eq!(result.metadata.year, 4); + Ok(()) } #[test] - fn test_volume() { + fn test_volume() -> Result<(), Box> { let test_f: String = test_file_bio_data(); - let result: Document = parse_document_bio(test_f).unwrap(); + let result: Document = parse_document_bio(test_f)?; assert_eq!(result.metadata.volume, 5); + Ok(()) } #[test] - fn test_number() { + fn test_number() -> Result<(), Box> { let test_f: String = test_file_bio_data(); - let result: Document = parse_document_bio(test_f).unwrap(); + let result: Document = parse_document_bio(test_f)?; assert_eq!(result.metadata.number, 6); + Ok(()) } #[test] - fn test_pages() { + fn test_pages() -> Result<(), Box> { let test_f: String = test_file_bio_data(); - let result: Document = parse_document_bio(test_f).unwrap(); + let result: Document = parse_document_bio(test_f)?; assert_eq!(result.metadata.pages, String::from("7-10")); + Ok(()) } } #[cfg(test)] mod tests_get_yaml_front_matter { use super::*; + use pretty_assertions::assert_eq; + use std::error::Error; fn test_file_bio_data() -> String { let test_f: String = String::from( @@ -117,24 +127,26 @@ this is a test } #[test] - fn test_title() { + fn test_title() -> Result<(), Box> { let test_f: String = test_file_bio_data(); let style: &str = "article_bio_like"; - let result: Document = get_yaml_front_matter(test_f, style).unwrap(); + let result: Document = get_yaml_front_matter(test_f, style)?; assert_eq!(result.metadata.title, String::from("1")); + Ok(()) } #[test] - fn test_pages() { + fn test_pages() -> Result<(), Box> { let test_f: String = test_file_bio_data(); let style: &str = "article_bio_like"; - let result: Document = get_yaml_front_matter(test_f, style).unwrap(); + let result: Document = get_yaml_front_matter(test_f, style)?; assert_eq!(result.metadata.pages, String::from("7-10")); + Ok(()) } #[test] #[ignore] - fn test_missing_title() { + fn test_missing_title() -> Result<(), Box> { let test_f: String = String::from( " --- @@ -151,7 +163,8 @@ this is a test ", ); let style: &str = "article_bio_like"; - let result: Document = get_yaml_front_matter(test_f, style).unwrap(); + let result: Document = get_yaml_front_matter(test_f, style)?; assert_eq!(result.metadata.author, String::from("2")); // remove the ignore after implementing + Ok(()) } } diff --git a/src/front_matter_styles/article_bio_like.rs b/src/front_matter_styles/article_bio_like.rs index abfa4d9..0e20a8a 100644 --- a/src/front_matter_styles/article_bio_like.rs +++ b/src/front_matter_styles/article_bio_like.rs @@ -27,9 +27,10 @@ pub fn generate_bib_metadata_lines<'a>(metadata: &MetadataBio) -> Vec { mod tests_generate_bib_metadata_lines { use super::*; use pretty_assertions::assert_eq; + use std::error::Error; #[test] - fn test_correct_lines() { + fn test_correct_lines() -> Result<(), Box> { let metadata: MetadataBio = MetadataBio { title: String::from("1"), author: String::from("2"), @@ -50,5 +51,6 @@ mod tests_generate_bib_metadata_lines { String::from("pages = {7-10}"), ]; assert_eq!(result, expected); + Ok(()) } } diff --git a/src/process_metadata.rs b/src/process_metadata.rs index b127345..7187b97 100644 --- a/src/process_metadata.rs +++ b/src/process_metadata.rs @@ -1,4 +1,3 @@ - pub fn get_first_author_last_name(authors: &str) -> Result<&str, &'static str> { // Note that we use iterators instead of collecting into vectors for performance let first_author: &str = authors.split("and").next().ok_or("No author found")?.trim(); @@ -28,9 +27,11 @@ pub fn wrap_metadata_lines(year: &u16, last_name: &str, lines: Vec) -> S #[cfg(test)] mod tests_get_first_author_last_name { use super::get_first_author_last_name; + use pretty_assertions::assert_eq; + use std::error::Error; #[test] - fn test_correct_last_name() { + fn test_correct_last_name() -> Result<(), Box> { let authors: String = String::from( " Test McTest and Test2 McTest2 and Test3 McTest3 @@ -39,10 +40,11 @@ mod tests_get_first_author_last_name { let result: &str = get_first_author_last_name(&authors).unwrap(); let expected: &str = "McTest"; assert_eq!(result, expected); + Ok(()) } #[test] - fn test_one_author() { + fn test_one_author() -> Result<(), Box> { let authors: String = String::from( " Test McTest @@ -51,6 +53,7 @@ mod tests_get_first_author_last_name { let result: &str = get_first_author_last_name(&authors).unwrap(); let expected: &str = "McTest"; assert_eq!(result, expected); + Ok(()) } } diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index 657d39d..640653f 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -1,9 +1,8 @@ use assert_cmd::Command; use predicates::prelude::*; use pretty_assertions::assert_eq; -use std::fs; use std::error::Error; - +use std::fs; #[test] fn test_help_command() { @@ -23,13 +22,12 @@ fn test_new_bib() -> Result<(), Box> { // Read the generated .bib file let mut generated_bib_content: String = fs::read_to_string("tests/data/temp_bib.bib")?; - // we strip spaces because they don't mean much in a .bib file - generated_bib_content.retain(|c: char| !c.is_whitespace()); + // we strip spaces because they don't mean much in a .bib file + generated_bib_content.retain(|c: char| !c.is_whitespace()); // Read the test .bib file let mut test_bib_content: String = fs::read_to_string("tests/data/test_bib.bib")?; - test_bib_content.retain(|c: char| !c.is_whitespace()); - + test_bib_content.retain(|c: char| !c.is_whitespace()); // Delete the generated .bib file fs::remove_file("tests/data/temp_bib.bib").expect("Failed to delete generated .bib file"); @@ -40,7 +38,7 @@ fn test_new_bib() -> Result<(), Box> { } #[test] -fn test_overwrite_bib() { +fn test_overwrite_bib() -> Result<(), Box> { let mut cmd: Command = Command::cargo_bin("yaml-front-matter-to-bib").unwrap(); cmd.arg("--input-directory").arg("tests/data"); cmd.arg("--output-path").arg("tests/data/temp_bib2.bib"); @@ -49,19 +47,18 @@ fn test_overwrite_bib() { cmd.assert().success(); // ran twice so that if overwrite is not working, the output will not be correct // Read the generated .bib file - let mut generated_bib_content: String = fs::read_to_string("tests/data/temp_bib2.bib") - .unwrap(); - // we strip spaces because they don't mean much in a .bib file - generated_bib_content.retain(|c: char| !c.is_whitespace()); + let mut generated_bib_content: String = fs::read_to_string("tests/data/temp_bib2.bib")?; + // we strip spaces because they don't mean much in a .bib file + generated_bib_content.retain(|c: char| !c.is_whitespace()); // Read the test .bib file - let mut test_bib_content: String = fs::read_to_string("tests/data/test_bib.bib") - .unwrap(); - test_bib_content.retain(|c: char| !c.is_whitespace()); + let mut test_bib_content: String = fs::read_to_string("tests/data/test_bib.bib")?; + test_bib_content.retain(|c: char| !c.is_whitespace()); // Delete the generated .bib file fs::remove_file("tests/data/temp_bib2.bib").expect("Failed to delete generated .bib file"); // Compare the contents of both files assert_eq!(generated_bib_content, test_bib_content); + Ok(()) }