LCOV - code coverage report
Current view: top level - sc/qa/unit/helper - csv_handler.hxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 82 86 95.3 %
Date: 2014-11-03 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        2860 : OUString getConditionalFormatString(ScDocument* pDoc, SCCOL nCol, SCROW nRow, SCTAB nTab)
      32             : {
      33        2860 :     OUString aString;
      34             :     Color* pColor;
      35        5720 :     ScRefCellValue aCell;
      36        2860 :     aCell.assign(*pDoc, ScAddress(nCol, nRow, nTab));
      37        2860 :     if (aCell.isEmpty())
      38        1408 :         return aString;
      39             : 
      40        1452 :     const SfxItemSet* pCondSet = pDoc->GetCondResult( nCol, nRow, nTab );
      41        1452 :     const ScPatternAttr* pPattern = pDoc->GetPattern(nCol, nRow, nTab);
      42        1452 :     SvNumberFormatter* pFormatter = pDoc->GetFormatTable();
      43        1452 :     sal_uInt32 nFormat = pPattern->GetNumberFormat( pFormatter, pCondSet );
      44        1452 :     aString = ScCellFormat::GetString(*pDoc, ScAddress(nCol, nRow, nTab), nFormat, &pColor, *pFormatter);
      45        1452 :     return aString;
      46             : }
      47             : 
      48        4972 : OString createErrorMessage(SCCOL nCol, SCROW nRow, SCTAB nTab)
      49             : {
      50        4972 :     OStringBuffer aString("Error in Table: ");
      51        4972 :     aString.append(static_cast<sal_Int32>(nTab));
      52        4972 :     aString.append(" Column: ");
      53        4972 :     aString.append(static_cast<sal_Int32>(nCol));
      54        4972 :     aString.append(" Row: ");
      55        4972 :     aString.append(nRow);
      56        4972 :     return aString.makeStringAndClear();
      57             : }
      58             : 
      59        4390 : OString createErrorMessage(SCCOL nCol, SCROW nRow, SCTAB nTab, const OUString& rExpectedString, const OUString& rString)
      60             : {
      61        4390 :     OStringBuffer aString(createErrorMessage(nCol, nRow, nTab));
      62        4390 :     aString.append("; Expected: '");
      63        4390 :     aString.append(OUStringToOString(rExpectedString, RTL_TEXTENCODING_UTF8));
      64        4390 :     aString.append("' Found: '");
      65        4390 :     aString.append(OUStringToOString(rString, RTL_TEXTENCODING_UTF8));
      66        4390 :     aString.append("'");
      67        4390 :     return aString.makeStringAndClear();
      68             : }
      69             : 
      70         582 : OString createErrorMessage(SCCOL nCol, SCROW nRow, SCTAB nTab, double aExpected, double aValue)
      71             : {
      72         582 :     OStringBuffer aString(createErrorMessage(nCol, nRow, nTab));
      73         582 :     aString.append("; Expected: '");
      74         582 :     aString.append(aExpected);
      75         582 :     aString.append("' Found: '");
      76         582 :     aString.append(aValue);
      77         582 :     aString.append("'");
      78         582 :     return aString.makeStringAndClear();
      79             : 
      80             : }
      81             : 
      82             : }
      83             : 
      84             : class csv_handler
      85             : {
      86             : public:
      87          70 :     csv_handler(ScDocument* pDoc, SCTAB nTab, StringType eType = StringValue):
      88             :             mpDoc(pDoc),
      89             :             mnCol(0),
      90             :             mnRow(0),
      91             :             mnTab(nTab),
      92          70 :             meStringType(eType)  {}
      93             : 
      94          70 :     void begin_parse() {}
      95             : 
      96          70 :     void end_parse() {}
      97             : 
      98         908 :     void begin_row() {}
      99             : 
     100         908 :     void end_row()
     101             :     {
     102         908 :         ++mnRow;
     103         908 :         mnCol = 0;
     104         908 :     }
     105             : 
     106        6906 :     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        6906 :         if (n == 0)
     112             :         {
     113             :             // Empty cell.
     114        4794 :             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        2112 :         else if (meStringType == PureString)
     121             :         {
     122        1154 :             OUString aCSVString(p, n, RTL_TEXTENCODING_UTF8);
     123        2308 :             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        2308 :             CPPUNIT_ASSERT_EQUAL_MESSAGE(createErrorMessage(mnCol, mnRow, mnTab, aCSVString, aString).getStr(), aCSVString, aString);
     132             :         }
     133             :         else
     134             :         {
     135         958 :             char* pRemainingChars = NULL;
     136         958 :             std::string aStr(p, n);
     137         958 :             double nValue = strtod(&aStr[0], &pRemainingChars);
     138         958 :             if (*pRemainingChars)
     139             :             {
     140         376 :                 OUString aString;
     141         376 :                 switch (meStringType)
     142             :                 {
     143             :                     case StringValue:
     144         376 :                         aString = mpDoc->GetString(mnCol, mnRow, mnTab);
     145         376 :                         break;
     146             :                     case FormulaValue:
     147           0 :                         mpDoc->GetFormula(mnCol, mnRow, mnTab, aString);
     148           0 :                         break;
     149             :                     default:
     150           0 :                         break;
     151             :                 }
     152         376 :                 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         376 :                 CPPUNIT_ASSERT_EQUAL_MESSAGE(createErrorMessage(mnCol, mnRow, mnTab, aCSVString, aString).getStr(), aCSVString, aString);
     160             :             }
     161             :             else
     162             :             {
     163             :                 double aValue;
     164         582 :                 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         582 :                 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(createErrorMessage(mnCol, mnRow, mnTab, nValue, aValue).getStr(), nValue, aValue, 1e-10);
     171         958 :             }
     172             :         }
     173        6906 :         ++mnCol;
     174        6906 :     }
     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          14 :     conditional_format_handler(ScDocument* pDoc, SCTAB nTab):
     188             :         mpDoc(pDoc),
     189             :         mnCol(0),
     190             :         mnRow(0),
     191          14 :         mnTab(nTab) {}
     192             : 
     193          14 :     void begin_parse() {}
     194             : 
     195          14 :     void end_parse() {}
     196             : 
     197         190 :     void begin_row() {}
     198             : 
     199         190 :     void end_row()
     200             :     {
     201         190 :         ++mnRow;
     202         190 :         mnCol = 0;
     203         190 :     }
     204             : 
     205        2860 :     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        2860 :         OUString aString = getConditionalFormatString(mpDoc, mnCol, mnRow, mnTab);
     211        5720 :         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        2860 :         CPPUNIT_ASSERT_MESSAGE(createErrorMessage(mnCol, mnRow, mnTab, aCSVString, aString).getStr(), aString == aCSVString );
     218        5720 :         ++mnCol;
     219        2860 :     }
     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.10