More coverage

This commit is contained in:
Dandaedre 2025-03-07 23:09:29 +01:00
parent e838e4e286
commit c0d008b6a4
Signed by: Dandaedre
GPG Key ID: 440C6592E8A26D99
5 changed files with 122 additions and 3 deletions

View File

@ -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"] }

View File

@ -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;

View File

@ -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}"

View File

@ -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> = regex_static::lazy_regex!(r"★+");
pub(super) static RE_PLUS_STARS: Lazy<Regex> = regex_static::lazy_regex!(r"([\+-])(★+)");
/// The regex for finding all (+/-) and replacing them with mathmode
pub(super) static RE_MATHS: Lazy<Regex> = 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());
}
}

View File

@ -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<PathBuf>, 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<PathBuf>, 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<dyn WriteTable> = 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<PathBuf> {
// println!("{}", pattern);
// println!("{:?}", glob(pattern));
@ -143,6 +152,7 @@ fn list_dir(pattern: &str) -> Vec<PathBuf> {
/// 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<dyn WriteTable>,
@ -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<T>(filename: &T) -> Result<ClassesArray, Error>
where
T: AsRef<Path>,
@ -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<Box<dyn WriteTable>> {
let file = File::open(filename)?;
let rdr = csv::ReaderBuilder::new()
@ -259,6 +271,7 @@ fn read_generic_tsv(filename: &Path, craft_type: &TableTargets) -> Result<Box<dy
///
/// This function takes in a Table object and returns its contents as a formatted string,
/// 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
pub(super) fn tsv_to_string(tsv: Box<dyn WriteTable>, config: &Config) -> Result<String> {
tsv.write_table(config)
}
@ -289,3 +302,66 @@ pub(super) fn tsv_to_string(tsv: Box<dyn WriteTable>, 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());
}
}