2013-12-07 13:42:22 +00:00
|
|
|
unit internaltests;
|
|
|
|
|
|
|
|
{ Other units test file read/write capability.
|
|
|
|
This unit tests functions, procedures and properties that fpspreadsheet provides.
|
|
|
|
}
|
|
|
|
{$mode objfpc}{$H+}
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
{
|
|
|
|
Adding tests/test data:
|
|
|
|
- just add your new test procedure
|
|
|
|
}
|
|
|
|
|
|
|
|
uses
|
|
|
|
// Not using lazarus package as the user may be working with multiple versions
|
|
|
|
// Instead, add .. to unit search path
|
|
|
|
Classes, SysUtils, fpcunit, testutils, testregistry,
|
|
|
|
fpsallformats, fpspreadsheet, xlsbiff8 {and a project requirement for lclbase for utf8 handling},
|
2014-07-18 22:48:38 +00:00
|
|
|
fpsutils, fpsstreams, testsutility, md5;
|
2013-12-07 13:42:22 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
{ TSpreadReadInternalTests }
|
2013-12-27 09:32:10 +00:00
|
|
|
// Tests fpspreadsheet functionality, especially internal functions
|
|
|
|
// Excel/LibreOffice/OpenOffice import/export compatibility should *NOT* be tested here
|
2013-12-10 12:06:12 +00:00
|
|
|
|
|
|
|
{ TSpreadInternalTests }
|
|
|
|
|
2013-12-07 13:42:22 +00:00
|
|
|
TSpreadInternalTests= class(TTestCase)
|
|
|
|
private
|
2014-07-23 21:51:06 +00:00
|
|
|
procedure WriteVirtualCellDataHandler(Sender: TObject; ARow, ACol: Cardinal;
|
2014-07-15 15:49:58 +00:00
|
|
|
var AValue:Variant; var AStyleCell: PCell);
|
2013-12-07 13:42:22 +00:00
|
|
|
protected
|
|
|
|
// Set up expected values:
|
|
|
|
procedure SetUp; override;
|
|
|
|
procedure TearDown; override;
|
2014-07-19 13:23:12 +00:00
|
|
|
procedure TestVirtualMode(AFormat: TsSpreadsheetFormat; ABufStreamMode: Boolean);
|
2014-07-18 22:48:38 +00:00
|
|
|
|
2013-12-07 13:42:22 +00:00
|
|
|
published
|
2014-07-04 08:48:12 +00:00
|
|
|
// Tests getting Excel style A1 cell locations from row/column based locations.
|
|
|
|
// Bug 26447
|
|
|
|
procedure TestCellString;
|
2013-12-07 13:42:22 +00:00
|
|
|
//todo: add more calls, rename sheets, try to get sheets with invalid indexes etc
|
|
|
|
//(see strings tests for how to deal with expected exceptions)
|
|
|
|
procedure GetSheetByIndex;
|
2013-12-10 12:06:12 +00:00
|
|
|
// Verify GetSheetByName returns the correct sheet number
|
|
|
|
// GetSheetByName was implemented in SVN revision 2857
|
|
|
|
procedure GetSheetByName;
|
2013-12-27 09:32:10 +00:00
|
|
|
// Tests whether overwriting existing file works
|
|
|
|
procedure OverwriteExistingFile;
|
2013-12-10 12:06:12 +00:00
|
|
|
// Write out date cell and try to read as UTF8; verify if contents the same
|
|
|
|
procedure ReadDateAsUTF8;
|
2014-07-18 22:48:38 +00:00
|
|
|
// Test buffered stream
|
2014-07-22 15:59:29 +00:00
|
|
|
procedure TestReadBufStream;
|
2014-08-07 12:52:26 +00:00
|
|
|
procedure TestWriteBufStream;
|
2014-07-15 15:49:58 +00:00
|
|
|
|
|
|
|
// Virtual mode tests for all file formats
|
|
|
|
procedure TestVirtualMode_BIFF2;
|
|
|
|
procedure TestVirtualMode_BIFF5;
|
|
|
|
procedure TestVirtualMode_BIFF8;
|
2014-08-07 08:43:11 +00:00
|
|
|
procedure TestVirtualMode_ODS;
|
2014-07-15 15:49:58 +00:00
|
|
|
procedure TestVirtualMode_OOXML;
|
2014-07-15 21:31:59 +00:00
|
|
|
|
2014-07-19 13:23:12 +00:00
|
|
|
procedure TestVirtualMode_BIFF2_BufStream;
|
|
|
|
procedure TestVirtualMode_BIFF5_BufStream;
|
|
|
|
procedure TestVirtualMode_BIFF8_BufStream;
|
2014-08-07 12:52:26 +00:00
|
|
|
procedure TestVirtualMode_ODS_BufStream;
|
|
|
|
procedure TestVirtualMode_OOXML_BufStream;
|
2013-12-07 13:42:22 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
2014-07-15 15:49:58 +00:00
|
|
|
uses
|
2014-07-15 21:31:59 +00:00
|
|
|
numberstests, stringtests;
|
2014-07-15 15:49:58 +00:00
|
|
|
|
2013-12-07 13:42:22 +00:00
|
|
|
const
|
|
|
|
InternalSheet = 'Internal'; //worksheet name
|
|
|
|
|
|
|
|
procedure TSpreadInternalTests.GetSheetByIndex;
|
|
|
|
var
|
|
|
|
MyWorksheet: TsWorksheet;
|
|
|
|
MyWorkbook: TsWorkbook;
|
|
|
|
begin
|
|
|
|
MyWorkbook := TsWorkbook.Create;
|
|
|
|
MyWorkSheet:=MyWorkBook.AddWorksheet(InternalSheet);
|
|
|
|
MyWorkSheet:=nil;
|
|
|
|
MyWorkSheet:=MyWorkBook.GetWorksheetByIndex(0);
|
|
|
|
CheckFalse((MyWorksheet=nil),'GetWorksheetByIndex should return a valid index');
|
|
|
|
MyWorkbook.Free;
|
|
|
|
end;
|
|
|
|
|
2013-12-10 12:06:12 +00:00
|
|
|
procedure TSpreadInternalTests.GetSheetByName;
|
|
|
|
const
|
|
|
|
AnotherSheet='AnotherSheet';
|
|
|
|
var
|
|
|
|
MyWorksheet: TsWorksheet;
|
|
|
|
MyWorkbook: TsWorkbook;
|
|
|
|
begin
|
|
|
|
MyWorkbook := TsWorkbook.Create;
|
|
|
|
MyWorkSheet:=MyWorkBook.AddWorksheet(InternalSheet);
|
|
|
|
MyWorkSheet:=MyWorkBook.AddWorksheet(AnotherSheet);
|
|
|
|
MyWorkSheet:=nil;
|
|
|
|
MyWorkSheet:=MyWorkBook.GetWorksheetByName(InternalSheet);
|
|
|
|
CheckFalse((MyWorksheet=nil),'GetWorksheetByName should return a valid index');
|
|
|
|
CheckEquals(MyWorksheet.Name,InternalSheet,'GetWorksheetByName should return correct name.');
|
|
|
|
MyWorkbook.Free;
|
|
|
|
end;
|
|
|
|
|
2013-12-27 09:32:10 +00:00
|
|
|
procedure TSpreadInternalTests.OverwriteExistingFile;
|
|
|
|
const
|
|
|
|
FirstFileCellText='Old version';
|
|
|
|
SecondFileCellText='New version';
|
|
|
|
var
|
|
|
|
FirstFileHash: string;
|
|
|
|
MyWorksheet: TsWorksheet;
|
|
|
|
MyWorkbook: TsWorkbook;
|
|
|
|
TempFile: string;
|
|
|
|
begin
|
|
|
|
// Write out first file
|
|
|
|
MyWorkbook:=TsWorkbook.Create;
|
|
|
|
try
|
|
|
|
MyWorkSheet:=MyWorkBook.AddWorksheet(InternalSheet);
|
|
|
|
MyWorkSheet.WriteUTF8Text(0,0,FirstFileCellText);
|
2014-07-19 09:20:47 +00:00
|
|
|
TempFile:=NewTempFile;
|
2013-12-27 09:32:10 +00:00
|
|
|
MyWorkBook.WriteToFile(TempFile,sfExcel8,false);
|
|
|
|
finally
|
|
|
|
MyWorkbook.Free;
|
|
|
|
end;
|
2013-12-27 13:55:56 +00:00
|
|
|
|
|
|
|
if not(FileExists(TempFile)) then
|
|
|
|
fail('Trying to write first file did not work.');
|
2013-12-27 09:32:10 +00:00
|
|
|
FirstFileHash:=MD5Print(MD5File(TempFile));
|
|
|
|
|
|
|
|
// Now overwrite with second file
|
|
|
|
MyWorkbook:=TsWorkbook.Create;
|
|
|
|
try
|
|
|
|
MyWorkSheet:=MyWorkBook.AddWorksheet(InternalSheet);
|
|
|
|
MyWorkSheet.WriteUTF8Text(0,0,SecondFileCellText);
|
|
|
|
MyWorkBook.WriteToFile(TempFile,sfExcel8,true);
|
|
|
|
finally
|
|
|
|
MyWorkbook.Free;
|
|
|
|
end;
|
|
|
|
if FirstFileHash=MD5Print(MD5File(TempFile)) then
|
|
|
|
fail('File contents are still those of the first file.');
|
|
|
|
end;
|
|
|
|
|
2013-12-10 12:06:12 +00:00
|
|
|
procedure TSpreadInternalTests.ReadDateAsUTF8;
|
|
|
|
var
|
|
|
|
ActualDT: TDateTime;
|
|
|
|
ActualDTString: string; //Result from ReadAsUTF8Text
|
|
|
|
MyWorksheet: TsWorksheet;
|
|
|
|
MyWorkbook: TsWorkbook;
|
|
|
|
Row,Column: Cardinal;
|
|
|
|
TestDT: TDateTime;
|
|
|
|
begin
|
|
|
|
Row:=0;
|
|
|
|
Column:=0;
|
|
|
|
TestDT:=EncodeDate(1969,7,21)+EncodeTime(2,56,0,0);
|
|
|
|
MyWorkbook:=TsWorkbook.Create;
|
|
|
|
MyWorkSheet:=MyWorkBook.AddWorksheet(InternalSheet);
|
|
|
|
MyWorkSheet.WriteDateTime(Row,Column,TestDT); //write date
|
|
|
|
|
|
|
|
// Reading as date/time should just work
|
|
|
|
if not(MyWorksheet.ReadAsDateTime(Row,Column,ActualDT)) then
|
|
|
|
Fail('Could not read date time for cell '+CellNotation(MyWorkSheet,Row,Column));
|
|
|
|
CheckEquals(TestDT,ActualDT,'Test date/time value mismatch '
|
|
|
|
+'cell '+CellNotation(MyWorkSheet,Row,Column));
|
|
|
|
|
|
|
|
//Check reading as string, convert to date & compare
|
|
|
|
ActualDTString:=MyWorkSheet.ReadAsUTF8Text(Row,Column);
|
|
|
|
ActualDT:=StrToDateTimeDef(ActualDTString,EncodeDate(1906,1,1));
|
|
|
|
CheckEquals(TestDT,ActualDT,'Date/time mismatch using ReadAsUTF8Text');
|
|
|
|
|
|
|
|
MyWorkbook.Free;
|
|
|
|
end;
|
|
|
|
|
2014-08-07 12:52:26 +00:00
|
|
|
procedure TSpreadInternalTests.TestWriteBufStream;
|
2014-07-18 22:48:38 +00:00
|
|
|
const
|
|
|
|
BUFSIZE = 1024;
|
|
|
|
var
|
|
|
|
stream: TBufStream;
|
|
|
|
readBuf, writeBuf1, writeBuf2: array of byte;
|
|
|
|
nRead, nWrite1, nWrite2: Integer;
|
|
|
|
i: Integer;
|
|
|
|
begin
|
|
|
|
stream := TBufStream.Create(BUFSIZE);
|
|
|
|
try
|
|
|
|
// Write 100 random bytes. They fit into the BUFSIZE of the memory buffer
|
|
|
|
nWrite1 := 100;
|
|
|
|
SetLength(writeBuf1, nWrite1);
|
|
|
|
for i:=0 to nWrite1-1 do writeBuf1[i] := Random(255);
|
|
|
|
stream.WriteBuffer(writeBuf1[0], nWrite1);
|
|
|
|
|
|
|
|
// Check stream size - must be equal to nWrite
|
|
|
|
CheckEquals(nWrite1, stream.Size, 'Stream size mismatch (#1)');
|
|
|
|
|
|
|
|
// Check stream position must be equal to nWrite
|
|
|
|
CheckEquals(nWrite1, stream.Position, 'Stream position mismatch (#2)');
|
|
|
|
|
|
|
|
// Bring stream pointer back to start
|
|
|
|
stream.Position := 0;
|
|
|
|
CheckEquals(0, stream.Position, 'Stream position mismatch (#3)');
|
|
|
|
|
|
|
|
// Read the first 10 bytes just written and compare
|
|
|
|
nRead := 10;
|
|
|
|
SetLength(readBuf, nRead);
|
|
|
|
nRead := stream.Read(readBuf[0], nRead);
|
|
|
|
CheckEquals(10, nRead, 'Read/write size mismatch (#4)');
|
|
|
|
for i:=0 to 9 do
|
|
|
|
CheckEquals(writeBuf1[i], readBuf[i], Format('Read/write mismatch at position %d (#5)', [i]));
|
|
|
|
|
|
|
|
// Back to start, and read the entire stream
|
|
|
|
stream.Position := 0;
|
|
|
|
nRead := stream.Size;
|
|
|
|
Setlength(readBuf, nRead);
|
|
|
|
nRead := stream.Read(readBuf[0], stream.Size);
|
|
|
|
CheckEquals(nWrite1, nRead, 'Stream read size mismatch (#6)');
|
|
|
|
for i:=0 to nWrite1-1 do
|
|
|
|
CheckEquals(writeBuf1[i], readBuf[i], Format('Read/write mismatch at position %d (#7)', [i]));
|
|
|
|
|
|
|
|
// Now put stream pointer to end and write another 2000 bytes. This crosses
|
|
|
|
// the size of the memory buffer, and the stream must swap to file.
|
|
|
|
stream.Seek(0, soFromEnd);
|
|
|
|
CheckEquals(stream.Size, stream.Position, 'Stream position not at end (#8)');
|
|
|
|
|
|
|
|
nWrite2 := 2000;
|
|
|
|
SetLength(writeBuf2, nWrite2);
|
|
|
|
for i:=0 to nWrite2-1 do writeBuf2[i] := Random(255);
|
|
|
|
stream.WriteBuffer(writeBuf2[0], nWrite2);
|
|
|
|
|
|
|
|
// The stream pointer must be at 100+2000, same for the size
|
|
|
|
CheckEquals(nWrite1+nWrite2, stream.Position, 'Stream position mismatch (#9)');
|
|
|
|
CheckEquals(nWrite1+nWrite2, stream.Size, 'Stream size mismatch (#10)');
|
|
|
|
|
|
|
|
// Read the last 10 bytes and compare
|
|
|
|
Stream.Seek(10, soFromEnd);
|
|
|
|
SetLength(readBuf, 10);
|
|
|
|
Stream.ReadBuffer(readBuf[0], 10);
|
|
|
|
for i:=0 to 9 do
|
|
|
|
CheckEquals(writeBuf2[nWrite2-10+i], readBuf[i], Format('Read/write mismatch at position %d from end (#11)', [i]));
|
|
|
|
|
|
|
|
// Now read all from beginning
|
|
|
|
Stream.Position := 0;
|
|
|
|
SetLength(readBuf, stream.Size);
|
|
|
|
nRead := Stream.Read(readBuf[0], stream.Size);
|
|
|
|
CheckEquals(nWrite1+nWrite2, nRead, 'Read/write size mismatch (#4)');
|
|
|
|
for i:=0 to nRead-1 do
|
|
|
|
if i < nWrite1 then
|
|
|
|
CheckEquals(writeBuf1[i], readBuf[i], Format('Read/write mismatch at position %d (#11)', [i]))
|
|
|
|
else
|
|
|
|
CheckEquals(writeBuf2[i-nWrite1], readBuf[i], Format('Read/write mismatch at position %d (#11)', [i]));
|
|
|
|
|
|
|
|
finally
|
|
|
|
stream.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2014-07-22 15:59:29 +00:00
|
|
|
procedure TSpreadInternalTests.TestReadBufStream;
|
|
|
|
const
|
|
|
|
BUF_SIZE = 1024;
|
|
|
|
FILE_SIZE = 2000;
|
|
|
|
var
|
|
|
|
tempFileName: String;
|
|
|
|
stream: TStream;
|
|
|
|
writedata: array of Byte;
|
|
|
|
readdata: array of Byte;
|
|
|
|
i, n, nread: Integer;
|
|
|
|
begin
|
|
|
|
RandSeed := 0;
|
|
|
|
|
|
|
|
// Create a test file
|
|
|
|
tempFileName := GetTempFileName;
|
|
|
|
stream := TFileStream.Create(tempFileName, fmCreate);
|
|
|
|
try
|
|
|
|
SetLength(writedata, FILE_SIZE);
|
|
|
|
for i:=0 to High(writedata) do
|
|
|
|
writedata[i] := random(256);
|
|
|
|
stream.WriteBuffer(writedata[0], Length(writedata));
|
|
|
|
finally
|
|
|
|
stream.Free;
|
|
|
|
end;
|
|
|
|
|
|
|
|
// Use a TBufStream to read parts of the file back
|
|
|
|
stream := TBufStream.Create(tempFilename, fmOpenRead, BUF_SIZE);
|
|
|
|
try
|
|
|
|
// Check stream size
|
|
|
|
CheckEquals(FILE_SIZE, stream.Size, 'Size mismatch');
|
|
|
|
|
|
|
|
// Read first 100 bytes and compare with data
|
|
|
|
nread := 100;
|
|
|
|
SetLength(readdata, nread);
|
|
|
|
n := stream.Read(readdata[0], nread);
|
|
|
|
CheckEquals(nread, n, 'Bytes count mismatch');
|
|
|
|
for i:=0 to nread-1 do
|
|
|
|
CheckEquals(writedata[i], readdata[i], Format('Read mismatch at position %d', [i]));
|
|
|
|
|
|
|
|
// Check stream size
|
|
|
|
CheckEquals(FILE_SIZE, stream.Size, 'Size mismatch');
|
|
|
|
|
|
|
|
// Read next 100 bytes and compare
|
|
|
|
stream.ReadBuffer(readdata[0], nread);
|
|
|
|
for i:=0 to nread-1 do
|
|
|
|
CheckEquals(writedata[i+nread], readdata[i], Format('Read mismatch at position %d', [i+nread]));
|
|
|
|
|
|
|
|
// Go to position 1000, this is 24 bytes to the end of the buffer, and read
|
|
|
|
// 100 bytes again - this process will require to refresh the buffer
|
|
|
|
stream.Position := 1000;
|
|
|
|
stream.ReadBuffer(readdata[0], nread);
|
|
|
|
for i:=0 to nread-1 do
|
|
|
|
CheckEquals(writedata[i+1000], readdata[i], Format('Read mismatch at position %d', [i+1000]));
|
|
|
|
|
|
|
|
// Check stream size
|
|
|
|
CheckEquals(FILE_SIZE, stream.Size, 'Size mismatch');
|
|
|
|
|
|
|
|
// Read next 100 bytes
|
|
|
|
stream.ReadBuffer(readdata[0], nread);
|
|
|
|
for i:=0 to nread-1 do
|
|
|
|
CheckEquals(writedata[i+1000+nread], readdata[i], Format('Read mismatch at position %d', [i+1000+nread]));
|
|
|
|
|
|
|
|
// Go back to start and fill the memory stream again with bytes 0..1023
|
|
|
|
stream.Position := 0;
|
|
|
|
stream.ReadBuffer(readdata[0], nread);
|
|
|
|
|
|
|
|
// Now read 100 bytes which are not in the buffer
|
|
|
|
stream.Position := 1500; // this is past the buffered range
|
|
|
|
stream.ReadBuffer(readdata[0], 100);
|
|
|
|
for i:=0 to nread-1 do
|
|
|
|
CheckEquals(writedata[i+1500], readdata[i], Format('Read mismatch at position %d', [i+1500]));
|
|
|
|
|
|
|
|
// Go back to start and fill the memory stream again with bytes 0..1023
|
|
|
|
stream.Position := 0;
|
|
|
|
stream.ReadBuffer(readdata[0], 100);
|
|
|
|
|
|
|
|
// Read last 100 bytes
|
|
|
|
stream.Seek(nread, soFromEnd);
|
|
|
|
stream.ReadBuffer(readdata[0], nread);
|
|
|
|
for i:=0 to nread-1 do
|
|
|
|
CheckEquals(writedata[i+FILE_SIZE-nread], readdata[i],
|
|
|
|
Format('Read mismatch at position %d', [i+FILE_SIZE-nread]));
|
|
|
|
|
|
|
|
finally
|
|
|
|
stream.Free;
|
|
|
|
DeleteFile(tempFileName);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2014-07-04 08:48:12 +00:00
|
|
|
procedure TSpreadInternalTests.TestCellString;
|
2014-07-04 09:17:31 +00:00
|
|
|
var
|
|
|
|
r,c: Cardinal;
|
|
|
|
s: String;
|
|
|
|
flags: TsRelFlags;
|
2014-07-04 08:48:12 +00:00
|
|
|
begin
|
|
|
|
CheckEquals('$A$1',GetCellString(0,0,[]));
|
|
|
|
CheckEquals('$Z$1',GetCellString(0,25,[])); //bug 26447
|
|
|
|
CheckEquals('$AA$2',GetCellString(1,26,[])); //just past the last letter
|
|
|
|
CheckEquals('$GW$5',GetCellString(4,204,[])); //some big value
|
2014-07-04 09:17:31 +00:00
|
|
|
CheckEquals('$IV$1',GetCellString(0,255,[])); //the last column of xls
|
2014-07-15 21:31:59 +00:00
|
|
|
CheckEquals('$IW$1',GetCellString(0,256,[])); //the first column beyond xls
|
2014-07-04 09:17:31 +00:00
|
|
|
CheckEquals('$XFD$1',GetCellString(0,16383,[])); // the last column of xlsx
|
2014-07-15 21:31:59 +00:00
|
|
|
CheckEquals('$XFE$1',GetCellString(0,16384,[])); // the first column beyond xlsx
|
2014-07-04 09:17:31 +00:00
|
|
|
|
|
|
|
// Something VERY big, beyond xlsx
|
|
|
|
s := 'ZZZZ1';
|
|
|
|
ParseCellString(s, r, c, flags);
|
|
|
|
CheckEquals(s, GetCellString(r, c, flags));
|
2014-07-04 08:48:12 +00:00
|
|
|
end;
|
|
|
|
|
2013-12-07 13:42:22 +00:00
|
|
|
|
|
|
|
procedure TSpreadInternalTests.SetUp;
|
|
|
|
begin
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TSpreadInternalTests.TearDown;
|
|
|
|
begin
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
2014-07-23 21:51:06 +00:00
|
|
|
procedure TSpreadInternalTests.WriteVirtualCellDataHandler(Sender: TObject;
|
2014-07-15 15:49:58 +00:00
|
|
|
ARow, ACol: Cardinal; var AValue:Variant; var AStyleCell: PCell);
|
|
|
|
begin
|
2014-07-15 21:31:59 +00:00
|
|
|
// First read the SollNumbers, then the first 4 SollStrings
|
|
|
|
// See comment in TestVirtualMode().
|
|
|
|
if ARow < Length(SollNumbers) then
|
|
|
|
AValue := SollNumbers[ARow]
|
|
|
|
else
|
|
|
|
AValue := SollStrings[ARow - Length(SollNumbers)];
|
2014-07-15 15:49:58 +00:00
|
|
|
end;
|
|
|
|
|
2014-07-15 21:31:59 +00:00
|
|
|
procedure TSpreadInternalTests.TestVirtualMode(AFormat: TsSpreadsheetFormat;
|
2014-07-19 13:23:12 +00:00
|
|
|
ABufStreamMode: Boolean);
|
2014-07-15 15:49:58 +00:00
|
|
|
var
|
|
|
|
tempFile: String;
|
|
|
|
workbook: TsWorkbook;
|
|
|
|
worksheet: TsWorksheet;
|
|
|
|
row, col: Integer;
|
|
|
|
value: Double;
|
2014-07-15 21:31:59 +00:00
|
|
|
s: String;
|
2014-07-15 15:49:58 +00:00
|
|
|
begin
|
|
|
|
try
|
2014-08-07 08:43:11 +00:00
|
|
|
workbook := TsWorkbook.Create;
|
|
|
|
try
|
|
|
|
worksheet := workbook.AddWorksheet('VirtualMode');
|
|
|
|
workbook.Options := workbook.Options + [boVirtualMode];
|
|
|
|
if ABufStreamMode then
|
|
|
|
workbook.Options := workbook.Options + [boBufStream];
|
|
|
|
workbook.VirtualColCount := 1;
|
|
|
|
workbook.VirtualRowCount := Length(SollNumbers) + 4;
|
|
|
|
// We'll use only the first 4 SollStrings, the others cause trouble due to utf8 and formatting.
|
|
|
|
workbook.OnWriteCellData := @WriteVirtualCellDataHandler;
|
|
|
|
tempFile:=NewTempFile;
|
|
|
|
workbook.WriteToFile(tempfile, AFormat, true);
|
|
|
|
finally
|
|
|
|
workbook.Free;
|
|
|
|
end;
|
2013-12-07 13:42:22 +00:00
|
|
|
|
2014-07-15 15:49:58 +00:00
|
|
|
workbook := TsWorkbook.Create;
|
|
|
|
try
|
|
|
|
workbook.ReadFromFile(tempFile, AFormat);
|
|
|
|
worksheet := workbook.GetWorksheetByIndex(0);
|
|
|
|
col := 0;
|
2014-07-15 21:31:59 +00:00
|
|
|
CheckEquals(Length(SollNumbers) + 4, worksheet.GetLastRowIndex+1,
|
2014-07-15 15:49:58 +00:00
|
|
|
'Row count mismatch');
|
2014-07-19 13:13:56 +00:00
|
|
|
for row := 0 to Length(SollNumbers)-1 do
|
|
|
|
begin
|
2014-07-15 15:49:58 +00:00
|
|
|
value := worksheet.ReadAsNumber(row, col);
|
|
|
|
CheckEquals(SollNumbers[row], value,
|
|
|
|
'Test number value mismatch, cell '+CellNotation(workSheet, row, col))
|
|
|
|
end;
|
2014-07-19 13:13:56 +00:00
|
|
|
for row := Length(SollNumbers) to worksheet.GetLastRowIndex do
|
|
|
|
begin
|
2014-07-15 21:31:59 +00:00
|
|
|
s := worksheet.ReadAsUTF8Text(row, col);
|
|
|
|
CheckEquals(SollStrings[row - Length(SollNumbers)], s,
|
|
|
|
'Test string value mismatch, cell '+CellNotation(workSheet, row, col));
|
|
|
|
end;
|
2014-07-15 15:49:58 +00:00
|
|
|
finally
|
|
|
|
workbook.Free;
|
|
|
|
end;
|
2014-07-04 08:48:12 +00:00
|
|
|
|
2014-08-07 08:43:11 +00:00
|
|
|
finally
|
|
|
|
DeleteFile(tempFile);
|
|
|
|
end;
|
2014-07-15 15:49:58 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TSpreadInternalTests.TestVirtualMode_BIFF2;
|
|
|
|
begin
|
2014-07-15 21:31:59 +00:00
|
|
|
TestVirtualMode(sfExcel2, false);
|
2014-07-15 15:49:58 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TSpreadInternalTests.TestVirtualMode_BIFF5;
|
|
|
|
begin
|
2014-07-15 21:31:59 +00:00
|
|
|
TestVirtualMode(sfExcel5, false);
|
2014-07-15 15:49:58 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TSpreadInternalTests.TestVirtualMode_BIFF8;
|
|
|
|
begin
|
2014-07-15 21:31:59 +00:00
|
|
|
TestVirtualMode(sfExcel8, false);
|
2014-07-15 15:49:58 +00:00
|
|
|
end;
|
|
|
|
|
2014-08-07 08:43:11 +00:00
|
|
|
procedure TSpreadInternalTests.TestVirtualMode_ODS;
|
|
|
|
begin
|
|
|
|
TestVirtualMode(sfOpenDocument, false);
|
|
|
|
end;
|
|
|
|
|
2014-07-15 15:49:58 +00:00
|
|
|
procedure TSpreadInternalTests.TestVirtualMode_OOXML;
|
|
|
|
begin
|
2014-07-15 21:31:59 +00:00
|
|
|
TestVirtualMode(sfOOXML, false);
|
|
|
|
end;
|
|
|
|
|
2014-07-19 13:23:12 +00:00
|
|
|
procedure TSpreadInternalTests.TestVirtualMode_BIFF2_BufStream;
|
2014-07-15 21:31:59 +00:00
|
|
|
begin
|
|
|
|
TestVirtualMode(sfExcel2, True);
|
|
|
|
end;
|
|
|
|
|
2014-07-19 13:23:12 +00:00
|
|
|
procedure TSpreadInternalTests.TestVirtualMode_BIFF5_BufStream;
|
2014-07-15 21:31:59 +00:00
|
|
|
begin
|
|
|
|
TestVirtualMode(sfExcel5, true);
|
|
|
|
end;
|
|
|
|
|
2014-07-19 13:23:12 +00:00
|
|
|
procedure TSpreadInternalTests.TestVirtualMode_BIFF8_BufStream;
|
2014-07-15 21:31:59 +00:00
|
|
|
begin
|
|
|
|
TestVirtualMode(sfExcel8, true);
|
|
|
|
end;
|
2014-08-07 12:52:26 +00:00
|
|
|
|
2014-08-07 08:43:11 +00:00
|
|
|
procedure TSpreadInternalTests.TestVirtualMode_ODS_BufStream;
|
|
|
|
begin
|
|
|
|
TestVirtualMode(sfOpenDocument, true);
|
|
|
|
end;
|
2014-07-15 21:31:59 +00:00
|
|
|
|
2014-07-19 13:23:12 +00:00
|
|
|
procedure TSpreadInternalTests.TestVirtualMode_OOXML_BufStream;
|
2014-07-15 21:31:59 +00:00
|
|
|
begin
|
|
|
|
TestVirtualMode(sfOOXML, true);
|
2014-07-15 15:49:58 +00:00
|
|
|
end;
|
2014-08-07 12:52:26 +00:00
|
|
|
|
2014-07-04 08:48:12 +00:00
|
|
|
|
2013-12-07 13:42:22 +00:00
|
|
|
initialization
|
|
|
|
// Register so these tests are included in a full run
|
|
|
|
RegisterTest(TSpreadInternalTests);
|
2014-08-07 12:52:26 +00:00
|
|
|
|
2013-12-07 13:42:22 +00:00
|
|
|
end.
|
|
|
|
|
|
|
|
|