xrust/testutils/
item_node.rs1#[macro_export]
2macro_rules! item_node_tests (
3 ( $x:expr, $y:expr, $z:expr ) => {
4 use std::cmp::Ordering;
5 use qualname::NcName;
6
7 #[test]
8 fn node_push_content() {
9 let mut d = $x();
10 let n = d.new_element(QName::from_local_name(NcName::try_from("Test").unwrap()))
11 .expect("unable to create element node");
12 d.push(n)
13 .expect("unable to add node");
14 assert_eq!(d.to_xml(), "<Test/>")
15 }
16
17 #[test]
20 fn item_node_type() {
21 assert_eq!(
22 $y(QName::from_local_name(NcName::try_from("Test").unwrap()), Value::from("foobar")).node_type(),
23 NodeType::Document
24 )
25 }
26
27 #[test]
28 fn item_node_name() {
29 let d = $y(QName::from_local_name(NcName::try_from("Test").unwrap()), Value::from("foobar"));
30 match d.child_iter().nth(0) {
31 Some(c) => {
32 assert_eq!(c.node_type(), NodeType::Element);
33 assert_eq!(c.name().map_or(String::from(""), |e| e.local_name().to_string()), "Test")
34 }
35 None => panic!("no toplevel element")
36 }
37 }
38
39 #[test]
40 fn item_node_value() {
41 let d = $y(QName::from_local_name(NcName::try_from("Test").unwrap()), Value::from("foobar"));
42 match d.child_iter().nth(0) {
43 Some(c) => {
44 assert_eq!(c.node_type(), NodeType::Element);
45 assert_eq!(c.name().map_or(String::from(""), |e| e.local_name().to_string()), "Test");
46 let mut it = c.child_iter();
47 match it.next() {
48 Some(t) => {
49 assert_eq!(t.node_type(), NodeType::Text);
50 assert_eq!(t.value().to_string(), "foobar");
51 match it.next() {
52 Some(_) => panic!("unexpected child node"),
53 None => assert!(true)
54 }
55 }
56 None => panic!("root element does not have child node")
57 }
58 }
59 None => panic!("no toplevel element")
60 }
61 }
62
63 #[test]
64 fn item_node_pop() {
65 let mut d = $x();
66 let mut new = d.new_element(QName::from_local_name(NcName::try_from("Test").unwrap()))
67 .expect("unable to create element node");
68 d.push(new.clone())
69 .expect("unable to add node");
70 assert_eq!(d.to_xml(), "<Test/>");
71 let mut e = d.new_element(QName::from_local_name(NcName::try_from("Foo").unwrap()))
72 .expect("unable to create element node");
73 new.push(e.clone())
74 .expect("unable to add node");
75 let mut f = d.new_element(QName::from_local_name(NcName::try_from("Bar").unwrap()))
76 .expect("unable to create element node");
77 e.push(f)
78 .expect("unable to add node");
79 assert_eq!(d.to_xml(), "<Test><Foo><Bar/></Foo></Test>");
80 e.pop()
81 .expect("unable to remove node");
82 assert_eq!(d.to_xml(), "<Test/>")
83 }
84
85 #[test]
86 fn item_node_insert_before() {
87 let mut d = $x();
88 let mut new = d.new_element(QName::from_local_name(NcName::try_from("Test").unwrap()))
89 .expect("unable to create element node");
90 d.push(new.clone())
91 .expect("unable to add node");
92 assert_eq!(d.to_xml(), "<Test/>");
93 let mut e = d.new_element(QName::from_local_name(NcName::try_from("Foo").unwrap()))
94 .expect("unable to create element node");
95 new.push(e.clone())
96 .expect("unable to add node");
97 let mut f = d.new_element(QName::from_local_name(NcName::try_from("Bar").unwrap()))
98 .expect("unable to create element node");
99 e.push(f.clone())
100 .expect("unable to add node");
101 assert_eq!(d.to_xml(), "<Test><Foo><Bar/></Foo></Test>");
102 let g = d.new_element(QName::from_local_name(NcName::try_from("Inserted").unwrap()))
103 .expect("unable to create element node");
104 f.insert_before(g)
105 .expect("unable to insert element");
106 assert_eq!(d.to_xml(), "<Test><Foo><Inserted/><Bar/></Foo></Test>")
107 }
108
109 #[test]
110 fn item_node_to_string_doc() {
111 let d = $y(QName::from_local_name(NcName::try_from("Test").unwrap()), Value::from("foobar"));
112 assert_eq!(d.to_string(), "foobar")
113 }
114
115 #[test]
116 fn item_node_to_xml_doc() {
117 let d = $y(QName::from_local_name(NcName::try_from("Test").unwrap()), Value::from("foobar"));
118 assert_eq!(d.to_xml(), "<Test>foobar</Test>")
119 }
120
121 #[test]
122 fn item_node_parent() {
123 let mut sd = $x();
124 let mut t = sd.new_element(QName::from_local_name(NcName::try_from("Test").unwrap()))
125 .expect("unable to create element");
126 sd.push(t.clone())
127 .expect("unable to append child");
128 let mut l1 = sd.new_element(QName::from_local_name(NcName::try_from("Level-1").unwrap()))
129 .expect("unable to create element");
130 t.push(l1.clone())
131 .expect("unable to append child");
132 let l2 = sd.new_element(QName::from_local_name(NcName::try_from("Level-2").unwrap()))
133 .expect("unable to create element");
134 l1.push(l2.clone())
135 .expect("unable to append child");
136 assert_eq!(l2.parent().unwrap().name().map(|e| e.local_name().to_string()),
137 Some("Level-1".to_string()))
138 }
139
140 #[test]
141 fn item_node_ancestor() {
142 let mut sd = $x();
143 let mut t = sd.new_element(QName::from_local_name(NcName::try_from("Test").unwrap()))
144 .expect("unable to create element");
145 sd.push(t.clone())
146 .expect("unable to append child");
147 let mut l1 = sd.new_element(QName::from_local_name(NcName::try_from("Level-1").unwrap()))
148 .expect("unable to create element");
149 t.push(l1.clone())
150 .expect("unable to append child");
151 let mut l2 = sd.new_element(QName::from_local_name(NcName::try_from("Level-2").unwrap()))
152 .expect("unable to create element");
153 l1.push(l2.clone())
154 .expect("unable to append child");
155 let leaf = sd.new_text(Rc::new(Value::from("leaf node")))
156 .expect("unable to create text node");
157 l2.push(leaf.clone())
158 .expect("unable to append child");
159 let mut aiter = leaf.ancestor_iter();
160 assert_eq!(aiter.next().unwrap().name().unwrap().local_name().to_string(), "Level-2");
161 assert_eq!(aiter.next().unwrap().name().unwrap().local_name().to_string(), "Level-1");
162 assert_eq!(aiter.next().unwrap().name().unwrap().local_name().to_string(), "Test");
163 assert_eq!(aiter.next().unwrap().node_type(), NodeType::Document);
164 match aiter.next() {
165 None => {},
166 _ => panic!("iterator should have no more items")
167 }
168 }
169 #[test]
170 fn item_node_owner_doc() {
171 let mut sd = $x();
172 let mut t = sd.new_element(QName::from_local_name(NcName::try_from("Test").unwrap()))
173 .expect("unable to create element");
174 sd.push(t.clone())
175 .expect("unable to append child");
176 let mut l1 = sd.new_element(QName::from_local_name(NcName::try_from("Level-1").unwrap()))
177 .expect("unable to create element");
178 t.push(l1.clone())
179 .expect("unable to append child");
180 let mut l2 = sd.new_element(QName::from_local_name(NcName::try_from("Level-2").unwrap()))
181 .expect("unable to create element");
182 l1.push(l2.clone())
183 .expect("unable to append child");
184 let leaf = sd.new_text(Rc::new(Value::from("leaf node")))
185 .expect("unable to create text node");
186 l2.push(leaf.clone())
187 .expect("unable to append child");
188 let od = leaf.owner_document();
189 assert_eq!(od.node_type(), NodeType::Document);
190 }
191 #[test]
192 fn item_node_owner_doc_root() {
193 let mut sd = $x();
194 let mut t = sd.new_element(QName::from_local_name(NcName::try_from("Test").unwrap()))
195 .expect("unable to create element");
196 sd.push(t.clone())
197 .expect("unable to append child");
198 let mut l1 = sd.new_element(QName::from_local_name(NcName::try_from("Level-1").unwrap()))
199 .expect("unable to create element");
200 t.push(l1.clone())
201 .expect("unable to append child");
202 let mut l2 = sd.new_element(QName::from_local_name(NcName::try_from("Level-2").unwrap()))
203 .expect("unable to create element");
204 l1.push(l2.clone())
205 .expect("unable to append child");
206 let leaf = sd.new_text(Rc::new(Value::from("leaf node")))
207 .expect("unable to create text node");
208 l2.push(leaf.clone())
209 .expect("unable to append child");
210 let od = sd.owner_document();
211 assert_eq!(od.node_type(), NodeType::Document);
212 }
213
214 #[test]
215 fn item_node_children() {
216 let mut sd = $x();
217 let mut t = sd.new_element(QName::from_local_name(NcName::try_from("Test").unwrap()))
218 .expect("unable to create element");
219 sd.push(t.clone())
220 .expect("unable to append child");
221 let mut l1 = sd.new_element(QName::from_local_name(NcName::try_from("Level-1").unwrap()))
222 .expect("unable to create element");
223 t.push(l1.clone())
224 .expect("unable to append child");
225 let mut l2 = sd.new_element(QName::from_local_name(NcName::try_from("Level-2").unwrap()))
226 .expect("unable to create element");
227 t.push(l2.clone())
228 .expect("unable to append child");
229 let leaf = sd.new_text(Rc::new(Value::from("leaf node")))
230 .expect("unable to create text node");
231 t.push(leaf.clone())
232 .expect("unable to append child");
233 let mut citer = t.child_iter();
234 assert_eq!(citer.next().unwrap().name().unwrap().local_name().to_string(), "Level-1");
235 assert_eq!(citer.next().unwrap().name().unwrap().local_name().to_string(), "Level-2");
236 assert_eq!(citer.next().unwrap().value().to_string(), "leaf node");
237 match citer.next() {
238 None => {},
239 _ => panic!("iterator should have no more items")
240 }
241 }
242
243 #[test]
244 fn item_node_first_child() {
245 let mut sd = $x();
246 let mut t = sd.new_element(QName::from_local_name(NcName::try_from("Test").unwrap()))
247 .expect("unable to create element");
248 sd.push(t.clone())
249 .expect("unable to append child");
250 let mut l1 = sd.new_element(QName::from_local_name(NcName::try_from("Level-1").unwrap()))
251 .expect("unable to create element");
252 t.push(l1.clone())
253 .expect("unable to append child");
254 let mut l2 = sd.new_element(QName::from_local_name(NcName::try_from("Level-2").unwrap()))
255 .expect("unable to create element");
256 t.push(l2.clone())
257 .expect("unable to append child");
258 let leaf = sd.new_text(Rc::new(Value::from("leaf node")))
259 .expect("unable to create text node");
260 t.push(leaf.clone())
261 .expect("unable to append child");
262 match t.first_child() {
263 Some(f) => assert_eq!(f.name().unwrap().local_name().to_string(), "Level-1"),
264 None => panic!("no first child")
265 }
266 }
267
268 #[test]
269 fn item_node_descend() {
270 let mut sd = $x();
271 let mut t = sd.new_element(QName::from_local_name(NcName::try_from("Test").unwrap()))
272 .expect("unable to create element");
273 sd.push(t.clone())
274 .expect("unable to append child");
275 let mut l1a = sd.new_element(QName::from_local_name(NcName::try_from("A").unwrap()))
276 .expect("unable to create element");
277 t.push(l1a.clone())
278 .expect("unable to append child");
279 let mut l1b = sd.new_element(QName::from_local_name(NcName::try_from("B").unwrap()))
280 .expect("unable to create element");
281 t.push(l1b.clone())
282 .expect("unable to append child");
283
284 let mut l2aa = sd.new_element(QName::from_local_name(NcName::try_from("A").unwrap()))
285 .expect("unable to create element");
286 l1a.push(l2aa.clone())
287 .expect("unable to append child");
288 l2aa.push(
289 sd.new_text(Rc::new(Value::from("AA")))
290 .expect("unable to create text")
291 ).expect("unable to append text");
292 let mut l2ab = sd.new_element(QName::from_local_name(NcName::try_from("B").unwrap()))
293 .expect("unable to create element");
294 l1a.push(l2ab.clone())
295 .expect("unable to append child");
296 l2ab.push(
297 sd.new_text(Rc::new(Value::from("AB")))
298 .expect("unable to create text")
299 ).expect("unable to append text");
300
301 let mut l2ba = sd.new_element(QName::from_local_name(NcName::try_from("A").unwrap()))
302 .expect("unable to create element");
303 l1b.push(l2ba.clone())
304 .expect("unable to append child");
305 l2ba.push(
306 sd.new_text(Rc::new(Value::from("BA")))
307 .expect("unable to create text")
308 ).expect("unable to append text");
309 let mut l2bb = sd.new_element(QName::from_local_name(NcName::try_from("B").unwrap()))
310 .expect("unable to create element");
311 l1b.push(l2bb.clone())
312 .expect("unable to append child");
313 l2bb.push(
314 sd.new_text(Rc::new(Value::from("BB")))
315 .expect("unable to create text")
316 ).expect("unable to append text");
317
318 let mut diter = t.descend_iter();
319 assert_eq!(diter.next().unwrap().name().unwrap().local_name().to_string(), "A");
320 assert_eq!(diter.next().unwrap().name().unwrap().local_name().to_string(), "A");
321 assert_eq!(diter.next().unwrap().value().to_string(), "AA");
322 assert_eq!(diter.next().unwrap().name().unwrap().local_name().to_string(), "B");
323 assert_eq!(diter.next().unwrap().value().to_string(), "AB");
324 assert_eq!(diter.next().unwrap().name().unwrap().local_name().to_string(), "B");
325 assert_eq!(diter.next().unwrap().name().unwrap().local_name().to_string(), "A");
326 assert_eq!(diter.next().unwrap().value().to_string(), "BA");
327 assert_eq!(diter.next().unwrap().name().unwrap().local_name().to_string(), "B");
328 assert_eq!(diter.next().unwrap().value().to_string(), "BB");
329 match diter.next() {
330 None => {},
331 _ => panic!("iterator should have no more items")
332 }
333 }
334
335 #[test]
336 fn item_node_next() {
337 let mut sd = $x();
338 let mut t = sd.new_element(QName::from_local_name(NcName::try_from("Test").unwrap()))
339 .expect("unable to create element");
340 sd.push(t.clone())
341 .expect("unable to append child");
342 let mut l1 = sd.new_element(QName::from_local_name(NcName::try_from("Level-1").unwrap()))
343 .expect("unable to create element");
344 t.push(l1.clone())
345 .expect("unable to append child");
346 let mut l2 = sd.new_element(QName::from_local_name(NcName::try_from("Level-2").unwrap()))
347 .expect("unable to create element");
348 t.push(l2.clone())
349 .expect("unable to append child");
350 let leaf = sd.new_text(Rc::new(Value::from("leaf node")))
351 .expect("unable to create text node");
352 t.push(leaf.clone())
353 .expect("unable to append child");
354 let mut niter = l1.next_iter();
355 assert_eq!(niter.next().unwrap().name().unwrap().local_name().to_string(), "Level-2");
356 assert_eq!(niter.next().unwrap().value().to_string(), "leaf node");
357 match niter.next() {
358 None => {},
359 _ => panic!("iterator should have no more items")
360 }
361 }
362
363 #[test]
364 fn item_node_prev() {
365 let mut sd = $x();
366 let mut t = sd.new_element(QName::from_local_name(NcName::try_from("Test").unwrap()))
367 .expect("unable to create element");
368 sd.push(t.clone())
369 .expect("unable to append child");
370 let mut l1 = sd.new_element(QName::from_local_name(NcName::try_from("Level-1").unwrap()))
371 .expect("unable to create element");
372 t.push(l1.clone())
373 .expect("unable to append child");
374 let mut l2 = sd.new_element(QName::from_local_name(NcName::try_from("Level-2").unwrap()))
375 .expect("unable to create element");
376 t.push(l2.clone())
377 .expect("unable to append child");
378 let leaf = sd.new_text(Rc::new(Value::from("leaf node")))
379 .expect("unable to create text node");
380 t.push(leaf.clone())
381 .expect("unable to append child");
382 let mut piter = leaf.prev_iter();
383 assert_eq!(piter.next().unwrap().name().unwrap().local_name().to_string(), "Level-2");
384 assert_eq!(piter.next().unwrap().name().unwrap().local_name().to_string(), "Level-1");
385 match piter.next() {
386 None => {},
387 _ => panic!("iterator should have no more items")
388 }
389 }
390
391 #[test]
392 fn item_node_attr() {
393 let mut sd = $x();
394 let mut t = sd.new_element(QName::from_local_name(NcName::try_from("Test").unwrap()))
395 .expect("unable to create element");
396 sd.push(t.clone())
397 .expect("unable to append child");
398 let a1 = sd.new_attribute(
399 QName::from_local_name(NcName::try_from("role").unwrap()),
400 Rc::new(Value::from("testing"))
401 ).expect("unable to create attribute");
402 t.add_attribute(a1)
403 .expect("unable to add attribute");
404 let a2 = sd.new_attribute(
405 QName::from_local_name(NcName::try_from("phase").unwrap()),
406 Rc::new(Value::from("one"))
407 ).expect("unable to create element");
408 t.add_attribute(a2)
409 .expect("unable to add attribute");
410
411 assert!(
413 sd.to_xml() == "<Test role='testing' phase='one'/>" ||
414 sd.to_xml() == "<Test phase='one' role='testing'/>"
415 );
416 let mut aiter = t.attribute_iter();
417 let v = aiter.next().unwrap().name().unwrap().local_name().to_string();
418 if v == "role" {
419 assert_eq!(aiter.next().unwrap().name().unwrap().local_name().to_string(), "phase");
420 } else if v == "phase" {
421 assert_eq!(aiter.next().unwrap().name().unwrap().local_name().to_string(), "role");
422 } else {
423 panic!("unexpected attribute value")
424 }
425 match aiter.next() {
426 None => {},
427 _ => panic!("iterator should have no more items")
428 }
429 }
430
431 #[test]
432 fn item_node_shallow_copy_element() {
433 let mut sd = $x();
434 let mut t = sd.new_element(QName::from_local_name(NcName::try_from("Test").unwrap()))
435 .expect("unable to create element");
436 sd.push(t.clone())
437 .expect("unable to append child");
438 let l = sd.new_element(QName::from_local_name(NcName::try_from("content").unwrap()))
439 .expect("unable to create element");
440 t.push(l)
441 .expect("unable to append child");
442 let it = Item::Node(t.clone());
443 let u = it.shallow_copy().expect("unable to shallow copy element");
444 assert_eq!(t.to_xml(), "<Test><content/></Test>");
445 assert_eq!(u.to_xml(), "<Test/>");
446 }
447 #[test]
448 fn item_node_cmp_doc_order_1() {
449 let sd = $z();
450 let b1: Vec<RNode> = sd.descend_iter().filter(|n| n.get_attribute(&QName::from_local_name(NcName::try_from("id").unwrap())).to_string() == String::from("b1")).collect();
451 let b9: Vec<RNode> = sd.descend_iter().filter(|n| n.get_attribute(&QName::from_local_name(NcName::try_from("id").unwrap())).to_string() == String::from("b9")).collect();
452 assert_eq!(b1[0].cmp_document_order(&b9[0]), Ordering::Less)
453 }
454 #[test]
455 fn item_node_cmp_doc_order_2() {
456 let sd = $z();
457 let b10: Vec<RNode> = sd.descend_iter().filter(|n| n.get_attribute(&QName::from_local_name(NcName::try_from("id").unwrap())).to_string() == String::from("b10")).collect();
458 let b6: Vec<RNode> = sd.descend_iter().filter(|n| n.get_attribute(&QName::from_local_name(NcName::try_from("id").unwrap())).to_string() == String::from("b6")).collect();
459 assert_eq!(b10[0].cmp_document_order(&b6[0]), Ordering::Greater)
460 }
461
462 #[test]
463 fn item_node_partialeq_1_pos() {
464 let mut sd = $x();
465 let mut t = sd.new_element(QName::from_local_name(NcName::try_from("Test").unwrap()))
466 .expect("unable to create element");
467 sd.push(t.clone())
468 .expect("unable to append child");
469 let a1 = sd.new_attribute(
470 QName::from_local_name(NcName::try_from("role").unwrap()),
471 Rc::new(Value::from("testing"))
472 ).expect("unable to create attribute");
473 t.add_attribute(a1)
474 .expect("unable to add attribute");
475 let a2 = sd.new_attribute(
476 QName::from_local_name(NcName::try_from("phase").unwrap()),
477 Rc::new(Value::from("one"))
478 ).expect("unable to create element");
479 t.add_attribute(a2)
480 .expect("unable to add attribute");
481 t.push(sd.new_text(Rc::new(Value::from("my test document"))).expect("unable to create text node"))
482 .expect("unable to add text node");
483
484 let mut od = $x();
486 let mut u = od.new_element(QName::from_local_name(NcName::try_from("Test").unwrap()))
487 .expect("unable to create element");
488 od.push(u.clone())
489 .expect("unable to append child");
490 let b1 = od.new_attribute(
491 QName::from_local_name(NcName::try_from("role").unwrap()),
492 Rc::new(Value::from("testing"))
493 ).expect("unable to create attribute");
494 let b2 = od.new_attribute(
495 QName::from_local_name(NcName::try_from("phase").unwrap()),
496 Rc::new(Value::from("one"))
497 ).expect("unable to create element");
498 u.add_attribute(b2)
499 .expect("unable to add attribute");
500 u.push(od.new_text(Rc::new(Value::from("my test document"))).expect("unable to create text node"))
501 .expect("unable to add text node");
502 u.add_attribute(b1)
503 .expect("unable to add attribute");
504
505 assert_eq!(sd == od, true)
506 }
507 #[test]
508 fn item_node_partialeq_1_neg() {
509 let mut sd = $x();
510 let mut t = sd.new_element(QName::from_local_name(NcName::try_from("Test").unwrap()))
511 .expect("unable to create element");
512 sd.push(t.clone())
513 .expect("unable to append child");
514 let a1 = sd.new_attribute(
515 QName::from_local_name(NcName::try_from("role").unwrap()),
516 Rc::new(Value::from("testing"))
517 ).expect("unable to create attribute");
518 t.add_attribute(a1)
519 .expect("unable to add attribute");
520 let a2 = sd.new_attribute(
521 QName::from_local_name(NcName::try_from("phase").unwrap()),
522 Rc::new(Value::from("one"))
523 ).expect("unable to create element");
524 t.add_attribute(a2)
525 .expect("unable to add attribute");
526 t.push(sd.new_text(Rc::new(Value::from("my test document"))).expect("unable to create text node"))
527 .expect("unable to add text node");
528
529 let mut od = $x();
531 let mut u = od.new_element(QName::from_local_name(NcName::try_from("Test").unwrap()))
532 .expect("unable to create element");
533 od.push(u.clone())
534 .expect("unable to append child");
535 let b1 = od.new_attribute(
536 QName::from_local_name(NcName::try_from("role").unwrap()),
537 Rc::new(Value::from("testing"))
538 ).expect("unable to create attribute");
539 let b2 = od.new_attribute(
540 QName::from_local_name(NcName::try_from("phase").unwrap()),
541 Rc::new(Value::from("one"))
542 ).expect("unable to create element");
543 u.add_attribute(b2)
544 .expect("unable to add attribute");
545 u.push(od.new_text(Rc::new(Value::from("not the same document"))).expect("unable to create text node"))
546 .expect("unable to add text node");
547 u.add_attribute(b1)
548 .expect("unable to add attribute");
549
550 assert_eq!(sd == od, false)
551 }
552 }
553);