bump product version to 5.0.4.1
[LibreOffice.git] / sc / qa / unit / helper / csv_handler.hxx
blob915ffc62992c9ec1fabbe3f1acd4c3890d748b9d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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 */
10 #ifndef INCLUDED_SC_QA_UNIT_HELPER_CSV_HANDLER_HXX
11 #define INCLUDED_SC_QA_UNIT_HELPER_CSV_HANDLER_HXX
13 #include <iostream>
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"
23 #include <rtl/strbuf.hxx>
25 #include <test/bootstrapfixture.hxx>
27 #define DEBUG_CSV_HANDLER 0
29 namespace {
31 OUString getConditionalFormatString(ScDocument* pDoc, SCCOL nCol, SCROW nRow, SCTAB nTab)
33 OUString aString;
34 Color* pColor;
35 ScRefCellValue aCell;
36 aCell.assign(*pDoc, ScAddress(nCol, nRow, nTab));
37 if (aCell.isEmpty())
38 return aString;
40 const SfxItemSet* pCondSet = pDoc->GetCondResult( nCol, nRow, nTab );
41 const ScPatternAttr* pPattern = pDoc->GetPattern(nCol, nRow, nTab);
42 SvNumberFormatter* pFormatter = pDoc->GetFormatTable();
43 sal_uInt32 nFormat = pPattern->GetNumberFormat( pFormatter, pCondSet );
44 aString = ScCellFormat::GetString(*pDoc, ScAddress(nCol, nRow, nTab), nFormat, &pColor, *pFormatter);
45 return aString;
48 OString createErrorMessage(SCCOL nCol, SCROW nRow, SCTAB nTab)
50 OStringBuffer aString("Error in Table: ");
51 aString.append(static_cast<sal_Int32>(nTab));
52 aString.append(" Column: ");
53 aString.append(static_cast<sal_Int32>(nCol));
54 aString.append(" Row: ");
55 aString.append(nRow);
56 return aString.makeStringAndClear();
59 OString createErrorMessage(SCCOL nCol, SCROW nRow, SCTAB nTab, const OUString& rExpectedString, const OUString& rString)
61 OStringBuffer aString(createErrorMessage(nCol, nRow, nTab));
62 aString.append("; Expected: '");
63 aString.append(OUStringToOString(rExpectedString, RTL_TEXTENCODING_UTF8));
64 aString.append("' Found: '");
65 aString.append(OUStringToOString(rString, RTL_TEXTENCODING_UTF8));
66 aString.append("'");
67 return aString.makeStringAndClear();
70 OString createErrorMessage(SCCOL nCol, SCROW nRow, SCTAB nTab, double aExpected, double aValue)
72 OStringBuffer aString(createErrorMessage(nCol, nRow, nTab));
73 aString.append("; Expected: '");
74 aString.append(aExpected);
75 aString.append("' Found: '");
76 aString.append(aValue);
77 aString.append("'");
78 return aString.makeStringAndClear();
84 class csv_handler
86 public:
87 csv_handler(ScDocument* pDoc, SCTAB nTab, StringType eType = StringValue):
88 mpDoc(pDoc),
89 mnCol(0),
90 mnRow(0),
91 mnTab(nTab),
92 meStringType(eType) {}
94 static void begin_parse() {}
96 static void end_parse() {}
98 static void begin_row() {}
100 void end_row()
102 ++mnRow;
103 mnCol = 0;
106 void cell(const char* p, size_t n)
108 #if DEBUG_CSV_HANDLER
109 std::cout << "Col: " << mnCol << " Row: " << mnRow << std::endl;
110 #endif //DEBUG_CSV_HANDLER
111 if (n == 0)
113 // Empty cell.
114 if (!mpDoc->GetString(mnCol, mnRow, mnTab).isEmpty())
116 // cell in the document is not empty.
117 CPPUNIT_ASSERT_MESSAGE(createErrorMessage(mnCol, mnRow, mnTab).getStr(), false);
120 else if (meStringType == PureString)
122 OUString aCSVString(p, n, RTL_TEXTENCODING_UTF8);
123 OUString aString = mpDoc->GetString(mnCol, mnRow, mnTab);
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
131 CPPUNIT_ASSERT_EQUAL_MESSAGE(createErrorMessage(mnCol, mnRow, mnTab, aCSVString, aString).getStr(), aCSVString, aString);
133 else
135 char* pRemainingChars = NULL;
136 std::string aStr(p, n);
137 double nValue = strtod(&aStr[0], &pRemainingChars);
138 if (*pRemainingChars)
140 OUString aString;
141 switch (meStringType)
143 case StringValue:
144 aString = mpDoc->GetString(mnCol, mnRow, mnTab);
145 break;
146 case FormulaValue:
147 mpDoc->GetFormula(mnCol, mnRow, mnTab, aString);
148 break;
149 default:
150 break;
152 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
159 CPPUNIT_ASSERT_EQUAL_MESSAGE(createErrorMessage(mnCol, mnRow, mnTab, aCSVString, aString).getStr(), aCSVString, aString);
161 else
163 double aValue;
164 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 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(createErrorMessage(mnCol, mnRow, mnTab, nValue, aValue).getStr(), nValue, aValue, 1e-10);
173 ++mnCol;
176 private:
177 ScDocument* mpDoc;
178 SCCOL mnCol;
179 SCROW mnRow;
180 SCTAB mnTab;
181 StringType meStringType;
184 class conditional_format_handler
186 public:
187 conditional_format_handler(ScDocument* pDoc, SCTAB nTab):
188 mpDoc(pDoc),
189 mnCol(0),
190 mnRow(0),
191 mnTab(nTab) {}
193 static void begin_parse() {}
195 static void end_parse() {}
197 static void begin_row() {}
199 void end_row()
201 ++mnRow;
202 mnCol = 0;
205 void cell(const char* p, size_t n)
207 #if DEBUG_CSV_HANDLER
208 std::cout << "Col: " << mnCol << " Row: " << mnRow << std::endl;
209 #endif //DEBUG_CSV_HANDLER
210 OUString aString = getConditionalFormatString(mpDoc, mnCol, mnRow, mnTab);
211 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 CPPUNIT_ASSERT_MESSAGE(createErrorMessage(mnCol, mnRow, mnTab, aCSVString, aString).getStr(), aString == aCSVString );
218 ++mnCol;
221 private:
222 ScDocument* mpDoc;
223 SCCOL mnCol;
224 SCROW mnRow;
225 SCTAB mnTab;
228 #endif
230 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */