ping.rs

 1use minidom::Element;
 2
 3use error::Error;
 4
 5use ns;
 6
 7#[derive(Debug, Clone)]
 8pub struct Ping;
 9
10pub fn parse_ping(root: &Element) -> Result<Ping, Error> {
11    if !root.is("ping", ns::PING) {
12        return Err(Error::ParseError("This is not a ping element."));
13    }
14
15    for _ in root.children() {
16        return Err(Error::ParseError("Unknown child in ping element."));
17    }
18    Ok(Ping {  })
19}
20
21#[cfg(test)]
22mod tests {
23    use minidom::Element;
24    use error::Error;
25    use ping;
26
27    #[test]
28    fn test_simple() {
29        let elem: Element = "<ping xmlns='urn:xmpp:ping'/>".parse().unwrap();
30        ping::parse_ping(&elem).unwrap();
31    }
32
33    #[test]
34    fn test_invalid() {
35        let elem: Element = "<ping xmlns='urn:xmpp:ping'><coucou/></ping>".parse().unwrap();
36        let error = ping::parse_ping(&elem).unwrap_err();
37        let message = match error {
38            Error::ParseError(string) => string,
39            _ => panic!(),
40        };
41        assert_eq!(message, "Unknown child in ping element.");
42    }
43
44    #[test]
45    #[ignore]
46    fn test_invalid_attribute() {
47        let elem: Element = "<ping xmlns='urn:xmpp:ping' coucou=''/>".parse().unwrap();
48        let error = ping::parse_ping(&elem).unwrap_err();
49        let message = match error {
50            Error::ParseError(string) => string,
51            _ => panic!(),
52        };
53        assert_eq!(message, "Unknown attribute in ping element.");
54    }
55}