ping.rs

 1// Copyright (c) 2017 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
 2// Copyright (c) 2017 Maxime “pep” Buquet <pep+code@bouah.net>
 3//
 4// This Source Code Form is subject to the terms of the Mozilla Public
 5// License, v. 2.0. If a copy of the MPL was not distributed with this
 6// file, You can obtain one at http://mozilla.org/MPL/2.0/.
 7
 8use ns;
 9
10generate_empty_element!(Ping, "ping", ns::PING);
11
12#[cfg(test)]
13mod tests {
14    use super::*;
15    use try_from::TryFrom;
16    use minidom::Element;
17    use error::Error;
18
19    #[test]
20    fn test_simple() {
21        let elem: Element = "<ping xmlns='urn:xmpp:ping'/>".parse().unwrap();
22        Ping::try_from(elem).unwrap();
23    }
24
25    #[test]
26    fn test_serialise() {
27        let elem1 = Element::from(Ping);
28        let elem2: Element = "<ping xmlns='urn:xmpp:ping'/>".parse().unwrap();
29        assert_eq!(elem1, elem2);
30    }
31
32    #[test]
33    fn test_invalid() {
34        let elem: Element = "<ping xmlns='urn:xmpp:ping'><coucou/></ping>".parse().unwrap();
35        let error = Ping::try_from(elem).unwrap_err();
36        let message = match error {
37            Error::ParseError(string) => string,
38            _ => panic!(),
39        };
40        assert_eq!(message, "Unknown child in ping element.");
41    }
42
43    #[test]
44    fn test_invalid_attribute() {
45        let elem: Element = "<ping xmlns='urn:xmpp:ping' coucou=''/>".parse().unwrap();
46        let error = Ping::try_from(elem).unwrap_err();
47        let message = match error {
48            Error::ParseError(string) => string,
49            _ => panic!(),
50        };
51        assert_eq!(message, "Unknown attribute in ping element.");
52    }
53}