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::util::error::Error;
25    use crate::Element;
26    use std::convert::TryFrom;
27
28    #[cfg(target_pointer_width = "32")]
29    #[test]
30    fn test_size() {
31        assert_size!(Replace, 12);
32    }
33
34    #[cfg(target_pointer_width = "64")]
35    #[test]
36    fn test_size() {
37        assert_size!(Replace, 24);
38    }
39
40    #[test]
41    fn test_simple() {
42        let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0' id='coucou'/>"
43            .parse()
44            .unwrap();
45        Replace::try_from(elem).unwrap();
46    }
47
48    #[cfg(not(feature = "disable-validation"))]
49    #[test]
50    fn test_invalid_attribute() {
51        let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0' coucou=''/>"
52            .parse()
53            .unwrap();
54        let error = Replace::try_from(elem).unwrap_err();
55        let message = match error {
56            Error::ParseError(string) => string,
57            _ => panic!(),
58        };
59        assert_eq!(message, "Unknown attribute in replace element.");
60    }
61
62    #[test]
63    fn test_invalid_child() {
64        let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0'><coucou/></replace>"
65            .parse()
66            .unwrap();
67        let error = Replace::try_from(elem).unwrap_err();
68        let message = match error {
69            Error::ParseError(string) => string,
70            _ => panic!(),
71        };
72        assert_eq!(message, "Unknown child in replace element.");
73    }
74
75    #[test]
76    fn test_invalid_id() {
77        let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0'/>"
78            .parse()
79            .unwrap();
80        let error = Replace::try_from(elem).unwrap_err();
81        let message = match error {
82            Error::ParseError(string) => string,
83            _ => panic!(),
84        };
85        assert_eq!(message, "Required attribute 'id' missing.");
86    }
87
88    #[test]
89    fn test_serialise() {
90        let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0' id='coucou'/>"
91            .parse()
92            .unwrap();
93        let replace = Replace {
94            id: String::from("coucou"),
95        };
96        let elem2 = replace.into();
97        assert_eq!(elem, elem2);
98    }
99}