123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- unit tccsvdocument;
- {$mode objfpc}{$H+}
- interface
- uses
- Classes, SysUtils, fpcunit, testregistry, csvdocument;
- Type
- { TTestCSVDocument }
- TTestCSVDocument = Class(TTestCase)
- private
- FDoc: TCSVDocument;
- procedure RemoveTestFile;
- function StripQuotes(S: String): string;
- procedure TestTestFile;
- Public
- Procedure SetUp; override;
- Procedure TearDown; override;
- Procedure CreateTestFile;
- Property Doc : TCSVDocument Read FDoc;
- Published
- Procedure TestEmpty;
- Procedure TestRead;
- end;
- implementation
- Const
- TestFileName = 'test.csv';
- { TTestCSVDocument }
- procedure TTestCSVDocument.SetUp;
- begin
- FDoc:=TCSVDocument.Create;
- Inherited;
- end;
- procedure TTestCSVDocument.TearDown;
- begin
- RemoveTestFile;
- FreeAndNil(FDoc);
- Inherited;
- end;
- procedure TTestCSVDocument.RemoveTestFile;
- begin
- If FileExists(TestFileName) then
- AssertTrue('Deleting test file',DeleteFile(TestFileName));
- end;
- Const
- ColCount = 3;
- RowCount = 4;
- Type
- TRow = Array[0..ColCount-1] of string;
- TCells = Array[0..RowCount-1] of TRow;
- Const
- Cells : TCells = (
- ('a','b','c'),
- ('1','"one"','1.1'),
- ('2','"two"','2.2'),
- ('3','"three"','3.3')
- );
- procedure TTestCSVDocument.CreateTestFile;
- Var
- L : TStringList;
- R,C : Integer;
- S : String;
- begin
- L:=TStringList.Create;
- try
- for R:=0 to RowCount-1 do
- begin
- S:='';
- for C:=0 to ColCount-1 do
- begin
- if S<>'' then
- S:=S+',';
- S:=S+Cells[R,C];
- end;
- L.Add(S);
- end;
- L.SaveToFile(TestFileName);
- finally
- L.Free;
- end;
- end;
- procedure TTestCSVDocument.TestEmpty;
- begin
- AssertNotNull('Have document',Doc);
- end;
- Function TTestCSVDocument.StripQuotes(S : String) : string;
- Var
- L : integer;
- begin
- Result:=S;
- L:=Length(Result);
- if (L>1) then
- if (Result[1]='"') and (Result[L]='"') then
- Result:=Copy(Result,2,L-2);
- end;
- procedure TTestCSVDocument.TestTestFile;
- Var
- R,C : Integer;
- begin
- AssertEquals('Row count',RowCount,Doc.RowCount);
- For R:=0 to RowCount-1 do
- For C:=0 to ColCount-1 do
- begin
- AssertEquals('Col['+IntToStr(R)+'] count',ColCount,Doc.ColCount[R]);
- AssertEquals(Format('Cell[%d,%d]',[C,R]),StripQuotes(Cells[R,C]),Doc.Cells[C,R]);
- end;
- end;
- procedure TTestCSVDocument.TestRead;
- begin
- CreateTestFile;
- Doc.LoadFromFile(TestFileName);
- TestTestFile;
- end;
- initialization
- RegisterTest(TTestCSVDocument);
- end.
|