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
 7use message::MessagePayload;
 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
30impl MessagePayload for ChatState {}
31
32#[cfg(test)]
33mod tests {
34    use super::*;
35    use try_from::TryFrom;
36    use minidom::Element;
37    use error::Error;
38    use ns;
39
40    #[test]
41    fn test_size() {
42        assert_size!(ChatState, 1);
43    }
44
45    #[test]
46    fn test_simple() {
47        let elem: Element = "<active xmlns='http://jabber.org/protocol/chatstates'/>".parse().unwrap();
48        ChatState::try_from(elem).unwrap();
49    }
50
51    #[test]
52    fn test_invalid() {
53        let elem: Element = "<coucou xmlns='http://jabber.org/protocol/chatstates'/>".parse().unwrap();
54        let error = ChatState::try_from(elem).unwrap_err();
55        let message = match error {
56            Error::ParseError(string) => string,
57            _ => panic!(),
58        };
59        assert_eq!(message, "This is not a chatstate element.");
60    }
61
62    #[test]
63    fn test_invalid_child() {
64        let elem: Element = "<gone xmlns='http://jabber.org/protocol/chatstates'><coucou/></gone>".parse().unwrap();
65        let error = ChatState::try_from(elem).unwrap_err();
66        let message = match error {
67            Error::ParseError(string) => string,
68            _ => panic!(),
69        };
70        assert_eq!(message, "Unknown child in chatstate element.");
71    }
72
73    #[test]
74    fn test_invalid_attribute() {
75        let elem: Element = "<inactive xmlns='http://jabber.org/protocol/chatstates' coucou=''/>".parse().unwrap();
76        let error = ChatState::try_from(elem).unwrap_err();
77        let message = match error {
78            Error::ParseError(string) => string,
79            _ => panic!(),
80        };
81        assert_eq!(message, "Unknown attribute in chatstate element.");
82    }
83
84    #[test]
85    fn test_serialise() {
86        let chatstate = ChatState::Active;
87        let elem: Element = chatstate.into();
88        assert!(elem.is("active", ns::CHATSTATES));
89    }
90}