1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
10 #ifndef INCLUDED_SC_QA_UNIT_HELPER_CSV_HANDLER_HXX
11 #define INCLUDED_SC_QA_UNIT_HELPER_CSV_HANDLER_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
31 OUString
getConditionalFormatString(ScDocument
* pDoc
, SCCOL nCol
, SCROW nRow
, SCTAB nTab
)
36 aCell
.assign(*pDoc
, ScAddress(nCol
, nRow
, nTab
));
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
);
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: ");
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
));
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
);
78 return aString
.makeStringAndClear();
87 csv_handler(ScDocument
* pDoc
, SCTAB nTab
, StringType eType
= StringValue
):
92 meStringType(eType
) {}
94 static void begin_parse() {}
96 static void end_parse() {}
98 static void begin_row() {}
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
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
);
135 char* pRemainingChars
= NULL
;
136 std::string
aStr(p
, n
);
137 double nValue
= strtod(&aStr
[0], &pRemainingChars
);
138 if (*pRemainingChars
)
141 switch (meStringType
)
144 aString
= mpDoc
->GetString(mnCol
, mnRow
, mnTab
);
147 mpDoc
->GetFormula(mnCol
, mnRow
, mnTab
, aString
);
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
);
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);
181 StringType meStringType
;
184 class conditional_format_handler
187 conditional_format_handler(ScDocument
* pDoc
, SCTAB nTab
):
193 static void begin_parse() {}
195 static void end_parse() {}
197 static void begin_row() {}
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
);
230 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */