message_correct.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 xso::{AsXml, FromXml};
  8
  9use crate::message::{Id, MessagePayload};
 10use crate::ns;
 11
 12/// Defines that the message containing this payload should replace a
 13/// previous message, identified by the id.
 14#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
 15#[xml(namespace = ns::MESSAGE_CORRECT, name = "replace")]
 16pub struct Replace {
 17    /// The 'id' attribute of the message getting corrected.
 18    #[xml(attribute)]
 19    pub id: Id,
 20}
 21
 22impl MessagePayload for Replace {}
 23
 24#[cfg(test)]
 25mod tests {
 26    use super::*;
 27    use minidom::Element;
 28    use xso::error::{Error, FromElementError};
 29
 30    #[cfg(target_pointer_width = "32")]
 31    #[test]
 32    fn test_size() {
 33        assert_size!(Replace, 12);
 34    }
 35
 36    #[cfg(target_pointer_width = "64")]
 37    #[test]
 38    fn test_size() {
 39        assert_size!(Replace, 24);
 40    }
 41
 42    #[test]
 43    fn test_simple() {
 44        let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0' id='coucou'/>"
 45            .parse()
 46            .unwrap();
 47        Replace::try_from(elem).unwrap();
 48    }
 49
 50    #[cfg(not(feature = "disable-validation"))]
 51    #[test]
 52    fn test_invalid_attribute() {
 53        let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0' id='coucou' coucou=''/>"
 54            .parse()
 55            .unwrap();
 56        let error = Replace::try_from(elem).unwrap_err();
 57        let message = match error {
 58            FromElementError::Invalid(Error::Other(string)) => string,
 59            _ => panic!(),
 60        };
 61        assert_eq!(message, "Unknown attribute in Replace element.");
 62    }
 63
 64    #[test]
 65    #[cfg_attr(feature = "disable-validation", should_panic = "Result::unwrap_err")]
 66    fn test_invalid_child() {
 67        let elem: Element =
 68            "<replace xmlns='urn:xmpp:message-correct:0' id='coucou'><coucou/></replace>"
 69                .parse()
 70                .unwrap();
 71        let error = Replace::try_from(elem).unwrap_err();
 72        let message = match error {
 73            FromElementError::Invalid(Error::Other(string)) => string,
 74            _ => panic!(),
 75        };
 76        assert_eq!(message, "Unknown child in Replace element.");
 77    }
 78
 79    #[test]
 80    fn test_invalid_id() {
 81        let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0'/>"
 82            .parse()
 83            .unwrap();
 84        let error = Replace::try_from(elem).unwrap_err();
 85        let message = match error {
 86            FromElementError::Invalid(Error::Other(string)) => string,
 87            _ => panic!(),
 88        };
 89        assert_eq!(
 90            message,
 91            "Required attribute field 'id' on Replace element missing."
 92        );
 93    }
 94
 95    #[test]
 96    fn test_serialise() {
 97        let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0' id='coucou'/>"
 98            .parse()
 99            .unwrap();
100        let replace = Replace {
101            id: Id(String::from("coucou")),
102        };
103        let elem2 = replace.into();
104        assert_eq!(elem, elem2);
105    }
106}