fpspreadsheet: improve temp file handling in tests

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@3333 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
bigchimp
2014-07-19 09:20:47 +00:00
parent 11bf00865b
commit 49aa26354e

View File

@@ -30,6 +30,12 @@ type
private private
procedure NeedVirtualCellData(Sender: TObject; ARow, ACol: Cardinal; procedure NeedVirtualCellData(Sender: TObject; ARow, ACol: Cardinal;
var AValue:Variant; var AStyleCell: PCell); var AValue:Variant; var AStyleCell: PCell);
// Gets new empty temp file and returns the file name
// Removes any existing file by that name
// Should be called just before writing to the file as
// GetTempFileName is used which does not guarantee
// file uniqueness
function NewTempFile: String;
protected protected
// Set up expected values: // Set up expected values:
procedure SetUp; override; procedure SetUp; override;
@@ -73,6 +79,18 @@ uses
const const
InternalSheet = 'Internal'; //worksheet name InternalSheet = 'Internal'; //worksheet name
function TSpreadInternalTests.NewTempFile: String;
var
tempFile: String;
begin
TempFile := GetTempFileName;
if FileExists(TempFile) then
begin
DeleteFile(TempFile);
sleep(40); //e.g. on Windows, give file system chance to perform changes
end;
Result:=tempFile;
end;
procedure TSpreadInternalTests.GetSheetByIndex; procedure TSpreadInternalTests.GetSheetByIndex;
var var
@@ -114,15 +132,12 @@ var
MyWorkbook: TsWorkbook; MyWorkbook: TsWorkbook;
TempFile: string; TempFile: string;
begin begin
TempFile:=GetTempFileName;
if fileexists(TempFile) then
DeleteFile(TempFile);
// Write out first file // Write out first file
MyWorkbook:=TsWorkbook.Create; MyWorkbook:=TsWorkbook.Create;
try try
MyWorkSheet:=MyWorkBook.AddWorksheet(InternalSheet); MyWorkSheet:=MyWorkBook.AddWorksheet(InternalSheet);
MyWorkSheet.WriteUTF8Text(0,0,FirstFileCellText); MyWorkSheet.WriteUTF8Text(0,0,FirstFileCellText);
TempFile:=NewTempFile;
MyWorkBook.WriteToFile(TempFile,sfExcel8,false); MyWorkBook.WriteToFile(TempFile,sfExcel8,false);
finally finally
MyWorkbook.Free; MyWorkbook.Free;
@@ -308,10 +323,6 @@ var
value: Double; value: Double;
s: String; s: String;
begin begin
TempFile := GetTempFileName;
if FileExists(TempFile) then
DeleteFile(TempFile);
workbook := TsWorkbook.Create; workbook := TsWorkbook.Create;
try try
worksheet := workbook.AddWorksheet('VirtualMode'); worksheet := workbook.AddWorksheet('VirtualMode');
@@ -322,6 +333,7 @@ begin
workbook.VirtualRowCount := Length(SollNumbers) + 4; workbook.VirtualRowCount := Length(SollNumbers) + 4;
// We'll use only the first 4 SollStrings, the others cause trouble due to utf8 and formatting. // We'll use only the first 4 SollStrings, the others cause trouble due to utf8 and formatting.
workbook.OnNeedCellData := @NeedVirtualCellData; workbook.OnNeedCellData := @NeedVirtualCellData;
tempFile:=NewTempFile;
workbook.WriteToFile(tempfile, AFormat, true); workbook.WriteToFile(tempfile, AFormat, true);
finally finally
workbook.Free; workbook.Free;