LCOV - code coverage report
Current view: top level - sc/qa/unit/helper - csv_handler.hxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 86 0.0 %
Date: 2014-04-14 Functions: 0 16 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  */
       9             : 
      10             : #ifndef SC_QA_CSV_HANDLER_HXX
      11             : #define SC_QA_CSV_HANDLER_HXX
      12             : 
      13             : #include <iostream>
      14             : 
      15             : #include "docsh.hxx"
      16             : #include "postit.hxx"
      17             : #include "patattr.hxx"
      18             : #include "scitems.hxx"
      19             : #include "document.hxx"
      20             : #include "cellform.hxx"
      21             : #include "cellvalue.hxx"
      22             : 
      23             : #include "rtl/strbuf.hxx"
      24             : 
      25             : #include <test/bootstrapfixture.hxx>
      26             : 
      27             : #define DEBUG_CSV_HANDLER 0
      28             : 
      29             : namespace {
      30             : 
      31           0 : OUString getConditionalFormatString(ScDocument* pDoc, SCCOL nCol, SCROW nRow, SCTAB nTab)
      32             : {
      33           0 :     OUString aString;
      34             :     Color* pColor;
      35           0 :     ScRefCellValue aCell;
      36           0 :     aCell.assign(*pDoc, ScAddress(nCol, nRow, nTab));
      37           0 :     if (aCell.isEmpty())
      38           0 :         return aString;
      39             : 
      40           0 :     const SfxItemSet* pCondSet = pDoc->GetCondResult( nCol, nRow, nTab );
      41           0 :     const ScPatternAttr* pPattern = pDoc->GetPattern(nCol, nRow, nTab);
      42           0 :     SvNumberFormatter* pFormatter = pDoc->GetFormatTable();
      43           0 :     sal_uInt32 nFormat = pPattern->GetNumberFormat( pFormatter, pCondSet );
      44           0 :     aString = ScCellFormat::GetString(*pDoc, ScAddress(nCol, nRow, nTab), nFormat, &pColor, *pFormatter);
      45           0 :     return aString;
      46             : }
      47             : 
      48           0 : OString createErrorMessage(SCCOL nCol, SCROW nRow, SCTAB nTab)
      49             : {
      50           0 :     OStringBuffer aString("Error in Table: ");
      51           0 :     aString.append(static_cast<sal_Int32>(nTab));
      52           0 :     aString.append(" Column: ");
      53           0 :     aString.append(static_cast<sal_Int32>(nCol));
      54           0 :     aString.append(" Row: ");
      55           0 :     aString.append(nRow);
      56           0 :     return aString.makeStringAndClear();
      57             : }
      58             : 
      59           0 : OString createErrorMessage(SCCOL nCol, SCROW nRow, SCTAB nTab, const OUString& rExpectedString, const OUString& rString)
      60             : {
      61           0 :     OStringBuffer aString(createErrorMessage(nCol, nRow, nTab));
      62           0 :     aString.append("; Expected: '");
      63           0 :     aString.append(OUStringToOString(rExpectedString, RTL_TEXTENCODING_UTF8));
      64           0 :     aString.append("' Found: '");
      65           0 :     aString.append(OUStringToOString(rString, RTL_TEXTENCODING_UTF8));
      66           0 :     aString.append("'");
      67           0 :     return aString.makeStringAndClear();
      68             : }
      69             : 
      70           0 : OString createErrorMessage(SCCOL nCol, SCROW nRow, SCTAB nTab, double aExpected, double aValue)
      71             : {
      72           0 :     OStringBuffer aString(createErrorMessage(nCol, nRow, nTab));
      73           0 :     aString.append("; Expected: '");
      74           0 :     aString.append(aExpected);
      75           0 :     aString.append("' Found: '");
      76           0 :     aString.append(aValue);
      77           0 :     aString.append("'");
      78           0 :     return aString.makeStringAndClear();
      79             : 
      80             : }
      81             : 
      82             : }
      83             : 
      84             : class csv_handler
      85             : {
      86             : public:
      87           0 :     csv_handler(ScDocument* pDoc, SCTAB nTab, StringType eType = StringValue):
      88             :             mpDoc(pDoc),
      89             :             mnCol(0),
      90             :             mnRow(0),
      91             :             mnTab(nTab),
      92           0 :             meStringType(eType)  {}
      93             : 
      94           0 :     void begin_parse() {}
      95             : 
      96           0 :     void end_parse() {}
      97             : 
      98           0 :     void begin_row() {}
      99             : 
     100           0 :     void end_row()
     101             :     {
     102           0 :         ++mnRow;
     103           0 :         mnCol = 0;
     104           0 :     }
     105             : 
     106           0 :     void cell(const char* p, size_t n)
     107             :     {
     108             : #if DEBUG_CSV_HANDLER
     109             :         std::cout << "Col: " << mnCol << " Row: " << mnRow << std::endl;
     110             : #endif //DEBUG_CSV_HANDLER
     111           0 :         if (n == 0)
     112             :         {
     113             :             // Empty cell.
     114           0 :             if (!mpDoc->GetString(mnCol, mnRow, mnTab).isEmpty())
     115             :             {
     116             :                 // cell in the document is not empty.
     117           0 :                 CPPUNIT_ASSERT_MESSAGE(createErrorMessage(mnCol, mnRow, mnTab).getStr(), false);
     118             :             }
     119             :         }
     120           0 :         else if (meStringType == PureString)
     121             :         {
     122           0 :             OUString aCSVString(p, n, RTL_TEXTENCODING_UTF8);
     123           0 :             OUString aString = mpDoc->GetString(mnCol, mnRow, mnTab);
     124             : 
     125             : #if DEBUG_CSV_HANDLER
     126             :                 std::cout << "String: " << OUStringToOString(aString, RTL_TEXTENCODING_UTF8).getStr() << std::endl;
     127             :                 std::cout << "CSVString: " << OUStringToOString(aCSVString, RTL_TEXTENCODING_UTF8).getStr() << std::endl;
     128             :                 std::cout << "result: " << (int)(aCSVString == aString) << std::endl;
     129             : #endif //DEBUG_CSV_HANDLER
     130             : 
     131           0 :             CPPUNIT_ASSERT_EQUAL_MESSAGE(createErrorMessage(mnCol, mnRow, mnTab, aCSVString, aString).getStr(), aCSVString, aString);
     132             :         }
     133             :         else
     134             :         {
     135           0 :             char* pRemainingChars = NULL;
     136           0 :             std::string aStr(p, n);
     137           0 :             double nValue = strtod(&aStr[0], &pRemainingChars);
     138           0 :             if (*pRemainingChars)
     139             :             {
     140           0 :                 OUString aString;
     141           0 :                 switch (meStringType)
     142             :                 {
     143             :                     case StringValue:
     144           0 :                         aString = mpDoc->GetString(mnCol, mnRow, mnTab);
     145           0 :                         break;
     146             :                     case FormulaValue:
     147           0 :                         mpDoc->GetFormula(mnCol, mnRow, mnTab, aString);
     148           0 :                         break;
     149             :                     default:
     150           0 :                         break;
     151             :                 }
     152           0 :                 OUString aCSVString(p, n, RTL_TEXTENCODING_UTF8);
     153             : #if DEBUG_CSV_HANDLER
     154             :                 std::cout << "String: " << OUStringToOString(aString, RTL_TEXTENCODING_UTF8).getStr() << std::endl;
     155             :                 std::cout << "CSVString: " << OUStringToOString(aCSVString, RTL_TEXTENCODING_UTF8).getStr() << std::endl;
     156             :                 std::cout << "result: " << (int)(aCSVString == aString) << std::endl;
     157             : #endif //DEBUG_CSV_HANDLER
     158             : 
     159           0 :                 CPPUNIT_ASSERT_EQUAL_MESSAGE(createErrorMessage(mnCol, mnRow, mnTab, aCSVString, aString).getStr(), aCSVString, aString);
     160             :             }
     161             :             else
     162             :             {
     163             :                 double aValue;
     164           0 :                 mpDoc->GetValue(mnCol, mnRow, mnTab, aValue);
     165             : #if DEBUG_CSV_HANDLER
     166             :                 std::cout << "Value: " << aValue << std::endl;
     167             :                 std::cout << "CSVValue: " << nValue << std::endl;
     168             :                 std::cout << "result: " << (int)(aValue == nValue) << std::endl;
     169             : #endif //DEBUG_CSV_HANDLER
     170           0 :                 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(createErrorMessage(mnCol, mnRow, mnTab, nValue, aValue).getStr(), nValue, aValue, 1e-10);
     171           0 :             }
     172             :         }
     173           0 :         ++mnCol;
     174           0 :     }
     175             : 
     176             : private:
     177             :     ScDocument* mpDoc;
     178             :     SCCOL mnCol;
     179             :     SCROW mnRow;
     180             :     SCTAB mnTab;
     181             :     StringType meStringType;
     182             : };
     183             : 
     184             : 
     185             : class conditional_format_handler
     186             : {
     187             : public:
     188           0 :     conditional_format_handler(ScDocument* pDoc, SCTAB nTab):
     189             :         mpDoc(pDoc),
     190             :         mnCol(0),
     191             :         mnRow(0),
     192           0 :         mnTab(nTab) {}
     193             : 
     194           0 :     void begin_parse() {}
     195             : 
     196           0 :     void end_parse() {}
     197             : 
     198           0 :     void begin_row() {}
     199             : 
     200           0 :     void end_row()
     201             :     {
     202           0 :         ++mnRow;
     203           0 :         mnCol = 0;
     204           0 :     }
     205             : 
     206           0 :     void cell(const char* p, size_t n)
     207             :     {
     208             : #if DEBUG_CSV_HANDLER
     209             :         std::cout << "Col: " << mnCol << " Row: " << mnRow << std::endl;
     210             : #endif //DEBUG_CSV_HANDLER
     211           0 :         OUString aString = getConditionalFormatString(mpDoc, mnCol, mnRow, mnTab);
     212           0 :         OUString aCSVString(p, n, RTL_TEXTENCODING_UTF8);
     213             : #if DEBUG_CSV_HANDLER
     214             :         std::cout << "String: " << OUStringToOString(aString, RTL_TEXTENCODING_UTF8).getStr() << std::endl;
     215             :         std::cout << "CSVString: " << OUStringToOString(aCSVString, RTL_TEXTENCODING_UTF8).getStr() << std::endl;
     216             :         std::cout << "result: " << (int)(aCSVString == aString) << std::endl;
     217             : #endif //DEBUG_CSV_HANDLER
     218           0 :         CPPUNIT_ASSERT_MESSAGE(createErrorMessage(mnCol, mnRow, mnTab, aCSVString, aString).getStr(), aString == aCSVString );
     219           0 :         ++mnCol;
     220           0 :     }
     221             : 
     222             : private:
     223             :     ScDocument* mpDoc;
     224             :     SCCOL mnCol;
     225             :     SCROW mnRow;
     226             :     SCTAB mnTab;
     227             : };
     228             : 
     229             : #endif
     230             : 
     231             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10