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