nick.rs

 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
 7#![deny(missing_docs)]
 8
 9generate_elem_id!(
10    /// Represents a global, memorable, friendly or informal name chosen by a user.
11    Nick, "nick", NICK
12);
13
14#[cfg(test)]
15mod tests {
16    use super::*;
17    use try_from::TryFrom;
18    use minidom::Element;
19    use error::Error;
20
21    #[test]
22    fn test_simple() {
23        let elem: Element = "<nick xmlns='http://jabber.org/protocol/nick'>Link Mauve</nick>".parse().unwrap();
24        let nick = Nick::try_from(elem).unwrap();
25        assert_eq!(&nick.0, "Link Mauve");
26    }
27
28    #[test]
29    fn test_serialise() {
30        let elem1 = Element::from(Nick(String::from("Link Mauve")));
31        let elem2: Element = "<nick xmlns='http://jabber.org/protocol/nick'>Link Mauve</nick>".parse().unwrap();
32        assert_eq!(elem1, elem2);
33    }
34
35    #[test]
36    fn test_invalid() {
37        let elem: Element = "<nick xmlns='http://jabber.org/protocol/nick'><coucou/></nick>".parse().unwrap();
38        let error = Nick::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 nick element.");
44    }
45
46    #[test]
47    fn test_invalid_attribute() {
48        let elem: Element = "<nick xmlns='http://jabber.org/protocol/nick' coucou=''/>".parse().unwrap();
49        let error = Nick::try_from(elem).unwrap_err();
50        let message = match error {
51            Error::ParseError(string) => string,
52            _ => panic!(),
53        };
54        assert_eq!(message, "Unknown attribute in nick element.");
55    }
56}