Skip to content

Commit aaa3327

Browse files
committed
copy README tests into tests/readme.rs
cargo test doesn't currently pick up doctests in README.md, tracking issue: rust-lang/cargo#383
1 parent f30e40d commit aaa3327

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,8 @@ impl Element {
272272
match T::from_str(text) {
273273
Err(_) => Err(errors::Error::ValueFromStr {
274274
t: text.to_string(),
275-
}.into()),
275+
}
276+
.into()),
276277
Ok(value) => Ok(Some(value)),
277278
}
278279
} else {

tests/readme.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
extern crate treexml;
2+
3+
mod readme {
4+
5+
use treexml::{Document, ElementBuilder as E};
6+
#[test]
7+
fn read() {
8+
let doc_raw = r#"
9+
<?xml version="1.1" encoding="UTF-8"?>
10+
<table>
11+
<fruit type="apple">worm</fruit>
12+
<vegetable />
13+
</table>
14+
"#;
15+
16+
let doc = Document::parse(doc_raw.as_bytes()).unwrap();
17+
let root = doc.root.unwrap();
18+
19+
let fruit = root.find_child(|tag| tag.name == "fruit").unwrap().clone();
20+
println!("{} [{:?}] = {:?}", fruit.name, fruit.attributes, fruit.text,);
21+
}
22+
23+
#[test]
24+
fn write() {
25+
let mut something = E::new("something");
26+
something.attr("key", "value");
27+
something.text("some-text");
28+
29+
let doc = Document::build(E::new("root").children(vec![E::new("list").children(vec![
30+
E::new("child").cdata("test data here"),
31+
E::new("child").attr("class", "foo").text("bar"),
32+
E::new("child").attr("class", 22).text(11),
33+
&mut E::new("child"),
34+
&mut something,
35+
])]));
36+
37+
println!("{}", doc);
38+
}
39+
}

tests/write.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,14 +163,13 @@ mod write {
163163
fn incremental_build() {
164164
let preexisting: Element = E::new("preexisting").element();
165165

166-
let doc = Document::build(E::new("root").children(vec![
167-
E::new("list").children(vec![
166+
let doc =
167+
Document::build(E::new("root").children(vec![E::new("list").children(vec![
168168
&mut preexisting.into(),
169169
&mut E::new("child"),
170170
E::new("child").attr("class", "foo").text("bar"),
171171
E::new("child").attr("class", 22).text(11),
172-
]),
173-
]));
172+
])]));
174173

175174
let doc_ref = concat!(
176175
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",

0 commit comments

Comments
 (0)