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 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 error::Error;
27
28    #[test]
29    fn test_size() {
30        assert_size!(Replace, 24);
31    }
32
33    #[test]
34    fn test_simple() {
35        let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0' id='coucou'/>".parse().unwrap();
36        Replace::try_from(elem).unwrap();
37    }
38
39    #[test]
40    fn test_invalid_attribute() {
41        let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0' coucou=''/>".parse().unwrap();
42        let error = Replace::try_from(elem).unwrap_err();
43        let message = match error {
44            Error::ParseError(string) => string,
45            _ => panic!(),
46        };
47        assert_eq!(message, "Unknown attribute in replace element.");
48    }
49
50    #[test]
51    fn test_invalid_child() {
52        let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0'><coucou/></replace>".parse().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 child in replace element.");
59    }
60
61    #[test]
62    fn test_invalid_id() {
63        let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0'/>".parse().unwrap();
64        let error = Replace::try_from(elem).unwrap_err();
65        let message = match error {
66            Error::ParseError(string) => string,
67            _ => panic!(),
68        };
69        assert_eq!(message, "Required attribute 'id' missing.");
70    }
71
72    #[test]
73    fn test_serialise() {
74        let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0' id='coucou'/>".parse().unwrap();
75        let replace = Replace { id: String::from("coucou") };
76        let elem2 = replace.into();
77        assert_eq!(elem, elem2);
78    }
79}