Debian Codegraph @

tests/selection_tests.rs

raw symbols SCIP
1 pub use select::document::Document;
2 pub use select::selection::*;
3
4 use speculate::speculate;
5
6 speculate! {
7 describe "selection" {
8 test "Iter" {
9 let document = Document::from("<html><head></head><body>\
10 <article id='post-0' class='post category-foo tag-bar'></article>\
11 </body></html>");
12 let selection = Selection::new(&document,
13 [0, 2, 3].iter().cloned().collect());
14 let mut iter = selection.iter();
15 let html = iter.next().unwrap();
16 let body = iter.next().unwrap();
17 let article = iter.next().unwrap();
18 assert_eq!(iter.next(), None);
19 assert_eq!(html.name(), Some("html"));
20 assert_eq!(body.name(), Some("body"));
21 assert_eq!(article.attr("id"), Some("post-0"));
22 }
23
24 test "Selection::filter()" {
25 use select::predicate::*;
26
27 let document = Document::from(include_str!("fixtures/struct.Vec.html"));
28 let all = document.find(Any).into_selection();
29
30 assert_eq!(all.filter(Any).len(), 11446);
31
32 let divs = all.filter(Name("div"));
33 assert_eq!(divs.len(), 208);
34 for div in &divs {
35 assert_eq!(div.name(), Some("div"))
36 }
37
38 assert_eq!(all.filter(Attr("id", "main")).len(), 1);
39
40 let structs = all.filter(Class("struct"));
41 assert_eq!(structs.len(), 168);
42 for struct_ in &structs {
43 assert!(struct_.attr("class").unwrap().contains("struct"))
44 };
45 }
46
47 test "Selection::find()" {
48 use select::predicate::*;
49
50 let document = Document::from(include_str!("fixtures/struct.Vec.html"));
51 let all = document.find(Any).into_selection();
52
53 let struct_divs = all.find(Class("struct")).find(Name("div"));
54 assert_eq!(struct_divs.len(), 204);
55 for struct_div in &struct_divs {
56 assert_eq!(struct_div.name(), Some("div"));
57 };
58
59 let struct_as = all.find(Class("struct")).find(Name("a"));
60 assert_eq!(struct_as.len(), 1260);
61 for struct_a in &struct_as {
62 assert_eq!(struct_a.name(), Some("a"));
63 };
64 }
65
66 test "Selection::parent()" {
67 use select::predicate::*;
68
69 let document = Document::from(include_str!("fixtures/struct.Vec.html"));
70
71 assert_eq!(document.find(Name("div")).into_selection().parent().len(), 8);
72 assert_eq!(document.find(Name("span")).into_selection().parent().len(), 205);
73 }
74
75 test "Selection::prev() / Selection::next()" {
76 use select::predicate::*;
77
78 let document = Document::from(include_str!("fixtures/struct.Vec.html"));
79
80 assert_eq!(document.find(Name("div")).into_selection().prev().len(), 208);
81 assert_eq!(document.find(Name("div")).into_selection().next().len(), 203);
82 assert_eq!(document.find(Name("span")).into_selection().prev().len(), 1729);
83 assert_eq!(document.find(Name("span")).into_selection().next().len(), 1690);
84 }
85
86 test "Selection::parents()" {
87 use select::predicate::*;
88
89 let document = Document::from(include_str!("fixtures/struct.Vec.html"));
90
91 assert_eq!(document.find(Name("div")).into_selection().parents().len(), 10);
92 assert_eq!(document.find(Name("span")).into_selection().parents().len(), 308);
93 }
94
95 test "Selection::children()" {
96 use select::predicate::*;
97
98 let document = Document::from(include_str!("fixtures/struct.Vec.html"));
99
100 let div_children = document.find(Name("div")).into_selection().children();
101 assert_eq!(div_children.len(), 1210);
102 for div_child in &div_children {
103 assert_eq!(div_child.parent().unwrap().name(), Some("div"));
104 }
105
106 let span_children = document.find(Name("span")).into_selection().children();
107 assert_eq!(span_children.len(), 1986);
108 for span_child in &span_children {
109 assert_eq!(span_child.parent().unwrap().name(), Some("span"));
110 };
111 }
112
113 test "Selection::first()" {
114 use select::predicate::*;
115
116 let document = Document::from(include_str!("fixtures/struct.Vec.html"));
117
118 assert!(document.find(Name("div")).into_selection().first().is_some());
119 assert!(document.find(Name("divv")).into_selection().first().is_none());
120 }
121
122 test "Selection::last()" {
123 use select::predicate::*;
124
125 let document = Document::from(include_str!("fixtures/struct.Vec.html"));
126
127 assert!(document.find(Name("div")).into_selection().last().is_some());
128 assert!(document.find(Name("divv")).into_selection().last().is_none());
129 }
130
131 test "Selection::len() == Selection::iter().count()" {
132 use select::predicate::*;
133
134 let document = Document::from(include_str!("fixtures/struct.Vec.html"));
135
136 fn check<P: Predicate>(document: &Document, predicate: P) {
137 let selection = document.find(predicate).into_selection();
138 assert_eq!(selection.len(), selection.iter().count());
139 }
140
141 check(&document, Any);
142 check(&document, Attr("id", "main"));
143 check(&document, Class("struct"));
144 check(&document, Name("div"));
145 check(&document, Name("span"));
146 }
147
148 test "Iter (lifetimes)" {
149 let document = Document::from("<html><head></head><body>\
150 <article id='post-0' class='post category-foo tag-bar'></article>\
151 </body></html>");
152 let html = {
153 let selection = Selection::new(&document,
154 [0, 2, 3].iter().cloned().collect());
155 selection.iter().next().unwrap()
156 };
157 assert_eq!(html.name(), Some("html"));
158 }
159 }
160 }