#![cfg(feature = "parser")]
use std::borrow::Cow;
use vcard::{
tree::{
cst::VcardCst,
prop::{r#fn::FN, n::N, nickname::NICKNAME, note::NOTE, org::ORG},
},
version::VcardVersion,
};
fn strs<'a>(list: &'a [Cow<'a, str>]) -> Vec<&'a str> {
list.iter().map(|c| c.as_ref()).collect()
}
#[test]
fn emersion_rfc_card_fn_and_n() {
let card = VcardCst::parse(concat!(
"BEGIN:VCARD\r\n",
"VERSION:4.0\r\n",
"UID:urn:uuid:4fbe8971-0bc3-424c-9c26-36c3e1eff6b1\r\n",
"FN;PID=1.1:J. Doe\r\n",
"N:Doe;J.;;;\r\n",
"EMAIL;PID=1.1:jdoe@example.com\r\n",
"CLIENTPIDMAP:1;urn:uuid:53e374d9-337e-4727-8803-a1e9c14e0556\r\n",
"END:VCARD\r\n",
))
.unwrap();
assert_eq!(&*card.prop::<FN>().unwrap().0, "J. Doe");
let n = card.prop::<N>().unwrap();
assert_eq!(strs(&n.family), ["Doe"]);
assert_eq!(strs(&n.given), ["J."]);
}
#[test]
fn emersion_escaped_note_unescapes() {
let card = VcardCst::parse(concat!(
"BEGIN:VCARD\r\n",
"VERSION:4.0\r\n",
"NOTE:Mythical Manager\\nHyjinx Software Division\\nBabsCo\\, Inc.\\n\r\n",
"END:VCARD\r\n",
))
.unwrap();
assert_eq!(
&*card.prop::<NOTE>().unwrap().0,
"Mythical Manager\nHyjinx Software Division\nBabsCo, Inc.\n",
);
}
#[test]
fn heymdall_structured_name() {
let card = VcardCst::parse("N:Gump;Forrest;;Mr.;").unwrap();
let n = card.prop::<N>().unwrap();
assert_eq!(strs(&n.family), ["Gump"]);
assert_eq!(strs(&n.given), ["Forrest"]);
assert_eq!(strs(&n.additional), [""]);
assert_eq!(strs(&n.prefixes), ["Mr."]);
assert_eq!(strs(&n.suffixes), [""]);
}
#[test]
fn heymdall_org_components() {
let card = VcardCst::parse("ORG:ABC\\, Inc.;North American Division;Marketing").unwrap();
assert_eq!(
strs(&card.prop::<ORG>().unwrap().0),
["ABC, Inc.", "North American Division", "Marketing"],
);
}
#[test]
fn heymdall_nickname_list() {
let card = VcardCst::parse("NICKNAME:Jim,Jimmie").unwrap();
assert_eq!(strs(&card.prop::<NICKNAME>().unwrap().0), ["Jim", "Jimmie"]);
}
#[test]
fn heymdall_formatted_name() {
let card = VcardCst::parse("FN:Forrest Gump").unwrap();
assert_eq!(&*card.prop::<FN>().unwrap().0, "Forrest Gump");
}
#[test]
fn nilclass_three_part_name() {
let card = VcardCst::parse(concat!(
"BEGIN:VCARD\r\n",
"VERSION:4.0\r\n",
"N:Lessing;Gotthold;Ephraim;;\r\n",
"END:VCARD\r\n",
))
.unwrap();
let n = card.prop::<N>().unwrap();
assert_eq!(strs(&n.family), ["Lessing"]);
assert_eq!(strs(&n.given), ["Gotthold"]);
assert_eq!(strs(&n.additional), ["Ephraim"]);
}
#[test]
fn nilclass_complex_name() {
let card = VcardCst::parse(concat!(
"BEGIN:VCARD\r\n",
"VERSION:4.0\r\n",
"N:Lessing;Gotthold;Ephraim,Soundso;Dr.,Prof.;von und zu hier und da\r\n",
"END:VCARD\r\n",
))
.unwrap();
let n = card.prop::<N>().unwrap();
assert_eq!(strs(&n.family), ["Lessing"]);
assert_eq!(strs(&n.given), ["Gotthold"]);
assert_eq!(strs(&n.additional), ["Ephraim", "Soundso"]);
assert_eq!(strs(&n.prefixes), ["Dr.", "Prof."]);
assert_eq!(strs(&n.suffixes), ["von und zu hier und da"]);
}
#[test]
fn nilclass_nicknames() {
let card = VcardCst::parse(concat!(
"BEGIN:VCARD\r\n",
"VERSION:4.0\r\n",
"NICKNAME:foo,bar,baz\r\n",
"END:VCARD\r\n",
))
.unwrap();
assert_eq!(
strs(&card.prop::<NICKNAME>().unwrap().0),
["foo", "bar", "baz"],
);
}
#[test]
fn nilclass_version() {
let card = VcardCst::parse("BEGIN:VCARD\r\nVERSION:4.0\r\nEND:VCARD\r\n").unwrap();
assert_eq!(card.version(), VcardVersion::V4_0);
}