Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jyn514 committed Nov 10, 2020
1 parent 3420cb8 commit 945b680
Showing 1 changed file with 13 additions and 24 deletions.
37 changes: 13 additions & 24 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@ pub fn parse_html_file(root_dir: &Path, path: &Path) -> HashSet<Url> {
let html = std::fs::read_to_string(path)
.unwrap_or_else(|e| panic!("{} did not contain valid UTF8: {}", path.display(), e));

// base_url is the relative file path. For `target/dir/crate_x/y`, it's `crate_x/y`.
let base_url = Url::from_file_path(path).unwrap();
// root_url is absolute *relative to* the documentation directory. For `target/dir/crate_x/y`, it's `crate_x`.
let root_url = Url::from_directory_path(root_dir).unwrap();
// base_url is the relative file path. For `target/dir/crate_x/y`, it's `crate_x/y`.
let base_url = Url::from_file_path(path).unwrap();

parse_a_hrefs(&html, root_url, base_url)
}

/// This is a pure function, unlike `parse_html_file`, allowing it to be easily tested.
fn parse_a_hrefs(html: &str, root_url: Url, base_url: Url) -> HashSet<Url> {
let mut urls = HashSet::new();
lol_html::rewrite_str(
&html,
html,
RewriteStrSettings {
element_content_handlers: vec![element!("a[href]", |el| {
let href = el.get_attribute("href").unwrap();
Expand Down Expand Up @@ -48,25 +53,9 @@ pub fn parse_html_file(root_dir: &Path, path: &Path) -> HashSet<Url> {

#[cfg(test)]
mod test {
use html5ever::parse_document;
use html5ever::{rcdom::RcDom, tendril::TendrilSink};
use std::collections::HashSet;
use super::parse_a_hrefs as gather_urls;
use url::Url;

use super::parse_a_hrefs;

fn gather_urls(html: &str, root: &Url, url: &Url) -> HashSet<Url> {
let dom = parse_document(RcDom::default(), Default::default())
.from_utf8()
.read_from(&mut html.as_bytes())
.unwrap();

let mut urls = HashSet::new();
parse_a_hrefs(&dom.document, &root, &url, &mut urls);

return urls;
}

#[test]
fn test_parse_a_hrefs() {
let html = r#"
Expand All @@ -80,8 +69,8 @@ mod test {

let urls = gather_urls(
html,
&Url::from_directory_path("/base").unwrap(),
&Url::from_file_path("/base/test.html").unwrap(),
Url::from_directory_path("/base").unwrap(),
Url::from_file_path("/base/test.html").unwrap(),
);

assert!(urls.contains(&Url::from_file_path("/base/a.html").unwrap()));
Expand All @@ -102,8 +91,8 @@ mod test {

let urls = gather_urls(
html,
&Url::from_directory_path("/root").unwrap(),
&Url::from_file_path("/root/base/test.html").unwrap(),
Url::from_directory_path("/root").unwrap(),
Url::from_file_path("/root/base/test.html").unwrap(),
);

assert!(urls.contains(&Url::from_file_path("/root/base/a.html").unwrap()));
Expand Down

0 comments on commit 945b680

Please sign in to comment.