2017-03-08 19:34:17 +00:00
use std ::iter ::Iterator ;
2018-04-11 01:01:18 +00:00
use quick_xml ::Reader ;
2017-03-08 19:34:17 +00:00
use element ::Element ;
2018-01-06 04:20:12 +00:00
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>"# ;
2017-03-08 19:34:17 +00:00
fn build_test_tree ( ) -> Element {
let mut root = Element ::builder ( " root " )
. ns ( " root_ns " )
2017-04-24 14:56:29 +00:00
. attr ( " xml:lang " , " en " )
2017-03-08 19:34:17 +00:00
. attr ( " a " , " b " )
. build ( ) ;
root . append_text_node ( " meow " ) ;
let child = Element ::builder ( " child " )
. attr ( " c " , " d " )
. build ( ) ;
root . append_child ( child ) ;
let other_child = Element ::builder ( " child " )
. ns ( " child_ns " )
. attr ( " d " , " e " )
2017-04-24 14:56:29 +00:00
. attr ( " xml:lang " , " fr " )
2017-03-08 19:34:17 +00:00
. build ( ) ;
root . append_child ( other_child ) ;
root . append_text_node ( " nya " ) ;
root
}
2018-01-06 04:20:12 +00:00
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
}
2017-03-08 19:34:17 +00:00
#[ test ]
fn reader_works ( ) {
2017-06-07 20:40:53 +00:00
let mut reader = Reader ::from_str ( TEST_STRING ) ;
2017-03-08 19:34:17 +00:00
assert_eq! ( Element ::from_reader ( & mut reader ) . unwrap ( ) , build_test_tree ( ) ) ;
}
#[ test ]
fn writer_works ( ) {
let root = build_test_tree ( ) ;
2017-06-07 20:40:53 +00:00
let mut writer = Vec ::new ( ) ;
2017-03-08 19:34:17 +00:00
{
root . write_to ( & mut writer ) . unwrap ( ) ;
}
2017-06-07 20:40:53 +00:00
assert_eq! ( String ::from_utf8 ( writer ) . unwrap ( ) , TEST_STRING ) ;
2017-03-08 19:34:17 +00:00
}
2017-08-12 00:05:18 +00:00
#[ test ]
fn writer_escapes_attributes ( ) {
let root = Element ::builder ( " root " )
. attr ( " a " , " \" Air \" quotes " )
. build ( ) ;
let mut writer = Vec ::new ( ) ;
{
root . write_to ( & mut writer ) . unwrap ( ) ;
}
assert_eq! ( String ::from_utf8 ( writer ) . unwrap ( ) ,
2018-01-06 04:20:12 +00:00
r # "<?xml version="1.0" encoding="utf-8"?><root a=""Air" quotes"/>"#
2017-08-12 00:05:18 +00:00
) ;
}
#[ test ]
fn writer_escapes_text ( ) {
let root = Element ::builder ( " root " )
. append ( " <3 " )
. build ( ) ;
let mut writer = Vec ::new ( ) ;
{
root . write_to ( & mut writer ) . unwrap ( ) ;
}
assert_eq! ( String ::from_utf8 ( writer ) . unwrap ( ) ,
r # "<?xml version="1.0" encoding="utf-8"?><root><3</root>"#
) ;
}
2017-03-08 19:34:17 +00:00
#[ test ]
fn builder_works ( ) {
let elem = Element ::builder ( " a " )
. ns ( " b " )
. attr ( " c " , " d " )
. append ( Element ::builder ( " child " ) )
. append ( " e " )
. build ( ) ;
assert_eq! ( elem . name ( ) , " a " ) ;
2017-07-28 21:58:03 +00:00
assert_eq! ( elem . ns ( ) , Some ( " b " . to_owned ( ) ) ) ;
2017-03-08 19:34:17 +00:00
assert_eq! ( elem . attr ( " c " ) , Some ( " d " ) ) ;
assert_eq! ( elem . attr ( " x " ) , None ) ;
assert_eq! ( elem . text ( ) , " e " ) ;
assert! ( elem . has_child ( " child " , " b " ) ) ;
assert! ( elem . is ( " a " , " b " ) ) ;
}
#[ test ]
fn children_iter_works ( ) {
let root = build_test_tree ( ) ;
let mut iter = root . children ( ) ;
assert! ( iter . next ( ) . unwrap ( ) . is ( " child " , " root_ns " ) ) ;
assert! ( iter . next ( ) . unwrap ( ) . is ( " child " , " child_ns " ) ) ;
assert_eq! ( iter . next ( ) , None ) ;
}
#[ test ]
fn get_child_works ( ) {
let root = build_test_tree ( ) ;
assert_eq! ( root . get_child ( " child " , " inexistent_ns " ) , None ) ;
assert_eq! ( root . get_child ( " not_a_child " , " root_ns " ) , None ) ;
assert! ( root . get_child ( " child " , " root_ns " ) . unwrap ( ) . is ( " child " , " root_ns " ) ) ;
assert! ( root . get_child ( " child " , " child_ns " ) . unwrap ( ) . is ( " child " , " child_ns " ) ) ;
assert_eq! ( root . get_child ( " child " , " root_ns " ) . unwrap ( ) . attr ( " c " ) , Some ( " d " ) ) ;
assert_eq! ( root . get_child ( " child " , " child_ns " ) . unwrap ( ) . attr ( " d " ) , Some ( " e " ) ) ;
}
#[ test ]
fn namespace_propagation_works ( ) {
let mut root = Element ::builder ( " root " ) . ns ( " root_ns " ) . build ( ) ;
let mut child = Element ::bare ( " child " ) ;
let grandchild = Element ::bare ( " grandchild " ) ;
child . append_child ( grandchild ) ;
root . append_child ( child ) ;
2017-07-28 21:58:03 +00:00
2017-03-08 19:34:17 +00:00
assert_eq! ( root . get_child ( " child " , " root_ns " ) . unwrap ( ) . ns ( ) , root . ns ( ) ) ;
assert_eq! ( root . get_child ( " child " , " root_ns " ) . unwrap ( )
. get_child ( " grandchild " , " root_ns " ) . unwrap ( )
. ns ( ) , root . ns ( ) ) ;
}
2017-04-24 18:06:21 +00:00
#[ test ]
fn two_elements_with_same_arguments_different_order_are_equal ( ) {
let elem1 : Element = " <a b='a' c=''/> " . parse ( ) . unwrap ( ) ;
let elem2 : Element = " <a c='' b='a'/> " . parse ( ) . unwrap ( ) ;
assert_eq! ( elem1 , elem2 ) ;
let elem1 : Element = " <a b='a' c=''/> " . parse ( ) . unwrap ( ) ;
let elem2 : Element = " <a c='d' b='a'/> " . parse ( ) . unwrap ( ) ;
assert_ne! ( elem1 , elem2 ) ;
}
2017-04-25 23:02:17 +00:00
2017-04-24 14:56:29 +00:00
#[ test ]
fn namespace_attributes_works ( ) {
2017-06-07 20:40:53 +00:00
let mut reader = Reader ::from_str ( TEST_STRING ) ;
2017-04-24 14:56:29 +00:00
let root = Element ::from_reader ( & mut reader ) . unwrap ( ) ;
assert_eq! ( " en " , root . attr ( " xml:lang " ) . unwrap ( ) ) ;
assert_eq! ( " fr " , root . get_child ( " child " , " child_ns " ) . unwrap ( ) . attr ( " xml:lang " ) . unwrap ( ) ) ;
}
2017-06-07 20:40:53 +00:00
#[ test ]
fn wrongly_closed_elements_error ( ) {
let elem1 = " <a></b> " . parse ::< Element > ( ) ;
assert! ( elem1 . is_err ( ) ) ;
let elem1 = " <a></c></a> " . parse ::< Element > ( ) ;
assert! ( elem1 . is_err ( ) ) ;
let elem1 = " <a><c><d/></c></a> " . parse ::< Element > ( ) ;
assert! ( elem1 . is_ok ( ) ) ;
}
2017-07-18 23:14:33 +00:00
#[ test ]
fn namespace_simple ( ) {
let elem : Element = " <message xmlns='jabber:client'/> " . parse ( ) . unwrap ( ) ;
assert_eq! ( elem . name ( ) , " message " ) ;
2017-07-28 21:58:03 +00:00
assert_eq! ( elem . ns ( ) , Some ( " jabber:client " . to_owned ( ) ) ) ;
2017-07-18 23:14:33 +00:00
}
#[ test ]
fn namespace_prefixed ( ) {
let elem : Element = " <stream:features xmlns:stream='http://etherx.jabber.org/streams'/> "
. parse ( ) . unwrap ( ) ;
assert_eq! ( elem . name ( ) , " features " ) ;
2017-07-28 21:58:03 +00:00
assert_eq! ( elem . ns ( ) , Some ( " http://etherx.jabber.org/streams " . to_owned ( ) ) ) ;
2017-07-18 23:14:33 +00:00
}
#[ test ]
fn namespace_inherited_simple ( ) {
let elem : Element = " <stream xmlns='jabber:client'><message/></stream> " . parse ( ) . unwrap ( ) ;
assert_eq! ( elem . name ( ) , " stream " ) ;
2017-07-28 21:58:03 +00:00
assert_eq! ( elem . ns ( ) , Some ( " jabber:client " . to_owned ( ) ) ) ;
2017-07-18 23:14:33 +00:00
let child = elem . children ( ) . next ( ) . unwrap ( ) ;
assert_eq! ( child . name ( ) , " message " ) ;
2017-07-28 21:58:03 +00:00
assert_eq! ( child . ns ( ) , Some ( " jabber:client " . to_owned ( ) ) ) ;
2017-07-18 23:14:33 +00:00
}
#[ test ]
fn namespace_inherited_prefixed1 ( ) {
2017-07-28 21:58:03 +00:00
let elem : Element = " <stream:features xmlns:stream='http://etherx.jabber.org/streams' xmlns='jabber:client'><message/></stream:features> "
2017-07-18 23:14:33 +00:00
. parse ( ) . unwrap ( ) ;
assert_eq! ( elem . name ( ) , " features " ) ;
2017-07-28 21:58:03 +00:00
assert_eq! ( elem . ns ( ) , Some ( " http://etherx.jabber.org/streams " . to_owned ( ) ) ) ;
2017-07-18 23:14:33 +00:00
let child = elem . children ( ) . next ( ) . unwrap ( ) ;
assert_eq! ( child . name ( ) , " message " ) ;
2017-07-28 21:58:03 +00:00
assert_eq! ( child . ns ( ) , Some ( " jabber:client " . to_owned ( ) ) ) ;
2017-07-18 23:14:33 +00:00
}
#[ test ]
fn namespace_inherited_prefixed2 ( ) {
let elem : Element = " <stream xmlns='http://etherx.jabber.org/streams' xmlns:jabber='jabber:client'><jabber:message/></stream> "
. parse ( ) . unwrap ( ) ;
assert_eq! ( elem . name ( ) , " stream " ) ;
2017-07-28 21:58:03 +00:00
assert_eq! ( elem . ns ( ) , Some ( " http://etherx.jabber.org/streams " . to_owned ( ) ) ) ;
2017-07-18 23:14:33 +00:00
let child = elem . children ( ) . next ( ) . unwrap ( ) ;
assert_eq! ( child . name ( ) , " message " ) ;
2017-07-28 21:58:03 +00:00
assert_eq! ( child . ns ( ) , Some ( " jabber:client " . to_owned ( ) ) ) ;
2017-07-18 23:14:33 +00:00
}
2018-01-06 04:20:12 +00:00
#[ 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 ) ;
}
2018-02-18 20:32:51 +00:00
2018-02-18 18:41:03 +00:00
#[ test ]
fn xml_error ( ) {
match " <a></b> " . parse ::< Element > ( ) {
2018-02-18 18:54:09 +00:00
Err ( ::error ::Error ::XmlError ( _ ) ) = > ( ) ,
2018-02-18 18:41:03 +00:00
err = > panic! ( " No or wrong error: {:?} " , err )
}
match " <a></ " . parse ::< Element > ( ) {
2018-02-18 18:54:09 +00:00
Err ( ::error ::Error ::XmlError ( _ ) ) = > ( ) ,
2018-02-18 18:41:03 +00:00
err = > panic! ( " No or wrong error: {:?} " , err )
}
}
#[ test ]
fn invalid_element_error ( ) {
match " <a:b:c> " . parse ::< Element > ( ) {
2018-02-18 18:54:09 +00:00
Err ( ::error ::Error ::InvalidElement ) = > ( ) ,
2018-02-18 18:41:03 +00:00
err = > panic! ( " No or wrong error: {:?} " , err )
}
}