bump product version to 4.1.6.2
[LibreOffice.git] / sc / qa / unit / helper / csv_handler.hxx
bloba591cf87b22d1eede57ee95d9b8b363180c8ed1d
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 #include <iostream>
12 #include "docsh.hxx"
13 #include "postit.hxx"
14 #include "patattr.hxx"
15 #include "scitems.hxx"
16 #include "document.hxx"
17 #include "cellform.hxx"
18 #include "cellvalue.hxx"
20 #define DEBUG_CSV_HANDLER 0
22 namespace {
24 OUString getConditionalFormatString(ScDocument* pDoc, SCCOL nCol, SCROW nRow, SCTAB nTab)
26 OUString aString;
27 Color* pColor;
28 ScRefCellValue aCell;
29 aCell.assign(*pDoc, ScAddress(nCol, nRow, nTab));
30 if (aCell.isEmpty())
31 return aString;
33 const SfxItemSet* pCondSet = pDoc->GetCondResult( nCol, nRow, nTab );
34 const ScPatternAttr* pPattern = pDoc->GetPattern(nCol, nRow, nTab);
35 SvNumberFormatter* pFormatter = pDoc->GetFormatTable();
36 sal_uInt32 nFormat = pPattern->GetNumberFormat( pFormatter, pCondSet );
37 aString = ScCellFormat::GetString(*pDoc, ScAddress(nCol, nRow, nTab), nFormat, &pColor, *pFormatter);
38 return aString;
41 OString createErrorMessage(SCCOL nCol, SCROW nRow, SCTAB nTab)
43 OStringBuffer aString("Error in Table: ");
44 aString.append(static_cast<sal_Int32>(nTab));
45 aString.append(" Column: ");
46 aString.append(static_cast<sal_Int32>(nCol));
47 aString.append(" Row: ");
48 aString.append(nRow);
49 return aString.makeStringAndClear();
52 OString createErrorMessage(SCCOL nCol, SCROW nRow, SCTAB nTab, const OUString& rExpectedString, const OUString& rString)
54 OStringBuffer aString(createErrorMessage(nCol, nRow, nTab));
55 aString.append("; Expected: '");
56 aString.append(OUStringToOString(rExpectedString, RTL_TEXTENCODING_UTF8));
57 aString.append("' Found: '");
58 aString.append(OUStringToOString(rString, RTL_TEXTENCODING_UTF8));
59 aString.append("'");
60 return aString.makeStringAndClear();
63 OString createErrorMessage(SCCOL nCol, SCROW nRow, SCTAB nTab, double aExpected, double aValue)
65 OStringBuffer aString(createErrorMessage(nCol, nRow, nTab));
66 aString.append("; Expected: '");
67 aString.append(aExpected);
68 aString.append("' Found: '");
69 aString.append(aValue);
70 aString.append("'");
71 return aString.makeStringAndClear();
77 enum StringType { PureString, FormulaValue, StringValue };
79 class csv_handler
81 public:
82 csv_handler(ScDocument* pDoc, SCTAB nTab, StringType eType = StringValue):
83 mpDoc(pDoc),
84 mnCol(0),
85 mnRow(0),
86 mnTab(nTab),
87 meStringType(eType) {}
89 void begin_parse() {}
91 void end_parse() {}
93 void begin_row() {}
95 void end_row()
97 ++mnRow;
98 mnCol = 0;
101 void cell(const char* p, size_t n)
103 #if DEBUG_CSV_HANDLER
104 std::cout << "Col: " << mnCol << " Row: " << mnRow << std::endl;
105 #endif //DEBUG_CSV_HANDLER
106 if (n == 0)
108 // Empty cell.
109 if (!mpDoc->GetString(mnCol, mnRow, mnTab).isEmpty())
111 // cell in the document is not empty.
112 CPPUNIT_ASSERT_MESSAGE(createErrorMessage(mnCol, mnRow, mnTab).getStr(), false);
115 else if (meStringType == PureString)
117 OUString aCSVString(p, n, RTL_TEXTENCODING_UTF8);
118 OUString aString = mpDoc->GetString(mnCol, mnRow, mnTab);
120 #if DEBUG_CSV_HANDLER
121 std::cout << "String: " << OUStringToOString(aString, RTL_TEXTENCODING_UTF8).getStr() << std::endl;
122 std::cout << "CSVString: " << OUStringToOString(aCSVString, RTL_TEXTENCODING_UTF8).getStr() << std::endl;
123 std::cout << "result: " << (int)(aCSVString == aString) << std::endl;
124 #endif //DEBUG_CSV_HANDLER
126 CPPUNIT_ASSERT_EQUAL_MESSAGE(createErrorMessage(mnCol, mnRow, mnTab, aCSVString, aString).getStr(), aString, aCSVString);
128 else
130 char* pRemainingChars = NULL;
131 std::string aStr(p, n);
132 double nValue = strtod(&aStr[0], &pRemainingChars);
133 if (*pRemainingChars)
135 OUString aString;
136 switch (meStringType)
138 case StringValue:
139 aString = mpDoc->GetString(mnCol, mnRow, mnTab);
140 break;
141 case FormulaValue:
142 mpDoc->GetFormula(mnCol, mnRow, mnTab, aString);
143 break;
144 default:
145 break;
147 OUString aCSVString(p, n, RTL_TEXTENCODING_UTF8);
148 #if DEBUG_CSV_HANDLER
149 std::cout << "String: " << OUStringToOString(aString, RTL_TEXTENCODING_UTF8).getStr() << std::endl;
150 std::cout << "CSVString: " << OUStringToOString(aCSVString, RTL_TEXTENCODING_UTF8).getStr() << std::endl;
151 std::cout << "result: " << (int)(aCSVString == aString) << std::endl;
152 #endif //DEBUG_CSV_HANDLER
154 CPPUNIT_ASSERT_EQUAL_MESSAGE(createErrorMessage(mnCol, mnRow, mnTab, aCSVString, aString).getStr(), aString, aCSVString);
156 else
158 double aValue;
159 mpDoc->GetValue(mnCol, mnRow, mnTab, aValue);
160 #if DEBUG_CSV_HANDLER
161 std::cout << "Value: " << aValue << std::endl;
162 std::cout << "CSVValue: " << nValue << std::endl;
163 std::cout << "result: " << (int)(aValue == nValue) << std::endl;
164 #endif //DEBUG_CSV_HANDLER
165 CPPUNIT_ASSERT_MESSAGE(createErrorMessage(mnCol, mnRow, mnTab, nValue, aValue).getStr(), aValue == nValue);
168 ++mnCol;
171 private:
172 ScDocument* mpDoc;
173 SCCOL mnCol;
174 SCROW mnRow;
175 SCTAB mnTab;
176 StringType meStringType;
180 class conditional_format_handler
182 public:
183 conditional_format_handler(ScDocument* pDoc, SCTAB nTab):
184 mpDoc(pDoc),
185 mnCol(0),
186 mnRow(0),
187 mnTab(nTab) {}
189 void begin_parse() {}
191 void end_parse() {}
193 void begin_row() {}
195 void end_row()
197 ++mnRow;
198 mnCol = 0;
201 void cell(const char* p, size_t n)
203 #if DEBUG_CSV_HANDLER
204 std::cout << "Col: " << mnCol << " Row: " << mnRow << std::endl;
205 #endif //DEBUG_CSV_HANDLER
206 OUString aString = getConditionalFormatString(mpDoc, mnCol, mnRow, mnTab);
207 OUString aCSVString(p, n, RTL_TEXTENCODING_UTF8);
208 #if DEBUG_CSV_HANDLER
209 std::cout << "String: " << OUStringToOString(aString, RTL_TEXTENCODING_UTF8).getStr() << std::endl;
210 std::cout << "CSVString: " << OUStringToOString(aCSVString, RTL_TEXTENCODING_UTF8).getStr() << std::endl;
211 std::cout << "result: " << (int)(aCSVString == aString) << std::endl;
212 #endif //DEBUG_CSV_HANDLER
213 CPPUNIT_ASSERT_MESSAGE(createErrorMessage(mnCol, mnRow, mnTab, aCSVString, aString).getStr(), aString == aCSVString );
214 ++mnCol;
217 private:
218 ScDocument* mpDoc;
219 SCCOL mnCol;
220 SCROW mnRow;
221 SCTAB mnTab;
224 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */