tdf#164803 sw: Fix SwTextSizeInfo applying grid metrics without grid
[LibreOffice.git] / sc / qa / unit / helper / csv_handler.hxx
blob3cb43bead360f8103fb8be303153aaa649f8df97
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 #pragma once
12 #include <sal/config.h>
14 #include <string_view>
16 #include "qahelper.hxx"
18 #include <patattr.hxx>
19 #include <document.hxx>
20 #include <cellform.hxx>
21 #include <cellvalue.hxx>
23 #include <orcus/csv_parser.hpp>
25 #define DEBUG_CSV_HANDLER 0
27 inline OUString getConditionalFormatString(ScDocument* pDoc, SCCOL nCol, SCROW nRow, SCTAB nTab)
29 OUString aString;
30 const Color* pColor;
31 ScRefCellValue aCell(*pDoc, ScAddress(nCol, nRow, nTab));
32 if (aCell.isEmpty())
33 return aString;
35 const SfxItemSet* pCondSet = pDoc->GetCondResult( nCol, nRow, nTab );
36 const ScPatternAttr* pPattern = pDoc->GetPattern(nCol, nRow, nTab);
37 ScInterpreterContext& rContext = pDoc->GetNonThreadedContext();
38 sal_uInt32 nFormat = pPattern->GetNumberFormat( rContext, pCondSet );
39 aString = ScCellFormat::GetString(*pDoc, ScAddress(nCol, nRow, nTab), nFormat, &pColor, &rContext);
40 return aString;
43 inline OString createErrorMessage(SCCOL nCol, SCROW nRow, SCTAB nTab)
45 return "Error in Table: " +
46 OString::number(static_cast<sal_Int32>(nTab)) +
47 " Column: " +
48 OString::number(static_cast<sal_Int32>(nCol)) +
49 " Row: " +
50 OString::number(nRow);
53 inline OString createErrorMessage(SCCOL nCol, SCROW nRow, SCTAB nTab, std::u16string_view rExpectedString, std::u16string_view rString)
55 return createErrorMessage(nCol, nRow, nTab) + "; Expected: '"
56 + OUStringToOString(rExpectedString, RTL_TEXTENCODING_UTF8) + "' Found: '"
57 + OUStringToOString(rString, RTL_TEXTENCODING_UTF8) + "'";
60 inline OString createErrorMessage(SCCOL nCol, SCROW nRow, SCTAB nTab, double aExpected, double aValue)
62 return createErrorMessage(nCol, nRow, nTab) + "; Expected: '" + OString::number(aExpected)
63 + "' Found: '" + OString::number(aValue) + "'";
67 class csv_handler : public orcus::csv_handler
69 public:
70 csv_handler(ScDocument* pDoc, SCTAB nTab, StringType eType):
71 mpDoc(pDoc),
72 mnCol(0),
73 mnRow(0),
74 mnTab(nTab),
75 meStringType(eType) {}
77 void end_row()
79 ++mnRow;
80 mnCol = 0;
83 void cell(std::string_view value, bool /*transient*/)
85 const char* p = value.data();
86 std::size_t n = value.size();
88 #if DEBUG_CSV_HANDLER
89 std::cout << "Col: " << mnCol << " Row: " << mnRow << std::endl;
90 #endif //DEBUG_CSV_HANDLER
91 if (n == 0)
93 // Empty cell.
94 if (!mpDoc->GetString(mnCol, mnRow, mnTab).isEmpty())
96 // cell in the document is not empty.
97 CPPUNIT_ASSERT_MESSAGE(createErrorMessage(mnCol, mnRow, mnTab).getStr(), false);
100 else if (meStringType == StringType::PureString)
102 OUString aCSVString(p, n, RTL_TEXTENCODING_UTF8);
103 OUString aString = mpDoc->GetString(mnCol, mnRow, mnTab);
105 #if DEBUG_CSV_HANDLER
106 std::cout << "String: " << OUStringToOString(aString, RTL_TEXTENCODING_UTF8).getStr() << std::endl;
107 std::cout << "CSVString: " << OUStringToOString(aCSVString, RTL_TEXTENCODING_UTF8).getStr() << std::endl;
108 std::cout << "result: " << (int)(aCSVString == aString) << std::endl;
109 #endif //DEBUG_CSV_HANDLER
111 CPPUNIT_ASSERT_EQUAL_MESSAGE(createErrorMessage(mnCol, mnRow, mnTab, aCSVString, aString).getStr(), aCSVString, aString);
113 else
115 char* pRemainingChars = nullptr;
116 std::string aStr(p, n);
117 double nValue = strtod(&aStr[0], &pRemainingChars);
118 if (*pRemainingChars)
120 OUString aString;
121 switch (meStringType)
123 case StringType::StringValue:
124 aString = mpDoc->GetString(mnCol, mnRow, mnTab);
125 break;
126 default:
127 break;
129 OUString aCSVString(p, n, RTL_TEXTENCODING_UTF8);
130 #if DEBUG_CSV_HANDLER
131 std::cout << "String: " << OUStringToOString(aString, RTL_TEXTENCODING_UTF8).getStr() << std::endl;
132 std::cout << "CSVString: " << OUStringToOString(aCSVString, RTL_TEXTENCODING_UTF8).getStr() << std::endl;
133 std::cout << "result: " << (int)(aCSVString == aString) << std::endl;
134 #endif //DEBUG_CSV_HANDLER
136 CPPUNIT_ASSERT_EQUAL_MESSAGE(createErrorMessage(mnCol, mnRow, mnTab, aCSVString, aString).getStr(), aCSVString, aString);
138 else
140 double aValue = mpDoc->GetValue(mnCol, mnRow, mnTab);
141 #if DEBUG_CSV_HANDLER
142 std::cout << "Value: " << aValue << std::endl;
143 std::cout << "CSVValue: " << nValue << std::endl;
144 std::cout << "result: " << (int)(aValue == nValue) << std::endl;
145 #endif //DEBUG_CSV_HANDLER
146 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(createErrorMessage(mnCol, mnRow, mnTab, nValue, aValue).getStr(), nValue, aValue, 1e-10);
149 ++mnCol;
152 private:
153 ScDocument* mpDoc;
154 SCCOL mnCol;
155 SCROW mnRow;
156 SCTAB mnTab;
157 StringType meStringType;
160 class conditional_format_handler : public orcus::csv_handler
162 public:
163 conditional_format_handler(ScDocument* pDoc, SCTAB nTab):
164 mpDoc(pDoc),
165 mnCol(0),
166 mnRow(0),
167 mnTab(nTab) {}
169 void end_row()
171 ++mnRow;
172 mnCol = 0;
175 void cell(std::string_view value, bool /*transient*/)
177 #if DEBUG_CSV_HANDLER
178 std::cout << "Col: " << mnCol << " Row: " << mnRow << std::endl;
179 #endif //DEBUG_CSV_HANDLER
180 OUString aString = getConditionalFormatString(mpDoc, mnCol, mnRow, mnTab);
181 OUString aCSVString(value.data(), value.size(), RTL_TEXTENCODING_UTF8);
182 #if DEBUG_CSV_HANDLER
183 std::cout << "String: " << OUStringToOString(aString, RTL_TEXTENCODING_UTF8).getStr() << std::endl;
184 std::cout << "CSVString: " << OUStringToOString(aCSVString, RTL_TEXTENCODING_UTF8).getStr() << std::endl;
185 std::cout << "result: " << (int)(aCSVString == aString) << std::endl;
186 #endif //DEBUG_CSV_HANDLER
187 CPPUNIT_ASSERT_EQUAL_MESSAGE(createErrorMessage(mnCol, mnRow, mnTab, aCSVString, aString).getStr(), aCSVString, aString );
188 ++mnCol;
191 private:
192 ScDocument* mpDoc;
193 SCCOL mnCol;
194 SCROW mnRow;
195 SCTAB mnTab;
198 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */