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