add unit test on deserializer
This commit is contained in:
parent
2c4451e32b
commit
6f424f1172
@ -53,16 +53,18 @@ impl<'de, R: Read> Deserializer<R> {
|
||||
|
||||
pub fn inner_next(&mut self) -> Result<XmlEvent, String> {
|
||||
loop {
|
||||
if let Ok(next) = self.reader.next() {
|
||||
match next {
|
||||
XmlEvent::StartDocument { .. }
|
||||
| XmlEvent::ProcessingInstruction { .. }
|
||||
| XmlEvent::Comment(_) => { /* skip */ }
|
||||
other => return Ok(other),
|
||||
match self.reader.next() {
|
||||
Ok(next) => {
|
||||
match next {
|
||||
XmlEvent::StartDocument { .. }
|
||||
| XmlEvent::ProcessingInstruction { .. }
|
||||
| XmlEvent::Comment(_) => { /* skip */ }
|
||||
other => return Ok(other),
|
||||
}
|
||||
}
|
||||
Err(msg) => {
|
||||
return Err(msg.msg().to_string());
|
||||
}
|
||||
} else {
|
||||
println!("{:?}", self.peeked);
|
||||
return Err(String::from("bad content"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
38
yaserde/tests/errors.rs
Normal file
38
yaserde/tests/errors.rs
Normal file
@ -0,0 +1,38 @@
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
extern crate xml;
|
||||
extern crate yaserde;
|
||||
#[macro_use]
|
||||
extern crate yaserde_derive;
|
||||
|
||||
use std::io::Read;
|
||||
use yaserde::YaDeserialize;
|
||||
use yaserde::de::from_str;
|
||||
|
||||
#[test]
|
||||
fn de_no_content() {
|
||||
#[derive(YaDeserialize, PartialEq, Debug)]
|
||||
#[yaserde(root = "book")]
|
||||
pub struct Book {
|
||||
author: String,
|
||||
title: String,
|
||||
}
|
||||
|
||||
let content = "";
|
||||
let loaded: Result<Book, String> = from_str(content);
|
||||
assert_eq!(loaded, Err(String::from("Unexpected end of stream: no root element found")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn de_wrong_end_balise() {
|
||||
#[derive(YaDeserialize, PartialEq, Debug)]
|
||||
#[yaserde(root = "book")]
|
||||
pub struct Book {
|
||||
author: String,
|
||||
title: String,
|
||||
}
|
||||
|
||||
let content = "<book><author>Antoine de Saint-Exupéry<title>Little prince</title></book>";
|
||||
let loaded: Result<Book, String> = from_str(content);
|
||||
assert_eq!(loaded, Err(String::from("Unexpected closing tag: book, expected author")));
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user