tccsvdocument.pp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. unit tccsvdocument;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, fpcunit, testregistry, csvdocument;
  6. Type
  7. { TTestCSVDocument }
  8. TTestCSVDocument = Class(TTestCase)
  9. private
  10. FDoc: TCSVDocument;
  11. procedure RemoveTestFile;
  12. function StripQuotes(S: String): string;
  13. procedure TestTestFile;
  14. Public
  15. Procedure SetUp; override;
  16. Procedure TearDown; override;
  17. Procedure CreateTestFile;
  18. Property Doc : TCSVDocument Read FDoc;
  19. Published
  20. Procedure TestEmpty;
  21. Procedure TestRead;
  22. end;
  23. implementation
  24. Const
  25. TestFileName = 'test.csv';
  26. { TTestCSVDocument }
  27. procedure TTestCSVDocument.SetUp;
  28. begin
  29. FDoc:=TCSVDocument.Create;
  30. Inherited;
  31. end;
  32. procedure TTestCSVDocument.TearDown;
  33. begin
  34. RemoveTestFile;
  35. FreeAndNil(FDoc);
  36. Inherited;
  37. end;
  38. procedure TTestCSVDocument.RemoveTestFile;
  39. begin
  40. If FileExists(TestFileName) then
  41. AssertTrue('Deleting test file',DeleteFile(TestFileName));
  42. end;
  43. Const
  44. ColCount = 3;
  45. RowCount = 4;
  46. Type
  47. TRow = Array[0..ColCount-1] of string;
  48. TCells = Array[0..RowCount-1] of TRow;
  49. Const
  50. Cells : TCells = (
  51. ('a','b','c'),
  52. ('1','"one"','1.1'),
  53. ('2','"two"','2.2'),
  54. ('3','"three"','3.3')
  55. );
  56. procedure TTestCSVDocument.CreateTestFile;
  57. Var
  58. L : TStringList;
  59. R,C : Integer;
  60. S : String;
  61. begin
  62. L:=TStringList.Create;
  63. try
  64. for R:=0 to RowCount-1 do
  65. begin
  66. S:='';
  67. for C:=0 to ColCount-1 do
  68. begin
  69. if S<>'' then
  70. S:=S+',';
  71. S:=S+Cells[R,C];
  72. end;
  73. L.Add(S);
  74. end;
  75. L.SaveToFile(TestFileName);
  76. finally
  77. L.Free;
  78. end;
  79. end;
  80. procedure TTestCSVDocument.TestEmpty;
  81. begin
  82. AssertNotNull('Have document',Doc);
  83. end;
  84. Function TTestCSVDocument.StripQuotes(S : String) : string;
  85. Var
  86. L : integer;
  87. begin
  88. Result:=S;
  89. L:=Length(Result);
  90. if (L>1) then
  91. if (Result[1]='"') and (Result[L]='"') then
  92. Result:=Copy(Result,2,L-2);
  93. end;
  94. procedure TTestCSVDocument.TestTestFile;
  95. Var
  96. R,C : Integer;
  97. begin
  98. AssertEquals('Row count',RowCount,Doc.RowCount);
  99. For R:=0 to RowCount-1 do
  100. For C:=0 to ColCount-1 do
  101. begin
  102. AssertEquals('Col['+IntToStr(R)+'] count',ColCount,Doc.ColCount[R]);
  103. AssertEquals(Format('Cell[%d,%d]',[C,R]),StripQuotes(Cells[R,C]),Doc.Cells[C,R]);
  104. end;
  105. end;
  106. procedure TTestCSVDocument.TestRead;
  107. begin
  108. CreateTestFile;
  109. Doc.LoadFromFile(TestFileName);
  110. TestTestFile;
  111. end;
  112. initialization
  113. RegisterTest(TTestCSVDocument);
  114. end.