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