Adds most testing and sanity
This commit is contained in:
parent
1c7511211e
commit
7b77e9dbe5
|
|
@ -1,4 +1,5 @@
|
|||
//! Reads and parse a skill sheet
|
||||
use core::panic;
|
||||
use std::{fs, path::Path};
|
||||
|
||||
use crate::{
|
||||
|
|
@ -50,9 +51,13 @@ impl Parser<SkillList> for SkillList {
|
|||
global_id += 1;
|
||||
}
|
||||
SkillTypes::NewLine => (),
|
||||
SkillTypes::Error => (),
|
||||
SkillTypes::Error => panic!(
|
||||
"An unrecoverable error occured while parsing line {}.\n{}",
|
||||
global_id, skill
|
||||
),
|
||||
SkillTypes::Root => (),
|
||||
SkillTypes::Repeating => todo!(),
|
||||
SkillTypes::RepeatingSkills => unimplemented!(),
|
||||
SkillTypes::RepeatingItem => unimplemented!(),
|
||||
}
|
||||
}
|
||||
let list_sect = globlist.make_skilllist()?;
|
||||
|
|
|
|||
|
|
@ -89,19 +89,6 @@ pub struct DataCell {
|
|||
pub childs: usize,
|
||||
}
|
||||
|
||||
impl DataCell {
|
||||
/// Create a datacell with the given string a ids
|
||||
pub fn with_string_and_id(name: &str, id: usize, global_id: usize) -> Self {
|
||||
Self {
|
||||
name: name.to_string(),
|
||||
stripped_name: to_skill_name(name),
|
||||
id,
|
||||
global_id,
|
||||
childs: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A Data point, holds the name and stripped name of element
|
||||
impl DataCell {
|
||||
/// Create a datacell from the given raw string
|
||||
|
|
@ -141,8 +128,8 @@ type ChildList = Option<Vec<Node>>;
|
|||
pub(crate) trait Renderable {
|
||||
/// Get a imutable view of the data
|
||||
fn get_data(&self) -> &DataCell;
|
||||
/// Get a mutable reference of the data
|
||||
fn get_mut_data(&mut self) -> &DataCell;
|
||||
// /// Get a mutable reference of the data
|
||||
// fn get_mut_data(&mut self) -> &DataCell;
|
||||
/// Create a new renderable
|
||||
fn new(name: &str, id: usize, global_id: usize, childs: ChildList, level: SkillTypes) -> Self;
|
||||
/// Get the vars hashmap
|
||||
|
|
@ -173,9 +160,9 @@ impl Renderable for Node {
|
|||
fn get_data(&self) -> &DataCell {
|
||||
&self.data
|
||||
}
|
||||
fn get_mut_data(&mut self) -> &DataCell {
|
||||
&self.data
|
||||
}
|
||||
// fn get_mut_data(&mut self) -> &mut DataCell {
|
||||
// &self.data
|
||||
// }
|
||||
fn get_vars(&self) -> HashReplace {
|
||||
self.data.to_hashmap()
|
||||
}
|
||||
|
|
@ -192,10 +179,9 @@ impl Renderable for SkillList {
|
|||
fn get_data(&self) -> &DataCell {
|
||||
&self.data
|
||||
}
|
||||
|
||||
fn get_mut_data(&mut self) -> &DataCell {
|
||||
&mut self.data
|
||||
}
|
||||
// fn get_mut_data(&mut self) -> &DataCell {
|
||||
// &mut self.data
|
||||
// }
|
||||
|
||||
fn get_vars(&self) -> HashReplace {
|
||||
self.data.to_hashmap()
|
||||
|
|
@ -211,6 +197,7 @@ impl Renderable for SkillList {
|
|||
}
|
||||
|
||||
/// A parsable type for dispatch
|
||||
#[derive(PartialEq, Debug)]
|
||||
pub enum SkillTypes {
|
||||
/// This is the Root of the tree (unrepresentable)
|
||||
Root,
|
||||
|
|
@ -223,7 +210,9 @@ pub enum SkillTypes {
|
|||
/// This line is a Subskill
|
||||
SubSkill,
|
||||
/// This is a repeating section
|
||||
Repeating,
|
||||
RepeatingSkills,
|
||||
/// This is a repeating item list
|
||||
RepeatingItem,
|
||||
/// This line is an empty new line
|
||||
NewLine,
|
||||
/// This line couldn't be parsed
|
||||
|
|
@ -237,6 +226,8 @@ impl FromStr for SkillTypes {
|
|||
Some(b'+') => Ok(SkillTypes::SubSection),
|
||||
Some(b'=') => Ok(SkillTypes::Skill),
|
||||
Some(b'-') => Ok(SkillTypes::SubSkill),
|
||||
Some(b'#') => Ok(SkillTypes::RepeatingItem),
|
||||
Some(b'.') => Ok(SkillTypes::RepeatingSkills),
|
||||
None => Ok(SkillTypes::NewLine),
|
||||
_ => Err(LoadError::LineParseError),
|
||||
}
|
||||
|
|
@ -247,11 +238,15 @@ impl FromStr for SkillTypes {
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use core::panic;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::skills_structures::{Node, SkillTypes};
|
||||
use crate::{
|
||||
loader::LoadError,
|
||||
skills_structures::{Node, SkillTypes},
|
||||
};
|
||||
|
||||
use super::{DataCell, Renderable};
|
||||
use super::{ConfigList, DataCell, Renderable, SkillList};
|
||||
|
||||
static S1: &str =
|
||||
"This is a test sentence with some non-ASCII characters like café and résumé.";
|
||||
|
|
@ -293,4 +288,71 @@ mod tests {
|
|||
map.insert("selected".to_string(), "checked=\"checked\"".to_string());
|
||||
assert_eq!(s1.get_vars(), map);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_config() {
|
||||
let name = "Johnéé".to_string();
|
||||
let id = 12;
|
||||
let global_id = 33;
|
||||
let capacity = 44;
|
||||
let cfg = ConfigList::new(name.clone(), id, global_id, capacity);
|
||||
let ckll = SkillList::with_config(&cfg);
|
||||
assert_eq!(ckll.get_data().name, name);
|
||||
assert_eq!(ckll.get_data().global_id, global_id);
|
||||
assert_eq!(ckll.get_data().id, id);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_skilllist() {
|
||||
let name = "Jognéé".to_string();
|
||||
let id = 20;
|
||||
let global_id = 30;
|
||||
let list = Vec::new();
|
||||
let skll = SkillList::from_vec(&name, id, global_id, Some(list));
|
||||
assert!(skll.childs.is_some());
|
||||
let vars = skll.get_vars();
|
||||
assert_eq!(vars["name"], name);
|
||||
assert_eq!(vars["id"], id.to_string());
|
||||
assert_eq!(vars["pname"], "jogn".to_string());
|
||||
assert_eq!(vars["selected"], "".to_string());
|
||||
assert_eq!(vars["global_id"], global_id.to_string());
|
||||
|
||||
let skill2 = SkillList::new(&name, id, global_id, None, SkillTypes::Root);
|
||||
assert_eq!(skill2.get_vars(), skll.get_vars());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_node() {
|
||||
let name = "Eeeé".to_string();
|
||||
let (id, global_id) = (10, 20);
|
||||
let skt = SkillTypes::Section;
|
||||
let childs = None;
|
||||
let n = Node::new(&name, id, global_id, childs, skt);
|
||||
let dat = n.get_data();
|
||||
assert_eq!(dat.name, name);
|
||||
assert_eq!(dat.id, id);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_fromstr() {
|
||||
let vals = [
|
||||
("+ ra", Ok(SkillTypes::SubSection)),
|
||||
("@ azé", Ok(SkillTypes::Section)),
|
||||
("=", Ok(SkillTypes::Skill)),
|
||||
("-é", Ok(SkillTypes::SubSkill)),
|
||||
("", Ok(SkillTypes::NewLine)),
|
||||
("f", Err(LoadError::LineParseError)),
|
||||
(".", Ok(SkillTypes::RepeatingSkills)),
|
||||
("#raE", Ok(SkillTypes::RepeatingItem)),
|
||||
];
|
||||
for v in vals.iter() {
|
||||
let res = v.0.parse::<SkillTypes>();
|
||||
|
||||
match (&res, &v.1) {
|
||||
(Ok(a), Ok(b)) => assert_eq!(a, b),
|
||||
(Err(_a), Err(_b)) => (),
|
||||
_ => panic!("{} value generated an error", v.0),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
Reference in New Issue