2014-05-19 22:26:42 +00:00
|
|
|
unit numformatparsertests;
|
|
|
|
|
|
|
|
{$mode objfpc}{$H+}
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
// Not using Lazarus package as the user may be working with multiple versions
|
|
|
|
// Instead, add .. to unit search path
|
2015-05-30 22:09:53 +00:00
|
|
|
Classes, SysUtils, fpcunit, testregistry,
|
|
|
|
fpstypes, fpspreadsheet, fpsnumformatparser
|
2014-05-19 22:26:42 +00:00
|
|
|
{and a project requirement for lclbase for utf8 handling},
|
|
|
|
testsutility;
|
|
|
|
|
|
|
|
type
|
|
|
|
TParserTestData = record
|
|
|
|
FormatString: String;
|
|
|
|
SollFormatString: String;
|
|
|
|
SollNumFormat: TsNumberFormat;
|
|
|
|
SollSectionCount: Integer;
|
|
|
|
SollDecimals: Byte;
|
2015-05-15 21:14:19 +00:00
|
|
|
SollFactor: Double;
|
|
|
|
SollNumeratorDigits: Integer;
|
|
|
|
SollDenominatorDigits: Integer;
|
2014-05-19 22:26:42 +00:00
|
|
|
SollCurrencySymbol: String;
|
2015-05-30 22:09:53 +00:00
|
|
|
SollSection2Color: TsColor;
|
2014-05-19 22:26:42 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
var
|
2015-05-30 22:09:53 +00:00
|
|
|
ParserTestData: Array[0..13] of TParserTestData;
|
2014-05-19 22:26:42 +00:00
|
|
|
|
|
|
|
procedure InitParserTestData;
|
|
|
|
|
|
|
|
type
|
|
|
|
TSpreadNumFormatParserTests = class(TTestCase)
|
|
|
|
private
|
|
|
|
protected
|
|
|
|
// Set up expected values:
|
|
|
|
procedure SetUp; override;
|
|
|
|
procedure TearDown; override;
|
|
|
|
// Reads numbers values from spreadsheet and checks against list
|
|
|
|
// One cell per test so some tests can fail and those further below may still work
|
|
|
|
published
|
|
|
|
procedure TestNumFormatParser;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
uses
|
|
|
|
TypInfo;
|
|
|
|
|
2014-06-12 22:20:45 +00:00
|
|
|
{ The test will use Excel strings and convert them to fpc dialect }
|
2014-05-19 22:26:42 +00:00
|
|
|
procedure InitParserTestData;
|
|
|
|
begin
|
|
|
|
// Tests with 1 format section only
|
|
|
|
with ParserTestData[0] do begin
|
|
|
|
FormatString := '0';
|
|
|
|
SollFormatString := '0';
|
|
|
|
SollNumFormat := nfFixed;
|
|
|
|
SollSectionCount := 1;
|
|
|
|
SollDecimals := 0;
|
2015-05-15 21:14:19 +00:00
|
|
|
SollFactor := 0;
|
|
|
|
SollNumeratorDigits := 0;
|
|
|
|
SollDenominatorDigits := 0;
|
2014-05-19 22:26:42 +00:00
|
|
|
SollCurrencySymbol := '';
|
|
|
|
end;
|
|
|
|
with ParserTestData[1] do begin
|
|
|
|
FormatString := '0.000';
|
|
|
|
SollFormatString := '0.000';
|
|
|
|
SollNumFormat := nfFixed;
|
|
|
|
SollSectionCount := 1;
|
|
|
|
SollDecimals := 3;
|
2015-05-15 21:14:19 +00:00
|
|
|
SollFactor := 0;
|
|
|
|
SollNumeratorDigits := 0;
|
|
|
|
SollDenominatorDigits := 0;
|
2014-05-19 22:26:42 +00:00
|
|
|
SollCurrencySymbol := '';
|
|
|
|
end;
|
|
|
|
with ParserTestData[2] do begin
|
|
|
|
FormatString := '#,##0.000';
|
|
|
|
SollFormatString := '#,##0.000';
|
|
|
|
SollNumFormat := nfFixedTh;
|
|
|
|
SollSectionCount := 1;
|
|
|
|
SollDecimals := 3;
|
2015-05-15 21:14:19 +00:00
|
|
|
SollFactor := 0;
|
|
|
|
SollNumeratorDigits := 0;
|
|
|
|
SollDenominatorDigits := 0;
|
2014-05-19 22:26:42 +00:00
|
|
|
SollCurrencySymbol := '';
|
|
|
|
end;
|
|
|
|
with ParserTestData[3] do begin
|
|
|
|
FormatString := '0.000%';
|
|
|
|
SollFormatString := '0.000%';
|
|
|
|
SollNumFormat := nfPercentage;
|
|
|
|
SollSectionCount := 1;
|
|
|
|
SollDecimals := 3;
|
2015-05-15 21:14:19 +00:00
|
|
|
SollFactor := 0;
|
|
|
|
SollNumeratorDigits := 0;
|
|
|
|
SollDenominatorDigits := 0;
|
2014-05-19 22:26:42 +00:00
|
|
|
SollCurrencySymbol := '';
|
|
|
|
end;
|
|
|
|
with ParserTestData[4] do begin
|
|
|
|
FormatString := 'hh:mm:ss';
|
2015-04-19 22:03:33 +00:00
|
|
|
SollFormatString := 'hh:mm:ss';
|
2014-05-19 22:26:42 +00:00
|
|
|
SollNumFormat := nfLongTime;
|
|
|
|
SollSectionCount := 1;
|
|
|
|
SollDecimals := 0;
|
2015-05-15 21:14:19 +00:00
|
|
|
SollFactor := 0;
|
|
|
|
SollNumeratorDigits := 0;
|
|
|
|
SollDenominatorDigits := 0;
|
2014-05-19 22:26:42 +00:00
|
|
|
SollCurrencySymbol := '';
|
|
|
|
end;
|
|
|
|
with ParserTestData[5] do begin
|
|
|
|
FormatString := 'hh:mm:ss AM/PM';
|
2015-04-19 22:03:33 +00:00
|
|
|
SollFormatString := 'hh:mm:ss AM/PM';
|
2014-05-19 22:26:42 +00:00
|
|
|
SollNumFormat := nfLongTimeAM;
|
|
|
|
SollSectionCount := 1;
|
|
|
|
SollDecimals := 0;
|
2015-05-15 21:14:19 +00:00
|
|
|
SollFactor := 0;
|
|
|
|
SollNumeratorDigits := 0;
|
|
|
|
SollDenominatorDigits := 0;
|
2014-05-19 22:26:42 +00:00
|
|
|
SollCurrencySymbol := '';
|
|
|
|
end;
|
2014-06-13 14:35:36 +00:00
|
|
|
with ParserTestData[6] do begin
|
|
|
|
FormatString := '[$-409]hh:mm:ss\ AM/PM;@';
|
2015-05-15 21:14:19 +00:00
|
|
|
SollFormatString := 'hh:mm:ss\ AM/PM;@';
|
|
|
|
SollNumFormat := nfCustom;
|
2014-06-13 14:35:36 +00:00
|
|
|
SollSectionCount := 2;
|
|
|
|
SollDecimals := 0;
|
2015-05-15 21:14:19 +00:00
|
|
|
SollFactor := 0;
|
|
|
|
SollNumeratorDigits := 0;
|
|
|
|
SollDenominatorDigits := 0;
|
2014-06-13 14:35:36 +00:00
|
|
|
SollCurrencySymbol := '';
|
|
|
|
end;
|
|
|
|
with ParserTestData[7] do begin
|
|
|
|
FormatString := '[$-F400]dd.mm.yy\ hh:mm';
|
2015-05-15 21:14:19 +00:00
|
|
|
SollFormatString := 'DD.MM.YY\ hh:mm';
|
|
|
|
SollNumFormat := nfCustom;
|
2014-06-13 14:35:36 +00:00
|
|
|
SollSectionCount := 1;
|
|
|
|
SollDecimals := 0;
|
2015-05-15 21:14:19 +00:00
|
|
|
SollFactor := 0;
|
|
|
|
SollNumeratorDigits := 0;
|
|
|
|
SollDenominatorDigits := 0;
|
2014-06-13 14:35:36 +00:00
|
|
|
SollCurrencySymbol := '';
|
|
|
|
end;
|
|
|
|
with ParserTestData[8] do begin
|
2015-05-15 21:14:19 +00:00
|
|
|
FormatString := '[$€] #,##0.00;-[$€] #,##0.00;[$€] 0.00';
|
|
|
|
SollFormatString := '[$€] #,##0.00;-[$€] #,##0.00;[$€] 0.00';
|
2014-05-19 22:26:42 +00:00
|
|
|
SollNumFormat := nfCurrency;
|
|
|
|
SollSectionCount := 3;
|
|
|
|
SollDecimals := 2;
|
2015-05-15 21:14:19 +00:00
|
|
|
SollFactor := 0;
|
|
|
|
SollNumeratorDigits := 0;
|
|
|
|
SollDenominatorDigits := 0;
|
2014-06-13 14:35:36 +00:00
|
|
|
SollCurrencySymbol := '€';
|
2015-05-30 22:09:53 +00:00
|
|
|
SollSection2Color := scBlack;
|
2014-05-19 22:26:42 +00:00
|
|
|
end;
|
2015-05-15 21:14:19 +00:00
|
|
|
with ParserTestData[9] do begin
|
2015-05-30 22:09:53 +00:00
|
|
|
FormatString := '[$€] #,##0.00;[red]-[$€] #,##0.00;[$€] 0.00';
|
|
|
|
SollFormatString := '[$€] #,##0.00;[red]-[$€] #,##0.00;[$€] 0.00';
|
|
|
|
SollNumFormat := nfCurrencyRed;
|
|
|
|
SollSectionCount := 3;
|
|
|
|
SollDecimals := 2;
|
|
|
|
SollFactor := 0;
|
|
|
|
SollNumeratorDigits := 0;
|
|
|
|
SollDenominatorDigits := 0;
|
|
|
|
SollCurrencySymbol := '€';
|
|
|
|
SollSection2Color := scRed;
|
|
|
|
end;
|
|
|
|
with ParserTestData[10] do begin
|
2015-05-15 21:14:19 +00:00
|
|
|
FormatString := '0.00,,';
|
|
|
|
SollFormatString := '0.00,,';
|
|
|
|
SollNumFormat := nfCustom;
|
|
|
|
SollSectionCount := 1;
|
|
|
|
SollDecimals := 2;
|
|
|
|
SollFactor := 1e-6;
|
|
|
|
SollNumeratorDigits := 0;
|
|
|
|
SollDenominatorDigits := 0;
|
|
|
|
SollCurrencySymbol := '';
|
|
|
|
end;
|
2015-05-30 22:09:53 +00:00
|
|
|
with ParserTestData[11] do begin
|
2015-05-15 21:14:19 +00:00
|
|
|
FormatString := '# ??/??';
|
|
|
|
SollFormatString := '# ??/??';
|
|
|
|
SollNumFormat := nfFraction;
|
|
|
|
SollSectionCount := 1;
|
|
|
|
SollDecimals := 0;
|
|
|
|
SollFactor := 0;
|
|
|
|
SollNumeratorDigits := 2;
|
|
|
|
SollDenominatorDigits := 2;
|
|
|
|
SollCurrencySymbol := '';
|
|
|
|
end;
|
2015-05-30 22:09:53 +00:00
|
|
|
with ParserTestData[12] do begin
|
|
|
|
FormatString := 'General;[Red]-General';
|
|
|
|
SollFormatString := 'General;[red]-General';
|
|
|
|
SollNumFormat := nfCustom;
|
|
|
|
SollSectionCount := 2;
|
|
|
|
SollDecimals := 0;
|
|
|
|
SollFactor := 0;
|
|
|
|
SollNumeratorDigits := 0;
|
|
|
|
SollDenominatorDigits := 0;
|
|
|
|
SollCurrencySymbol := '';
|
|
|
|
SollSection2Color := scRed;
|
|
|
|
end;
|
|
|
|
with ParserTestData[13] do begin
|
|
|
|
FormatString := 'General';
|
|
|
|
SollFormatString := 'General';
|
|
|
|
SollNumFormat := nfGeneral;
|
|
|
|
SollSectionCount := 1;
|
|
|
|
SollDecimals := 0;
|
|
|
|
SollFactor := 0;
|
|
|
|
SollNumeratorDigits := 0;
|
|
|
|
SollDenominatorDigits := 0;
|
|
|
|
SollCurrencySymbol := '';
|
|
|
|
end;
|
|
|
|
|
2014-06-13 14:35:36 +00:00
|
|
|
{
|
2014-05-19 22:26:42 +00:00
|
|
|
with ParserTestData[5] do begin
|
|
|
|
FormatString := '#,##0.00 "$";-#,##0.00 "$";-';
|
|
|
|
SollFormatString := '#,##0.00 "$";-#,##0.00 "$";-';
|
|
|
|
SollNumFormat := nfCurrencyDash;
|
|
|
|
SollSectionCount := 3;
|
|
|
|
SollDecimals := 2;
|
|
|
|
SollCurrencySymbol := '$';
|
|
|
|
end; }
|
|
|
|
|
|
|
|
{
|
|
|
|
// This case will report a mismatching FormatString because of the [RED] --> ignore
|
|
|
|
with ParserTestData[6] do begin
|
|
|
|
FormatString := '#,##0.00 "$";[RED]-#,##0.00 "$";-';
|
|
|
|
SollFormatString := '#,##0.00 "$";-#,##0.00 "$";-';
|
|
|
|
SollNumFormat := nfCurrencyDashRed;
|
|
|
|
SollSectionCount := 3;
|
|
|
|
SollDecimals := 2;
|
|
|
|
SollCurrencySymbol := '$';
|
|
|
|
end;
|
|
|
|
}
|
|
|
|
end;
|
|
|
|
|
|
|
|
{ TSpreadNumFormatParserTests }
|
|
|
|
|
|
|
|
procedure TSpreadNumFormatParserTests.SetUp;
|
|
|
|
begin
|
|
|
|
inherited SetUp;
|
|
|
|
InitParserTestData;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TSpreadNumFormatParserTests.TearDown;
|
|
|
|
begin
|
|
|
|
inherited TearDown;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TSpreadNumFormatParserTests.TestNumFormatParser;
|
|
|
|
var
|
|
|
|
i: Integer;
|
|
|
|
parser: TsNumFormatParser;
|
|
|
|
MyWorkbook: TsWorkbook;
|
2014-06-12 22:20:45 +00:00
|
|
|
actual: String;
|
2014-05-19 22:26:42 +00:00
|
|
|
begin
|
|
|
|
MyWorkbook := TsWorkbook.Create; // needed to provide the FormatSettings for the parser
|
|
|
|
try
|
2015-05-15 21:14:19 +00:00
|
|
|
for i:=0 to High(ParserTestData) do begin
|
2015-05-31 16:06:22 +00:00
|
|
|
parser := TsNumFormatParser.Create(ParserTestData[i].FormatString, MyWorkbook.FormatSettings);
|
2014-05-19 22:26:42 +00:00
|
|
|
try
|
2015-04-19 22:03:33 +00:00
|
|
|
actual := parser.FormatString;
|
2014-06-12 22:20:45 +00:00
|
|
|
CheckEquals(ParserTestData[i].SollFormatString, actual,
|
|
|
|
'Test format string ' + ParserTestData[i].SollFormatString + ' construction mismatch');
|
2015-05-15 21:14:19 +00:00
|
|
|
CheckEquals(
|
|
|
|
GetEnumName(TypeInfo(TsNumberFormat), ord(ParserTestData[i].SollNumFormat)),
|
|
|
|
GetEnumName(TypeInfo(TsNumberformat), ord(parser.ParsedSections[0].NumFormat)),
|
|
|
|
'Test format (' + ParserTestData[i].FormatString + ') detection mismatch');
|
2014-05-19 22:26:42 +00:00
|
|
|
CheckEquals(ParserTestData[i].SollDecimals, parser.ParsedSections[0].Decimals,
|
|
|
|
'Test format (' + ParserTestData[i].FormatString + ') decimal detection mismatch');
|
|
|
|
CheckEquals(ParserTestData[i].SollCurrencySymbol, parser.ParsedSections[0].CurrencySymbol,
|
|
|
|
'Test format (' + ParserTestData[i].FormatString + ') currency symbol detection mismatch');
|
|
|
|
CheckEquals(ParserTestData[i].SollSectionCount, parser.ParsedSectionCount,
|
|
|
|
'Test format (' + ParserTestData[i].FormatString + ') section count mismatch');
|
2015-05-15 21:14:19 +00:00
|
|
|
CheckEquals(ParserTestData[i].SollFactor, parser.ParsedSections[0].Factor,
|
|
|
|
'Test format (' + ParserTestData[i].FormatString + ') factor mismatch');
|
|
|
|
CheckEquals(ParserTestData[i].SollNumeratorDigits, parser.ParsedSections[0].FracNumerator,
|
|
|
|
'Test format (' + ParserTestData[i].FormatString + ') numerator digits mismatch');
|
|
|
|
CheckEquals(ParserTestData[i].SollDenominatorDigits, parser.ParsedSections[0].FracDenominator,
|
|
|
|
'Test format (' + ParserTestData[i].FormatString + ') denominator digits mismatch');
|
2015-05-30 22:09:53 +00:00
|
|
|
if ParserTestData[i].SollSectionCount > 1 then
|
|
|
|
CheckEquals(ParserTestData[i].SollSection2Color, parser.ParsedSections[1].Color,
|
|
|
|
'Test format (' + ParserTestData[i].FormatString + ') section 2 color mismatch');
|
2014-05-19 22:26:42 +00:00
|
|
|
finally
|
|
|
|
parser.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
finally
|
|
|
|
MyWorkbook.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
initialization
|
|
|
|
// Register so these tests are included in a full run
|
|
|
|
RegisterTest(TSpreadNumFormatParserTests);
|
|
|
|
InitParserTestData; //useful to have norm data if other code want to use this unit
|
|
|
|
end.
|
|
|
|
|
|
|
|
end.
|
|
|
|
|