From d0343cee6bf06a166b58a4866e3174475353db43 Mon Sep 17 00:00:00 2001 From: ef3d0c3e Date: Fri, 26 Jul 2024 18:18:28 +0200 Subject: [PATCH] Add tests --- src/elements/media.rs | 56 +++++++++++++++++++++++++++++++++++-------- src/elements/raw.rs | 32 +++++++++++++++++++++++++ src/parser/util.rs | 4 +++- 3 files changed, 81 insertions(+), 11 deletions(-) diff --git a/src/elements/media.rs b/src/elements/media.rs index ece4fe0..c1a7d9c 100644 --- a/src/elements/media.rs +++ b/src/elements/media.rs @@ -37,7 +37,7 @@ use crate::parser::util::PropertyParser; use super::paragraph::Paragraph; use super::reference::Reference; -#[derive(Debug)] +#[derive(Debug, PartialEq, Eq)] pub enum MediaType { IMAGE, VIDEO, @@ -148,14 +148,15 @@ impl Element for Medium { .width .as_ref() .map_or(String::new(), |w| format!(r#" style="width:{w};""#)); - result.push_str(format!(r#"
"#).as_str()); - match self.media_type { - MediaType::IMAGE => result.push_str( - format!(r#""#, self.uri).as_str(), - ), - MediaType::VIDEO => todo!(), - MediaType::AUDIO => todo!(), - } + result.push_str(format!(r#"
"#).as_str()); + result += match self.media_type { + MediaType::IMAGE => + format!(r#""#, self.uri), + MediaType::VIDEO => + format!(r#""#, self.uri), + MediaType::AUDIO => + format!(r#""#, self.uri), + }.as_str(); let caption = self .caption @@ -311,6 +312,8 @@ impl MediaRule { match filename.split_at(sep + 1).1.to_ascii_lowercase().as_str() { "png" | "apng" | "avif" | "gif" | "webp" | "svg" | "bmp" | "jpg" | "jpeg" | "jfif" | "pjpeg" | "pjp" => Some(MediaType::IMAGE), + "mp4" | "m4v" | "webm" | "mov" => Some(MediaType::VIDEO), + "mp3" | "ogg" | "flac" | "wav" => Some(MediaType::AUDIO), _ => None, } } @@ -353,7 +356,7 @@ impl RegexRule for MediaRule { matches.get(2).unwrap(), MediaRule::validate_uri(matches.get(2).unwrap().as_str()), ) { - (_, Ok(uri)) => uri.to_string(), + (_, Ok(uri)) => util::process_escaped('\\', ")", uri), (m, Err(err)) => { reports.push( Report::build(ReportKind::Error, token.source(), m.start()) @@ -514,6 +517,9 @@ impl RegexRule for MediaRule { #[cfg(test)] mod tests { + use crate::parser::langparser::LangParser; + use crate::parser::source::SourceFile; + use super::*; #[test] @@ -527,4 +533,34 @@ mod tests { )); assert!(re.is_match_at("![r1](uri1)[props1] desc1\n![r2](uri2)[props2] desc2", 26)); } + + #[test] + fn element_test() { + let source = Rc::new(SourceFile::with_content( + "".to_string(), + r#" +![ref1]( image.png )[width = 200px, caption = Caption\,] Description +![ref2]( ur\)i\\)[type=audio] + "# + .to_string(), + None, + )); + let parser = LangParser::default(); + let doc = parser.parse(source, None); + + let borrow = doc.content().borrow(); + let group = borrow.first().as_ref().unwrap().as_container().unwrap(); + + let first = group.contained()[0].downcast_ref::().unwrap(); + assert_eq!(first.reference, "ref1"); + assert_eq!(first.uri, "image.png"); + assert_eq!(first.media_type, MediaType::IMAGE); + assert_eq!(first.width, Some("200px".to_string())); + assert_eq!(first.caption, Some("Caption,".to_string())); + + let second = group.contained()[1].downcast_ref::().unwrap(); + assert_eq!(second.reference, "ref2"); + assert_eq!(second.uri, "ur)i\\"); + assert_eq!(second.media_type, MediaType::AUDIO); + } } diff --git a/src/elements/raw.rs b/src/elements/raw.rs index a1b0309..34d0760 100644 --- a/src/elements/raw.rs +++ b/src/elements/raw.rs @@ -273,3 +273,35 @@ impl RegexRule for RawRule { bindings } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::compiler::compiler::Target; + use crate::parser::langparser::LangParser; + use crate::parser::source::SourceFile; + + #[test] + fn raw_tests() { + let source = Rc::new(SourceFile::with_content( + "".to_string(), + r#" +Break{?[kind=block]?}NewParagraph + "# + .to_string(), + None, + )); + let parser = LangParser::default(); + let compiler = Compiler::new(Target::HTML, None); + let doc = parser.parse(source, None); + + let borrow = doc.content().borrow(); + let found = borrow + .iter() + .filter_map(|e| e.downcast_ref::()) + .collect::>(); + + assert_eq!(found[0].compile(&compiler, &*doc), Ok("".to_string())); + //assert_eq!(found[1].compile(&compiler, &*doc), Ok("".to_string())); + } +} diff --git a/src/parser/util.rs b/src/parser/util.rs index dcfa20d..e79748d 100644 --- a/src/parser/util.rs +++ b/src/parser/util.rs @@ -128,7 +128,7 @@ pub fn process_escaped>(escape: char, token: &'static str, content } } // Add trailing escapes - (0..escaped).for_each(|_| processed.push('\\')); + (0..escaped / 2).for_each(|_| processed.push('\\')); processed } @@ -458,6 +458,8 @@ mod tests { process_escaped('\\', "]", "Unescaped \\\\\\\\]"), "Unescaped \\\\\\\\]".to_string() ); + assert_eq!(process_escaped('\\', ")", "A\\)B\\"), "A)B".to_string(),); + assert_eq!(process_escaped('\\', ")", "A\\)B\\\\"), "A)B\\".to_string(),); } #[test]