LCOV - code coverage report
Current view: top level - sc/qa/unit/helper - csv_handler.hxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 78 81 96.3 %
Date: 2012-08-25 Functions: 16 16 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 81 161 50.3 %

           Branch data     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                 :         27 : rtl::OUString getConditionalFormatString(ScDocument* pDoc, SCCOL nCol, SCROW nRow, SCTAB nTab)
      43                 :            : {
      44                 :         27 :     rtl::OUString aString;
      45                 :            :     Color* pColor;
      46         [ +  - ]:         27 :     ScBaseCell* pCell = pDoc->GetCell(ScAddress(nCol, nRow, nTab));
      47         [ +  - ]:         27 :     const SfxItemSet* pCondSet = pDoc->GetCondResult( nCol, nRow, nTab );
      48         [ +  - ]:         27 :     const ScPatternAttr* pPattern = pDoc->GetPattern(nCol, nRow, nTab);
      49         [ +  - ]:         27 :     SvNumberFormatter* pFormatter = pDoc->GetFormatTable();
      50         [ +  - ]:         27 :     sal_uInt32 nFormat = pPattern->GetNumberFormat( pFormatter, pCondSet );
      51         [ +  - ]:         27 :     ScCellFormat::GetString( pCell, nFormat, aString, &pColor, *pFormatter);
      52                 :         27 :     return aString;
      53                 :            : }
      54                 :            : 
      55                 :       9540 : rtl::OString createErrorMessage(SCCOL nCol, SCROW nRow, SCTAB nTab)
      56                 :            : {
      57                 :       9540 :     rtl::OStringBuffer aString("Error in Table: ");
      58         [ +  - ]:       9540 :     aString.append(static_cast<sal_Int32>(nTab));
      59         [ +  - ]:       9540 :     aString.append(" Column: ");
      60         [ +  - ]:       9540 :     aString.append(static_cast<sal_Int32>(nCol));
      61         [ +  - ]:       9540 :     aString.append(" Row: ");
      62         [ +  - ]:       9540 :     aString.append(nRow);
      63                 :       9540 :     return aString.makeStringAndClear();
      64                 :            : }
      65                 :            : 
      66                 :       8283 : rtl::OString createErrorMessage(SCCOL nCol, SCROW nRow, SCTAB nTab, const rtl::OUString& rExpectedString, const rtl::OUString& rString)
      67                 :            : {
      68 [ +  - ][ +  - ]:       8283 :     rtl::OStringBuffer aString(createErrorMessage(nCol, nRow, nTab));
      69         [ +  - ]:       8283 :     aString.append("; Expected: '");
      70 [ +  - ][ +  - ]:       8283 :     aString.append(rtl::OUStringToOString(rExpectedString, RTL_TEXTENCODING_UTF8));
      71         [ +  - ]:       8283 :     aString.append("' Found: '");
      72 [ +  - ][ +  - ]:       8283 :     aString.append(rtl::OUStringToOString(rString, RTL_TEXTENCODING_UTF8));
      73         [ +  - ]:       8283 :     aString.append("'");
      74                 :       8283 :     return aString.makeStringAndClear();
      75                 :            : }
      76                 :            : 
      77                 :       1257 : rtl::OString createErrorMessage(SCCOL nCol, SCROW nRow, SCTAB nTab, double aExpected, double aValue)
      78                 :            : {
      79 [ +  - ][ +  - ]:       1257 :     rtl::OStringBuffer aString(createErrorMessage(nCol, nRow, nTab));
      80         [ +  - ]:       1257 :     aString.append("; Expected: '");
      81         [ +  - ]:       1257 :     aString.append(aExpected);
      82         [ +  - ]:       1257 :     aString.append("' Found: '");
      83         [ +  - ]:       1257 :     aString.append(aValue);
      84         [ +  - ]:       1257 :     aString.append("'");
      85                 :       1257 :     return aString.makeStringAndClear();
      86                 :            : 
      87                 :            : }
      88                 :            : 
      89                 :            : }
      90                 :            : 
      91                 :            : enum StringType { PureString, FormulaValue, StringValue };
      92                 :            : 
      93                 :            : class csv_handler
      94                 :            : {
      95                 :            : public:
      96                 :            : 
      97                 :            : 
      98                 :         84 :     csv_handler(ScDocument* pDoc, SCTAB nTab, StringType aType = StringValue):
      99                 :            :             mpDoc(pDoc),
     100                 :            :             mnCol(0),
     101                 :            :             mnRow(0),
     102                 :            :             mnTab(nTab),
     103                 :         84 :             maStringType(aType)  {}
     104                 :            : 
     105                 :         84 :     void begin_parse() {}
     106                 :            : 
     107                 :         84 :     void end_parse() {}
     108                 :            : 
     109                 :        849 :     void begin_row() {}
     110                 :            : 
     111                 :        849 :     void end_row()
     112                 :            :     {
     113                 :        849 :         ++mnRow;
     114                 :        849 :         mnCol = 0;
     115                 :        849 :     }
     116                 :            : 
     117                 :       9516 :     void cell(const char* p, size_t n)
     118                 :            :     {
     119                 :            : #if DEBUG_CSV_HANDLER
     120                 :            :         std::cout << "Col: " << mnCol << " Row: " << mnRow << std::endl;
     121                 :            : #endif //DEBUG_CSV_HANDLER
     122         [ +  + ]:       9516 :         if (maStringType == PureString)
     123                 :            :         {
     124         [ +  - ]:       7743 :             rtl::OUString aCSVString(p, n, RTL_TEXTENCODING_UTF8);
     125                 :       7743 :             rtl::OUString aString;
     126         [ +  - ]:       7743 :             mpDoc->GetString(mnCol, mnRow, mnTab, aString);
     127                 :            : 
     128                 :            : #if DEBUG_CSV_HANDLER
     129                 :            :                 std::cout << "String: " << rtl::OUStringToOString(aString, RTL_TEXTENCODING_UTF8).getStr() << std::endl;
     130                 :            :                 std::cout << "CSVString: " << rtl::OUStringToOString(aCSVString, RTL_TEXTENCODING_UTF8).getStr() << std::endl;
     131                 :            :                 std::cout << "result: " << (int)(aCSVString == aString) << std::endl;
     132                 :            : #endif //DEBUG_CSV_HANDLER
     133                 :            : 
     134 [ +  - ][ +  - ]:       7743 :             CPPUNIT_ASSERT_MESSAGE(createErrorMessage(mnCol, mnRow, mnTab, aCSVString, aString).getStr(), aString == aCSVString);
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
     135                 :            :         }
     136                 :            :         else
     137                 :            :         {
     138                 :       1773 :             char* pRemainingChars = NULL;
     139         [ +  - ]:       1773 :             std::string aStr(p, n);
     140         [ +  - ]:       1773 :             double nValue = strtod(&aStr[0], &pRemainingChars);
     141         [ +  + ]:       1773 :             if (*pRemainingChars)
     142                 :            :             {
     143                 :        516 :                 rtl::OUString aString;
     144      [ +  -  - ]:        516 :                 switch (maStringType)
     145                 :            :                 {
     146                 :            :                     case StringValue:
     147         [ +  - ]:        516 :                         mpDoc->GetString(mnCol, mnRow, mnTab, aString);
     148                 :        516 :                         break;
     149                 :            :                     case FormulaValue:
     150         [ #  # ]:          0 :                         mpDoc->GetFormula(mnCol, mnRow, mnTab, aString);
     151                 :          0 :                         break;
     152                 :            :                     default:
     153                 :          0 :                         break;
     154                 :            :                 }
     155         [ +  - ]:        516 :                 rtl::OUString aCSVString(p, n, RTL_TEXTENCODING_UTF8);
     156                 :            : #if DEBUG_CSV_HANDLER
     157                 :            :                 std::cout << "String: " << rtl::OUStringToOString(aString, RTL_TEXTENCODING_UTF8).getStr() << std::endl;
     158                 :            :                 std::cout << "CSVString: " << rtl::OUStringToOString(aCSVString, RTL_TEXTENCODING_UTF8).getStr() << std::endl;
     159                 :            :                 std::cout << "result: " << (int)(aCSVString == aString) << std::endl;
     160                 :            : #endif //DEBUG_CSV_HANDLER
     161                 :            : 
     162 [ +  - ][ +  - ]:        516 :                 CPPUNIT_ASSERT_MESSAGE(createErrorMessage(mnCol, mnRow, mnTab, aCSVString, aString).getStr(), aString == aCSVString);
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
     163                 :            :             }
     164                 :            :             else
     165                 :            :             {
     166                 :            :                 double aValue;
     167         [ +  - ]:       1257 :                 mpDoc->GetValue(mnCol, mnRow, mnTab, aValue);
     168                 :            : #if DEBUG_CSV_HANDLER
     169                 :            :                 std::cout << "Value: " << aValue << std::endl;
     170                 :            :                 std::cout << "CSVValue: " << nValue << std::endl;
     171                 :            :                 std::cout << "result: " << (int)(aValue == nValue) << std::endl;
     172                 :            : #endif //DEBUG_CSV_HANDLER
     173 [ +  - ][ +  - ]:       1257 :                 CPPUNIT_ASSERT_MESSAGE(createErrorMessage(mnCol, mnRow, mnTab, nValue, aValue).getStr(), aValue == nValue);
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
     174                 :       1773 :             }
     175                 :            :         }
     176                 :       9516 :         ++mnCol;
     177                 :            : 
     178                 :       9516 :     }
     179                 :            : 
     180                 :            : private:
     181                 :            :     ScDocument* mpDoc;
     182                 :            :     SCCOL mnCol;
     183                 :            :     SCROW mnRow;
     184                 :            :     SCTAB mnTab;
     185                 :            :     StringType maStringType;
     186                 :            : };
     187                 :            : 
     188                 :            : 
     189                 :            : class conditional_format_handler
     190                 :            : {
     191                 :            : public:
     192                 :          3 :     conditional_format_handler(ScDocument* pDoc, SCTAB nTab):
     193                 :            :         mpDoc(pDoc),
     194                 :            :         mnCol(0),
     195                 :            :         mnRow(0),
     196                 :          3 :         mnTab(nTab) {}
     197                 :            : 
     198                 :          3 :     void begin_parse() {}
     199                 :            : 
     200                 :          3 :     void end_parse() {}
     201                 :            : 
     202                 :          6 :     void begin_row() {}
     203                 :            : 
     204                 :          6 :     void end_row()
     205                 :            :     {
     206                 :          6 :         ++mnRow;
     207                 :          6 :         mnCol = 0;
     208                 :          6 :     }
     209                 :            : 
     210                 :         24 :     void cell(const char* p, size_t n)
     211                 :            :     {
     212                 :            : #if DEBUG_CSV_HANDLER
     213                 :            :         std::cout << "Col: " << mnCol << " Row: " << mnRow << std::endl;
     214                 :            : #endif //DEBUG_CSV_HANDLER
     215         [ +  - ]:         24 :         rtl::OUString aString = getConditionalFormatString(mpDoc, mnCol, mnRow, mnTab);
     216         [ +  - ]:         24 :         rtl::OUString aCSVString(p, n, RTL_TEXTENCODING_UTF8);
     217                 :            : #if DEBUG_CSV_HANDLER
     218                 :            :         std::cout << "String: " << rtl::OUStringToOString(aString, RTL_TEXTENCODING_UTF8).getStr() << std::endl;
     219                 :            :         std::cout << "CSVString: " << rtl::OUStringToOString(aCSVString, RTL_TEXTENCODING_UTF8).getStr() << std::endl;
     220                 :            :         std::cout << "result: " << (int)(aCSVString == aString) << std::endl;
     221                 :            : #endif //DEBUG_CSV_HANDLER
     222 [ +  - ][ +  - ]:         24 :         CPPUNIT_ASSERT_MESSAGE(createErrorMessage(mnCol, mnRow, mnTab, aCSVString, aString).getStr(), aString == aCSVString );
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
     223                 :         24 :         ++mnCol;
     224                 :         24 :     }
     225                 :            : 
     226                 :            : private:
     227                 :            :     ScDocument* mpDoc;
     228                 :            :     SCCOL mnCol;
     229                 :            :     SCROW mnRow;
     230                 :            :     SCTAB mnTab;
     231                 :            : };
     232                 :            : 
     233                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10