Adds more testing and coverage

This commit is contained in:
Dandaedre 2024-04-10 03:06:35 +02:00
parent 7b77e9dbe5
commit ce77dfb7e9
4 changed files with 194 additions and 54 deletions

View File

@ -9,6 +9,7 @@ edition = "2021"
[dependencies]
criterion = "0.5.1"
divan = "0.1.14"
pretty_assertions = "1.4.0"
serde = { version = "1.0.197", features = ["derive"] }
strfmt = "0.2.4"
toml = { version = "0.8.12", features = ["preserve_order"] }

View File

@ -1,5 +1,4 @@
//! Reads and parse a skill sheet
use core::panic;
use std::{fs, path::Path};
use crate::{
@ -19,47 +18,8 @@ impl Parser<SkillList> for SkillList {
type Err = LoadError;
fn from_path(path: impl AsRef<Path> + ToString) -> Result<SkillList, LoadError> {
let file_content = fs::read_to_string(&path).map_err(LoadError::Io)?;
let mut global_id = 0;
// let mut globlist = Vec::with_capacity(100);
let mut globlist = ListId::new(100);
let (mut last_sect, mut last_subs, mut last_skill, mut last_sskill) = (0, 0, 0, 0);
for line in file_content.lines() {
let (index, skill) = line.split_at(1);
let kind = index.parse::<SkillTypes>()?;
match kind {
SkillTypes::Section => {
last_sect += 1;
(last_subs, last_skill, last_sskill) = (0, 0, 0);
globlist.add_item(kind, skill, global_id, last_sect);
global_id += 1;
}
SkillTypes::SubSection => {
last_subs += 1;
(last_skill, last_sskill) = (0, 0);
globlist.add_item(kind, skill, global_id, last_subs);
global_id += 1;
}
SkillTypes::Skill => {
last_skill += 1;
last_sskill = 0;
globlist.add_item(kind, skill, global_id, last_skill);
global_id += 1;
}
SkillTypes::SubSkill => {
last_sskill += 1;
globlist.add_item(kind, skill, global_id, last_sskill);
global_id += 1;
}
SkillTypes::NewLine => (),
SkillTypes::Error => panic!(
"An unrecoverable error occured while parsing line {}.\n{}",
global_id, skill
),
SkillTypes::Root => (),
SkillTypes::RepeatingSkills => unimplemented!(),
SkillTypes::RepeatingItem => unimplemented!(),
}
}
let mut globlist = ListId::from_string(file_content)?;
let list_sect = globlist.make_skilllist()?;
// Make a skillist with the given vec
let ls = SkillList::from_vec(&path.to_string(), 0, 0, Some(list_sect));
@ -70,7 +30,7 @@ impl Parser<SkillList> for SkillList {
}
/// The structure representing the list of LineIds
struct ListId {
pub struct ListId {
/// The inner list holding all values
list: Vec<LineId>,
}
@ -80,7 +40,7 @@ impl ListId {
pub fn add_item(&mut self, kind: SkillTypes, value: &str, global_id: usize, id: usize) {
self.list.push(LineId {
kind,
value: value.to_owned(),
value: value.trim_start().to_owned(),
global_id,
id,
});
@ -112,7 +72,7 @@ impl ListId {
/// Make a section starting from the given index
fn make_section(&self, index: &mut usize) -> Result<Node, LoadError> {
let mut list_subsection = Vec::new();
let (name, id, global_id) = self.get_info(index);
let (name, global_id, id) = self.get_info(index);
while *index < self.list.len() {
if matches!(self.get_kind(index), SkillTypes::SubSection) {
list_subsection.push(self.make_subsection(index)?);
@ -133,7 +93,7 @@ impl ListId {
/// Make a section starting from the given index
fn make_subsection(&self, index: &mut usize) -> Result<Node, LoadError> {
let mut list_skills = Vec::new();
let (name, id, global_id) = self.get_info(index);
let (name, global_id, id) = self.get_info(index);
while *index < self.list.len() {
if matches!(self.get_kind(index), SkillTypes::SubSection) {
list_skills.push(self.make_skill(index)?);
@ -154,7 +114,7 @@ impl ListId {
/// Make a skill from the given index
fn make_skill(&self, index: &mut usize) -> Result<Node, LoadError> {
let mut list_subskills = Vec::new();
let (name, id, global_id) = self.get_info(index);
let (name, global_id, id) = self.get_info(index);
while *index < self.list.len() {
if matches!(self.get_kind(index), SkillTypes::SubSection) {
list_subskills.push(self.make_subsection(index)?);
@ -174,7 +134,7 @@ impl ListId {
/// Make a subskill
fn make_subskill(&self, index: &mut usize) -> Result<Node, LoadError> {
let (name, id, global_id) = self.get_info(index);
let (name, global_id, id) = self.get_info(index);
if !matches!(self.get_kind(index), SkillTypes::SubSkill) {
return Err(LoadError::WrongHierarchy);
}
@ -196,7 +156,7 @@ impl ListId {
#[inline]
/// Get the global_id of the element at the given index
fn get_global_id(&self, index: &usize) -> usize {
self.list[*index].id
self.list[*index].global_id
}
#[inline]
/// Get the name of the element at the given index
@ -204,14 +164,68 @@ impl ListId {
&self.list[*index].value
}
/// Returns the value, id and global_id of the element
/// Returns the value, global_id and id of the element
fn get_info(&self, index: &usize) -> (&str, usize, usize) {
(
&self.list[*index].value,
self.list[*index].id,
self.list[*index].global_id,
self.list[*index].id,
)
}
/// Convert the content of a file to a list of lines
fn from_string(file_content: String) -> Result<Self, LoadError> {
let mut global_id = 0;
let mut globlist = ListId::new(100);
let (mut last_sect, mut last_subs, mut last_skill, mut last_sskill) = (0, 0, 0, 0);
for line in file_content.lines() {
// Can't parse an empty symbol
if line.len() < 2 {
continue;
}
let (index, skill) = line.split_at(1);
let kind = index.parse::<SkillTypes>()?;
match kind {
SkillTypes::Section => {
(last_subs, last_skill, last_sskill) = (0, 0, 0);
globlist.add_item(kind, skill, global_id, last_sect);
last_sect += 1;
global_id += 1;
}
SkillTypes::SubSection => {
(last_skill, last_sskill) = (0, 0);
globlist.add_item(kind, skill, global_id, last_subs);
last_subs += 1;
global_id += 1;
}
SkillTypes::Skill => {
last_sskill = 0;
globlist.add_item(kind, skill, global_id, last_skill);
last_skill += 1;
global_id += 1;
}
SkillTypes::SubSkill => {
globlist.add_item(kind, skill, global_id, last_sskill);
last_sskill += 1;
global_id += 1;
}
SkillTypes::RepeatingSkills => {
globlist.add_item(SkillTypes::RepeatingSkills, skill, global_id, 0);
global_id += 1;
}
SkillTypes::RepeatingItem => {
globlist.add_item(SkillTypes::RepeatingItem, skill, global_id, 0);
global_id += 1;
}
SkillTypes::NewLine => (),
// SkillTypes::Error => panic!(
// "An unrecoverable error occured while parsing line {}.\n{}",
// global_id, skill
// ),
SkillTypes::Root => (),
}
}
Ok(globlist)
}
}
/// Temporary data to preparse the file
@ -225,3 +239,128 @@ pub struct LineId {
/// The id relative to the indent level
id: usize,
}
#[cfg(test)]
mod test {
use crate::skills_structures::SkillTypes;
use super::LineId;
use super::ListId;
use pretty_assertions::assert_eq;
#[test]
fn test_lineids() {
let _line = LineId {
kind: SkillTypes::Section,
value: "EE".to_string(),
global_id: 20,
id: 2,
};
let mut list = ListId::new(100);
list.add_item(SkillTypes::Section, " EE", 20, 2);
assert_eq!(list.get_info(&0), ("EE", 20, 2));
assert_eq!(list.get_name(&0), "EE");
assert_eq!(list.get_global_id(&0), 20);
assert_eq!(list.get_id(&0), 2);
assert_eq!(list.get_kind(&0), &SkillTypes::Section);
}
#[test]
fn test_parser() {}
#[test]
fn test_skilllist() {}
#[test]
fn test_node() {}
#[test]
fn test_listid() {}
#[test]
fn test_edge_cases() {
let line_id = ListId::from_string("\n".to_string()).unwrap();
assert!(line_id.list.is_empty());
let line_id = ListId::from_string("".to_string()).unwrap();
assert!(line_id.list.is_empty());
let line_id = ListId::from_string("nope".to_string());
assert!(line_id.is_err());
}
#[test]
fn test_fromstring() {
let tst = [
("@ Sect", vec![(SkillTypes::Section, "Sect", 0, 0)]),
(
"+ SubSecté",
vec![(SkillTypes::SubSection, "SubSecté", 0, 0)],
),
("= Skill", vec![(SkillTypes::Skill, "Skill", 0, 0)]),
("- SubSkill", vec![(SkillTypes::SubSkill, "SubSkill", 0, 0)]),
(". rpts", vec![(SkillTypes::RepeatingSkills, "rpts", 0, 0)]),
("#rpitm", vec![(SkillTypes::RepeatingItem, "rpitm", 0, 0)]),
(
"@s\n+ss\n=sk\n-sks",
vec![
(SkillTypes::Section, "s", 0, 0),
(SkillTypes::SubSection, "ss", 1, 0),
(SkillTypes::Skill, "sk", 2, 0),
(SkillTypes::SubSkill, "sks", 3, 0),
],
),
(
"@s0\n+ss0\n=sk0\n-sks0\n-sks1\n-sks2\n-sks3\n\
=sk1\n-sks0\n-sks1\n-sks2\n-sks3\n+ss1\n=sk0\n\
-sks0\n-sks1\n-sks2\n-sks3\n=sk1\n-sks0\n-sks1\n\
-sks2\n-sks3\n@s1\n+ss0\n#ri0\n#ri1\n+ss1\n.rpts0\n.rpts1",
vec![
(SkillTypes::Section, "s0", 0, 0),
(SkillTypes::SubSection, "ss0", 1, 0),
(SkillTypes::Skill, "sk0", 2, 0),
(SkillTypes::SubSkill, "sks0", 3, 0),
(SkillTypes::SubSkill, "sks1", 4, 1),
(SkillTypes::SubSkill, "sks2", 5, 2),
(SkillTypes::SubSkill, "sks3", 6, 3),
(SkillTypes::Skill, "sk1", 7, 1),
(SkillTypes::SubSkill, "sks0", 8, 0),
(SkillTypes::SubSkill, "sks1", 9, 1),
(SkillTypes::SubSkill, "sks2", 10, 2),
(SkillTypes::SubSkill, "sks3", 11, 3),
(SkillTypes::SubSection, "ss1", 12, 1),
(SkillTypes::Skill, "sk0", 13, 0),
(SkillTypes::SubSkill, "sks0", 14, 0),
(SkillTypes::SubSkill, "sks1", 15, 1),
(SkillTypes::SubSkill, "sks2", 16, 2),
(SkillTypes::SubSkill, "sks3", 17, 3),
(SkillTypes::Skill, "sk1", 18, 1),
(SkillTypes::SubSkill, "sks0", 19, 0),
(SkillTypes::SubSkill, "sks1", 20, 1),
(SkillTypes::SubSkill, "sks2", 21, 2),
(SkillTypes::SubSkill, "sks3", 22, 3),
(SkillTypes::Section, "s1", 23, 1),
(SkillTypes::SubSection, "ss0", 24, 0),
(SkillTypes::RepeatingItem, "ri0", 25, 0),
(SkillTypes::RepeatingItem, "ri1", 26, 0),
(SkillTypes::SubSection, "ss1", 27, 1),
(SkillTypes::RepeatingSkills, "rpts0", 28, 0),
(SkillTypes::RepeatingSkills, "rpts1", 29, 0),
],
),
];
for (elt, res) in tst {
let line = ListId::from_string(elt.to_string());
println!("Testing list {}", elt);
let list_id = line.unwrap();
assert_eq!(list_id.list.len(), res.len());
for (i, (kind, value, global_id, id)) in res.iter().enumerate() {
println!("Testing element '{}' gid:{} id:{}", value, global_id, id);
assert_eq!(list_id.get_kind(&i), kind);
assert_eq!(list_id.get_name(&i), *value);
assert_eq!(list_id.get_global_id(&i), *global_id);
assert_eq!(list_id.get_id(&i), *id);
}
}
}
}

View File

@ -215,8 +215,8 @@ pub enum SkillTypes {
RepeatingItem,
/// This line is an empty new line
NewLine,
/// This line couldn't be parsed
Error,
// /// This line couldn't be parsed
// Error,
}
impl FromStr for SkillTypes {

File diff suppressed because one or more lines are too long