add unit test on deserializer

This commit is contained in:
Marc-Antoine Arnaud 2018-05-24 18:27:21 +02:00
parent 2c4451e32b
commit 6f424f1172
2 changed files with 49 additions and 9 deletions

View File

@ -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
View 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")));
}