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 date::DateTime;
8
9generate_element_with_only_attributes!(Idle, "idle", IDLE, [
10 since: DateTime = "since" => required,
11]);
12
13#[cfg(test)]
14mod tests {
15 use super::*;
16 use try_from::TryFrom;
17 use minidom::Element;
18 use error::Error;
19 use std::str::FromStr;
20 use std::error::Error as StdError;
21
22 #[test]
23 fn test_simple() {
24 let elem: Element = "<idle xmlns='urn:xmpp:idle:1' since='2017-05-21T20:19:55+01:00'/>".parse().unwrap();
25 Idle::try_from(elem).unwrap();
26 }
27
28 #[test]
29 fn test_invalid_child() {
30 let elem: Element = "<idle xmlns='urn:xmpp:idle:1'><coucou/></idle>".parse().unwrap();
31 let error = Idle::try_from(elem).unwrap_err();
32 let message = match error {
33 Error::ParseError(string) => string,
34 _ => panic!(),
35 };
36 assert_eq!(message, "Unknown child in idle element.");
37 }
38
39 #[test]
40 fn test_invalid_id() {
41 let elem: Element = "<idle xmlns='urn:xmpp:idle:1'/>".parse().unwrap();
42 let error = Idle::try_from(elem).unwrap_err();
43 let message = match error {
44 Error::ParseError(string) => string,
45 _ => panic!(),
46 };
47 assert_eq!(message, "Required attribute 'since' missing.");
48 }
49
50 #[test]
51 fn test_invalid_date() {
52 // There is no thirteenth month.
53 let elem: Element = "<idle xmlns='urn:xmpp:idle:1' since='2017-13-01T12:23:34Z'/>".parse().unwrap();
54 let error = Idle::try_from(elem).unwrap_err();
55 let message = match error {
56 Error::ChronoParseError(string) => string,
57 _ => panic!(),
58 };
59 assert_eq!(message.description(), "input is out of range");
60
61 // Timezone ≥24:00 aren’t allowed.
62 let elem: Element = "<idle xmlns='urn:xmpp:idle:1' since='2017-05-27T12:11:02+25:00'/>".parse().unwrap();
63 let error = Idle::try_from(elem).unwrap_err();
64 let message = match error {
65 Error::ChronoParseError(string) => string,
66 _ => panic!(),
67 };
68 assert_eq!(message.description(), "input is out of range");
69
70 // Timezone without the : separator aren’t allowed.
71 let elem: Element = "<idle xmlns='urn:xmpp:idle:1' since='2017-05-27T12:11:02+0100'/>".parse().unwrap();
72 let error = Idle::try_from(elem).unwrap_err();
73 let message = match error {
74 Error::ChronoParseError(string) => string,
75 _ => panic!(),
76 };
77 assert_eq!(message.description(), "input contains invalid characters");
78
79 // No seconds, error message could be improved.
80 let elem: Element = "<idle xmlns='urn:xmpp:idle:1' since='2017-05-27T12:11+01:00'/>".parse().unwrap();
81 let error = Idle::try_from(elem).unwrap_err();
82 let message = match error {
83 Error::ChronoParseError(string) => string,
84 _ => panic!(),
85 };
86 assert_eq!(message.description(), "input contains invalid characters");
87
88 // TODO: maybe we’ll want to support this one, as per XEP-0082 §4.
89 let elem: Element = "<idle xmlns='urn:xmpp:idle:1' since='20170527T12:11:02+01:00'/>".parse().unwrap();
90 let error = Idle::try_from(elem).unwrap_err();
91 let message = match error {
92 Error::ChronoParseError(string) => string,
93 _ => panic!(),
94 };
95 assert_eq!(message.description(), "input contains invalid characters");
96
97 // No timezone.
98 let elem: Element = "<idle xmlns='urn:xmpp:idle:1' since='2017-05-27T12:11:02'/>".parse().unwrap();
99 let error = Idle::try_from(elem).unwrap_err();
100 let message = match error {
101 Error::ChronoParseError(string) => string,
102 _ => panic!(),
103 };
104 assert_eq!(message.description(), "premature end of input");
105 }
106
107 #[test]
108 fn test_serialise() {
109 let elem: Element = "<idle xmlns='urn:xmpp:idle:1' since='2017-05-21T20:19:55+01:00'/>".parse().unwrap();
110 let idle = Idle { since: DateTime::from_str("2017-05-21T20:19:55+01:00").unwrap() };
111 let elem2 = idle.into();
112 assert_eq!(elem, elem2);
113 }
114}