Debian Codegraph @

tests/document_tests.rs

raw symbols SCIP
1 #![allow(clippy::many_single_char_names)]
2 pub use std::collections::HashMap;
3
4 pub use select::document::Document;
5 pub use select::node;
6
7 use speculate::speculate;
8
9 speculate! {
10 describe "document" {
11 test "Document::from(&str)" {
12 let document = Document::from("<a b=c>d<e><f></e>g<h><i></i><j><!--k-->");
13
14 // html, head, and body are automatically added by the parser.
15 assert_eq!(document.nodes.len(), 12);
16
17 let html = document.nth(0).unwrap();
18 let head = document.nth(1).unwrap();
19 let body = document.nth(2).unwrap();
20 let a = document.nth(3).unwrap();
21 let d = document.nth(4).unwrap();
22 let e = document.nth(5).unwrap();
23 let f = document.nth(6).unwrap();
24 let g = document.nth(7).unwrap();
25 let h = document.nth(8).unwrap();
26 let i = document.nth(9).unwrap();
27 let j = document.nth(10).unwrap();
28 let k = document.nth(11).unwrap();
29
30 assert_eq!(html.name(), Some("html"));
31
32 assert_eq!(head.name(), Some("head"));
33
34 assert_eq!(body.name(), Some("body"));
35
36 assert_eq!(a.name(), Some("a"));
37 assert_eq!(a.attr("b"), Some("c"));
38
39 assert_eq!(d.name(), None);
40 assert_eq!(d.next(), Some(e));
41 assert_eq!(d.parent(), Some(a));
42
43 assert_eq!(e.name(), Some("e"));
44 assert_eq!(e.prev(), Some(d));
45
46 assert_eq!(f.name(), Some("f"));
47
48 assert_eq!(g.name(), None);
49
50 assert_eq!(h.name(), Some("h"));
51
52 assert_eq!(i.name(), Some("i"));
53 assert_eq!(i.parent(), Some(h));
54
55 assert_eq!(j.name(), Some("j"));
56 assert_eq!(j.parent(), Some(h));
57
58 assert_eq!(k.name(), None);
59 assert_eq!(k.parent(), Some(j));
60 }
61
62 test "Docucment::from_read()" {
63 use select::predicate::*;
64 use std::io::Cursor;
65
66 let html = "<html><body><p>Hello</p></body></html>";
67 let cursor = Cursor::new(html);
68 let document = Document::from_read(cursor);
69
70 assert!(document.is_ok());
71 assert_eq!(document.unwrap().find(Name("p")).count(), 1);
72 }
73
74 test "Document::find()" {
75 use select::predicate::*;
76
77 let document = Document::from(include_str!("fixtures/struct.Vec.html"));
78 assert_eq!(document.find(Any).count(), 11446);
79 assert_eq!(document.find(Name("div")).count(), 208);
80 assert_eq!(document.find(Attr("id", "main")).count(), 1);
81 assert_eq!(document.find(Class("struct")).count(), 168);
82 }
83 }
84 }