From 1058738e5dbd3826d75a05dbf97907d4c46ffc00 Mon Sep 17 00:00:00 2001 From: Huck Boles Date: Sun, 21 May 2023 11:03:16 -0500 Subject: [PATCH] refactored: added unit test macro and cleaned up unit tests --- src/tests.rs | 183 +++++++++++++++++++++++---------------------------- 1 file changed, 82 insertions(+), 101 deletions(-) diff --git a/src/tests.rs b/src/tests.rs index 179dc70..6276cfb 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -1,107 +1,87 @@ use crate::{MetaFile, Options}; -use eyre::{Result, WrapErr}; -use std::{error::Error, fs, path::PathBuf}; - -fn unit_test(test: (&str, &str)) -> Result<()> { - let dir = PathBuf::from("files/test_site").canonicalize()?; - - let mut opts = Options::new(); - opts.root = dir.clone(); - opts.source = dir.join("source"); - opts.build = dir.join("build"); - opts.pattern = dir.join("pattern"); - opts.clean = true; - opts.undefined = true; - - let test_dir = opts.source.join("unit_tests"); - let mut file_path = test_dir.join(test.0); - file_path.set_extension("meta"); - let file = MetaFile::build(file_path, &opts)?; - - let output = file.construct().wrap_err_with(|| test.0.to_string())?; - - if output == test.1 { - Ok(()) - } else { - let err = eyre::eyre!("{} - failed", test.0); - eprintln!("{:?}", err); - eprintln!("\nTEST:\n{}\nOUTPUT:\n{}", test.1, output); - Err(err) - } -} - -fn clean_build_dir() -> Result<()> { - let build = PathBuf::from("files/test_site") - .canonicalize()? - .join("build"); - - if build.exists() { - fs::remove_dir_all(&build)?; - } - - fs::create_dir_all(&build)?; - Ok(()) -} - -#[test] -fn builder_tests() -> Result<()> { - clean_build_dir()?; - - let tests: Vec<(&str, &str)> = vec![ - ("find_dest", "\n\n"), - ("blank/blank_pattern", ""), - ("blank/blank_variable", "\n\n"), - ("blank/blank_array", "\n\n"), - ("blank/comment", "\n\n"), - ( - "blank/inline_comment", - "\n

inline comment

\n\n", - ), - ( - "expand/variable_in_source", - "\n

GOOD

\n\n", - ), - ("expand/variable_in_pattern", "\nGOOD\n"), - ("expand/array_in_source", "\n

12345

\n\n"), - ("expand/array_in_pattern", "\n12345\n"), - ("expand/pattern_in_source", "

GOOD

\n"), - ("expand/pattern_in_pattern", "\nGOOD\nGOOD\n\n"), - ("override/variable", "\n

GOOD

\n\n"), - ("override/pattern", "\nGOOD\nGOOD\n\n"), - ("header/pandoc", "# This should not become html\n"), - ("header/blank", ""), - ]; - - let mut err = false; - let mut errs: Vec> = Vec::new(); - for test in tests.iter() { - match unit_test(*test) { - Ok(_) => continue, - Err(e) => { - err = true; - errs.push(e.into()); - } - } - } - - if let Err(e) = test_filetype_header() { - errs.push(e.into()); - } - - if let Err(e) = test_global() { - errs.push(e.into()); - } - - if err { - for e in errs.iter() { - eprintln!("{}", e); +use eyre::Result; +use std::{fs, path::PathBuf}; + +macro_rules! unit_test ( + ($name:ident, $file:expr,$test:literal) => { + #[test] + fn $name() -> Result<()> { + let dir = PathBuf::from("files/test_site").canonicalize()?; + + let mut opts = Options::new(); + opts.root = dir.clone(); + opts.source = dir.join("source"); + opts.build = dir.join("build"); + opts.pattern = dir.join("pattern"); + + let test_dir = opts.source.join("unit_tests"); + let mut path = test_dir.join($file); + path.set_extension("meta"); + let file = MetaFile::build(path, &opts)?; + assert_eq!(file.construct()?, $test); + Ok(()) } - return Err(eyre::eyre!("failed tests")); - } - - Ok(()) -} + }; +); + +unit_test!(blank_pattern, "blank/blank_pattern", ""); +unit_test!(blank_variable, "blank/blank_variable", "\n\n"); +unit_test!(blank_array, "blank/blank_array", "\n\n"); +unit_test!(blank_comment, "blank/comment", "\n\n"); +unit_test!( + inline_comment, + "blank/inline_comment", + "\n

inline comment

\n\n" +); +unit_test!( + expand_var_in_src, + "expand/variable_in_source", + "\n

GOOD

\n\n" +); +unit_test!( + expand_var_in_pat, + "expand/variable_in_pattern", + "\nGOOD\n" +); +unit_test!( + expand_arr_in_src, + "expand/array_in_source", + "\n

12345

\n\n" +); +unit_test!( + expand_arr_in_pat, + "expand/array_in_pattern", + "\n12345\n" +); +unit_test!( + expand_pat_in_src, + "expand/pattern_in_source", + "

GOOD

\n" +); +unit_test!( + expand_pat_in_pat, + "expand/pattern_in_pattern", + "\nGOOD\nGOOD\n\n" +); +unit_test!( + override_var, + "override/variable", + "\n

GOOD

\n\n" +); +unit_test!( + override_pat, + "override/pattern", + "\nGOOD\nGOOD\n\n" +); +unit_test!( + header_no_pandoc, + "header/pandoc", + "# This should not become html\n" +); + +unit_test!(header_blank, "header/blank", ""); +#[test] fn test_filetype_header() -> Result<()> { let dir = PathBuf::from("files/test_site").canonicalize()?; @@ -127,6 +107,7 @@ fn test_filetype_header() -> Result<()> { } } +#[test] fn test_global() -> Result<()> { let dir = PathBuf::from("files/test_site/").canonicalize()?; -- 2.45.2