ping.rs

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