diff options
author | Markus Mohrhard <markus.mohrhard@googlemail.com> | 2011-09-09 04:54:23 +0200 |
---|---|---|
committer | Markus Mohrhard <markus.mohrhard@googlemail.com> | 2011-09-09 04:58:04 +0200 |
commit | 148aeaaa9167921a419940dbb5acc872d4675560 (patch) | |
tree | 736e10820264c9bbb9d9fbd8464a25470dda0ab0 /sc | |
parent | externalrefmgr.cxx is String free (diff) | |
download | core-148aeaaa9167921a419940dbb5acc872d4675560.tar.gz core-148aeaaa9167921a419940dbb5acc872d4675560.zip |
add logical function unit test
it is now possible to add csv files to create easy filter test
Diffstat (limited to 'sc')
-rw-r--r-- | sc/qa/unit/data/contentCSV/logicalFunctions.csv | 6 | ||||
-rw-r--r-- | sc/qa/unit/data/ods/functions.ods | bin | 0 -> 7792 bytes | |||
-rw-r--r-- | sc/qa/unit/filters-test.cxx | 85 | ||||
-rw-r--r-- | sc/qa/unit/helper/csv_handler.hxx | 97 |
4 files changed, 188 insertions, 0 deletions
diff --git a/sc/qa/unit/data/contentCSV/logicalFunctions.csv b/sc/qa/unit/data/contentCSV/logicalFunctions.csv new file mode 100644 index 000000000000..2223bafb9372 --- /dev/null +++ b/sc/qa/unit/data/contentCSV/logicalFunctions.csv @@ -0,0 +1,6 @@ +FALSE, FALSE, FALSE, TRUE, FALSE +FALSE +TRUE, FALSE +TRUE, FALSE +FALSE, TRUE, TRUE, TRUE, TRUE +TRUE diff --git a/sc/qa/unit/data/ods/functions.ods b/sc/qa/unit/data/ods/functions.ods Binary files differnew file mode 100644 index 000000000000..374ca1543969 --- /dev/null +++ b/sc/qa/unit/data/ods/functions.ods diff --git a/sc/qa/unit/filters-test.cxx b/sc/qa/unit/filters-test.cxx index b25da7241f81..170713f57bb3 100644 --- a/sc/qa/unit/filters-test.cxx +++ b/sc/qa/unit/filters-test.cxx @@ -59,6 +59,12 @@ #include "document.hxx" #include "postit.hxx" +#include "helper/csv_handler.hxx" +#include "orcus/csv_parser.hpp" +#include <fstream> +#include <string> +#include <sstream> + const int indeterminate = 2; #define ODS_FORMAT_TYPE 50331943 @@ -73,6 +79,43 @@ struct { { "xlsx", "Calc MS Excel 2007 XML" , "MS Excel 2007 XML", XLSX_FORMAT_TYPE } }; +namespace { + + void loadFile(const rtl::OUString& aFileName, std::string& aContent) + { + rtl::OString aOFileName = rtl::OUStringToOString(aFileName, RTL_TEXTENCODING_UTF8); + std::ifstream aFile(aOFileName.getStr()); + + CPPUNIT_ASSERT_MESSAGE("could not open csv file", aFile); + std::ostringstream aOStream; + aOStream << aFile.rdbuf(); + aFile.close(); + aContent = aOStream.str(); + } + + void testFile(rtl::OUString& aFileName, ScDocument* pDoc, SCTAB nTab) + { + csv_handler aHandler(pDoc, nTab); + orcus::csv_parser_config aConfig; + aConfig.delimiters.push_back(','); + aConfig.delimiters.push_back(';'); + aConfig.text_qualifier = '"'; + std::string aContent; + loadFile(aFileName, aContent); + orcus::csv_parser<csv_handler> parser ( &aContent[0], aContent.size() , aHandler, aConfig); + try + { + parser.parse(); + } + catch (const orcus::csv_parse_error& e) + { + std::cout << "reading csv content file failed" << e.what() << std::endl; + CPPUNIT_ASSERT_MESSAGE("csv parser error", false); + } + } + +} + using namespace ::com::sun::star; @@ -90,6 +133,9 @@ public: void recursiveScan(const rtl::OUString &rFilter, const rtl::OUString &rURL, const rtl::OUString &rUserData, int nExpected); ScDocShellRef load(const rtl::OUString &rFilter, const rtl::OUString &rURL, const rtl::OUString &rUserData, const rtl::OUString& rTypeName, sal_uLong nFormatType = 0); + void createFilePath(const rtl::OUString& aFileBase, const rtl::OUString& aFileExtension, rtl::OUString& rFilePath); + void createCSVPath(const rtl::OUString& aFileBase, rtl::OUString& rFilePath); + /** * Ensure CVEs remain unbroken */ @@ -100,11 +146,13 @@ public: void testRangeNameImpl(ScDocument* pDoc); void testContent(); void testContentImpl(ScDocument* pDoc); //same code for ods, xls, xlsx + void testFunctions(); CPPUNIT_TEST_SUITE(FiltersTest); CPPUNIT_TEST(testCVEs); CPPUNIT_TEST(testRangeName); CPPUNIT_TEST(testContent); + CPPUNIT_TEST(testFunctions); CPPUNIT_TEST_SUITE_END(); private: @@ -112,6 +160,7 @@ private: uno::Reference<lang::XMultiComponentFactory> m_xFactory; uno::Reference<uno::XInterface> m_xCalcComponent; ::rtl::OUString m_aSrcRoot; + ::rtl::OUString m_aFileRoot; //m_aSrcRoot without "file://" prefix ::rtl::OUString m_aBaseString; }; @@ -184,6 +233,23 @@ void FiltersTest::recursiveScan(const rtl::OUString &rFilter, const rtl::OUStrin CPPUNIT_ASSERT(osl::FileBase::E_None == aDir.close()); } +void FiltersTest::createFilePath(const rtl::OUString& aFileBase, const rtl::OUString& aFileExtension, rtl::OUString& rFilePath) +{ + rtl::OUString aSep(RTL_CONSTASCII_USTRINGPARAM("/")); + rtl::OUStringBuffer aBuffer(m_aSrcRoot); + aBuffer.append(m_aBaseString).append(aSep).append(aFileExtension); + aBuffer.append(aSep).append(aFileBase).append(aFileExtension); + rFilePath = aBuffer.makeStringAndClear(); +} + +void FiltersTest::createCSVPath(const rtl::OUString& aFileBase, rtl::OUString& rCSVPath) +{ + rtl::OUStringBuffer aBuffer(m_aFileRoot); + aBuffer.append(m_aBaseString).append(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("/contentCSV/"))); + aBuffer.append(aFileBase).append(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("csv"))); + rCSVPath = aBuffer.makeStringAndClear(); +} + void FiltersTest::testCVEs() { recursiveScan(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Quattro Pro 6.0")), @@ -310,6 +376,24 @@ void FiltersTest::testContent() } } +void FiltersTest::testFunctions() +{ + const rtl::OUString aFileNameBase(RTL_CONSTASCII_USTRINGPARAM("functions.")); + rtl::OUString aFileExtension(aFileFormats[0].pName, strlen(aFileFormats[0].pName), RTL_TEXTENCODING_UTF8 ); + rtl::OUString aFilterName(aFileFormats[0].pFilterName, strlen(aFileFormats[0].pFilterName), RTL_TEXTENCODING_UTF8) ; + rtl::OUString aFileName; + createFilePath(aFileNameBase, aFileExtension, aFileName); + rtl::OUString aFilterType(aFileFormats[0].pTypeName, strlen(aFileFormats[0].pTypeName), RTL_TEXTENCODING_UTF8); + std::cout << aFileFormats[0].pName << " Test" << std::endl; + ScDocShellRef xDocSh = load (aFilterName, aFileName, rtl::OUString(), aFilterType, aFileFormats[0].nFormatType); + + CPPUNIT_ASSERT_MESSAGE("Failed to load functions.*", xDocSh.Is()); + ScDocument* pDoc = xDocSh->GetDocument(); + rtl::OUString aCSVFileName; + createCSVPath(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("logicalFunctions.")), aCSVFileName); + testFile(aCSVFileName, pDoc, 0); +} + FiltersTest::FiltersTest() : m_aSrcRoot(RTL_CONSTASCII_USTRINGPARAM("file://")), m_aBaseString(RTL_CONSTASCII_USTRINGPARAM("/sc/qa/unit/data")) @@ -366,6 +450,7 @@ FiltersTest::FiltersTest() m_aSrcRoot += rtl::OUString::createFromAscii( "/" ); #endif m_aSrcRoot += rtl::OUString::createFromAscii( pSrcRoot ); + m_aFileRoot += rtl::OUString::createFromAscii( pSrcRoot ); } void FiltersTest::setUp() diff --git a/sc/qa/unit/helper/csv_handler.hxx b/sc/qa/unit/helper/csv_handler.hxx new file mode 100644 index 000000000000..56269846a41e --- /dev/null +++ b/sc/qa/unit/helper/csv_handler.hxx @@ -0,0 +1,97 @@ +/* + * Version: MPL 1.1 / GPLv3+ / LGPLv3+ + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License or as specified alternatively below. You may obtain a copy of + * the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * Major Contributor(s): + * Copyright (C) 2011 Markus Mohrhard <markus.mohrhard@googlemail.com> (initial developer) + * + * All Rights Reserved. + * + * For minor contributions see the git repository. + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 3 or later (the "GPLv3+"), or + * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"), + * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable + * instead of those above. + */ + +#include <iostream> + +#define DEBUG_CSV_HANDLER 0 + +class csv_handler +{ +public: + csv_handler(ScDocument* pDoc, SCTAB nTab): + mpDoc(pDoc), + mnCol(0), + mnRow(0), + mnTab(nTab) {} + + void begin_parse() + { + + } + + void end_parse() + { + + } + + void begin_row() + { + + } + + void end_row() + { + ++mnRow; + mnCol = 0; + } + + void cell(const char* p, size_t n) + { +#if DEBUG_CSV_HANDLER + std::cout << "Col: " << mnCol << " Row: " << mnRow << std::endl; +#endif //DEBUG_CSV_HANDLER + char* pRemainingChars = NULL; + std::string aStr(p, n); + double nValue = strtod(&aStr[0], &pRemainingChars); + if (*pRemainingChars) + { + rtl::OUString aString; + mpDoc->GetString(mnCol, mnRow, mnTab, aString); + rtl::OUString aCSVString(p, n, RTL_TEXTENCODING_UTF8); +#if DEBUG_CSV_HANDLER + std::cout << "String: " << rtl::OUStringToOString(aString, RTL_TEXTENCODING_UTF8).getStr() << std::endl; + std::cout << "CSVString: " << rtl::OUStringToOString(aCSVString, RTL_TEXTENCODING_UTF8).getStr() << std::endl; + std::cout << "result: " << (int)(aCSVString == aString) << std::endl; +#endif //DEBUG_CSV_HANDLER + + CPPUNIT_ASSERT_MESSAGE("content is not correct in cell", aString == aCSVString); + } + else + { + double aValue; + mpDoc->GetValue(mnCol, mnRow, mnTab, aValue); + CPPUNIT_ASSERT_MESSAGE("content is not correct in cell", aValue == nValue); + } + ++mnCol; + } + +private: + ScDocument* mpDoc; + SCCOL mnCol; + SCROW mnRow; + SCTAB mnTab; +}; |