Add tests

This commit is contained in:
ef3d0c3e 2024-07-26 18:18:28 +02:00
parent e5ba622e0d
commit d0343cee6b
3 changed files with 81 additions and 11 deletions

View file

@ -37,7 +37,7 @@ use crate::parser::util::PropertyParser;
use super::paragraph::Paragraph; use super::paragraph::Paragraph;
use super::reference::Reference; use super::reference::Reference;
#[derive(Debug)] #[derive(Debug, PartialEq, Eq)]
pub enum MediaType { pub enum MediaType {
IMAGE, IMAGE,
VIDEO, VIDEO,
@ -149,13 +149,14 @@ impl Element for Medium {
.as_ref() .as_ref()
.map_or(String::new(), |w| format!(r#" style="width:{w};""#)); .map_or(String::new(), |w| format!(r#" style="width:{w};""#));
result.push_str(format!(r#"<div class="medium"{width}>"#).as_str()); result.push_str(format!(r#"<div class="medium"{width}>"#).as_str());
match self.media_type { result += match self.media_type {
MediaType::IMAGE => result.push_str( MediaType::IMAGE =>
format!(r#"<a href="{0}"><img src="{0}"></a>"#, self.uri).as_str(), format!(r#"<a href="{0}"><img src="{0}"></a>"#, self.uri),
), MediaType::VIDEO =>
MediaType::VIDEO => todo!(), format!(r#"<video controls{width}><source src="{0}"></video>"#, self.uri),
MediaType::AUDIO => todo!(), MediaType::AUDIO =>
} format!(r#"<audio controls src="{0}"{width}></audio>"#, self.uri),
}.as_str();
let caption = self let caption = self
.caption .caption
@ -311,6 +312,8 @@ impl MediaRule {
match filename.split_at(sep + 1).1.to_ascii_lowercase().as_str() { match filename.split_at(sep + 1).1.to_ascii_lowercase().as_str() {
"png" | "apng" | "avif" | "gif" | "webp" | "svg" | "bmp" | "jpg" | "jpeg" | "jfif" "png" | "apng" | "avif" | "gif" | "webp" | "svg" | "bmp" | "jpg" | "jpeg" | "jfif"
| "pjpeg" | "pjp" => Some(MediaType::IMAGE), | "pjpeg" | "pjp" => Some(MediaType::IMAGE),
"mp4" | "m4v" | "webm" | "mov" => Some(MediaType::VIDEO),
"mp3" | "ogg" | "flac" | "wav" => Some(MediaType::AUDIO),
_ => None, _ => None,
} }
} }
@ -353,7 +356,7 @@ impl RegexRule for MediaRule {
matches.get(2).unwrap(), matches.get(2).unwrap(),
MediaRule::validate_uri(matches.get(2).unwrap().as_str()), MediaRule::validate_uri(matches.get(2).unwrap().as_str()),
) { ) {
(_, Ok(uri)) => uri.to_string(), (_, Ok(uri)) => util::process_escaped('\\', ")", uri),
(m, Err(err)) => { (m, Err(err)) => {
reports.push( reports.push(
Report::build(ReportKind::Error, token.source(), m.start()) Report::build(ReportKind::Error, token.source(), m.start())
@ -514,6 +517,9 @@ impl RegexRule for MediaRule {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::parser::langparser::LangParser;
use crate::parser::source::SourceFile;
use super::*; use super::*;
#[test] #[test]
@ -527,4 +533,34 @@ mod tests {
)); ));
assert!(re.is_match_at("![r1](uri1)[props1] desc1\n![r2](uri2)[props2] desc2", 26)); 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::<Medium>().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::<Medium>().unwrap();
assert_eq!(second.reference, "ref2");
assert_eq!(second.uri, "ur)i\\");
assert_eq!(second.media_type, MediaType::AUDIO);
}
} }

View file

@ -273,3 +273,35 @@ impl RegexRule for RawRule {
bindings 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]<RAW>?}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::<Raw>())
.collect::<Vec<_>>();
assert_eq!(found[0].compile(&compiler, &*doc), Ok("<RAW>".to_string()));
//assert_eq!(found[1].compile(&compiler, &*doc), Ok("<RAW>".to_string()));
}
}

View file

@ -128,7 +128,7 @@ pub fn process_escaped<S: AsRef<str>>(escape: char, token: &'static str, content
} }
} }
// Add trailing escapes // Add trailing escapes
(0..escaped).for_each(|_| processed.push('\\')); (0..escaped / 2).for_each(|_| processed.push('\\'));
processed processed
} }
@ -458,6 +458,8 @@ mod tests {
process_escaped('\\', "]", "Unescaped \\\\\\\\]"), process_escaped('\\', "]", "Unescaped \\\\\\\\]"),
"Unescaped \\\\\\\\]".to_string() "Unescaped \\\\\\\\]".to_string()
); );
assert_eq!(process_escaped('\\', ")", "A\\)B\\"), "A)B".to_string(),);
assert_eq!(process_escaped('\\', ")", "A\\)B\\\\"), "A)B\\".to_string(),);
} }
#[test] #[test]