LCOV - code coverage report
Current view: top level - libreoffice/sc/qa/unit/helper - csv_handler.hxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 80 85 94.1 %
Date: 2012-12-27 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             :  * Version: MPL 1.1 / GPLv3+ / LGPLv3+
       4             :  *
       5             :  * The contents of this file are subject to the Mozilla Public License Version
       6             :  * 1.1 (the "License"); you may not use this file except in compliance with
       7             :  * the License or as specified alternatively below. You may obtain a copy of
       8             :  * the License at http://www.mozilla.org/MPL/
       9             :  *
      10             :  * Software distributed under the License is distributed on an "AS IS" basis,
      11             :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
      12             :  * for the specific language governing rights and limitations under the
      13             :  * License.
      14             :  *
      15             :  * Major Contributor(s):
      16             :  *  Copyright (C) 2011 Markus Mohrhard <markus.mohrhard@googlemail.com> (initial developer)
      17             :  *
      18             :  * All Rights Reserved.
      19             :  *
      20             :  * For minor contributions see the git repository.
      21             :  *
      22             :  * Alternatively, the contents of this file may be used under the terms of
      23             :  * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
      24             :  * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
      25             :  * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
      26             :  * instead of those above.
      27             :  */
      28             : 
      29             : #include <iostream>
      30             : 
      31             : #include "docsh.hxx"
      32             : #include "postit.hxx"
      33             : #include "patattr.hxx"
      34             : #include "scitems.hxx"
      35             : #include "document.hxx"
      36             : #include "cellform.hxx"
      37             : 
      38             : #define DEBUG_CSV_HANDLER 0
      39             : 
      40             : namespace {
      41             : 
      42         722 : rtl::OUString getConditionalFormatString(ScDocument* pDoc, SCCOL nCol, SCROW nRow, SCTAB nTab)
      43             : {
      44         722 :     rtl::OUString aString;
      45             :     Color* pColor;
      46         722 :     ScBaseCell* pCell = pDoc->GetCell(ScAddress(nCol, nRow, nTab));
      47         722 :     if(!pCell)
      48             :         return aString;
      49             : 
      50         370 :     const SfxItemSet* pCondSet = pDoc->GetCondResult( nCol, nRow, nTab );
      51         370 :     const ScPatternAttr* pPattern = pDoc->GetPattern(nCol, nRow, nTab);
      52         370 :     SvNumberFormatter* pFormatter = pDoc->GetFormatTable();
      53         370 :     sal_uInt32 nFormat = pPattern->GetNumberFormat( pFormatter, pCondSet );
      54         370 :     ScCellFormat::GetString( pCell, nFormat, aString, &pColor, *pFormatter);
      55           0 :     return aString;
      56             : }
      57             : 
      58        1518 : rtl::OString createErrorMessage(SCCOL nCol, SCROW nRow, SCTAB nTab)
      59             : {
      60        1518 :     rtl::OStringBuffer aString("Error in Table: ");
      61        1518 :     aString.append(static_cast<sal_Int32>(nTab));
      62        1518 :     aString.append(" Column: ");
      63        1518 :     aString.append(static_cast<sal_Int32>(nCol));
      64        1518 :     aString.append(" Row: ");
      65        1518 :     aString.append(nRow);
      66        1518 :     return aString.makeStringAndClear();
      67             : }
      68             : 
      69        1306 : rtl::OString createErrorMessage(SCCOL nCol, SCROW nRow, SCTAB nTab, const rtl::OUString& rExpectedString, const rtl::OUString& rString)
      70             : {
      71        1306 :     rtl::OStringBuffer aString(createErrorMessage(nCol, nRow, nTab));
      72        1306 :     aString.append("; Expected: '");
      73        1306 :     aString.append(rtl::OUStringToOString(rExpectedString, RTL_TEXTENCODING_UTF8));
      74        1306 :     aString.append("' Found: '");
      75        1306 :     aString.append(rtl::OUStringToOString(rString, RTL_TEXTENCODING_UTF8));
      76        1306 :     aString.append("'");
      77        1306 :     return aString.makeStringAndClear();
      78             : }
      79             : 
      80         212 : rtl::OString createErrorMessage(SCCOL nCol, SCROW nRow, SCTAB nTab, double aExpected, double aValue)
      81             : {
      82         212 :     rtl::OStringBuffer aString(createErrorMessage(nCol, nRow, nTab));
      83         212 :     aString.append("; Expected: '");
      84         212 :     aString.append(aExpected);
      85         212 :     aString.append("' Found: '");
      86         212 :     aString.append(aValue);
      87         212 :     aString.append("'");
      88         212 :     return aString.makeStringAndClear();
      89             : 
      90             : }
      91             : 
      92             : }
      93             : 
      94             : enum StringType { PureString, FormulaValue, StringValue };
      95             : 
      96             : class csv_handler
      97             : {
      98             : public:
      99          28 :     csv_handler(ScDocument* pDoc, SCTAB nTab, StringType eType = StringValue):
     100             :             mpDoc(pDoc),
     101             :             mnCol(0),
     102             :             mnRow(0),
     103             :             mnTab(nTab),
     104          28 :             meStringType(eType)  {}
     105             : 
     106          28 :     void begin_parse() {}
     107             : 
     108          28 :     void end_parse() {}
     109             : 
     110         285 :     void begin_row() {}
     111             : 
     112         285 :     void end_row()
     113             :     {
     114         285 :         ++mnRow;
     115         285 :         mnCol = 0;
     116         285 :     }
     117             : 
     118        3180 :     void cell(const char* p, size_t n)
     119             :     {
     120             : #if DEBUG_CSV_HANDLER
     121             :         std::cout << "Col: " << mnCol << " Row: " << mnRow << std::endl;
     122             : #endif //DEBUG_CSV_HANDLER
     123        3180 :         if (n == 0)
     124             :         {
     125             :             // Empty cell.
     126        2382 :             if (!mpDoc->GetString(mnCol, mnRow, mnTab).isEmpty())
     127             :             {
     128             :                 // cell in the document is not empty.
     129           0 :                 CPPUNIT_ASSERT_MESSAGE(createErrorMessage(mnCol, mnRow, mnTab).getStr(), false);
     130             :             }
     131             :         }
     132         798 :         else if (meStringType == PureString)
     133             :         {
     134         412 :             rtl::OUString aCSVString(p, n, RTL_TEXTENCODING_UTF8);
     135         412 :             rtl::OUString aString;
     136         412 :             mpDoc->GetString(mnCol, mnRow, mnTab, aString);
     137             : 
     138             : #if DEBUG_CSV_HANDLER
     139             :                 std::cout << "String: " << rtl::OUStringToOString(aString, RTL_TEXTENCODING_UTF8).getStr() << std::endl;
     140             :                 std::cout << "CSVString: " << rtl::OUStringToOString(aCSVString, RTL_TEXTENCODING_UTF8).getStr() << std::endl;
     141             :                 std::cout << "result: " << (int)(aCSVString == aString) << std::endl;
     142             : #endif //DEBUG_CSV_HANDLER
     143             : 
     144         412 :             CPPUNIT_ASSERT_MESSAGE(createErrorMessage(mnCol, mnRow, mnTab, aCSVString, aString).getStr(), aString == aCSVString);
     145             :         }
     146             :         else
     147             :         {
     148         386 :             char* pRemainingChars = NULL;
     149         386 :             std::string aStr(p, n);
     150         386 :             double nValue = strtod(&aStr[0], &pRemainingChars);
     151         386 :             if (*pRemainingChars)
     152             :             {
     153         174 :                 rtl::OUString aString;
     154         174 :                 switch (meStringType)
     155             :                 {
     156             :                     case StringValue:
     157         174 :                         mpDoc->GetString(mnCol, mnRow, mnTab, aString);
     158         174 :                         break;
     159             :                     case FormulaValue:
     160           0 :                         mpDoc->GetFormula(mnCol, mnRow, mnTab, aString);
     161           0 :                         break;
     162             :                     default:
     163           0 :                         break;
     164             :                 }
     165         174 :                 rtl::OUString aCSVString(p, n, RTL_TEXTENCODING_UTF8);
     166             : #if DEBUG_CSV_HANDLER
     167             :                 std::cout << "String: " << rtl::OUStringToOString(aString, RTL_TEXTENCODING_UTF8).getStr() << std::endl;
     168             :                 std::cout << "CSVString: " << rtl::OUStringToOString(aCSVString, RTL_TEXTENCODING_UTF8).getStr() << std::endl;
     169             :                 std::cout << "result: " << (int)(aCSVString == aString) << std::endl;
     170             : #endif //DEBUG_CSV_HANDLER
     171             : 
     172         174 :                 CPPUNIT_ASSERT_MESSAGE(createErrorMessage(mnCol, mnRow, mnTab, aCSVString, aString).getStr(), aString == aCSVString);
     173             :             }
     174             :             else
     175             :             {
     176             :                 double aValue;
     177         212 :                 mpDoc->GetValue(mnCol, mnRow, mnTab, aValue);
     178             : #if DEBUG_CSV_HANDLER
     179             :                 std::cout << "Value: " << aValue << std::endl;
     180             :                 std::cout << "CSVValue: " << nValue << std::endl;
     181             :                 std::cout << "result: " << (int)(aValue == nValue) << std::endl;
     182             : #endif //DEBUG_CSV_HANDLER
     183         212 :                 CPPUNIT_ASSERT_MESSAGE(createErrorMessage(mnCol, mnRow, mnTab, nValue, aValue).getStr(), aValue == nValue);
     184         386 :             }
     185             :         }
     186        3180 :         ++mnCol;
     187        3180 :     }
     188             : 
     189             : private:
     190             :     ScDocument* mpDoc;
     191             :     SCCOL mnCol;
     192             :     SCROW mnRow;
     193             :     SCTAB mnTab;
     194             :     StringType meStringType;
     195             : };
     196             : 
     197             : 
     198             : class conditional_format_handler
     199             : {
     200             : public:
     201           4 :     conditional_format_handler(ScDocument* pDoc, SCTAB nTab):
     202             :         mpDoc(pDoc),
     203             :         mnCol(0),
     204             :         mnRow(0),
     205           4 :         mnTab(nTab) {}
     206             : 
     207           4 :     void begin_parse() {}
     208             : 
     209           4 :     void end_parse() {}
     210             : 
     211          48 :     void begin_row() {}
     212             : 
     213          48 :     void end_row()
     214             :     {
     215          48 :         ++mnRow;
     216          48 :         mnCol = 0;
     217          48 :     }
     218             : 
     219         720 :     void cell(const char* p, size_t n)
     220             :     {
     221             : #if DEBUG_CSV_HANDLER
     222             :         std::cout << "Col: " << mnCol << " Row: " << mnRow << std::endl;
     223             : #endif //DEBUG_CSV_HANDLER
     224         720 :         rtl::OUString aString = getConditionalFormatString(mpDoc, mnCol, mnRow, mnTab);
     225         720 :         rtl::OUString aCSVString(p, n, RTL_TEXTENCODING_UTF8);
     226             : #if DEBUG_CSV_HANDLER
     227             :         std::cout << "String: " << rtl::OUStringToOString(aString, RTL_TEXTENCODING_UTF8).getStr() << std::endl;
     228             :         std::cout << "CSVString: " << rtl::OUStringToOString(aCSVString, RTL_TEXTENCODING_UTF8).getStr() << std::endl;
     229             :         std::cout << "result: " << (int)(aCSVString == aString) << std::endl;
     230             : #endif //DEBUG_CSV_HANDLER
     231         720 :         CPPUNIT_ASSERT_MESSAGE(createErrorMessage(mnCol, mnRow, mnTab, aCSVString, aString).getStr(), aString == aCSVString );
     232         720 :         ++mnCol;
     233         720 :     }
     234             : 
     235             : private:
     236             :     ScDocument* mpDoc;
     237             :     SCCOL mnCol;
     238             :     SCROW mnRow;
     239             :     SCTAB mnTab;
     240             : };
     241             : 
     242             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10