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
 7generate_element_enum!(
 8    /// Enum representing chatstate elements part of the
 9    /// `http://jabber.org/protocol/chatstates` namespace.
10    ChatState, "chatstate", CHATSTATES, {
11        /// `<active xmlns='http://jabber.org/protocol/chatstates'/>`
12        Active => "active",
13
14        /// `<composing xmlns='http://jabber.org/protocol/chatstates'/>`
15        Composing => "composing",
16
17        /// `<gone xmlns='http://jabber.org/protocol/chatstates'/>`
18        Gone => "gone",
19
20        /// `<inactive xmlns='http://jabber.org/protocol/chatstates'/>`
21        Inactive => "inactive",
22
23        /// `<paused xmlns='http://jabber.org/protocol/chatstates'/>`
24        Paused => "paused",
25    }
26);
27
28#[cfg(test)]
29mod tests {
30    use super::*;
31    use try_from::TryFrom;
32    use minidom::Element;
33    use error::Error;
34    use ns;
35
36    #[test]
37    fn test_simple() {
38        let elem: Element = "<active xmlns='http://jabber.org/protocol/chatstates'/>".parse().unwrap();
39        ChatState::try_from(elem).unwrap();
40    }
41
42    #[test]
43    fn test_invalid() {
44        let elem: Element = "<coucou xmlns='http://jabber.org/protocol/chatstates'/>".parse().unwrap();
45        let error = ChatState::try_from(elem).unwrap_err();
46        let message = match error {
47            Error::ParseError(string) => string,
48            _ => panic!(),
49        };
50        assert_eq!(message, "This is not a chatstate element.");
51    }
52
53    #[test]
54    fn test_invalid_child() {
55        let elem: Element = "<gone xmlns='http://jabber.org/protocol/chatstates'><coucou/></gone>".parse().unwrap();
56        let error = ChatState::try_from(elem).unwrap_err();
57        let message = match error {
58            Error::ParseError(string) => string,
59            _ => panic!(),
60        };
61        assert_eq!(message, "Unknown child in chatstate element.");
62    }
63
64    #[test]
65    fn test_invalid_attribute() {
66        let elem: Element = "<inactive xmlns='http://jabber.org/protocol/chatstates' coucou=''/>".parse().unwrap();
67        let error = ChatState::try_from(elem).unwrap_err();
68        let message = match error {
69            Error::ParseError(string) => string,
70            _ => panic!(),
71        };
72        assert_eq!(message, "Unknown attribute in chatstate element.");
73    }
74
75    #[test]
76    fn test_serialise() {
77        let chatstate = ChatState::Active;
78        let elem: Element = chatstate.into();
79        assert!(elem.is("active", ns::CHATSTATES));
80    }
81}