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