From 897c8933dd7f0efded4c34273361659e05350e13 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Thu, 11 Apr 2024 05:23:18 +0200 Subject: [PATCH] Adds display and testing --- src/parser.rs | 55 +++++++++++++++++++++-- src/skills_structures.rs | 94 +++++++++++++++++++++++++++++++++++----- tarpaulin-report.html | 4 +- 3 files changed, 136 insertions(+), 17 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index f7ff7e6..65c9ada 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -202,6 +202,13 @@ pub struct LineId { #[cfg(test)] mod test { + use core::panic; + use std::cmp::min; + use std::fmt::Display; + use std::iter::zip; + + use crate::skills_structures::Node; + use crate::skills_structures::Renderable; use crate::skills_structures::SkillTypes; use super::LineId; @@ -244,6 +251,18 @@ mod test { level: SkillTypes, } + impl Display for BareNode { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "{:0width$} {:?}", + " ", + self.level, + width = min(self.level as usize, 5) + ) + } + } + impl BareNode { fn new(level: SkillTypes, list_lower: Option>) -> Self { Self { @@ -255,6 +274,33 @@ mod test { fn new_end(level: SkillTypes) -> BareNode { Self { list: None, level } } + + fn print_self(&self) { + println!("{}", self.to_string()); + } + fn print_all(&self) { + self.print_self(); + if let Some(list) = &self.list { + for elt in list { + elt.print_all(); + } + } + } + fn compare(&self, node: &Node) { + assert_eq!(&self.level, node.get_level()); + if let Some(cha) = &self.list { + if let Some(chb) = node.get_childs() { + assert_eq!(cha.len(), chb.len()); + for (a, b) in zip(cha, chb) { + a.compare(b) + } + } else { + panic!("{} has childs and the node doesn't", self) + } + } else if let Some(_chb) = node.get_childs() { + panic!("The node doesn't have childs and {} does", self) + } + } } macro_rules! ends { @@ -274,13 +320,14 @@ mod test { }; } #[test] - fn test_end() { - // let end1 = ; + fn test_barenodes() { let node = a!(Skill, ends!(NewLine, RepeatingSkills, RepeatingItem)); let node = a!(SubSection, l!(node)); let node = a!(Section, l!(node)); - println!("{:?}", node); - assert!(false); + + node.print_self(); + node.print_all(); + // assert!(false); } #[test] fn test_edge_cases() { diff --git a/src/skills_structures.rs b/src/skills_structures.rs index cacd8e8..47b63b5 100644 --- a/src/skills_structures.rs +++ b/src/skills_structures.rs @@ -4,7 +4,8 @@ //! //! Includes all Section/Subsection/Skill/Sub-skill and a skill-list parser // #![warn(dead_code)] -use std::{collections::HashMap, str::FromStr}; +use std::fmt::Write; +use std::{cmp::min, collections::HashMap, fmt::Display, str::FromStr}; use crate::{generator::to_skill_name, loader::LoadError}; @@ -128,12 +129,16 @@ type ChildList = Option>; 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 the list of childs + fn get_level(&self) -> &SkillTypes; + /// Get the level of the object + fn get_childs(&self) -> &ChildList; /// Create a new renderable fn new(name: &str, id: usize, global_id: usize, childs: ChildList, level: SkillTypes) -> Self; /// Get the vars hashmap fn get_vars(&self) -> HashReplace; + /// Debug display of the whole structure + fn debug_display(&self) -> String; // /// Render the element and all sub elements // fn render(&self, model: &str) -> Result; // /// Render the element header @@ -160,9 +165,6 @@ impl Renderable for Node { fn get_data(&self) -> &DataCell { &self.data } - // fn get_mut_data(&mut self) -> &mut DataCell { - // &self.data - // } fn get_vars(&self) -> HashReplace { self.data.to_hashmap() } @@ -173,20 +175,34 @@ impl Renderable for Node { level, } } + + fn get_level(&self) -> &SkillTypes { + &self.level + } + + fn get_childs(&self) -> &ChildList { + &self.childs + } + + fn debug_display(&self) -> String { + let mut f = String::new(); + write!(f, "{}\n", self.to_string()).unwrap(); + if let Some(childs) = &self.childs { + for child in childs.iter() { + write!(f, "{}\n", child.to_string()).unwrap(); + } + } + f + } } impl Renderable for SkillList { fn get_data(&self) -> &DataCell { &self.data } - // fn get_mut_data(&mut self) -> &DataCell { - // &mut self.data - // } - fn get_vars(&self) -> HashReplace { self.data.to_hashmap() } - fn new(name: &str, id: usize, global_id: usize, childs: ChildList, level: SkillTypes) -> Self { Self { childs, @@ -194,6 +210,39 @@ impl Renderable for SkillList { level, } } + + fn get_level(&self) -> &SkillTypes { + &self.level + } + + fn get_childs(&self) -> &ChildList { + &self.childs + } + + fn debug_display(&self) -> String { + let mut s = String::new(); + writeln!(s, "[{:?}]{:?}", self.level, self.data.name).unwrap(); + if let Some(childs) = &self.childs { + for chld in childs.iter() { + write!(s, "{}", chld.to_string()).unwrap(); + } + } + s + } +} +impl Display for Node { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "{:0width$} [{level:?}] {name} [{id}/{global_id}]", + " ", + width = min(self.level as usize, SkillTypes::NewLine as usize), + level = self.get_level(), + name = self.get_data().name, + id = self.get_data().id, + global_id = self.get_data().global_id + ) + } } /// A parsable type for dispatch @@ -279,6 +328,8 @@ mod tests { use core::panic; use std::collections::HashMap; + use pretty_assertions::assert_eq; + use crate::{ loader::LoadError, skills_structures::{Node, SkillTypes}, @@ -357,6 +408,8 @@ mod tests { let skill2 = SkillList::new(&name, id, global_id, None, SkillTypes::Root); assert_eq!(skill2.get_vars(), skll.get_vars()); + assert_eq!(skll.get_level(), &SkillTypes::Root); + assert!(skll.get_childs().is_some()); } #[test] @@ -369,6 +422,25 @@ mod tests { let dat = n.get_data(); assert_eq!(dat.name, name); assert_eq!(dat.id, id); + assert_eq!(n.get_level(), &skt); + assert!(n.get_childs().is_none()); + } + + #[test] + fn test_display() { + let n1 = Node::new("Name0", 0, 2, None, SkillTypes::Section); + let n2 = Node::new("Name1", 0, 1, Some(vec![n1]), SkillTypes::Section); + assert_eq!(n2.to_string(), " [Section] Name1 [0/1]".to_owned()); + assert_eq!( + n2.debug_display(), + " [Section] Name1 [0/1]\n [Section] Name0 [0/2]\n".to_owned() + ); + let sk = SkillList::new("Biglist", 0, 0, Some(vec![n2]), SkillTypes::Root); + assert_eq!(sk.get_childs().as_ref().unwrap().len(), 1); + assert_eq!( + sk.debug_display(), + "[Root]\"Biglist\"\n [Section] Name1 [0/1]".to_owned() + ) } #[test] diff --git a/tarpaulin-report.html b/tarpaulin-report.html index 79d6946..16adbbb 100644 --- a/tarpaulin-report.html +++ b/tarpaulin-report.html @@ -118,8 +118,8 @@