chatstates.rs

 1// Copyright (c) 2017 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
 2//
 3// This Source Code Form is subject to the terms of the Mozilla Public
 4// License, v. 2.0. If a copy of the MPL was not distributed with this
 5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
 6
 7#![deny(missing_docs)]
 8
 9generate_element_enum!(
10    /// Enum representing chatstate elements part of the
11    /// `http://jabber.org/protocol/chatstates` namespace.
12    ChatState, "chatstate", CHATSTATES, {
13        /// `<active xmlns='http://jabber.org/protocol/chatstates'/>`
14        Active => "active",
15
16        /// `<composing xmlns='http://jabber.org/protocol/chatstates'/>`
17        Composing => "composing",
18
19        /// `<gone xmlns='http://jabber.org/protocol/chatstates'/>`
20        Gone => "gone",
21
22        /// `<inactive xmlns='http://jabber.org/protocol/chatstates'/>`
23        Inactive => "inactive",
24
25        /// `<paused xmlns='http://jabber.org/protocol/chatstates'/>`
26        Paused => "paused",
27    }
28);
29
30#[cfg(test)]
31mod tests {
32    use super::*;
33    use try_from::TryFrom;
34    use minidom::Element;
35    use error::Error;
36    use ns;
37
38    #[test]
39    fn test_simple() {
40        let elem: Element = "<active xmlns='http://jabber.org/protocol/chatstates'/>".parse().unwrap();
41        ChatState::try_from(elem).unwrap();
42    }
43
44    #[test]
45    fn test_invalid() {
46        let elem: Element = "<coucou xmlns='http://jabber.org/protocol/chatstates'/>".parse().unwrap();
47        let error = ChatState::try_from(elem).unwrap_err();
48        let message = match error {
49            Error::ParseError(string) => string,
50            _ => panic!(),
51        };
52        assert_eq!(message, "This is not a chatstate element.");
53    }
54
55    #[test]
56    fn test_invalid_child() {
57        let elem: Element = "<gone xmlns='http://jabber.org/protocol/chatstates'><coucou/></gone>".parse().unwrap();
58        let error = ChatState::try_from(elem).unwrap_err();
59        let message = match error {
60            Error::ParseError(string) => string,
61            _ => panic!(),
62        };
63        assert_eq!(message, "Unknown child in chatstate element.");
64    }
65
66    #[test]
67    fn test_invalid_attribute() {
68        let elem: Element = "<inactive xmlns='http://jabber.org/protocol/chatstates' coucou=''/>".parse().unwrap();
69        let error = ChatState::try_from(elem).unwrap_err();
70        let message = match error {
71            Error::ParseError(string) => string,
72            _ => panic!(),
73        };
74        assert_eq!(message, "Unknown attribute in chatstate element.");
75    }
76
77    #[test]
78    fn test_serialise() {
79        let chatstate = ChatState::Active;
80        let elem: Element = chatstate.into();
81        assert!(elem.is("active", ns::CHATSTATES));
82    }
83}