| 1 |
#![allow(unused_variables, clippy::disallowed_names)] |
| 2 |
|
| 3 |
pub use std::collections::HashMap; |
| 4 |
|
| 5 |
pub use select::document::Document; |
| 6 |
pub use select::node; |
| 7 |
|
| 8 |
use speculate::speculate; |
| 9 |
|
| 10 |
speculate! { |
| 11 |
describe "node" { |
| 12 |
before { |
| 13 |
let document = Document::from("<html><head></head><body id=something>\ |
| 14 |
foo<bar>baz<quux class=another-thing>\ |
| 15 |
<!--comment-->"); |
| 16 |
|
| 17 |
let html = document.nth(0).unwrap(); |
| 18 |
let head = document.nth(1).unwrap(); |
| 19 |
let body = document.nth(2).unwrap(); |
| 20 |
let foo = document.nth(3).unwrap(); |
| 21 |
let bar = document.nth(4).unwrap(); |
| 22 |
let baz = document.nth(5).unwrap(); |
| 23 |
let quux = document.nth(6).unwrap(); |
| 24 |
let comment = document.nth(7).unwrap(); |
| 25 |
} |
| 26 |
|
| 27 |
test "Node::name()" { |
| 28 |
assert_eq!(html.name(), Some("html")); |
| 29 |
assert_eq!(head.name(), Some("head")); |
| 30 |
assert_eq!(body.name(), Some("body")); |
| 31 |
assert_eq!(foo.name(), None); |
| 32 |
assert_eq!(bar.name(), Some("bar")); |
| 33 |
assert_eq!(baz.name(), None); |
| 34 |
assert_eq!(quux.name(), Some("quux")); |
| 35 |
|
| 36 |
|
| 37 |
let name = { |
| 38 |
html.clone().name() |
| 39 |
}; |
| 40 |
assert_eq!(name, Some("html")); |
| 41 |
} |
| 42 |
|
| 43 |
test "Node::attr()" { |
| 44 |
assert_eq!(html.attr("id"), None); |
| 45 |
assert_eq!(head.attr("id"), None); |
| 46 |
assert_eq!(body.attr("id"), Some("something")); |
| 47 |
assert_eq!(body.attr("class"), None); |
| 48 |
assert_eq!(foo.attr("id"), None); |
| 49 |
assert_eq!(bar.attr("id"), None); |
| 50 |
assert_eq!(baz.attr("id"), None); |
| 51 |
assert_eq!(quux.attr("id"), None); |
| 52 |
assert_eq!(quux.attr("class"), Some("another-thing")); |
| 53 |
|
| 54 |
|
| 55 |
let attr = { |
| 56 |
html.clone().attr("id") |
| 57 |
}; |
| 58 |
assert_eq!(attr, None); |
| 59 |
} |
| 60 |
|
| 61 |
test "Node::raw()" { |
| 62 |
|
| 63 |
let raw = { |
| 64 |
&html.raw().data |
| 65 |
}; |
| 66 |
} |
| 67 |
|
| 68 |
test "Node::data()" { |
| 69 |
|
| 70 |
let data = { |
| 71 |
html.data() |
| 72 |
}; |
| 73 |
} |
| 74 |
|
| 75 |
test "Node::parent()" { |
| 76 |
assert_eq!(html.parent(), None); |
| 77 |
assert_eq!(head.parent(), Some(html)); |
| 78 |
assert_eq!(body.parent(), Some(html)); |
| 79 |
assert_eq!(foo.parent(), Some(body)); |
| 80 |
assert_eq!(bar.parent(), Some(body)); |
| 81 |
assert_eq!(baz.parent(), Some(bar)); |
| 82 |
assert_eq!(quux.parent(), Some(bar)); |
| 83 |
} |
| 84 |
|
| 85 |
test "Node::prev() / Node::next()" { |
| 86 |
assert_eq!(html.prev(), None); |
| 87 |
assert_eq!(html.next(), None); |
| 88 |
assert_eq!(head.prev(), None); |
| 89 |
assert_eq!(head.next(), Some(body)); |
| 90 |
assert_eq!(body.prev(), Some(head)); |
| 91 |
assert_eq!(body.next(), None); |
| 92 |
assert_eq!(foo.prev(), None); |
| 93 |
assert_eq!(foo.next(), Some(bar)); |
| 94 |
assert_eq!(bar.prev(), Some(foo)); |
| 95 |
assert_eq!(bar.next(), None); |
| 96 |
assert_eq!(baz.prev(), None); |
| 97 |
assert_eq!(baz.next(), Some(quux)); |
| 98 |
assert_eq!(quux.prev(), Some(baz)); |
| 99 |
assert_eq!(quux.next(), None); |
| 100 |
} |
| 101 |
|
| 102 |
test "Node::first_child()" { |
| 103 |
for i in 0..document.nodes.len() { |
| 104 |
let node = document.nth(i).unwrap(); |
| 105 |
assert_eq!(node.first_child(), node.children().next()); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
test "Node::last_child()" { |
| 110 |
for i in 0..document.nodes.len() { |
| 111 |
let node = document.nth(i).unwrap(); |
| 112 |
assert_eq!(node.last_child(), node.children().last()); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
test "Node::text()" { |
| 117 |
assert_eq!(html.text(), "foobaz"); |
| 118 |
assert_eq!(head.text(), ""); |
| 119 |
assert_eq!(body.text(), "foobaz"); |
| 120 |
assert_eq!(foo.text(), "foo"); |
| 121 |
assert_eq!(bar.text(), "baz"); |
| 122 |
assert_eq!(baz.text(), "baz"); |
| 123 |
assert_eq!(quux.text(), ""); |
| 124 |
} |
| 125 |
|
| 126 |
test "Node::find()" { |
| 127 |
{ |
| 128 |
use select::predicate::*; |
| 129 |
let document = Document::from(include_str!("fixtures/struct.Vec.html")); |
| 130 |
let main = document.find(Attr("id", "main")).next().unwrap(); |
| 131 |
|
| 132 |
let (div, span) = (Name("div"), Name("span")); |
| 133 |
|
| 134 |
assert_eq!(main.find(div).count(), 204); |
| 135 |
assert_eq!(main.find(span).count(), 1785); |
| 136 |
assert_eq!(main.find(div.child(div).descendant(span.child(span))).count(), 3); |
| 137 |
assert_eq!(main.find(div.child(div).descendant(span).child(span)).count(), 3); |
| 138 |
}; |
| 139 |
} |
| 140 |
|
| 141 |
test "Node::is()" { |
| 142 |
{ |
| 143 |
use select::predicate::*; |
| 144 |
let document = Document::from(include_str!("fixtures/struct.Vec.html")); |
| 145 |
for div in document.find(Name("div")) { |
| 146 |
assert!(div.is(Name("div"))); |
| 147 |
} |
| 148 |
}; |
| 149 |
} |
| 150 |
|
| 151 |
test "Node::as_text()" { |
| 152 |
assert_eq!(foo.as_text(), Some("foo")); |
| 153 |
assert_eq!(bar.as_text(), None); |
| 154 |
assert_eq!(baz.as_text(), Some("baz")); |
| 155 |
|
| 156 |
|
| 157 |
let text = { |
| 158 |
foo.as_text() |
| 159 |
}; |
| 160 |
assert_eq!(text, Some("foo")); |
| 161 |
} |
| 162 |
|
| 163 |
test "Node::as_comment()" { |
| 164 |
assert_eq!(foo.as_comment(), None); |
| 165 |
assert_eq!(comment.as_comment(), Some("comment")); |
| 166 |
|
| 167 |
|
| 168 |
let comment = { |
| 169 |
comment.as_comment() |
| 170 |
}; |
| 171 |
assert_eq!(comment, Some("comment")); |
| 172 |
} |
| 173 |
|
| 174 |
test "Node::html()" { |
| 175 |
assert_eq!(html.html(), "<html><head></head><body id=\"something\">\ |
| 176 |
foo<bar>baz<quux class=\"another-thing\">\ |
| 177 |
<!--comment--></quux></bar></body></html>"); |
| 178 |
assert_eq!(head.html(), "<head></head>"); |
| 179 |
assert_eq!(foo.html(), "foo"); |
| 180 |
assert_eq!(quux.html(), "<quux class=\"another-thing\"><!--comment--></quux>"); |
| 181 |
assert_eq!(comment.html(), "<!--comment-->"); |
| 182 |
|
| 183 |
let document = Document::from("<div a=b c=d e=f g=h i=j>"); |
| 184 |
let div = document.nth(3).unwrap(); |
| 185 |
assert_eq!(div.name(), Some("div")); |
| 186 |
assert_eq!(div.html(), r#"<div a="b" c="d" e="f" g="h" i="j"></div>"#); |
| 187 |
} |
| 188 |
|
| 189 |
test "Node::inner_html()" { |
| 190 |
assert_eq!(html.inner_html(), "<head></head><body id=\"something\">\ |
| 191 |
foo<bar>baz<quux class=\"another-thing\">\ |
| 192 |
<!--comment--></quux></bar></body>"); |
| 193 |
assert_eq!(head.inner_html(), ""); |
| 194 |
assert_eq!(foo.inner_html(), ""); |
| 195 |
assert_eq!(quux.inner_html(), "<!--comment-->"); |
| 196 |
assert_eq!(comment.inner_html(), ""); |
| 197 |
} |
| 198 |
|
| 199 |
test "Node::children()" { |
| 200 |
let mut children = html.children(); |
| 201 |
assert_eq!(children.next().unwrap().name(), Some("head")); |
| 202 |
assert_eq!(children.next().unwrap().name(), Some("body")); |
| 203 |
assert_eq!(children.next(), None); |
| 204 |
|
| 205 |
assert_eq!(body.children().count(), 2); |
| 206 |
|
| 207 |
assert_eq!(baz.children().count(), 0); |
| 208 |
|
| 209 |
assert_eq!(quux.children().count(), 1); |
| 210 |
} |
| 211 |
|
| 212 |
test "Node::descendants()" { |
| 213 |
use select::predicate::*; |
| 214 |
let document = Document::from(include_str!("fixtures/struct.Vec.html")); |
| 215 |
for i in 0..document.nodes.len() { |
| 216 |
let node = document.nth(i).unwrap(); |
| 217 |
let actual = node.descendants().map(|node| node.index()).collect::<Vec<_>>(); |
| 218 |
let expected = node.find(Any).map(|node| node.index()).collect::<Vec<_>>(); |
| 219 |
assert_eq!(actual, expected); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
test "Node::attrs()" { |
| 224 |
let mut attrs = quux.attrs(); |
| 225 |
assert_eq!(attrs.next(), Some(("class", "another-thing"))); |
| 226 |
assert_eq!(attrs.next(), None); |
| 227 |
} |
| 228 |
|
| 229 |
test "std::fmt::Debug for Node" { |
| 230 |
assert_eq!(format!("{:?}", bar).replace(' ', ""), r#"Element { |
| 231 |
name: "bar", |
| 232 |
attrs: [], |
| 233 |
children: [ |
| 234 |
Text("baz"), |
| 235 |
Element { |
| 236 |
name: "quux", |
| 237 |
attrs: [("class", "another-thing")], |
| 238 |
children: [Comment("comment")] |
| 239 |
} |
| 240 |
]}"#.replace(['\n', ' '], "")); |
| 241 |
|
| 242 |
assert_eq!(format!("{:?}", baz), "Text(\"baz\")"); |
| 243 |
|
| 244 |
assert_eq!(format!("{:?}", quux).replace(' ', ""), r#"Element { |
| 245 |
name: "quux", |
| 246 |
attrs: [("class", "another-thing")], |
| 247 |
children: [Comment("comment")] |
| 248 |
}"#.replace(['\n', ' '], "")); |
| 249 |
|
| 250 |
assert_eq!(format!("{:?}", comment), "Comment(\"comment\")"); |
| 251 |
} |
| 252 |
|
| 253 |
test "Children::into_selection()" { |
| 254 |
let document = Document::from(include_str!("fixtures/struct.Vec.html")); |
| 255 |
for i in 0..document.nodes.len() { |
| 256 |
let node = document.nth(i).unwrap(); |
| 257 |
let actual = node.children().into_selection().iter().map(|node| { |
| 258 |
node.index() |
| 259 |
}).collect::<Vec<_>>(); |
| 260 |
let expected = node.children().map(|node| node.index()).collect::<Vec<_>>(); |
| 261 |
assert_eq!(actual, expected); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
test "issue #38" { |
| 267 |
{ |
| 268 |
use select::predicate::*; |
| 269 |
let _bar = { |
| 270 |
let body = html.find(Name("body")).next().unwrap(); |
| 271 |
body.find(Name("bar")).next().unwrap() |
| 272 |
}; |
| 273 |
} |
| 274 |
} |
| 275 |
} |
| 276 |
} |