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