Debian Codegraph @

tests/tree_tests.rs

raw symbols SCIP
1 //! Tree module tests
2 //!
3
4 use libxml::parser::Parser;
5 use libxml::tree::{Document, Namespace, Node, NodeType};
6
7 #[test]
8 /// Root node and first child of root node are different
9 /// (There is a tiny chance this might fail for a correct program)
10 fn child_of_root_has_different_hash() {
11 let parser = Parser::default();
12 {
13 let doc_result = parser.parse_file("tests/resources/file01.xml");
14 assert!(doc_result.is_ok());
15 let doc = doc_result.unwrap();
16 let root = doc.get_root_element().unwrap();
17 assert!(!root.is_text_node());
18 match root.get_first_child() { Some(child) => {
19 assert!(root != child);
20 } _ => {
21 unreachable!("test failed - first child doesn't exist");
22 }}
23 // same check with last child
24 match root.get_last_child() { Some(child) => {
25 assert!(root != child);
26 } _ => {
27 unreachable!("test failed - last child doesn't exist");
28 }}
29 }
30 }
31
32 #[test]
33 /// Siblings basic unit tests
34 fn node_sibling_accessors() {
35 let mut doc = Document::new().unwrap();
36 let hello_element_result = Node::new("hello", None, &doc);
37 assert!(hello_element_result.is_ok());
38 let mut hello_element = hello_element_result.unwrap();
39 doc.set_root_element(&hello_element);
40
41 let mut new_sibling = Node::new("sibling", None, &doc).unwrap();
42 assert!(hello_element.add_prev_sibling(&mut new_sibling).is_ok());
43 }
44
45 #[test]
46 /// Constructing a comment node, attaching it, and serializing it
47 fn node_new_comment() {
48 let mut doc = Document::new().unwrap();
49 let mut root = Node::new("root", None, &doc).unwrap();
50 doc.set_root_element(&root);
51
52 let mut comment = Node::new_comment(" hello ", &doc).unwrap();
53 assert_eq!(comment.get_type(), Some(NodeType::CommentNode));
54 assert!(root.add_child(&mut comment).is_ok());
55
56 let serialized = doc.to_string();
57 assert!(
58 serialized.contains("<!-- hello -->"),
59 "expected comment in serialized doc, got: {serialized}"
60 );
61
62 // Embedded NUL should return Err rather than panic.
63 assert!(Node::new_comment("bad\0content", &doc).is_err());
64 }
65
66 #[test]
67 /// `Document::remove_internal_subset` strips the DOCTYPE preamble
68 /// from a parsed document, mirroring XML::LibXML's
69 /// `removeInternalSubset`.
70 fn document_remove_internal_subset() {
71 let parser = Parser::default();
72 let xml = r#"<?xml version="1.0"?>
73 <!DOCTYPE root SYSTEM "example.dtd">
74 <root><child>hi</child></root>"#;
75 let mut doc = parser.parse_string(xml).expect("parse");
76
77 // Before removal: serialisation includes the DOCTYPE.
78 let before = doc.to_string();
79 assert!(
80 before.contains("<!DOCTYPE"),
81 "expected DOCTYPE in serialized doc before remove, got: {before}"
82 );
83
84 doc.remove_internal_subset();
85
86 // After: no DOCTYPE preamble, root element untouched.
87 let after = doc.to_string();
88 assert!(
89 !after.contains("<!DOCTYPE"),
90 "DOCTYPE still present after remove_internal_subset: {after}"
91 );
92 assert!(after.contains("<root>"));
93 assert!(after.contains("<child>hi</child>"));
94
95 // Idempotent: second call on a subset-less doc is a no-op.
96 doc.remove_internal_subset();
97 let after2 = doc.to_string();
98 assert_eq!(after, after2);
99 }
100
101 #[test]
102 /// `Document::remove_internal_subset` on a doc without a DTD is a no-op.
103 fn document_remove_internal_subset_no_doctype_noop() {
104 let parser = Parser::default();
105 let mut doc = parser
106 .parse_string(r#"<?xml version="1.0"?><root/>"#)
107 .expect("parse");
108 let before = doc.to_string();
109 doc.remove_internal_subset();
110 let after = doc.to_string();
111 assert_eq!(before, after);
112 }
113
114 #[test]
115 fn node_children_accessors() {
116 // Setup
117 let parser = Parser::default();
118 let doc_result = parser.parse_file("tests/resources/file01.xml");
119 assert!(doc_result.is_ok());
120 let doc = doc_result.unwrap();
121 let root = doc.get_root_element().unwrap();
122
123 // Tests
124 let root_children = root.get_child_nodes();
125 assert_eq!(root_children.len(), 5, "file01 root has five child nodes");
126 let mut element_children = root.get_child_elements();
127 assert_eq!(
128 element_children.len(),
129 2,
130 "file01 root has two child elements"
131 );
132 assert_eq!(element_children.pop().unwrap().get_name(), "child");
133 assert_eq!(element_children.pop().unwrap().get_name(), "child");
134 assert!(element_children.is_empty());
135 }
136
137 #[test]
138 fn node_attributes_accessor() {
139 // Setup
140 let parser = Parser::default();
141 let doc_result = parser.parse_file("tests/resources/file01.xml");
142 assert!(doc_result.is_ok());
143 let doc = doc_result.unwrap();
144 let root = doc.get_root_element().unwrap();
145 let mut root_elements = root.get_child_elements();
146 let child_opt = root_elements.first_mut();
147 assert!(child_opt.is_some());
148 let child = child_opt.unwrap();
149
150 // All attributes
151 let attributes = child.get_attributes();
152 assert_eq!(attributes.len(), 1);
153 assert_eq!(attributes.get("attribute"), Some(&"value".to_string()));
154
155 // Has
156 assert!(child.has_attribute("attribute"));
157 // Get
158 assert_eq!(child.get_attribute("attribute"), Some("value".to_string()));
159 // Get as node
160 let attr_node_opt = child.get_attribute_node("attribute");
161 assert!(attr_node_opt.is_some());
162 let attr_node = attr_node_opt.unwrap();
163 assert_eq!(attr_node.get_name(), "attribute");
164 assert_eq!(attr_node.get_type(), Some(NodeType::AttributeNode));
165
166 // Set
167 assert!(child.set_attribute("attribute", "setter_value").is_ok());
168 assert_eq!(
169 child.get_attribute("attribute"),
170 Some("setter_value".to_string())
171 );
172 // Remove
173 assert!(child.remove_attribute("attribute").is_ok());
174 assert_eq!(child.get_attribute("attribute"), None);
175 assert!(!child.has_attribute("attribute"));
176 // Recount
177 let attributes = child.get_attributes();
178 assert_eq!(attributes.len(), 0);
179 }
180
181 #[test]
182 fn node_attributes_ns_accessor() {
183 // Setup
184 let parser = Parser::default();
185 let doc_result = parser.parse_file("tests/resources/file01_ns.xml");
186 assert!(doc_result.is_ok());
187 let doc = doc_result.unwrap();
188 let root = doc.get_root_element().unwrap();
189 let mut root_elements = root.get_child_elements();
190 let child_opt = root_elements.first_mut();
191 assert!(child_opt.is_some());
192 let child = child_opt.unwrap();
193
194 // All attributes
195 let attributes = child.get_attributes_ns();
196 assert_eq!(attributes.len(), 3);
197 assert_eq!(
198 attributes.get(&("attribute".to_string(), None)),
199 Some(&"value1".to_string())
200 );
201 let namespaces = child.get_namespaces(&doc);
202 assert_eq!(namespaces.len(), 2);
203 let foo_ns = namespaces[0].clone();
204 let bar_ns = namespaces[1].clone();
205
206 assert_eq!(
207 attributes.get(&("attribute".to_string(), Some(foo_ns.clone()))),
208 Some(&"foo1".to_string())
209 );
210 assert_eq!(
211 attributes.get(&("attr".to_string(), Some(bar_ns.clone()))),
212 Some(&"bar1".to_string())
213 );
214
215 // Has
216 assert!(child.has_attribute("attribute"));
217 assert!(child.has_attribute_no_ns("attribute"));
218 assert!(child.has_attribute_ns("attribute", "http://www.example.com/myns"),);
219 assert!(child.has_attribute("attr"));
220 assert!(!child.has_attribute_no_ns("attr"));
221 assert!(child.has_attribute_ns("attr", "http://www.example.com/myns"));
222
223 // Get
224 assert_eq!(
225 child.get_attribute_no_ns("attribute"),
226 Some("value1".to_string())
227 );
228 assert_eq!(
229 child.get_attribute_ns("attribute", "http://www.example.com/myns"),
230 Some("foo1".to_string())
231 );
232 assert_eq!(
233 child.get_attribute_ns("attr", "http://www.example.com/myns"),
234 Some("bar1".to_string())
235 );
236
237 // Get as node
238 let attr_node_opt = child.get_attribute_node_no_ns("attribute");
239 assert!(attr_node_opt.is_some());
240 let attr_node = attr_node_opt.unwrap();
241 assert_eq!(attr_node.get_name(), "attribute");
242 assert_eq!(attr_node.get_type(), Some(NodeType::AttributeNode));
243 let attr_node_opt = child.get_attribute_node_no_ns("attr");
244 assert!(attr_node_opt.is_none());
245 let attr_node_opt = child.get_attribute_node_ns("attr", "http://www.example.com/myns");
246 assert!(attr_node_opt.is_some());
247 let attr_node = attr_node_opt.unwrap();
248 assert_eq!(attr_node.get_name(), "attr");
249 assert_eq!(attr_node.get_type(), Some(NodeType::AttributeNode));
250
251 // Set
252 assert!(child.set_attribute("attribute", "setter_value").is_ok());
253 assert_eq!(
254 child.get_attribute_no_ns("attribute"),
255 Some("setter_value".to_string())
256 );
257 assert!(child
258 .set_attribute_ns("attribute", "foo_value", &foo_ns)
259 .is_ok());
260 assert_eq!(
261 child.get_attribute_no_ns("attribute"),
262 Some("setter_value".to_string())
263 );
264 // Remove
265 assert!(child.has_attribute_no_ns("attribute"));
266 assert!(child.remove_attribute_no_ns("attribute").is_ok());
267 assert_eq!(child.get_attribute_no_ns("attribute"), None);
268 assert!(!child.has_attribute_no_ns("attribute"));
269 // Recount
270 let attributes = child.get_attributes_ns();
271 assert_eq!(attributes.len(), 2);
272 }
273
274 #[test]
275 fn namespace_partial_eq() {
276 // Setup
277 let parser = Parser::default();
278 let doc_result = parser.parse_file("tests/resources/file01_ns.xml");
279 assert!(doc_result.is_ok());
280 let doc = doc_result.unwrap();
281 let root = doc.get_root_element().unwrap();
282 let mut root_elements = root.get_child_elements();
283 let child1_opt = root_elements.first_mut();
284 assert!(child1_opt.is_some());
285 let child1 = child1_opt.unwrap();
286
287 // Child 1 namespaces
288 let namespaces1 = child1.get_namespaces(&doc);
289 assert_eq!(namespaces1.len(), 2);
290 let foo_ns1 = namespaces1[0].clone();
291 assert_eq!(foo_ns1.get_prefix(), "foo");
292 assert_eq!(foo_ns1.get_href(), "http://www.example.com/myns");
293 let bar_ns1 = namespaces1[1].clone();
294 assert_eq!(bar_ns1.get_prefix(), "bar");
295 assert_eq!(bar_ns1.get_href(), "http://www.example.com/myns");
296 // The current implementation of PartialEq for Namespace compares the prefix
297 // and href
298 assert!(foo_ns1 != bar_ns1);
299
300 // Compare with child2 namespace
301 let child2_opt = child1.get_next_element_sibling();
302 assert!(child2_opt.is_some());
303 let child2 = child2_opt.unwrap();
304 let attributes2 = child2.get_attributes_ns();
305 assert_eq!(attributes2.len(), 2);
306 let namespaces2 = child2.get_namespaces(&doc);
307 assert_eq!(namespaces2.len(), 1);
308 let foo_ns2 = namespaces2[0].clone();
309 // The current implementation of PartialEq for Namespace compares the prefix
310 // and href not the pointer
311 assert!(foo_ns1 == foo_ns2);
312 assert_eq!(foo_ns1.get_href(), foo_ns2.get_href());
313 assert_eq!(foo_ns1.get_prefix(), foo_ns2.get_prefix());
314 assert_ne!(foo_ns1.ns_ptr(), foo_ns2.ns_ptr());
315 }
316
317 #[test]
318 fn attribute_namespace_accessors() {
319 let mut doc = Document::new().unwrap();
320 let element_result = Node::new("example", None, &doc);
321 assert!(element_result.is_ok());
322
323 let mut element = element_result.unwrap();
324 doc.set_root_element(&element);
325
326 let ns_result = Namespace::new(
327 "myxml",
328 "http://www.w3.org/XML/1998/namespace",
329 &mut element,
330 );
331 assert!(ns_result.is_ok());
332 let ns = ns_result.unwrap();
333 assert!(element.set_attribute_ns("id", "testing", &ns).is_ok());
334
335 let id_attr = element.get_attribute_ns("id", "http://www.w3.org/XML/1998/namespace");
336 assert!(id_attr.is_some());
337 assert_eq!(id_attr.unwrap(), "testing");
338
339 let id_regular = element.get_attribute("id");
340 assert!(id_regular.is_some());
341 assert_eq!(id_regular.unwrap(), "testing");
342
343 let id_false_ns = element.get_attribute_ns("id", "http://www.foobar.org");
344 assert!(id_false_ns.is_none());
345 let fb_ns_result = Namespace::new("fb", "http://www.foobar.org", &mut element);
346 assert!(fb_ns_result.is_ok());
347 let fb_ns = fb_ns_result.unwrap();
348 assert!(element.set_attribute_ns("fb", "fb", &fb_ns).is_ok());
349 assert_eq!(
350 element.get_attribute_ns("fb", "http://www.foobar.org"),
351 Some("fb".to_string())
352 );
353 assert!(element
354 .remove_attribute_ns("fb", "http://www.foobar.org")
355 .is_ok());
356 assert_eq!(
357 element.get_attribute_ns("fb", "http://www.foobar.org"),
358 None
359 );
360
361 let ns_prefix = element.lookup_namespace_prefix("http://www.w3.org/XML/1998/namespace");
362 assert_eq!(ns_prefix, Some("xml".to_string())); // system ns has the global prefix when doing global lookup
363 let fb_prefix = element.lookup_namespace_prefix("http://www.foobar.org");
364 assert_eq!(fb_prefix, Some("fb".to_string())); // system ns has the global prefix when doing global lookup
365
366 let ns_uri = element.lookup_namespace_uri("myxml");
367 assert_eq!(
368 ns_uri,
369 Some("http://www.w3.org/XML/1998/namespace".to_string())
370 ); // system ns has the global uri when doing global lookup
371 let fb_uri = element.lookup_namespace_uri("fb");
372 assert_eq!(fb_uri, Some("http://www.foobar.org".to_string())); // system ns has the global prefix when doing global lookup
373 }
374
375 #[test]
376 fn attribute_no_namespace() {
377 let mut doc = Document::new().unwrap();
378 let element_result = Node::new("example", None, &doc);
379 assert!(element_result.is_ok());
380
381 let mut element = element_result.unwrap();
382 doc.set_root_element(&element);
383
384 let ns_result = Namespace::new("myns", "https://www.example.com/myns", &mut element);
385 assert!(ns_result.is_ok());
386 let ns = ns_result.unwrap();
387 assert!(element.set_attribute_ns("foo", "ns", &ns).is_ok());
388
389 let foo_ns_attr = element.get_attribute_ns("foo", "https://www.example.com/myns");
390 assert!(foo_ns_attr.is_some());
391 assert_eq!(foo_ns_attr.unwrap(), "ns");
392
393 let foo_no_ns_attr = element.get_attribute_no_ns("foo");
394 assert!(foo_no_ns_attr.is_none());
395
396 assert!(element.set_attribute("foo", "no_ns").is_ok());
397
398 let foo_no_ns_attr = element.get_attribute_no_ns("foo");
399 assert!(foo_no_ns_attr.is_some());
400 assert_eq!(foo_no_ns_attr.unwrap(), "no_ns");
401
402 assert!(element.remove_attribute_no_ns("foo").is_ok());
403 let foo_no_ns_attr = element.get_attribute_no_ns("foo");
404 assert!(foo_no_ns_attr.is_none());
405
406 assert!(element.set_attribute("bar", "bar").is_ok());
407 let bar_no_ns_attr = element.get_attribute_no_ns("bar");
408 assert!(bar_no_ns_attr.is_some());
409 assert_eq!(bar_no_ns_attr.unwrap(), "bar");
410 }
411
412 #[test]
413 fn node_can_unbind() {
414 let mut doc = Document::new().unwrap();
415 let element_result = Node::new("example", None, &doc);
416 assert!(element_result.is_ok());
417
418 let mut element = element_result.unwrap();
419 doc.set_root_element(&element);
420
421 let mut first_child = Node::new("first", None, &doc).unwrap();
422 let mut second_child = Node::new("second", None, &doc).unwrap();
423 let mut third_child = Node::new("third", None, &doc).unwrap();
424
425 assert!(element.add_child(&mut first_child).is_ok());
426 assert!(element.add_child(&mut second_child).is_ok());
427 assert!(element.add_child(&mut third_child).is_ok());
428
429 assert_eq!(element.get_child_nodes().len(), 3);
430 first_child.unbind_node();
431 assert_eq!(element.get_child_nodes().len(), 2);
432 second_child.unlink_node();
433 assert_eq!(element.get_child_nodes().len(), 1);
434 third_child.unlink();
435 assert_eq!(element.get_child_nodes().len(), 0);
436
437 // Test reparenting via unlink
438 let mut transfer = Node::new("transfer", None, &doc).unwrap();
439 assert!(element.add_child(&mut transfer).is_ok());
440 assert!(transfer.append_text("test text").is_ok());
441 let mut receiver = Node::new("receiver", None, &doc).unwrap();
442 assert!(element.add_child(&mut receiver).is_ok());
443 assert_eq!(element.get_child_nodes().len(), 2);
444 assert_eq!(transfer.get_child_nodes().len(), 1);
445 assert_eq!(receiver.get_child_nodes().len(), 0);
446
447 transfer.unlink();
448 assert_eq!(element.get_child_nodes().len(), 1);
449 assert_eq!(receiver.get_child_nodes().len(), 0);
450 assert!(receiver.add_child(&mut transfer).is_ok());
451 assert_eq!(receiver.get_child_nodes().len(), 1);
452 assert_eq!(transfer.get_content(), "test text".to_owned());
453 assert_eq!(transfer.get_parent(), Some(receiver));
454 }
455
456 #[test]
457 /// Can mock a node object (useful for defaults that will be overridden)
458 fn can_mock_node() {
459 let doc_mock = Document::new().unwrap();
460 let node_mock = Node::mock(&doc_mock);
461 assert!(!node_mock.is_text_node());
462 }
463
464 #[test]
465 /// Can make a mock node hashable
466 fn can_hash_mock_node() {
467 let doc_mock = Document::new().unwrap();
468 let node_mock = Node::mock(&doc_mock);
469 assert!(node_mock.to_hashable() > 0);
470 }
471
472 #[test]
473 /// Can make null nodes and documents, to avoid memory allocations
474 fn can_null_node() {
475 let null_node = Node::null();
476 let second_null_node = Node::null();
477 assert!(null_node.is_null());
478 assert!(second_null_node.is_null());
479 assert_eq!(null_node, second_null_node);
480 }
481
482 #[test]
483 /// Can set and get attributes
484 fn can_manage_attributes() {
485 let mut doc = Document::new().unwrap();
486 let hello_element_result = Node::new("hello", None, &doc);
487 assert!(hello_element_result.is_ok());
488 let mut hello_element = hello_element_result.unwrap();
489 doc.set_root_element(&hello_element);
490
491 let key = "examplekey";
492 let value = "examplevalue";
493 let pre_value = hello_element.get_attribute(key);
494 assert_eq!(pre_value, None);
495 let pre_prop_check = hello_element.has_property(key);
496 assert!(!pre_prop_check);
497 let pre_prop_value = hello_element.get_property(key);
498 assert_eq!(pre_prop_value, None);
499
500 assert!(hello_element.set_attribute(key, value).is_ok());
501 let new_check = hello_element.has_attribute(key);
502 assert!(new_check);
503 let new_value = hello_element.get_attribute(key);
504 assert_eq!(new_value, Some(value.to_owned()));
505 }
506
507 #[test]
508 /// Can set and get text node content
509 fn can_set_get_text_node_content() {
510 let mut doc = Document::new().unwrap();
511 let hello_element_result = Node::new("hello", None, &doc);
512 assert!(hello_element_result.is_ok());
513 let mut hello_element = hello_element_result.unwrap();
514 doc.set_root_element(&hello_element);
515
516 assert!(hello_element.get_content().is_empty());
517 assert!(hello_element.append_text("hello ").is_ok());
518 assert_eq!(hello_element.get_content(), "hello ");
519 assert!(hello_element.append_text("world!").is_ok());
520 assert_eq!(hello_element.get_content(), "hello world!");
521 }
522
523 #[test]
524 /// Basic namespace workflow
525 fn can_work_with_namespaces() {
526 let mut doc = Document::new().unwrap();
527 let mut root_node = Node::new("root", None, &doc).unwrap();
528 doc.set_root_element(&root_node);
529
530 let initial_namespace_list = root_node.get_namespaces(&doc);
531 assert_eq!(initial_namespace_list.len(), 0);
532
533 let mock_ns_result = Namespace::new("mock", "http://example.com/ns/mock", &mut root_node);
534 assert!(mock_ns_result.is_ok());
535 let second_ns_result = Namespace::new("second", "http://example.com/ns/second", &mut root_node);
536 assert!(second_ns_result.is_ok());
537
538 // try to attach this namespace to a node
539 assert!(root_node.get_namespace().is_none());
540 assert!(root_node.set_namespace(&mock_ns_result.unwrap()).is_ok());
541 let active_ns_opt = root_node.get_namespace();
542 assert!(active_ns_opt.is_some());
543 let active_ns = active_ns_opt.unwrap();
544 assert_eq!(active_ns.get_prefix(), "mock");
545 assert_eq!(active_ns.get_href(), "http://example.com/ns/mock");
546
547 // now get all namespaces for the node and check we have ours
548 let mut namespace_list = root_node.get_namespaces(&doc);
549 assert_eq!(namespace_list.len(), 2);
550
551 let second_ns = namespace_list.pop().unwrap();
552 assert_eq!(second_ns.get_prefix(), "second");
553 assert_eq!(second_ns.get_href(), "http://example.com/ns/second");
554
555 let first_ns = namespace_list.pop().unwrap();
556 assert_eq!(first_ns.get_prefix(), "mock");
557 assert_eq!(first_ns.get_href(), "http://example.com/ns/mock");
558 }
559
560 #[test]
561 fn can_work_with_ns_declarations() {
562 let mut doc = Document::new().unwrap();
563 let mut root_node = Node::new("root", None, &doc).unwrap();
564 doc.set_root_element(&root_node);
565
566 let mock_ns_result = Namespace::new("mock1", "http://example.com/ns/mock1", &mut root_node);
567 assert!(mock_ns_result.is_ok());
568 let second_ns_result = Namespace::new("mock2", "http://example.com/ns/mock2", &mut root_node);
569 assert!(second_ns_result.is_ok());
570
571 let declarations = root_node.get_namespace_declarations();
572 assert_eq!(declarations.len(), 2);
573 }
574
575 #[test]
576 /// Can view documents as nodes
577 fn can_cast_doc_to_node() {
578 // Setup
579 let parser = Parser::default();
580 let doc_result = parser.parse_file("tests/resources/file01.xml");
581 assert!(doc_result.is_ok());
582
583 let doc = doc_result.unwrap();
584 let doc_node = doc.as_node();
585 assert_eq!(doc_node.get_type(), Some(NodeType::DocumentNode));
586 let root_node_opt = doc_node.get_first_child();
587 assert!(root_node_opt.is_some());
588 let root_node = root_node_opt.unwrap();
589 assert_eq!(root_node.get_name(), "root");
590 }
591
592 #[test]
593 fn can_replace_child() {
594 let mut doc = Document::new().unwrap();
595 let mut root_node = Node::new("root", None, &doc).unwrap();
596 doc.set_root_element(&root_node);
597 let mut a = Node::new("a", None, &doc).unwrap();
598 let mut b = Node::new("b", None, &doc).unwrap();
599 let mut c = Node::new("c", None, &doc).unwrap();
600 let mut d = Node::new("d", None, &doc).unwrap();
601 let mut e = Node::new("e", None, &doc).unwrap();
602
603 assert!(root_node.add_child(&mut a).is_ok());
604 assert!(root_node.add_child(&mut b).is_ok());
605 assert!(root_node.add_child(&mut c).is_ok());
606 assert!(root_node.add_child(&mut d).is_ok());
607 assert!(root_node.add_child(&mut e).is_ok());
608 assert_eq!(
609 doc.to_string(),
610 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<root><a/><b/><c/><d/><e/></root>\n",
611 "document initialized correctly."
612 );
613
614 // replace first child with new F
615 let f = Node::new("F", None, &doc).unwrap();
616 let a_result = root_node.replace_child_node(f, a);
617 assert!(a_result.is_ok());
618
619 assert_eq!(
620 doc.to_string(),
621 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<root><F/><b/><c/><d/><e/></root>\n",
622 "document initialized correctly."
623 );
624
625 // replace last child with new G
626 let g = Node::new("G", None, &doc).unwrap();
627 assert!(root_node.replace_child_node(g, e).is_ok());
628 assert_eq!(
629 doc.to_string(),
630 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<root><F/><b/><c/><d/><G/></root>\n",
631 "document initialized correctly."
632 );
633
634 // replace middle child with new H
635 let h = Node::new("H", None, &doc).unwrap();
636 assert!(root_node.replace_child_node(h, c).is_ok());
637 assert_eq!(
638 doc.to_string(),
639 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<root><F/><b/><H/><d/><G/></root>\n",
640 "document initialized correctly."
641 );
642
643 // fail to replace a, as it is already removed.
644 let none = Node::new("none", None, &doc).unwrap();
645 assert!(root_node
646 .replace_child_node(none, a_result.unwrap())
647 .is_err());
648 // no change.
649 assert_eq!(
650 doc.to_string(),
651 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<root><F/><b/><H/><d/><G/></root>\n",
652 "document initialized correctly."
653 );
654
655 // replacing with self succeeds without change.
656 assert!(root_node.replace_child_node(b.clone(), b).is_ok());
657 assert_eq!(
658 doc.to_string(),
659 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<root><F/><b/><H/><d/><G/></root>\n",
660 "document initialized correctly."
661 );
662 // replacing with parent succeeds without change.
663 assert!(root_node.replace_child_node(root_node.clone(), d).is_ok());
664 assert_eq!(
665 doc.to_string(),
666 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<root><F/><b/><H/><d/><G/></root>\n",
667 "document initialized correctly."
668 );
669 }