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