LCOV - code coverage report
Current view: top level - sc/qa/unit/helper - csv_handler.hxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 82 86 95.3 %
Date: 2015-06-13 12:38:46 Functions: 16 16 100.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 INCLUDED_SC_QA_UNIT_HELPER_CSV_HANDLER_HXX
      11             : #define INCLUDED_SC_QA_UNIT_HELPER_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        1430 : OUString getConditionalFormatString(ScDocument* pDoc, SCCOL nCol, SCROW nRow, SCTAB nTab)
      32             : {
      33        1430 :     OUString aString;
      34             :     Color* pColor;
      35        2860 :     ScRefCellValue aCell;
      36        1430 :     aCell.assign(*pDoc, ScAddress(nCol, nRow, nTab));
      37        1430 :     if (aCell.isEmpty())
      38         704 :         return aString;
      39             : 
      40         726 :     const SfxItemSet* pCondSet = pDoc->GetCondResult( nCol, nRow, nTab );
      41         726 :     const ScPatternAttr* pPattern = pDoc->GetPattern(nCol, nRow, nTab);
      42         726 :     SvNumberFormatter* pFormatter = pDoc->GetFormatTable();
      43         726 :     sal_uInt32 nFormat = pPattern->GetNumberFormat( pFormatter, pCondSet );
      44         726 :     aString = ScCellFormat::GetString(*pDoc, ScAddress(nCol, nRow, nTab), nFormat, &pColor, *pFormatter);
      45         726 :     return aString;
      46             : }
      47             : 
      48        2714 : OString createErrorMessage(SCCOL nCol, SCROW nRow, SCTAB nTab)
      49             : {
      50        2714 :     OStringBuffer aString("Error in Table: ");
      51        2714 :     aString.append(static_cast<sal_Int32>(nTab));
      52        2714 :     aString.append(" Column: ");
      53        2714 :     aString.append(static_cast<sal_Int32>(nCol));
      54        2714 :     aString.append(" Row: ");
      55        2714 :     aString.append(nRow);
      56        2714 :     return aString.makeStringAndClear();
      57             : }
      58             : 
      59        2398 : OString createErrorMessage(SCCOL nCol, SCROW nRow, SCTAB nTab, const OUString& rExpectedString, const OUString& rString)
      60             : {
      61        2398 :     OStringBuffer aString(createErrorMessage(nCol, nRow, nTab));
      62        2398 :     aString.append("; Expected: '");
      63        2398 :     aString.append(OUStringToOString(rExpectedString, RTL_TEXTENCODING_UTF8));
      64        2398 :     aString.append("' Found: '");
      65        2398 :     aString.append(OUStringToOString(rString, RTL_TEXTENCODING_UTF8));
      66        2398 :     aString.append("'");
      67        2398 :     return aString.makeStringAndClear();
      68             : }
      69             : 
      70         316 : OString createErrorMessage(SCCOL nCol, SCROW nRow, SCTAB nTab, double aExpected, double aValue)
      71             : {
      72         316 :     OStringBuffer aString(createErrorMessage(nCol, nRow, nTab));
      73         316 :     aString.append("; Expected: '");
      74         316 :     aString.append(aExpected);
      75         316 :     aString.append("' Found: '");
      76         316 :     aString.append(aValue);
      77         316 :     aString.append("'");
      78         316 :     return aString.makeStringAndClear();
      79             : 
      80             : }
      81             : 
      82             : }
      83             : 
      84             : class csv_handler
      85             : {
      86             : public:
      87          34 :     csv_handler(ScDocument* pDoc, SCTAB nTab, StringType eType = StringValue):
      88             :             mpDoc(pDoc),
      89             :             mnCol(0),
      90             :             mnRow(0),
      91             :             mnTab(nTab),
      92          34 :             meStringType(eType)  {}
      93             : 
      94          34 :     static void begin_parse() {}
      95             : 
      96          33 :     static void end_parse() {}
      97             : 
      98         493 :     static void begin_row() {}
      99             : 
     100         492 :     void end_row()
     101             :     {
     102         492 :         ++mnRow;
     103         492 :         mnCol = 0;
     104         492 :     }
     105             : 
     106        3675 :     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        3675 :         if (n == 0)
     112             :         {
     113             :             // Empty cell.
     114        2391 :             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        1284 :         else if (meStringType == PureString)
     121             :         {
     122         742 :             OUString aCSVString(p, n, RTL_TEXTENCODING_UTF8);
     123        1484 :             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        1485 :             CPPUNIT_ASSERT_EQUAL_MESSAGE(createErrorMessage(mnCol, mnRow, mnTab, aCSVString, aString).getStr(), aCSVString, aString);
     132             :         }
     133             :         else
     134             :         {
     135         542 :             char* pRemainingChars = NULL;
     136         542 :             std::string aStr(p, n);
     137         542 :             double nValue = strtod(&aStr[0], &pRemainingChars);
     138         542 :             if (*pRemainingChars)
     139             :             {
     140         226 :                 OUString aString;
     141         226 :                 switch (meStringType)
     142             :                 {
     143             :                     case StringValue:
     144         226 :                         aString = mpDoc->GetString(mnCol, mnRow, mnTab);
     145         226 :                         break;
     146             :                     case FormulaValue:
     147           0 :                         mpDoc->GetFormula(mnCol, mnRow, mnTab, aString);
     148           0 :                         break;
     149             :                     default:
     150           0 :                         break;
     151             :                 }
     152         226 :                 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         226 :                 CPPUNIT_ASSERT_EQUAL_MESSAGE(createErrorMessage(mnCol, mnRow, mnTab, aCSVString, aString).getStr(), aCSVString, aString);
     160             :             }
     161             :             else
     162             :             {
     163             :                 double aValue;
     164         316 :                 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         316 :                 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(createErrorMessage(mnCol, mnRow, mnTab, nValue, aValue).getStr(), nValue, aValue, 1e-10);
     171         542 :             }
     172             :         }
     173        3674 :         ++mnCol;
     174        3674 :     }
     175             : 
     176             : private:
     177             :     ScDocument* mpDoc;
     178             :     SCCOL mnCol;
     179             :     SCROW mnRow;
     180             :     SCTAB mnTab;
     181             :     StringType meStringType;
     182             : };
     183             : 
     184             : class conditional_format_handler
     185             : {
     186             : public:
     187           7 :     conditional_format_handler(ScDocument* pDoc, SCTAB nTab):
     188             :         mpDoc(pDoc),
     189             :         mnCol(0),
     190             :         mnRow(0),
     191           7 :         mnTab(nTab) {}
     192             : 
     193           7 :     static void begin_parse() {}
     194             : 
     195           7 :     static void end_parse() {}
     196             : 
     197          95 :     static void begin_row() {}
     198             : 
     199          95 :     void end_row()
     200             :     {
     201          95 :         ++mnRow;
     202          95 :         mnCol = 0;
     203          95 :     }
     204             : 
     205        1430 :     void cell(const char* p, size_t n)
     206             :     {
     207             : #if DEBUG_CSV_HANDLER
     208             :         std::cout << "Col: " << mnCol << " Row: " << mnRow << std::endl;
     209             : #endif //DEBUG_CSV_HANDLER
     210        1430 :         OUString aString = getConditionalFormatString(mpDoc, mnCol, mnRow, mnTab);
     211        2860 :         OUString aCSVString(p, n, RTL_TEXTENCODING_UTF8);
     212             : #if DEBUG_CSV_HANDLER
     213             :         std::cout << "String: " << OUStringToOString(aString, RTL_TEXTENCODING_UTF8).getStr() << std::endl;
     214             :         std::cout << "CSVString: " << OUStringToOString(aCSVString, RTL_TEXTENCODING_UTF8).getStr() << std::endl;
     215             :         std::cout << "result: " << (int)(aCSVString == aString) << std::endl;
     216             : #endif //DEBUG_CSV_HANDLER
     217        1430 :         CPPUNIT_ASSERT_MESSAGE(createErrorMessage(mnCol, mnRow, mnTab, aCSVString, aString).getStr(), aString == aCSVString );
     218        2860 :         ++mnCol;
     219        1430 :     }
     220             : 
     221             : private:
     222             :     ScDocument* mpDoc;
     223             :     SCCOL mnCol;
     224             :     SCROW mnRow;
     225             :     SCTAB mnTab;
     226             : };
     227             : 
     228             : #endif
     229             : 
     230             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11