From 824b418f80da0c02c89f8aa574589b9afcae098d Mon Sep 17 00:00:00 2001 From: Vincent Flyson Date: Fri, 30 Aug 2019 22:18:14 -0400 Subject: [PATCH] Add more tests --- src/html.rs | 121 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) diff --git a/src/html.rs b/src/html.rs index d34f599..76730ab 100644 --- a/src/html.rs +++ b/src/html.rs @@ -390,7 +390,10 @@ mod tests { #[test] fn test_is_icon() { assert_eq!(is_icon("icon"), true); + assert_eq!(is_icon("Shortcut Icon"), true); + assert_eq!(is_icon("ICON"), true); assert_eq!(is_icon("stylesheet"), false); + assert_eq!(is_icon(""), false); } #[test] @@ -407,4 +410,122 @@ mod tests { assert_eq!(has_protocol(""), false); assert_eq!(has_protocol("MAILTO:somebody@somewhere.com?subject=hello"), true); } + + #[test] + fn test_get_parent_node_name() { + let html = "

"; + let dom = html_to_dom(&html); + let mut count = 0; + + fn test_walk(node: &Handle, i: &mut i8) { + *i += 1; + + match &node.data { + NodeData::Document => { + for child in node.children.borrow().iter() { + test_walk(child, &mut *i); + } + } + NodeData::Doctype { .. } => (), + NodeData::Text { .. } => (), + NodeData::Comment { .. } => (), + NodeData::Element { ref name, attrs: _, .. } => { + let node_name = name.local.as_ref().to_string(); + let parent_node_name = get_parent_node_name(node); + if node_name == "head" || node_name == "body" { + assert_eq!(parent_node_name, "html"); + } else if node_name == "div" { + assert_eq!(parent_node_name, "body"); + } else if node_name == "p" { + assert_eq!(parent_node_name, "div"); + } + + println!("{}", node_name); + + for child in node.children.borrow().iter() { + test_walk(child, &mut *i); + } + } + NodeData::ProcessingInstruction { .. } => unreachable!() + }; + } + + test_walk(&dom.document, &mut count); + + assert_eq!(count, 7); + } + + #[test] + fn test_walk_and_embed_assets() { + let html = "

"; + let dom = html_to_dom(&html); + let url = "http://localhost"; + + walk_and_embed_assets(&url, &dom.document, true, true, "", true, true); + + let mut buf: Vec = Vec::new(); + serialize(&mut buf, &dom.document, SerializeOpts::default()).unwrap(); + + assert_eq!( + buf.iter().map(|&c| c as char).collect::(), + "

" + ); + } + + #[test] + fn test_walk_and_embed_assets_iframe() { + let html = "

"; + let dom = html_to_dom(&html); + let url = "http://localhost"; + + walk_and_embed_assets(&url, &dom.document, true, true, "", true, true); + + let mut buf: Vec = Vec::new(); + serialize(&mut buf, &dom.document, SerializeOpts::default()).unwrap(); + + assert_eq!( + buf.iter().map(|&c| c as char).collect::(), + "

" + ); + } + + #[test] + fn test_walk_and_embed_assets_img() { + let html = "
"; + let dom = html_to_dom(&html); + let url = "http://localhost"; + + walk_and_embed_assets(&url, &dom.document, true, true, "", true, true); + + let mut buf: Vec = Vec::new(); + serialize(&mut buf, &dom.document, SerializeOpts::default()).unwrap(); + + assert_eq!( + buf.iter().map(|&c| c as char).collect::(), + "
\ + \ +
" + ); + } + + #[test] + fn test_walk_and_embed_assets_js() { + let html = "
\ +
"; + let dom = html_to_dom(&html); + let url = "http://localhost"; + + walk_and_embed_assets(&url, &dom.document, true, true, "", true, true); + + let mut buf: Vec = Vec::new(); + serialize(&mut buf, &dom.document, SerializeOpts::default()).unwrap(); + + assert_eq!( + buf.iter().map(|&c| c as char).collect::(), + "
\ +
" + ); + } }