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: String = "id" => required,
16    ]
17);
18
19impl MessagePayload for Replace {}
20
21#[cfg(test)]
22mod tests {
23    use super::*;
24    use crate::error::Error;
25    use minidom::Element;
26    use try_from::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    #[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            Error::ParseError(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            Error::ParseError(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            Error::ParseError(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}