Skip to content

Commit

Permalink
test: ✅ make test functions return a Return<(), E> and replace all un…
Browse files Browse the repository at this point in the history
…wraps with ?
  • Loading branch information
AndreMacedo88 committed May 6, 2024
1 parent d8cac85 commit acaffbc
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 58 deletions.
53 changes: 33 additions & 20 deletions src/file_handling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn Error>> {
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<dyn Error>> {
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<dyn Error>> {
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(())
}
}
53 changes: 33 additions & 20 deletions src/front_matter_styles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -43,58 +44,67 @@ this is a test
}

#[test]
fn test_title() {
fn test_title() -> Result<(), Box<dyn Error>> {
let test_f: String = test_file_bio_data();
let result: Document<MetadataBio> = parse_document_bio(test_f).unwrap();
let result: Document<MetadataBio> = parse_document_bio(test_f)?;
assert_eq!(result.metadata.title, String::from("1"));
Ok(())
}

#[test]
fn test_author() {
fn test_author() -> Result<(), Box<dyn Error>> {
let test_f: String = test_file_bio_data();
let result: Document<MetadataBio> = parse_document_bio(test_f).unwrap();
let result: Document<MetadataBio> = parse_document_bio(test_f)?;
assert_eq!(result.metadata.author, String::from("2"));
Ok(())
}

#[test]
fn test_journal() {
fn test_journal() -> Result<(), Box<dyn Error>> {
let test_f: String = test_file_bio_data();
let result: Document<MetadataBio> = parse_document_bio(test_f).unwrap();
let result: Document<MetadataBio> = parse_document_bio(test_f)?;
assert_eq!(result.metadata.journal, String::from("3"));
Ok(())
}

#[test]
fn test_year() {
fn test_year() -> Result<(), Box<dyn Error>> {
let test_f: String = test_file_bio_data();
let result: Document<MetadataBio> = parse_document_bio(test_f).unwrap();
let result: Document<MetadataBio> = parse_document_bio(test_f)?;
assert_eq!(result.metadata.year, 4);
Ok(())
}

#[test]
fn test_volume() {
fn test_volume() -> Result<(), Box<dyn Error>> {
let test_f: String = test_file_bio_data();
let result: Document<MetadataBio> = parse_document_bio(test_f).unwrap();
let result: Document<MetadataBio> = parse_document_bio(test_f)?;
assert_eq!(result.metadata.volume, 5);
Ok(())
}

#[test]
fn test_number() {
fn test_number() -> Result<(), Box<dyn Error>> {
let test_f: String = test_file_bio_data();
let result: Document<MetadataBio> = parse_document_bio(test_f).unwrap();
let result: Document<MetadataBio> = parse_document_bio(test_f)?;
assert_eq!(result.metadata.number, 6);
Ok(())
}

#[test]
fn test_pages() {
fn test_pages() -> Result<(), Box<dyn Error>> {
let test_f: String = test_file_bio_data();
let result: Document<MetadataBio> = parse_document_bio(test_f).unwrap();
let result: Document<MetadataBio> = 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(
Expand All @@ -117,24 +127,26 @@ this is a test
}

#[test]
fn test_title() {
fn test_title() -> Result<(), Box<dyn Error>> {
let test_f: String = test_file_bio_data();
let style: &str = "article_bio_like";
let result: Document<MetadataBio> = get_yaml_front_matter(test_f, style).unwrap();
let result: Document<MetadataBio> = 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<dyn Error>> {
let test_f: String = test_file_bio_data();
let style: &str = "article_bio_like";
let result: Document<MetadataBio> = get_yaml_front_matter(test_f, style).unwrap();
let result: Document<MetadataBio> = 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<dyn Error>> {
let test_f: String = String::from(
"
---
Expand All @@ -151,7 +163,8 @@ this is a test
",
);
let style: &str = "article_bio_like";
let result: Document<MetadataBio> = get_yaml_front_matter(test_f, style).unwrap();
let result: Document<MetadataBio> = get_yaml_front_matter(test_f, style)?;
assert_eq!(result.metadata.author, String::from("2")); // remove the ignore after implementing
Ok(())
}
}
4 changes: 3 additions & 1 deletion src/front_matter_styles/article_bio_like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ pub fn generate_bib_metadata_lines<'a>(metadata: &MetadataBio) -> Vec<String> {
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<dyn Error>> {
let metadata: MetadataBio = MetadataBio {
title: String::from("1"),
author: String::from("2"),
Expand All @@ -50,5 +51,6 @@ mod tests_generate_bib_metadata_lines {
String::from("pages = {7-10}"),
];
assert_eq!(result, expected);
Ok(())
}
}
9 changes: 6 additions & 3 deletions src/process_metadata.rs
Original file line number Diff line number Diff line change
@@ -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();
Expand Down Expand Up @@ -28,9 +27,11 @@ pub fn wrap_metadata_lines(year: &u16, last_name: &str, lines: Vec<String>) -> 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<dyn Error>> {
let authors: String = String::from(
"
Test McTest and Test2 McTest2 and Test3 McTest3
Expand All @@ -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<dyn Error>> {
let authors: String = String::from(
"
Test McTest
Expand All @@ -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(())
}
}

Expand Down
25 changes: 11 additions & 14 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -23,13 +22,12 @@ fn test_new_bib() -> Result<(), Box<dyn Error>> {

// 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");
Expand All @@ -40,7 +38,7 @@ fn test_new_bib() -> Result<(), Box<dyn Error>> {
}

#[test]
fn test_overwrite_bib() {
fn test_overwrite_bib() -> Result<(), Box<dyn Error>> {
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");
Expand All @@ -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(())
}

0 comments on commit acaffbc

Please sign in to comment.