diff --git a/tablegen/Cargo.toml b/tablegen/Cargo.toml index 88ded94..cf2c6be 100644 --- a/tablegen/Cargo.toml +++ b/tablegen/Cargo.toml @@ -14,3 +14,6 @@ phf = { version = "0.11.3", features = ["macros"] } regex = "1.11.1" regex_static = "0.1.1" serde = { version = "1.0.217", features = ["derive", "alloc"] } + +[dev-dependencies] +insta = { version = "1.42.2", features = ["yaml"] } diff --git a/tablegen/src/lib.rs b/tablegen/src/lib.rs index fa29283..4e374b3 100644 --- a/tablegen/src/lib.rs +++ b/tablegen/src/lib.rs @@ -18,7 +18,7 @@ //! The following functions are re-exported from the module `tablegen`: //! //! - [`table_gen`]: Handling the whole conversion and auto dispatch. - +#![feature(coverage_attribute)] #![warn(missing_docs, clippy::missing_docs_in_private_items)] mod table_structs; mod table_types; diff --git a/tablegen/src/snapshots/tablegen__tablegen__test__str_clean.snap b/tablegen/src/snapshots/tablegen__tablegen__test__str_clean.snap new file mode 100644 index 0000000..3861b91 --- /dev/null +++ b/tablegen/src/snapshots/tablegen__tablegen__test__str_clean.snap @@ -0,0 +1,5 @@ +--- +source: tablegen/src/tablegen.rs +expression: "make_latex_string(\"πŸ©Έβš‘β˜½β—…β§†Γ˜βŒ­β˜†πŸ—˜Β½0.5& ++ (++) (--) -β˜…β˜…β˜… β˜…β˜…β˜… +β˜…β˜…β˜…\")" +--- +"\\blood{}\\gauge{}\\slash{}\\pierce{}\\blunt{}\\o{}\\torque{}\\ddzero{}\\reload{}\\sfrac{1}{2}\\sfrac{1}{2}\\& ++ $(++)$ $(--)$ \\ddmod{-}{3} \\ddlvl{3} \\ddmod{+}{3}" diff --git a/tablegen/src/table_structs.rs b/tablegen/src/table_structs.rs index 00a8d25..97beaaf 100644 --- a/tablegen/src/table_structs.rs +++ b/tablegen/src/table_structs.rs @@ -8,6 +8,7 @@ use regex_static::once_cell::sync::Lazy; /// All the type of tsv that can be processed by the program // #[enum_dispatch] #[derive(Clone)] +#[cfg_attr(test, derive(Debug, PartialEq))] pub enum TableTargets { /// A class tsv Classes, @@ -51,3 +52,37 @@ pub(super) static RE_STARS: Lazy = regex_static::lazy_regex!(r"β˜…+"); pub(super) static RE_PLUS_STARS: Lazy = regex_static::lazy_regex!(r"([\+-])(β˜…+)"); /// The regex for finding all (+/-) and replacing them with mathmode pub(super) static RE_MATHS: Lazy = regex_static::lazy_regex!(r"\([\+-]+\)"); + +#[cfg(test)] +mod test { + use crate::table_structs::{RE_MATHS, RE_PLUS_STARS, RE_STARS}; + + #[test] + fn test_regex() { + assert_eq!(RE_STARS.find("Bob β˜…β˜…β˜…β˜… zz").unwrap().as_str(), "β˜…β˜…β˜…β˜…"); + assert!(RE_STARS.find("Bob zz").is_none()); + + assert_eq!( + RE_PLUS_STARS + .captures("Bob +β˜…β˜…β˜…β˜… zz") + .unwrap() + .get(2) + .unwrap() + .as_str(), + "β˜…β˜…β˜…β˜…" + ); + assert_eq!( + RE_PLUS_STARS + .captures("Bob -β˜…β˜…β˜…β˜… zz") + .unwrap() + .get(2) + .unwrap() + .as_str(), + "β˜…β˜…β˜…β˜…" + ); + assert!(RE_PLUS_STARS.captures("Bob β˜…β˜…β˜…β˜… zz").is_none()); + assert_eq!(RE_MATHS.find("Bob (+++)").unwrap().as_str(), "(+++)"); + assert_eq!(RE_MATHS.find("Bob (--)").unwrap().as_str(), "(--)"); + assert!(RE_MATHS.find("Bob -- ++ ").is_none()); + } +} diff --git a/tablegen/src/tablegen.rs b/tablegen/src/tablegen.rs index b04ebb4..c5ae8ed 100644 --- a/tablegen/src/tablegen.rs +++ b/tablegen/src/tablegen.rs @@ -26,7 +26,7 @@ //! This module consists of several submodules: //! - `table_structs`: Defines the structure and behavior of tables. //! - `table_types`: Contains types related to TSV files and LaTeX code generation. -use anyhow::{Context, Result}; +use anyhow::{Context, Result, anyhow}; use config::tablegen::{TableArgs, TableGenerationTarget}; use glob::glob; use regex::Captures; @@ -46,6 +46,7 @@ use crate::{ /// Generate the tables for the specified files /// /// In charge of the dispatch and the whole conversion +#[coverage(off)] // DISPATCH: Can't cover a dispatch pub fn table_gen(args: &TableArgs, config: &Config) -> Result<()> { let list_files = match args.kind { TableGenerationTarget::Classes => { @@ -83,6 +84,7 @@ pub fn table_gen(args: &TableArgs, config: &Config) -> Result<()> { } /// Process any type of file, one by one and call the right converter function +#[coverage(off)] // IO: This function does I/O and can't be tested fn process_files(list_files: &mut Vec, args: &TableArgs, config: &Config) -> Result<()> { for f in list_files { if let Err(e) = process_one_file(f, args, config) { @@ -93,6 +95,7 @@ fn process_files(list_files: &mut Vec, args: &TableArgs, config: &Confi } /// Process one file +#[coverage(off)] // IO: This function does I/O and can't be tested fn process_one_file(f: &mut PathBuf, args: &TableArgs, config: &Config) -> Result<()> { let tt = extract_type(f)?; let tsv: Box = match tt { @@ -107,13 +110,18 @@ fn process_one_file(f: &mut PathBuf, args: &TableArgs, config: &Config) -> Resul } #[inline] +#[coverage(off)] // INLINE: The coverage is badly reported it is however covered in the testing scenario /// Extract the type of target of the given file fn extract_type(f: &Path) -> Result<&TableTargets> { let key = &f .file_name() .context("The filename of can't be extracted...skipping")? .to_str() - .context("Can't convert filename to string")?[7..10]; + .context("Can't convert filename to string")?; + if key.len() < 10 { + return Err(anyhow!("Filename doens't have the correct formatting")); + } + let key = &key[7..10]; // TODO[!]: Horrible practice, clean this STR_TO_TARGET_FULL.get(key).with_context(|| { format!( "Could not find the type of file given based on the prefix of your file ({})\n\ @@ -128,6 +136,7 @@ fn extract_type(f: &Path) -> Result<&TableTargets> { /// List all files matching a pattern in the given directory #[inline] +#[coverage(off)] // IO: This function does I/O and can't be tested fn list_dir(pattern: &str) -> Vec { // println!("{}", pattern); // println!("{:?}", glob(pattern)); @@ -143,6 +152,7 @@ fn list_dir(pattern: &str) -> Vec { /// This function reads in a TSV file, processes its contents according to the provided configuration, /// and writes out the resulting LaTeX code as a string. If an error occurs during processing or writing, /// it will be returned as part of the result. +#[coverage(off)] // BAD_IO: This function does I/O and can't be tested pub fn export_tsv( f: &Path, tsv: Box, @@ -197,6 +207,7 @@ pub(super) fn make_latex_string(arg: &str) -> String { /// /// This function reads in a tab-separated values (TSV) file and returns it as a Table object, /// which can be used to generate LaTeX code or perform other operations on the data. +#[coverage(off)] // BAD_IO: This function does I/O and can't be tested fn read_tsv_classes(filename: &T) -> Result where T: AsRef, @@ -231,6 +242,7 @@ where /// Reads a TSV file into a table. /// /// Reads a tab-separated values (TSV) file and deserializes its contents into a table. +#[coverage(off)] // BAD_IO: This function does I/O and can't be tested fn read_generic_tsv(filename: &Path, craft_type: &TableTargets) -> Result> { let file = File::open(filename)?; let rdr = csv::ReaderBuilder::new() @@ -259,6 +271,7 @@ fn read_generic_tsv(filename: &Path, craft_type: &TableTargets) -> Result, config: &Config) -> Result { tsv.write_table(config) } @@ -289,3 +302,66 @@ pub(super) fn tsv_to_string(tsv: Box, config: &Config) -> Result // let craft_array = CraftArray { row: levels }; // Ok(Box::new(craft_array)) // } + +#[cfg(test)] +mod test { + use std::path::Path; + + use crate::{table_structs::TableTargets, tablegen::extract_type}; + + use super::make_latex_string; + + #[test] + fn test_str_clean() { + insta::assert_yaml_snapshot!(make_latex_string( + "πŸ©Έβš‘β˜½β—…β§†Γ˜βŒ­β˜†πŸ—˜Β½0.5& ++ (++) (--) -β˜…β˜…β˜… β˜…β˜…β˜… +β˜…β˜…β˜…" + )); + } + #[test] + fn test_extension() { + Path::new(""); + + assert!(extract_type(Path::new("short")).is_err()); + assert_eq!( + extract_type(Path::new("000000-cls")).unwrap(), + &TableTargets::Classes + ); + assert_eq!( + extract_type(Path::new("000000-fud")).unwrap(), + &TableTargets::Food + ); + assert_eq!( + extract_type(Path::new("000000-alc")).unwrap(), + &TableTargets::Alchemy + ); + assert_eq!( + extract_type(Path::new("000000-frg")).unwrap(), + &TableTargets::Forge + ); + assert_eq!( + extract_type(Path::new("000000-mpi")).unwrap(), + &TableTargets::MPieges + ); + assert_eq!( + extract_type(Path::new("000000-wpn")).unwrap(), + &TableTargets::Weapons + ); + assert_eq!( + extract_type(Path::new("000000-arm")).unwrap(), + &TableTargets::Armors + ); + assert_eq!( + extract_type(Path::new("000000-gdg")).unwrap(), + &TableTargets::Gadgets + ); + assert_eq!( + extract_type(Path::new("000000-pot")).unwrap(), + &TableTargets::Potions + ); + assert_eq!( + extract_type(Path::new("000000-trp")).unwrap(), + &TableTargets::Traps + ); + assert!(extract_type(Path::new("000000-bad")).is_err()); + } +}