tests.rs

  1use crate::element::Element;
  2
  3use quick_xml::Reader;
  4
  5const 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>"#;
  6
  7fn build_test_tree() -> Element {
  8    let mut root = Element::builder("root")
  9                           .ns("root_ns")
 10                           .attr("xml:lang", "en")
 11                           .attr("a", "b")
 12                           .build();
 13    root.append_text_node("meow");
 14    let child = Element::builder("child")
 15                        .attr("c", "d")
 16                        .build();
 17    root.append_child(child);
 18    let other_child = Element::builder("child")
 19                              .ns("child_ns")
 20                              .attr("d", "e")
 21                              .attr("xml:lang", "fr")
 22                              .build();
 23    root.append_child(other_child);
 24    root.append_text_node("nya");
 25    root
 26}
 27
 28#[cfg(feature = "comments")]
 29const 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>"#;
 30
 31#[cfg(feature = "comments")]
 32fn build_comment_test_tree() -> Element {
 33    let mut root = Element::builder("root").build();
 34    root.append_comment_node("This is a child.");
 35    let mut child = Element::builder("child").attr("attr", "val").build();
 36    child.append_comment_node("This is a grandchild.");
 37    let grand_child = Element::builder("grandchild").build();
 38    child.append_child(grand_child);
 39    root.append_child(child);
 40    root
 41}
 42
 43#[test]
 44fn reader_works() {
 45    let mut reader = Reader::from_str(TEST_STRING);
 46    assert_eq!(Element::from_reader(&mut reader).unwrap(), build_test_tree());
 47}
 48
 49#[test]
 50fn writer_works() {
 51    let root = build_test_tree();
 52    let mut writer = Vec::new();
 53    {
 54        root.write_to(&mut writer).unwrap();
 55    }
 56    assert_eq!(String::from_utf8(writer).unwrap(), TEST_STRING);
 57}
 58
 59#[test]
 60fn writer_escapes_attributes() {
 61    let root = Element::builder("root")
 62        .attr("a", "\"Air\" quotes")
 63        .build();
 64    let mut writer = Vec::new();
 65    {
 66        root.write_to(&mut writer).unwrap();
 67    }
 68    assert_eq!(String::from_utf8(writer).unwrap(),
 69               r#"<?xml version="1.0" encoding="utf-8"?><root a="&quot;Air&quot; quotes"/>"#
 70    );
 71}
 72
 73#[test]
 74fn writer_escapes_text() {
 75    let root = Element::builder("root")
 76        .append("<3")
 77        .build();
 78    let mut writer = Vec::new();
 79    {
 80        root.write_to(&mut writer).unwrap();
 81    }
 82    assert_eq!(String::from_utf8(writer).unwrap(),
 83               r#"<?xml version="1.0" encoding="utf-8"?><root>&lt;3</root>"#
 84    );
 85}
 86
 87#[test]
 88fn builder_works() {
 89    let elem = Element::builder("a")
 90                       .ns("b")
 91                       .attr("c", "d")
 92                       .append(Element::builder("child"))
 93                       .append("e")
 94                       .build();
 95    assert_eq!(elem.name(), "a");
 96    assert_eq!(elem.ns(), Some("b".to_owned()));
 97    assert_eq!(elem.attr("c"), Some("d"));
 98    assert_eq!(elem.attr("x"), None);
 99    assert_eq!(elem.text(), "e");
100    assert!(elem.has_child("child", "b"));
101    assert!(elem.is("a", "b"));
102}
103
104#[test]
105fn children_iter_works() {
106    let root = build_test_tree();
107    let mut iter = root.children();
108    assert!(iter.next().unwrap().is("child", "root_ns"));
109    assert!(iter.next().unwrap().is("child", "child_ns"));
110    assert_eq!(iter.next(), None);
111}
112
113#[test]
114fn get_child_works() {
115    let root = build_test_tree();
116    assert_eq!(root.get_child("child", "inexistent_ns"), None);
117    assert_eq!(root.get_child("not_a_child", "root_ns"), None);
118    assert!(root.get_child("child", "root_ns").unwrap().is("child", "root_ns"));
119    assert!(root.get_child("child", "child_ns").unwrap().is("child", "child_ns"));
120    assert_eq!(root.get_child("child", "root_ns").unwrap().attr("c"), Some("d"));
121    assert_eq!(root.get_child("child", "child_ns").unwrap().attr("d"), Some("e"));
122}
123
124#[test]
125fn namespace_propagation_works() {
126    let mut root = Element::builder("root").ns("root_ns").build();
127    let mut child = Element::bare("child");
128    let grandchild = Element::bare("grandchild");
129    child.append_child(grandchild);
130    root.append_child(child);
131
132    assert_eq!(root.get_child("child", "root_ns").unwrap().ns(), root.ns());
133    assert_eq!(root.get_child("child", "root_ns").unwrap()
134                   .get_child("grandchild", "root_ns").unwrap()
135                   .ns(), root.ns());
136}
137
138#[test]
139fn two_elements_with_same_arguments_different_order_are_equal() {
140    let elem1: Element = "<a b='a' c=''/>".parse().unwrap();
141    let elem2: Element = "<a c='' b='a'/>".parse().unwrap();
142    assert_eq!(elem1, elem2);
143
144    let elem1: Element = "<a b='a' c=''/>".parse().unwrap();
145    let elem2: Element = "<a c='d' b='a'/>".parse().unwrap();
146    assert_ne!(elem1, elem2);
147}
148
149#[test]
150fn namespace_attributes_works() {
151    let mut reader = Reader::from_str(TEST_STRING);
152    let root = Element::from_reader(&mut reader).unwrap();
153    assert_eq!("en", root.attr("xml:lang").unwrap());
154    assert_eq!("fr", root.get_child("child", "child_ns").unwrap().attr("xml:lang").unwrap());
155}
156
157#[test]
158fn wrongly_closed_elements_error() {
159    let elem1 = "<a></b>".parse::<Element>();
160    assert!(elem1.is_err());
161    let elem1 = "<a></c></a>".parse::<Element>();
162    assert!(elem1.is_err());
163    let elem1 = "<a><c><d/></c></a>".parse::<Element>();
164    assert!(elem1.is_ok());
165}
166
167#[test]
168fn namespace_simple() {
169    let elem: Element = "<message xmlns='jabber:client'/>".parse().unwrap();
170    assert_eq!(elem.name(), "message");
171    assert_eq!(elem.ns(), Some("jabber:client".to_owned()));
172}
173
174#[test]
175fn namespace_prefixed() {
176    let elem: Element = "<stream:features xmlns:stream='http://etherx.jabber.org/streams'/>"
177        .parse().unwrap();
178    assert_eq!(elem.name(), "features");
179    assert_eq!(elem.ns(), Some("http://etherx.jabber.org/streams".to_owned()));
180}
181
182#[test]
183fn namespace_inherited_simple() {
184    let elem: Element = "<stream xmlns='jabber:client'><message/></stream>".parse().unwrap();
185    assert_eq!(elem.name(), "stream");
186    assert_eq!(elem.ns(), Some("jabber:client".to_owned()));
187    let child = elem.children().next().unwrap();
188    assert_eq!(child.name(), "message");
189    assert_eq!(child.ns(), Some("jabber:client".to_owned()));
190}
191
192#[test]
193fn namespace_inherited_prefixed1() {
194    let elem: Element = "<stream:features xmlns:stream='http://etherx.jabber.org/streams' xmlns='jabber:client'><message/></stream:features>"
195        .parse().unwrap();
196    assert_eq!(elem.name(), "features");
197    assert_eq!(elem.ns(), Some("http://etherx.jabber.org/streams".to_owned()));
198    let child = elem.children().next().unwrap();
199    assert_eq!(child.name(), "message");
200    assert_eq!(child.ns(), Some("jabber:client".to_owned()));
201}
202
203#[test]
204fn namespace_inherited_prefixed2() {
205    let elem: Element = "<stream xmlns='http://etherx.jabber.org/streams' xmlns:jabber='jabber:client'><jabber:message/></stream>"
206        .parse().unwrap();
207    assert_eq!(elem.name(), "stream");
208    assert_eq!(elem.ns(), Some("http://etherx.jabber.org/streams".to_owned()));
209    let child = elem.children().next().unwrap();
210    assert_eq!(child.name(), "message");
211    assert_eq!(child.ns(), Some("jabber:client".to_owned()));
212}
213
214#[cfg(feature = "comments")]
215#[test]
216fn read_comments() {
217    let mut reader = Reader::from_str(COMMENT_TEST_STRING);
218    assert_eq!(Element::from_reader(&mut reader).unwrap(), build_comment_test_tree());
219}
220
221#[cfg(feature = "comments")]
222#[test]
223fn write_comments() {
224    let root = build_comment_test_tree();
225    let mut writer = Vec::new();
226    {
227        root.write_to(&mut writer).unwrap();
228    }
229    assert_eq!(String::from_utf8(writer).unwrap(), COMMENT_TEST_STRING);
230}
231
232#[test]
233fn xml_error() {
234    match "<a></b>".parse::<Element>() {
235        Err(crate::error::Error::XmlError(_)) => (),
236        err => panic!("No or wrong error: {:?}", err)
237    }
238
239    match "<a></".parse::<Element>() {
240        Err(crate::error::Error::XmlError(_)) => (),
241        err => panic!("No or wrong error: {:?}", err)
242    }
243}
244
245#[test]
246fn invalid_element_error() {
247    match "<a:b:c>".parse::<Element>() {
248        Err(crate::error::Error::InvalidElement) => (),
249        err => panic!("No or wrong error: {:?}", err)
250    }
251}