Skip to content

Commit 3a06b1e

Browse files
committed
Add tests for HTML parsing
1 parent b6f81fa commit 3a06b1e

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed

src/utils/html.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,38 @@ fn extract_class(node: &Handle) -> String {
7272
_ => String::new()
7373
}
7474
}
75+
76+
#[cfg(test)]
77+
mod test {
78+
use super::*;
79+
80+
fn round_trip(a: &str) -> String {
81+
let parser = parse_document(RcDom::default(), ParseOpts::default());
82+
stringify(parser.one(a).document)
83+
}
84+
fn mostly_equal(a: &str, b: &str) -> bool {
85+
round_trip(a.trim()).trim() == round_trip(b.trim()).trim()
86+
}
87+
88+
#[test]
89+
fn small_html() {
90+
let (head, body, class) = super::extract_head_and_body(
91+
r#"<head><meta name="generator" content="rustdoc"></head><body class="rustdoc struct"><p>hello</p>"#
92+
).unwrap();
93+
assert_eq!(head, r#"<head><meta name="generator" content="rustdoc"></head>"#);
94+
assert_eq!(body, "<body><p>hello</p>");
95+
assert_eq!(class, "rustdoc struct");
96+
}
97+
98+
// more of an integration test
99+
#[test]
100+
fn parse_regex_html() {
101+
let original = std::fs::read_to_string("benches/struct.CaptureMatches.html").unwrap();
102+
let expected_head = std::fs::read_to_string("tests/regex/head.html").unwrap();
103+
let expected_body = std::fs::read_to_string("tests/regex/body.html").unwrap();
104+
let (head, body, class) = super::extract_head_and_body(&original).unwrap();
105+
assert!(mostly_equal(&head, &expected_head));
106+
assert!(mostly_equal(&body, &expected_body));
107+
assert_eq!(class, "rustdoc struct");
108+
}
109+
}

0 commit comments

Comments
 (0)