123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- unit utcCSVDocument;
- {$mode objfpc}{$H+}
- interface
- uses
- Classes, SysUtils, punit, csvdocument;
- procedure RegisterTests;
- implementation
- const
- TestFileName = 'test.csv';
- 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')
- );
- var
- FDoc: TCSVDocument;
- procedure RemoveTestFile;
- begin
- if FileExists(TestFileName) then
- DeleteFile(TestFileName);
- end;
- function StripQuotes(S: String): String;
- var
- L: integer;
- begin
- Result := S;
- L := Length(Result);
- if (L > 1) and (Result[1] = '"') and (Result[L] = '"') then
- Result := Copy(Result, 2, L - 2);
- end;
- procedure 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 TestTheFile;
- var
- R, C: Integer;
- begin
- AssertEquals('Row count', RowCount, FDoc.RowCount);
- for R := 0 to RowCount - 1 do
- for C := 0 to ColCount - 1 do
- begin
- AssertEquals('Col[' + IntToStr(R) + '] count', ColCount, FDoc.ColCount[R]);
- AssertEquals(Format('Cell[%d,%d]', [C, R]), StripQuotes(Cells[R, C]), FDoc.Cells[C, R]);
- end;
- end;
- function Setup: TTestString;
- begin
- Result := '';
- FDoc := TCSVDocument.Create;
- end;
- function TearDown: TTestString;
- begin
- Result := '';
- RemoveTestFile;
- FreeAndNil(FDoc);
- end;
- function TCSVDocument_TestEmpty: TTestString;
- begin
- Result := '';
- AssertNotNull('Have document', FDoc);
- end;
- function TCSVDocument_TestRead: TTestString;
- begin
- Result := '';
- CreateTestFile;
- FDoc.LoadFromFile(TestFileName);
- TestTheFile;
- end;
- procedure RegisterTests;
- begin
- AddSuite('TCSVDocumentTests', @Setup, @TearDown);
- AddTest('TestEmpty', @TCSVDocument_TestEmpty, 'TCSVDocumentTests');
- AddTest('TestRead', @TCSVDocument_TestRead, 'TCSVDocumentTests');
- end;
- end.
|