forked from netvl/xml-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent_writer.rs
168 lines (134 loc) · 4.46 KB
/
event_writer.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
extern crate xml;
use std::io::{BufReader, SeekFrom};
use std::io::prelude::*;
use std::fs::File;
use std::str;
use xml::reader::EventReader;
use xml::writer::EmitterConfig;
macro_rules! unwrap_all {
($($e:expr);+) => {{
$($e.unwrap();)+
}}
}
#[test]
fn reading_writing_equal_with_namespaces() {
let mut f = File::open("tests/documents/sample_2.xml").unwrap();
let mut b = Vec::new();
{
let r = EventReader::new(BufReader::new(&mut f));
let mut w = EmitterConfig::default().perform_indent(true).create_writer(&mut b);
for e in r {
match e {
Ok(e) => if let Some(e) = e.as_writer_event() {
match w.write(e) {
Ok(_) => {},
Err(e) => panic!("Writer error: {:?}", e)
}
},
Err(e) => panic!("Error: {}", e)
}
}
}
f.seek(SeekFrom::Start(0)).unwrap();
let mut fs = String::new();
f.read_to_string(&mut fs).unwrap();
let bs = String::from_utf8(b).unwrap();
assert_eq!(fs.trim(), bs.trim());
}
#[test]
fn writing_simple() {
use xml::writer::XmlEvent;
let mut b = Vec::new();
{
let mut w = EmitterConfig::new().write_document_declaration(false).create_writer(&mut b);
w.write(XmlEvent::start_element("h:hello").ns("h", "urn:hello-world")).unwrap();
w.write("hello world").unwrap();
w.write(XmlEvent::end_element()).unwrap();
}
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"<h:hello xmlns:h="urn:hello-world">hello world</h:hello>"#
);
}
#[test]
fn writing_empty_elements_with_normalizing() {
use xml::writer::XmlEvent;
let mut b = Vec::new();
{
let mut w = EmitterConfig::new().write_document_declaration(false).create_writer(&mut b);
unwrap_all! {
w.write(XmlEvent::start_element("hello"));
w.write(XmlEvent::start_element("world"));
w.write(XmlEvent::end_element());
w.write(XmlEvent::end_element())
}
}
assert_eq!(str::from_utf8(&b).unwrap(), r#"<hello><world /></hello>"#);
}
#[test]
fn writing_empty_elements_without_normalizing() {
use xml::writer::XmlEvent;
let mut b = Vec::new();
{
let mut w = EmitterConfig::new()
.write_document_declaration(false)
.normalize_empty_elements(false)
.create_writer(&mut b);
unwrap_all! {
w.write(XmlEvent::start_element("hello"));
w.write(XmlEvent::start_element("world"));
w.write(XmlEvent::end_element());
w.write(XmlEvent::end_element())
}
}
assert_eq!(str::from_utf8(&b).unwrap(), r#"<hello><world></world></hello>"#);
}
#[test]
fn writing_comments_with_indentation() {
use xml::writer::XmlEvent;
let mut b = Vec::new();
{
let mut w = EmitterConfig::new()
.write_document_declaration(false)
.perform_indent(true)
.create_writer(&mut b);
unwrap_all! {
w.write(XmlEvent::start_element("hello"));
w.write(XmlEvent::start_element("world"));
w.write(XmlEvent::comment(" this is a manually padded comment\t"));
w.write(XmlEvent::comment("this is an unpadded comment"));
w.write(XmlEvent::end_element());
w.write(XmlEvent::end_element())
}
}
assert_eq!(
str::from_utf8(&b).unwrap(),
"<hello>
<world>
<!-- this is a manually padded comment\t-->
<!-- this is an unpadded comment -->
</world>
</hello>");
}
#[test]
fn issue_112_overriding_namepace_prefix() {
use xml::writer::XmlEvent;
let mut b = Vec::new();
{
let mut w = EmitterConfig::new()
.write_document_declaration(false)
.create_writer(&mut b);
unwrap_all! {
w.write(XmlEvent::start_element("iq").ns("", "jabber:client").ns("a", "urn:A"));
w.write(XmlEvent::start_element("bind").ns("", "urn:ietf:params:xml:ns:xmpp-bind"));
w.write(XmlEvent::end_element());
w.write(XmlEvent::start_element("whatever").ns("a", "urn:X"));
w.write(XmlEvent::end_element());
w.write(XmlEvent::end_element())
}
}
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"<iq xmlns="jabber:client" xmlns:a="urn:A"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind" /><whatever xmlns:a="urn:X" /></iq>"#
)
}