From a98a75327e7c73317f8e453c5887999a8a157a1c Mon Sep 17 00:00:00 2001 From: bigchimp Date: Tue, 10 Dec 2013 12:06:12 +0000 Subject: [PATCH] Tests: + added test for GetSheetByName function + Added TSpreadInternalTests.ReadDateAsUTF8 to test wp's ReadAsUTF8 forum problem report with date/time cells * Fixed displaying sheet+column where problems occur (CellNotation function) git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@2859 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- components/fpspreadsheet/tests/datetests.pas | 16 +++-- .../fpspreadsheet/tests/internaltests.pas | 57 ++++++++++++++++++ .../fpspreadsheet/tests/numberstests.pas | 6 +- .../fpspreadsheet/tests/stringtests.pas | 12 ++-- .../fpspreadsheet/tests/testdbwriter.res | Bin 20340 -> 20760 bytes .../fpspreadsheet/tests/testsutility.pas | 17 ++++-- 6 files changed, 87 insertions(+), 21 deletions(-) diff --git a/components/fpspreadsheet/tests/datetests.pas b/components/fpspreadsheet/tests/datetests.pas index 9714031fb..b565b702a 100644 --- a/components/fpspreadsheet/tests/datetests.pas +++ b/components/fpspreadsheet/tests/datetests.pas @@ -145,8 +145,8 @@ begin MyWorkSheet.WriteDateTime(Row,0,SollDates[Row]); // Some checks inside worksheet itself if not(MyWorkSheet.ReadAsDateTime(Row,0,ActualDateTime)) then - Fail('Failed writing date time for cell '+CellNotation(Row)); - CheckEquals(SollDates[Row],ActualDateTime,'Test date/time value mismatch cell '+CellNotation(Row)); + Fail('Failed writing date time for cell '+CellNotation(MyWorkSheet,Row)); + CheckEquals(SollDates[Row],ActualDateTime,'Test date/time value mismatch cell '+CellNotation(MyWorksheet,Row)); end; MyWorkBook.WriteToFile(TempFile,sfExcel8,true); MyWorkbook.Free; @@ -162,8 +162,8 @@ begin for Row := Low(SollDates) to High(SollDates) do begin if not(MyWorkSheet.ReadAsDateTime(Row,0,ActualDateTime)) then - Fail('Could not read date time for cell '+CellNotation(Row)); - CheckEquals(SollDates[Row],ActualDateTime,'Test date/time value mismatch cell '+CellNotation(Row)); + Fail('Could not read date time for cell '+CellNotation(MyWorkSheet,Row)); + CheckEquals(SollDates[Row],ActualDateTime,'Test date/time value mismatch cell '+CellNotation(MyWorkSheet,Row)); end; // Finalization MyWorkbook.Free; @@ -186,11 +186,15 @@ begin MyWorksheet:=GetWorksheetByName(MyWorkBook,DatesSheet); if MyWorksheet=nil then fail('Error in test code. Failed to get named worksheet'); + // We know these are valid time/date/datetime values.... + // Just test for empty string; we'll probably end up in a maze of localized date/time stuff + // if we don't. + CheckNotEquals(MyWorkSheet.ReadAsUTF8Text(Row, 0), '','Could not read date time as string for cell '+CellNotation(MyWorkSheet,Row)); if not(MyWorkSheet.ReadAsDateTime(Row, 0, ActualDateTime)) then - Fail('Could not read date time for cell '+CellNotation(Row)); + Fail('Could not read date time for cell '+CellNotation(MyWorkSheet,Row)); CheckEquals(SollDates[Row],ActualDateTime,'Test date/time value mismatch ' - +'cell '+CellNotation(Row)); + +'cell '+CellNotation(MyWorksheet,Row)); // Finalization MyWorkbook.Free; diff --git a/components/fpspreadsheet/tests/internaltests.pas b/components/fpspreadsheet/tests/internaltests.pas index d576a831c..92a9a4e54 100644 --- a/components/fpspreadsheet/tests/internaltests.pas +++ b/components/fpspreadsheet/tests/internaltests.pas @@ -22,6 +22,9 @@ uses type { TSpreadReadInternalTests } // Read from xls/xml file with known values + + { TSpreadInternalTests } + TSpreadInternalTests= class(TTestCase) private protected @@ -32,6 +35,11 @@ type //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; + // Verify GetSheetByName returns the correct sheet number + // GetSheetByName was implemented in SVN revision 2857 + procedure GetSheetByName; + // Write out date cell and try to read as UTF8; verify if contents the same + procedure ReadDateAsUTF8; end; implementation @@ -54,6 +62,55 @@ begin MyWorkbook.Free; end; +procedure TSpreadInternalTests.GetSheetByName; +const + AnotherSheet='AnotherSheet'; +var + MyWorksheet: TsWorksheet; + MyWorkbook: TsWorkbook; + Row: Cardinal; +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; + +procedure TSpreadInternalTests.ReadDateAsUTF8; +var + ActualDT: TDateTime; + ActualDTString: string; //Result from ReadAsUTF8Text + Cell: PCell; + 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; + procedure TSpreadInternalTests.SetUp; begin diff --git a/components/fpspreadsheet/tests/numberstests.pas b/components/fpspreadsheet/tests/numberstests.pas index 6ef754b86..a500c869b 100644 --- a/components/fpspreadsheet/tests/numberstests.pas +++ b/components/fpspreadsheet/tests/numberstests.pas @@ -134,7 +134,7 @@ begin MyWorkSheet.WriteNumber(Row,0,SollNumbers[Row]); // Some checks inside worksheet itself ActualNumber:=MyWorkSheet.ReadAsNumber(Row,0); - CheckEquals(SollNumbers[Row],ActualNumber,'Test value mismatch cell '+CellNotation(Row)); + CheckEquals(SollNumbers[Row],ActualNumber,'Test value mismatch cell '+CellNotation(MyWorkSheet,Row)); end; MyWorkBook.WriteToFile(TempFile,sfExcel8,true); MyWorkbook.Free; @@ -150,7 +150,7 @@ begin for Row := Low(SollNumbers) to High(SollNumbers) do begin ActualNumber:=MyWorkSheet.ReadAsNumber(Row,0); - CheckEquals(SollNumbers[Row],ActualNumber,'Test value mismatch cell '+CellNotation(Row)); + CheckEquals(SollNumbers[Row],ActualNumber,'Test value mismatch cell '+CellNotation(MyWorkSheet,Row)); end; // Finalization MyWorkbook.Free; @@ -176,7 +176,7 @@ begin ActualNumber:=MyWorkSheet.ReadAsNumber(Row, 0); CheckEquals(SollNumbers[Row],ActualNumber,'Test value mismatch ' - +'cell '+CellNotation(Row)); + +'cell '+CellNotation(MyWorkSheet,Row)); // Finalization MyWorkbook.Free; diff --git a/components/fpspreadsheet/tests/stringtests.pas b/components/fpspreadsheet/tests/stringtests.pas index 90ce02319..1fe7def2b 100644 --- a/components/fpspreadsheet/tests/stringtests.pas +++ b/components/fpspreadsheet/tests/stringtests.pas @@ -120,7 +120,7 @@ begin MyWorkSheet.WriteUTF8Text(Row,0,SollStrings[Row]); // Some checks inside worksheet itself ActualString:=MyWorkSheet.ReadAsUTF8Text(Row,0); - CheckEquals(SollStrings[Row],ActualString,'Test value mismatch cell '+CellNotation(Row)); + CheckEquals(SollStrings[Row],ActualString,'Test value mismatch cell '+CellNotation(MyWorkSheet,Row)); end; MyWorkBook.WriteToFile(TempFile,sfExcel8,true); MyWorkbook.Free; @@ -136,7 +136,7 @@ begin for Row := Low(SollStrings) to High(SollStrings) do begin ActualString:=MyWorkSheet.ReadAsUTF8Text(Row,0); - CheckEquals(SollStrings[Row],ActualString,'Test value mismatch cell '+CellNotation(Row)); + CheckEquals(SollStrings[Row],ActualString,'Test value mismatch cell '+CellNotation(MyWorkSheet,Row)); end; // Finalization MyWorkbook.Free; @@ -180,7 +180,7 @@ begin // Some checks inside worksheet itself ActualString:=MyWorkSheet.ReadAsUTF8Text(Row,0); CheckEquals(length(LocalNormStrings[Row]),length(ActualString), - 'Test value mismatch cell '+CellNotation(Row)+ + 'Test value mismatch cell '+CellNotation(MyWorkSheet,Row)+ ' for string length.'); except { When over size limit we expect to hit this: @@ -236,11 +236,11 @@ begin // Allow for truncation of excessive strings by fpspreadsheet if length(LocalNormStrings[Row])>MaxBytesBIFF8 then CheckEquals(MaxBytesBIFF8,length(ActualString), - 'Test value mismatch cell '+CellNotation(Row)+ + 'Test value mismatch cell '+CellNotation(MyWorkSheet,Row)+ ' for string length.') else CheckEquals(length(LocalNormStrings[Row]),length(ActualString), - 'Test value mismatch cell '+CellNotation(Row)+ + 'Test value mismatch cell '+CellNotation(MyWorkSheet,Row)+ ' for string length.'); end; // Finalization @@ -267,7 +267,7 @@ begin ActualString:=MyWorkSheet.ReadAsUTF8Text(Row,0); CheckEquals(SollStrings[Row],ActualString,'Test value mismatch ' - +'cell '+CellNotation(Row)); + +'cell '+CellNotation(MyWorkSheet,Row)); // Finalization MyWorkbook.Free; diff --git a/components/fpspreadsheet/tests/testdbwriter.res b/components/fpspreadsheet/tests/testdbwriter.res index 7f534f94f3ea0bfbeb0ded9294e4d6f1c1331d76..44b6e1470a8a52bc6bacaba7f2a86efba8ea305e 100644 GIT binary patch delta 3586 zcmb7HU2Gdw7Ow3$jwhb{I&B0nl`EHCKG!SyRm1inQ^Enw1hT=(2_!u zf}ln80UiLsHoaR^s_nL_7MAi-M6}ZKyM16+S`aEfLi{aDfCpZ-tSUi4LMnWB#vYGR zip9(EJ@}^_uk|0{%kpU&T@>i{xi3@?)EB{VVT9;n#I#zI~_)d#n!y|E@y+X zcxAl>Vm0ZpIP}4Gi(4NoSX%VK8B4u3a7fDTbTX#Fmn{wYV#HcaTvoQI=*R|DP*hn^ z@)@;6dBEz@QU9>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^-`Xo~1&2nC+xa^bwlgb($x$Tr=yB$4?y>aMjo6sv0*|&TOooUCnH)$MZRC`Qixr zzy1G_oC~-p$-&K=f$v&zGNdY_a4S&2woZR5By7{Jw(4_5}ddq!y! zW`|Cb>A<@@x+7j{a^p9N0B#SAN0?p7k)1t%+n+)mjIg!v;^3G5?Z1X?umi(Byc-!} z(9Js$!O|EjamqW9Rm?>lczIwCW};6wa``YiZbY$y7h)G1K^I2`Oz4**=NmzPx`*eH z?rjB2;-yB=k-Zs{_0M}RH-f$sug7{Z(Z#oSG4WU7mi zFN`^`G4}n&D|m1q6*67!-`9!1rheGiC1%3EF;3_T2|PExylZL|6kQM@+{>+}*wetmk9A#r2cVcNRW&*Qb31g@nW_$Ymn$%M9mW7Rf*rw%M} zGs*NIm^semHfCm>4ZqHmxN&->9fxKXsXT*EW@_ucYOdiwxW(n5^bU^BCT+BZ!Z}-{ z@)%y6U0^!2XC3fmAHb8@No;0iI{M+t1$o>HIgQ3Wgx}>-_*d>eu3MA8M|FaY?U(H{ zbLb`f{!kiZj;-&b`MPYQhZS$+!ZHKb8x*pOaOYP!!9gS(Tv(z~Iq gRkB%GtCQkwRW$s(rCKCUlxor4DvdS~a?d?q0TXI&DgXcg diff --git a/components/fpspreadsheet/tests/testsutility.pas b/components/fpspreadsheet/tests/testsutility.pas index 62b7df79a..b73ad42fe 100644 --- a/components/fpspreadsheet/tests/testsutility.pas +++ b/components/fpspreadsheet/tests/testsutility.pas @@ -20,9 +20,8 @@ const NumbersSheet = 'Numbers'; //worksheet name StringsSheet = 'Texts'; //worksheet name -// Returns an A.. notation based on row (e.g. A1). -// Useful as all test values should be put in the A column of the spreadsheet -function CellNotation(Row: integer): string; +// Returns an A.. notation based on sheet, row, optional column (e.g. A1). +function CellNotation(WorkSheet: TsWorksheet; Row: integer; Column: integer=0): string; // Note: using this function instead of GetWorkSheetByName for compatibility with // older fpspreadsheet versions that don't have that function @@ -56,11 +55,17 @@ begin end; end; -function CellNotation(Row: integer): string; +function CellNotation(WorkSheet: TsWorksheet; Row: integer; Column: integer=0): string; begin // From 0-based to Excel A1 notation - // Note: we're only testing in the A column, that's why we hardcode the value - result:=DatesSheet+'!A'+inttostr(Row+1); + // Only goes from column A to Z... + if not(assigned(Worksheet)) then + result:='CellNotation: error getting worksheet.' + else + if Column<26 then + result:=WorkSheet.Name+'!'+char(Column+65)+inttostr(Row+1) + else + result:=WorkSheet.Name+'!'+inttostr(Column+1)+':'+inttostr(Row+1) end;