1// Copyright (c) 2018 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_elem_id!(
8 /// Represents a global, memorable, friendly or informal name chosen by a user.
9 Nick, "nick", NICK
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 = "<nick xmlns='http://jabber.org/protocol/nick'>Link Mauve</nick>".parse().unwrap();
22 let nick = Nick::try_from(elem).unwrap();
23 assert_eq!(&nick.0, "Link Mauve");
24 }
25
26 #[test]
27 fn test_serialise() {
28 let elem1 = Element::from(Nick(String::from("Link Mauve")));
29 let elem2: Element = "<nick xmlns='http://jabber.org/protocol/nick'>Link Mauve</nick>".parse().unwrap();
30 assert_eq!(elem1, elem2);
31 }
32
33 #[test]
34 fn test_invalid() {
35 let elem: Element = "<nick xmlns='http://jabber.org/protocol/nick'><coucou/></nick>".parse().unwrap();
36 let error = Nick::try_from(elem).unwrap_err();
37 let message = match error {
38 Error::ParseError(string) => string,
39 _ => panic!(),
40 };
41 assert_eq!(message, "Unknown child in nick element.");
42 }
43
44 #[test]
45 fn test_invalid_attribute() {
46 let elem: Element = "<nick xmlns='http://jabber.org/protocol/nick' coucou=''/>".parse().unwrap();
47 let error = Nick::try_from(elem).unwrap_err();
48 let message = match error {
49 Error::ParseError(string) => string,
50 _ => panic!(),
51 };
52 assert_eq!(message, "Unknown attribute in nick element.");
53 }
54}