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 crate::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 crate::ns;
 36    use crate::util::error::Error;
 37    use crate::Element;
 38    use std::convert::TryFrom;
 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'/>"
 48            .parse()
 49            .unwrap();
 50        ChatState::try_from(elem).unwrap();
 51    }
 52
 53    #[test]
 54    fn test_invalid() {
 55        let elem: Element = "<coucou xmlns='http://jabber.org/protocol/chatstates'/>"
 56            .parse()
 57            .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, "This is not a chatstate element.");
 64    }
 65
 66    #[cfg(not(feature = "disable-validation"))]
 67    #[test]
 68    fn test_invalid_child() {
 69        let elem: Element = "<gone xmlns='http://jabber.org/protocol/chatstates'><coucou/></gone>"
 70            .parse()
 71            .unwrap();
 72        let error = ChatState::try_from(elem).unwrap_err();
 73        let message = match error {
 74            Error::ParseError(string) => string,
 75            _ => panic!(),
 76        };
 77        assert_eq!(message, "Unknown child in chatstate element.");
 78    }
 79
 80    #[cfg(not(feature = "disable-validation"))]
 81    #[test]
 82    fn test_invalid_attribute() {
 83        let elem: Element = "<inactive xmlns='http://jabber.org/protocol/chatstates' coucou=''/>"
 84            .parse()
 85            .unwrap();
 86        let error = ChatState::try_from(elem).unwrap_err();
 87        let message = match error {
 88            Error::ParseError(string) => string,
 89            _ => panic!(),
 90        };
 91        assert_eq!(message, "Unknown attribute in chatstate element.");
 92    }
 93
 94    #[test]
 95    fn test_serialise() {
 96        let chatstate = ChatState::Active;
 97        let elem: Element = chatstate.into();
 98        assert!(elem.is("active", ns::CHATSTATES));
 99    }
100}