2014-05-24 20:28:05 +00:00
|
|
|
unit formulatests;
|
|
|
|
|
|
|
|
{$mode objfpc}{$H+}
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
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},
|
|
|
|
testsutility;
|
|
|
|
|
|
|
|
type
|
|
|
|
{ TSpreadWriteReadFormula }
|
|
|
|
//Write to xls/xml file and read back
|
|
|
|
TSpreadWriteReadFormulaTests = class(TTestCase)
|
|
|
|
private
|
|
|
|
protected
|
|
|
|
// Set up expected values:
|
|
|
|
procedure SetUp; override;
|
|
|
|
procedure TearDown; override;
|
|
|
|
// Test formula strings
|
|
|
|
procedure TestWriteReadFormulaStrings(AFormat: TsSpreadsheetFormat);
|
|
|
|
|
|
|
|
published
|
|
|
|
// Writes out numbers & reads back.
|
|
|
|
// If previous read tests are ok, this effectively tests writing.
|
2014-05-25 16:49:45 +00:00
|
|
|
{ BIFF2 Tests }
|
2014-06-06 12:17:45 +00:00
|
|
|
procedure TestWriteRead_BIFF2_FormulaStrings;
|
2014-05-25 16:49:45 +00:00
|
|
|
{ BIFF5 Tests }
|
2014-06-06 12:17:45 +00:00
|
|
|
procedure TestWriteRead_BIFF5_FormulaStrings;
|
2014-05-24 20:28:05 +00:00
|
|
|
{ BIFF8 Tests }
|
2014-06-06 12:17:45 +00:00
|
|
|
procedure TestWriteRead_BIFF8_FormulaStrings;
|
2014-05-24 20:28:05 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
uses
|
2014-05-25 16:49:45 +00:00
|
|
|
fpsUtils, rpnFormulaUnit;
|
2014-05-24 20:28:05 +00:00
|
|
|
|
|
|
|
{ TSpreadWriteReadFormatTests }
|
|
|
|
|
|
|
|
procedure TSpreadWriteReadFormulaTests.SetUp;
|
|
|
|
begin
|
|
|
|
inherited SetUp;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TSpreadWriteReadFormulaTests.TearDown;
|
|
|
|
begin
|
|
|
|
inherited TearDown;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TSpreadWriteReadFormulaTests.TestWriteReadFormulaStrings(AFormat: TsSpreadsheetFormat);
|
|
|
|
const
|
|
|
|
SHEET = 'Sheet1';
|
|
|
|
var
|
|
|
|
MyWorksheet: TsWorksheet;
|
|
|
|
MyWorkbook: TsWorkbook;
|
|
|
|
ActualString: String;
|
|
|
|
Row, Col: Integer;
|
|
|
|
TempFile: string; //write xls/xml to this file and read back from it
|
|
|
|
expected: String;
|
|
|
|
actual: String;
|
|
|
|
cell: PCell;
|
2014-05-25 16:49:45 +00:00
|
|
|
fs: TFormatSettings;
|
2014-05-24 20:28:05 +00:00
|
|
|
begin
|
|
|
|
TempFile := GetTempFileName;
|
|
|
|
|
|
|
|
// Create test workbook
|
|
|
|
MyWorkbook := TsWorkbook.Create;
|
2014-05-25 16:49:45 +00:00
|
|
|
// MyWorkbook.FormatSettings.DecimalSeparator := '.';
|
|
|
|
// MyWorkbook.FormatSettings.ShortDateFormat := 'yyyy-mm-dd';
|
2014-05-24 20:28:05 +00:00
|
|
|
MyWorkSheet:= MyWorkBook.AddWorksheet(SHEET);
|
|
|
|
|
|
|
|
// Write out all test formulas
|
|
|
|
// All formulas are in column B
|
2014-05-25 16:49:45 +00:00
|
|
|
WriteRPNFormulaSamples(MyWorksheet, AFormat, true);
|
2014-05-24 20:28:05 +00:00
|
|
|
MyWorkBook.WriteToFile(TempFile, AFormat, true);
|
|
|
|
MyWorkbook.Free;
|
|
|
|
|
|
|
|
// Open the spreadsheet
|
|
|
|
MyWorkbook := TsWorkbook.Create;
|
2014-05-25 16:49:45 +00:00
|
|
|
MyWorkbook.ReadFormulas := true;
|
|
|
|
|
2014-05-24 20:28:05 +00:00
|
|
|
MyWorkbook.ReadFromFile(TempFile, AFormat);
|
|
|
|
if AFormat = sfExcel2 then
|
|
|
|
MyWorksheet := MyWorkbook.GetFirstWorksheet
|
|
|
|
else
|
|
|
|
MyWorksheet := GetWorksheetByName(MyWorkBook, SHEET);
|
|
|
|
if MyWorksheet=nil then
|
|
|
|
fail('Error in test code. Failed to get named worksheet');
|
2014-05-26 15:27:35 +00:00
|
|
|
for Row := 0 to MyWorksheet.GetLastRowIndex do begin
|
2014-05-24 20:28:05 +00:00
|
|
|
cell := MyWorksheet.FindCell(Row, 1);
|
|
|
|
if (cell <> nil) and (Length(cell^.RPNFormulaValue) > 0) then begin
|
|
|
|
actual := MyWorksheet.ReadRPNFormulaAsString(cell);
|
|
|
|
expected := MyWorksheet.ReadAsUTF8Text(Row, 0);
|
2014-05-25 16:49:45 +00:00
|
|
|
CheckEquals(expected, actual, 'Test read formula mismatch, cell '+CellNotation(MyWorkSheet,Row,1));
|
2014-05-24 20:28:05 +00:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
// Finalization
|
|
|
|
MyWorkbook.Free;
|
|
|
|
DeleteFile(TempFile);
|
|
|
|
end;
|
|
|
|
|
2014-06-06 12:17:45 +00:00
|
|
|
procedure TSpreadWriteReadFormulaTests.TestWriteRead_BIFF2_FormulaStrings;
|
2014-05-25 16:49:45 +00:00
|
|
|
begin
|
|
|
|
TestWriteReadFormulaStrings(sfExcel2);
|
|
|
|
end;
|
|
|
|
|
2014-06-06 12:17:45 +00:00
|
|
|
procedure TSpreadWriteReadFormulaTests.TestWriteRead_BIFF5_FormulaStrings;
|
2014-05-25 16:49:45 +00:00
|
|
|
begin
|
|
|
|
TestWriteReadFormulaStrings(sfExcel5);
|
|
|
|
end;
|
|
|
|
|
2014-06-06 12:17:45 +00:00
|
|
|
procedure TSpreadWriteReadFormulaTests.TestWriteRead_BIFF8_FormulaStrings;
|
2014-05-24 20:28:05 +00:00
|
|
|
begin
|
|
|
|
TestWriteReadFormulaStrings(sfExcel8);
|
|
|
|
end;
|
|
|
|
|
|
|
|
initialization
|
|
|
|
// Register so these tests are included in a full run
|
|
|
|
RegisterTest(TSpreadWriteReadFormulaTests);
|
|
|
|
|
|
|
|
|
|
|
|
end.
|
|
|
|
|