Impress Remote 1.0.5, tag sdremote-1.0.5
[LibreOffice.git] / sc / qa / unit / helper / csv_handler.hxx
blob8493f104992e269235cbb87e1e6b43bd78be43ee
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * Version: MPL 1.1 / GPLv3+ / LGPLv3+
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/
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.
15 * Major Contributor(s):
16 * Copyright (C) 2011 Markus Mohrhard <markus.mohrhard@googlemail.com> (initial developer)
18 * All Rights Reserved.
20 * For minor contributions see the git repository.
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.
29 #include <iostream>
31 #include "docsh.hxx"
32 #include "postit.hxx"
33 #include "patattr.hxx"
34 #include "scitems.hxx"
35 #include "document.hxx"
36 #include "cellform.hxx"
38 #define DEBUG_CSV_HANDLER 0
40 namespace {
42 rtl::OUString getConditionalFormatString(ScDocument* pDoc, SCCOL nCol, SCROW nRow, SCTAB nTab)
44 rtl::OUString aString;
45 Color* pColor;
46 ScBaseCell* pCell = pDoc->GetCell(ScAddress(nCol, nRow, nTab));
47 if(!pCell)
48 return aString;
50 const SfxItemSet* pCondSet = pDoc->GetCondResult( nCol, nRow, nTab );
51 const ScPatternAttr* pPattern = pDoc->GetPattern(nCol, nRow, nTab);
52 SvNumberFormatter* pFormatter = pDoc->GetFormatTable();
53 sal_uInt32 nFormat = pPattern->GetNumberFormat( pFormatter, pCondSet );
54 ScCellFormat::GetString( pCell, nFormat, aString, &pColor, *pFormatter);
55 return aString;
58 rtl::OString createErrorMessage(SCCOL nCol, SCROW nRow, SCTAB nTab)
60 rtl::OStringBuffer aString("Error in Table: ");
61 aString.append(static_cast<sal_Int32>(nTab));
62 aString.append(" Column: ");
63 aString.append(static_cast<sal_Int32>(nCol));
64 aString.append(" Row: ");
65 aString.append(nRow);
66 return aString.makeStringAndClear();
69 rtl::OString createErrorMessage(SCCOL nCol, SCROW nRow, SCTAB nTab, const rtl::OUString& rExpectedString, const rtl::OUString& rString)
71 rtl::OStringBuffer aString(createErrorMessage(nCol, nRow, nTab));
72 aString.append("; Expected: '");
73 aString.append(rtl::OUStringToOString(rExpectedString, RTL_TEXTENCODING_UTF8));
74 aString.append("' Found: '");
75 aString.append(rtl::OUStringToOString(rString, RTL_TEXTENCODING_UTF8));
76 aString.append("'");
77 return aString.makeStringAndClear();
80 rtl::OString createErrorMessage(SCCOL nCol, SCROW nRow, SCTAB nTab, double aExpected, double aValue)
82 rtl::OStringBuffer aString(createErrorMessage(nCol, nRow, nTab));
83 aString.append("; Expected: '");
84 aString.append(aExpected);
85 aString.append("' Found: '");
86 aString.append(aValue);
87 aString.append("'");
88 return aString.makeStringAndClear();
94 enum StringType { PureString, FormulaValue, StringValue };
96 class csv_handler
98 public:
99 csv_handler(ScDocument* pDoc, SCTAB nTab, StringType eType = StringValue):
100 mpDoc(pDoc),
101 mnCol(0),
102 mnRow(0),
103 mnTab(nTab),
104 meStringType(eType) {}
106 void begin_parse() {}
108 void end_parse() {}
110 void begin_row() {}
112 void end_row()
114 ++mnRow;
115 mnCol = 0;
118 void cell(const char* p, size_t n)
120 #if DEBUG_CSV_HANDLER
121 std::cout << "Col: " << mnCol << " Row: " << mnRow << std::endl;
122 #endif //DEBUG_CSV_HANDLER
123 if (n == 0)
125 // Empty cell.
126 if (!mpDoc->GetString(mnCol, mnRow, mnTab).isEmpty())
128 // cell in the document is not empty.
129 CPPUNIT_ASSERT_MESSAGE(createErrorMessage(mnCol, mnRow, mnTab).getStr(), false);
132 else if (meStringType == PureString)
134 rtl::OUString aCSVString(p, n, RTL_TEXTENCODING_UTF8);
135 rtl::OUString aString;
136 mpDoc->GetString(mnCol, mnRow, mnTab, aString);
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
144 CPPUNIT_ASSERT_MESSAGE(createErrorMessage(mnCol, mnRow, mnTab, aCSVString, aString).getStr(), aString == aCSVString);
146 else
148 char* pRemainingChars = NULL;
149 std::string aStr(p, n);
150 double nValue = strtod(&aStr[0], &pRemainingChars);
151 if (*pRemainingChars)
153 rtl::OUString aString;
154 switch (meStringType)
156 case StringValue:
157 mpDoc->GetString(mnCol, mnRow, mnTab, aString);
158 break;
159 case FormulaValue:
160 mpDoc->GetFormula(mnCol, mnRow, mnTab, aString);
161 break;
162 default:
163 break;
165 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
172 CPPUNIT_ASSERT_MESSAGE(createErrorMessage(mnCol, mnRow, mnTab, aCSVString, aString).getStr(), aString == aCSVString);
174 else
176 double aValue;
177 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 CPPUNIT_ASSERT_MESSAGE(createErrorMessage(mnCol, mnRow, mnTab, nValue, aValue).getStr(), aValue == nValue);
186 ++mnCol;
189 private:
190 ScDocument* mpDoc;
191 SCCOL mnCol;
192 SCROW mnRow;
193 SCTAB mnTab;
194 StringType meStringType;
198 class conditional_format_handler
200 public:
201 conditional_format_handler(ScDocument* pDoc, SCTAB nTab):
202 mpDoc(pDoc),
203 mnCol(0),
204 mnRow(0),
205 mnTab(nTab) {}
207 void begin_parse() {}
209 void end_parse() {}
211 void begin_row() {}
213 void end_row()
215 ++mnRow;
216 mnCol = 0;
219 void cell(const char* p, size_t n)
221 #if DEBUG_CSV_HANDLER
222 std::cout << "Col: " << mnCol << " Row: " << mnRow << std::endl;
223 #endif //DEBUG_CSV_HANDLER
224 rtl::OUString aString = getConditionalFormatString(mpDoc, mnCol, mnRow, mnTab);
225 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 CPPUNIT_ASSERT_MESSAGE(createErrorMessage(mnCol, mnRow, mnTab, aCSVString, aString).getStr(), aString == aCSVString );
232 ++mnCol;
235 private:
236 ScDocument* mpDoc;
237 SCCOL mnCol;
238 SCROW mnRow;
239 SCTAB mnTab;
242 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */