IdTestVCard.pas 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. unit IdTestVCard;
  2. {
  3. http://www.faqs.org/rfcs/rfc2425.html A MIME Content-Type for Directory Information
  4. http://www.faqs.org/rfcs/rfc2426.html vCard MIME Directory Profile
  5. http://www.faqs.org/rfcs/rfc2739.html Calendar Attributes for vCard and LDAP
  6. }
  7. interface
  8. uses
  9. IdSys,
  10. IdTest,
  11. IdObjs,
  12. IdVCard;
  13. type
  14. TIdTestVCard = class(TIdTest)
  15. published
  16. procedure TestLoad;
  17. procedure TestParseDateTime;
  18. end;
  19. implementation
  20. const
  21. cEol=#13#10;
  22. cName='Holmes;Sherlock;;Mr';
  23. cFullName='Sherlock Holmes';
  24. //should test with more variations here
  25. cCompany='Example Company';
  26. cDivisions='Division 1;Division 2';
  27. cPhoneWorkVoice='+49 (555) 12345';
  28. cEmail='[email protected]';
  29. {
  30. The structured type value corresponds,
  31. in sequence, to the post office box; the extended address; the street
  32. address; the locality (e.g., city); the region (e.g., state or
  33. province); the postal code; the country Name.
  34. }
  35. cAddrPOBox='My PO Box';
  36. cAddrExtended='More Address';
  37. cAddrStreet='My Street';
  38. cAddrLocality='My Locality';
  39. cAddrRegion='My Region';
  40. cAddrPostCode='12345';
  41. cAddrNation='My Nation';
  42. cData='BEGIN:VCARD'+cEol
  43. +'BEGIN:VCARD'+cEol
  44. +'VERSION:2.1'+cEol
  45. +'N:'+cName+cEol
  46. +'FN:'+cFullName+cEol
  47. +'ORG:'+cCompany+';'+cDivisions+cEol
  48. +'TEL;WORK;VOICE:'+cPhoneWorkVoice+cEol
  49. +'ADR;WORK:'+cAddrPOBox+';'+cAddrExtended+';'+cAddrStreet+';'+cAddrLocality+';'+cAddrRegion+';'+cAddrPostCode+';'+cAddrNation+cEol
  50. //LABEL;WORK;ENCODING=QUOTED-PRINTABLE:Testroad 1=0D=0AEntenhausen 12345=0D=0ADeutschland
  51. +'EMAIL;PREF;INTERNET:'+cEMail+cEol
  52. //REV:20050531T195358Z
  53. +'END:VCARD';
  54. procedure TIdTestVCard.TestLoad;
  55. //could also test
  56. var
  57. aCard:TIdVCard;
  58. aList:TIdStringList;
  59. aPhone:TIdCardPhoneNumber;
  60. aMail:TIdVCardEMailItem;
  61. aAddr:TIdCardAddressItem;
  62. begin
  63. aCard:=TIdVCard.Create(nil);
  64. aList:=TIdStringList.Create;
  65. try
  66. aList.Text:=cData;
  67. aCard.ReadFromStrings(aList);
  68. Assert(aCard.Telephones.Count=1);
  69. aPhone:=aCard.Telephones[0];
  70. Assert(aPhone.Number=cPhoneWorkVoice);
  71. Assert(aPhone.PhoneAttributes=[tpaWork,tpaVoice]);
  72. Assert(aCard.EMailAddresses.Count=1);
  73. aMail:=aCard.EMailAddresses[0];
  74. Assert(aMail.Preferred);
  75. Assert(aMail.Address=cEmail);
  76. Assert(aMail.EMailType=ematInternet);
  77. //see rfc2426 3.2.1 ADR Type Definition
  78. Assert(aCard.Addresses.Count=1);
  79. aAddr:=aCard.Addresses[0];
  80. Assert(aAddr.AddressAttributes=[tatWork]);
  81. Assert(aAddr.POBox=cAddrPOBox);
  82. Assert(aAddr.ExtendedAddress=cAddrExtended);
  83. Assert(aAddr.StreetAddress=cAddrStreet);
  84. Assert(aAddr.Locality=cAddrLocality);
  85. Assert(aAddr.Region=cAddrRegion);
  86. Assert(aAddr.PostalCode=cAddrPostCode);
  87. Assert(aAddr.Nation=cAddrNation);
  88. //LastRevised isnt published by vcard, fix?
  89. //see rfc2426 3.5.5 ORG Type Definition
  90. //fails, bug in procedure ParseOrg
  91. //example:
  92. //ORG:ABC\, Inc.;North American Division;Marketing
  93. //todo1 add a test for parsing an escaped-comma. could be in ANY property
  94. Assert(aCard.BusinessInfo.Organization=cCompany);
  95. aCard.BusinessInfo.Divisions.Delimiter:=';';
  96. //delimitedtext adds quotes
  97. //Assert(aCard.BusinessInfo.Divisions.DelimitedText=cDivisions);
  98. Assert(aCard.BusinessInfo.Divisions.Count=2);
  99. finally
  100. Sys.FreeAndNil(aCard);
  101. Sys.FreeAndNil(aList);
  102. end;
  103. end;
  104. procedure TIdTestVCard.TestParseDateTime;
  105. const
  106. cFormat='yyyy-mm-dd hh:nn:ss';
  107. cDate1='1995-10-31T22:27:10Z';
  108. cDate1b='1995-10-31 22:27:10';
  109. cDate2='1997-11-15';
  110. cDate2b='1997-11-15 00:00:00';
  111. cOutlookFormat='20050531T195358Z';
  112. cOutlookFormatb='2005-05-31 19:53:58';
  113. var
  114. aDate:TDateTime;
  115. begin
  116. aDate:=ParseDateTimeStamp(cDate1);
  117. Assert(Sys.FormatDateTime(cFormat,aDate)=cDate1b);
  118. aDate:=ParseDateTimeStamp(cDate2);
  119. Assert(Sys.FormatDateTime(cFormat,aDate)=cDate2b);
  120. aDate:=ParseDateTimeStamp(cOutlookFormat);
  121. Assert(Sys.FormatDateTime(cFormat,aDate)=cOutlookFormatb);
  122. end;
  123. initialization
  124. TIdTest.RegisterTest(TIdTestVCard);
  125. end.