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 try_from::TryFrom;
25    use minidom::Element;
26    use crate::error::Error;
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'/>".parse().unwrap();
43        Replace::try_from(elem).unwrap();
44    }
45
46    #[test]
47    fn test_invalid_attribute() {
48        let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0' coucou=''/>".parse().unwrap();
49        let error = Replace::try_from(elem).unwrap_err();
50        let message = match error {
51            Error::ParseError(string) => string,
52            _ => panic!(),
53        };
54        assert_eq!(message, "Unknown attribute in replace element.");
55    }
56
57    #[test]
58    fn test_invalid_child() {
59        let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0'><coucou/></replace>".parse().unwrap();
60        let error = Replace::try_from(elem).unwrap_err();
61        let message = match error {
62            Error::ParseError(string) => string,
63            _ => panic!(),
64        };
65        assert_eq!(message, "Unknown child in replace element.");
66    }
67
68    #[test]
69    fn test_invalid_id() {
70        let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0'/>".parse().unwrap();
71        let error = Replace::try_from(elem).unwrap_err();
72        let message = match error {
73            Error::ParseError(string) => string,
74            _ => panic!(),
75        };
76        assert_eq!(message, "Required attribute 'id' missing.");
77    }
78
79    #[test]
80    fn test_serialise() {
81        let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0' id='coucou'/>".parse().unwrap();
82        let replace = Replace { id: String::from("coucou") };
83        let elem2 = replace.into();
84        assert_eq!(elem, elem2);
85    }
86}