Support comment and write to quick-xml Writer

This commit is contained in:
Yue Liu 2018-01-05 20:20:12 -08:00 committed by Yue Liu
commit f456600efd
2 changed files with 125 additions and 36 deletions

View file

@ -4,7 +4,7 @@ use quick_xml::reader::Reader;
use element::Element;
const TEST_STRING: &'static str = r#"<?xml version="1.0" encoding="utf-8"?><root xmlns="root_ns" a="b" xml:lang="en">meow<child c="d" /><child xmlns="child_ns" d="e" xml:lang="fr" />nya</root>"#;
const TEST_STRING: &'static str = r#"<?xml version="1.0" encoding="utf-8"?><root xmlns="root_ns" a="b" xml:lang="en">meow<child c="d"/><child xmlns="child_ns" d="e" xml:lang="fr"/>nya</root>"#;
fn build_test_tree() -> Element {
let mut root = Element::builder("root")
@ -27,6 +27,19 @@ fn build_test_tree() -> Element {
root
}
const COMMENT_TEST_STRING: &'static str = r#"<?xml version="1.0" encoding="utf-8"?><root><!--This is a child.--><child attr="val"><!--This is a grandchild.--><grandchild/></child></root>"#;
fn build_comment_test_tree() -> Element {
let mut root = Element::builder("root").build();
root.append_comment_node("This is a child.");
let mut child = Element::builder("child").attr("attr", "val").build();
child.append_comment_node("This is a grandchild.");
let grand_child = Element::builder("grandchild").build();
child.append_child(grand_child);
root.append_child(child);
root
}
#[test]
fn reader_works() {
let mut reader = Reader::from_str(TEST_STRING);
@ -53,7 +66,7 @@ fn writer_escapes_attributes() {
root.write_to(&mut writer).unwrap();
}
assert_eq!(String::from_utf8(writer).unwrap(),
r#"<?xml version="1.0" encoding="utf-8"?><root a="&quot;Air&quot; quotes" />"#
r#"<?xml version="1.0" encoding="utf-8"?><root a="&quot;Air&quot; quotes"/>"#
);
}
@ -197,3 +210,19 @@ fn namespace_inherited_prefixed2() {
assert_eq!(child.name(), "message");
assert_eq!(child.ns(), Some("jabber:client".to_owned()));
}
#[test]
fn read_comments() {
let mut reader = Reader::from_str(COMMENT_TEST_STRING);
assert_eq!(Element::from_reader(&mut reader).unwrap(), build_comment_test_tree());
}
#[test]
fn write_comments() {
let root = build_comment_test_tree();
let mut writer = Vec::new();
{
root.write_to(&mut writer).unwrap();
}
assert_eq!(String::from_utf8(writer).unwrap(), COMMENT_TEST_STRING);
}