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