Debian Codegraph @

tests/predicate_tests.rs

raw symbols SCIP
1 #![allow(
2 unused_variables,
3 clippy::disallowed_names,
4 clippy::many_single_char_names
5 )]
6
7 pub use select::document::Document;
8 pub use select::node;
9 pub use select::predicate::*;
10
11 use speculate::speculate;
12
13 speculate! {
14 describe "predicate" {
15 before {
16 let document = Document::from("<html><head></head><body>\
17 <article id='post-0' class='post category-foo tag-bar'>foo</article>\
18 <!--A Comment-->\
19 <div class='a'><div class='b'><div class='c'><div class='d'></div></div></div></div>
20 </body></html>");
21 let html = document.nth(0).unwrap();
22 let head = document.nth(1).unwrap();
23 let body = document.nth(2).unwrap();
24 let article = document.nth(3).unwrap();
25 let foo = document.nth(4).unwrap();
26 let comment = document.nth(5).unwrap();
27 let a = document.nth(6).unwrap();
28 let b = document.nth(7).unwrap();
29 let c = document.nth(8).unwrap();
30 let d = document.nth(9).unwrap();
31 }
32
33 test "Any" {
34 assert!(super::Any.matches(&html));
35 assert!(super::Any.matches(&head));
36 assert!(super::Any.matches(&body));
37 assert!(super::Any.matches(&article));
38 }
39
40 test "Name()" {
41 assert!(Name("html").matches(&html));
42 assert!(!Name("head").matches(&html));
43 assert!(!Name("body").matches(&html));
44 assert!(!Name("html").matches(&head));
45 assert!(Name("head").matches(&head));
46 assert!(!Name("body").matches(&head));
47 assert!(!Name("html").matches(&body));
48 assert!(!Name("head").matches(&body));
49 assert!(Name("body").matches(&body));
50 }
51
52 test "Class()" {
53 assert!(!Class("post").matches(&html));
54 assert!(Class("post").matches(&article));
55 assert!(Class("category-foo").matches(&article));
56 assert!(Class("tag-bar").matches(&article));
57 assert!(!Class("foo").matches(&article));
58 assert!(!Class("bar").matches(&article));
59 }
60
61 test "Not()" {
62 assert!(!Not(Name("html")).matches(&html));
63 assert!(Not(Name("html")).matches(&head));
64 assert!(Not(Name("head")).matches(&html));
65 assert!(!Not(Name("head")).matches(&head));
66 }
67
68 test "Attr()" {
69 assert!(!Attr("id", "post-0").matches(&html));
70 assert!(Attr("id", "post-0").matches(&article));
71 assert!(!Attr("id", ()).matches(&html));
72 assert!(Attr("id", ()).matches(&article));
73 }
74
75 test "Fn(&Node) -> bool" {
76 let f = |node: &node::Node| node.name() == Some("html");
77 assert!(f.matches(&html));
78 assert!(!f.matches(&head));
79 assert!(!f.matches(&body));
80 }
81
82 test "Element" {
83 assert!(super::Element.matches(&html));
84 assert!(super::Element.matches(&head));
85 assert!(super::Element.matches(&body));
86 assert!(super::Element.matches(&article));
87 assert!(!super::Element.matches(&foo));
88 }
89
90 test "Text" {
91 assert!(!super::Text.matches(&html));
92 assert!(!super::Text.matches(&head));
93 assert!(!super::Text.matches(&body));
94 assert!(!super::Text.matches(&article));
95 assert!(super::Text.matches(&foo));
96 assert!(!super::Text.matches(&comment));
97 }
98
99 test "Comment" {
100 assert!(!super::Comment.matches(&html));
101 assert!(!super::Comment.matches(&head));
102 assert!(!super::Comment.matches(&body));
103 assert!(!super::Comment.matches(&article));
104 assert!(!super::Comment.matches(&foo));
105 assert!(super::Comment.matches(&comment));
106 }
107
108 test "Or()" {
109 let html_or_head = Or(Name("html"), Name("head"));
110 assert!(html_or_head.matches(&html));
111 assert!(html_or_head.matches(&head));
112 assert!(!html_or_head.matches(&body));
113 assert!(!html_or_head.matches(&article));
114 assert!(!html_or_head.matches(&foo));
115 }
116
117 test "And()" {
118 let article_and_post_0 = And(Name("article"), Attr("id", "post-0"));
119 assert!(!article_and_post_0.matches(&html));
120 assert!(!article_and_post_0.matches(&head));
121 assert!(!article_and_post_0.matches(&body));
122 assert!(article_and_post_0.matches(&article));
123 assert!(!article_and_post_0.matches(&foo));
124 }
125
126 test "Child()" {
127 let html_article = Child(Name("html"), Name("article"));
128 assert!(!html_article.matches(&html));
129 assert!(!html_article.matches(&article));
130
131 let body_article = Child(Name("body"), Name("article"));
132 assert!(!body_article.matches(&html));
133 assert!(body_article.matches(&article));
134 }
135
136 test "Descendant()" {
137 let check = |parent: &str, child: &str, matching: Option<usize>| {
138 let selector = Descendant(Class(parent), Class(child));
139 for node in &[a, b, c, d] {
140 let expected = matching.map_or(false, |index| node.index() == index);
141 assert_eq!(selector.matches(node), expected);
142 }
143 };
144
145 check("a", "a", None);
146 check("a", "b", Some(b.index()));
147 check("a", "c", Some(c.index()));
148 check("a", "d", Some(d.index()));
149 check("b", "a", None);
150 check("b", "b", None);
151 check("b", "c", Some(c.index()));
152 check("b", "d", Some(d.index()));
153 check("c", "a", None);
154 check("c", "b", None);
155 check("c", "c", None);
156 check("c", "d", Some(d.index()));
157 check("d", "a", None);
158 check("d", "b", None);
159 check("d", "c", None);
160 check("d", "d", None);
161 }
162
163 // https://github.com/utkarshkukreti/select.rs/issues/35
164 test "Box<Predicate>" {
165 let post_0: Box<dyn Predicate> = Box::new(Attr("id", "post-0"));
166 assert!(!post_0.matches(&html));
167 assert!(!post_0.matches(&head));
168 assert!(post_0.matches(&article));
169 let not_html: Box<dyn Predicate> = Box::new(Not(Name("html")));
170 assert!(!not_html.matches(&html));
171 assert!(not_html.matches(&head));
172 assert!(not_html.matches(&article));
173 }
174 }
175 }