IdTestCoderQuotedPrintable.pas 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. unit IdTestCoderQuotedPrintable;
  2. interface
  3. //http://www.freesoft.org/CIE/RFC/1521/6.htm
  4. uses
  5. IdCoder,
  6. IdCoderQuotedPrintable,
  7. IdObjs,
  8. IdStack,
  9. IdStream,
  10. IdTest,
  11. IdSys;
  12. type
  13. TIdTestCoderQuotedPrintable = class(TIdTest)
  14. private
  15. protected
  16. function EncDec(const aStr:string):boolean;
  17. published
  18. procedure TestCodec;
  19. end;
  20. implementation
  21. //this should be elsewhere, or a proper function
  22. function StrRight(const aStr:string;const aCount:integer):string;
  23. begin
  24. Result:=Copy(aStr,Length(aStr)-aCount+1,aCount);
  25. end;
  26. function TIdTestCoderQuotedPrintable.EncDec;
  27. //do a round-trip encode>decode, and check
  28. var
  29. aEnc,aDec:string;
  30. aExpect:string;
  31. aList:TIdStringList;
  32. i:Integer;
  33. begin
  34. aEnc:=EncodeString(TIdEncoderQuotedPrintable,aStr);
  35. //check that no lines start with a .
  36. aList:=TIdStringList.Create;
  37. try
  38. aList.Text:=aEnc;
  39. for i:=0 to aList.Count-1 do
  40. begin
  41. if aList[i]='' then Continue;
  42. if aList[i][1]='.' then Assert(False,aList[i]);
  43. end;
  44. finally
  45. Sys.FreeAndNil(aList);
  46. end;
  47. aDec:=DecodeString(TIdDecoderQuotedPrintable,aEnc);
  48. //seems that it adds a crlf if not already there
  49. if StrRight(aStr,2)=#13#10 then aExpect:=aStr
  50. else aExpect:=aStr+#13#10;
  51. Result:=(aDec=aExpect);
  52. end;
  53. procedure TIdTestCoderQuotedPrintable.TestCodec;
  54. var
  55. s:string;
  56. const
  57. cStr='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
  58. begin
  59. //should do a loop from 1..100?
  60. s:=cStr+#13#10;
  61. Assert(EncDec(s));
  62. Assert(EncDec('one'#13#10'.two.'#13#10));
  63. Assert(EncDec('='));
  64. Assert(EncDec('1aAzZ<>!@#$%^&*()-=/;[]"'));
  65. Assert(EncDec('.'));
  66. Assert(EncDec('.test.'));
  67. Assert(EncDec('.line1.'#13#10'.line2.'));
  68. s:=EncodeString(TIdEncoderQuotedPrintable,'abc ');
  69. Assert(s='abc =20'#13#10);
  70. s:=EncodeString(TIdEncoderQuotedPrintable,'...');
  71. Assert(s='=2E..'#13#10);
  72. s:='123456789 123456789 123456789 123456789 123456789 123456789 123456789 .23456789';
  73. Assert(EncDec(s));
  74. end;
  75. initialization
  76. TIdTest.RegisterTest(TIdTestCoderQuotedPrintable);
  77. end.